桌面悬浮球/mini窗口 功能实例
- 管理悬浮球窗口创建以及配置
- 解决electron拖拽和点击事件冲突
- 初始化组件位置,计算窗口拖动位置
- 折叠展开动画和事件处理
- 主题样式统一
html
<template>
<div class="mini-window"
:class="{ 'expanded': isExpanded }"
@mousedown="handleMouseDown"
@mouseenter="handleMouseEnter"
@mouseleave="handleMouseLeave">
<!-- 折叠状态 -->
<div class="mini-content">
<span class="mini-bg"></span>
</div>
<!-- 展开状态 -->
<div class="expanded-content" @click.stop>
<div class="actions">
<div class="action-item" @click="handleAction('restore')">
<el-icon><FullScreen /></el-icon>
<span>还原</span>
</div>
<div class="action-item" @click="handleAction('settings')">
<el-icon><Setting /></el-icon>
<span>设置</span>
</div>
<div class="action-item" @click="handleAction('dashboard')">
<el-icon><House /></el-icon>
<span>仪表盘</span>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { House, Setting, FullScreen, Close } from '@element-plus/icons-vue'
import { ipcRenderService } from '@/render/services/ipcService'
const isExpanded = ref(false)
let isDragging = false
let initialMouseX = 0
let initialMouseY = 0
let mouseDownTime = 0
let windowInitialX = 0
let windowInitialY = 0
// 处理鼠标按下事件
const handleMouseDown = (e: MouseEvent) => {
if (isExpanded.value) return // 展开状态不允许拖动
isDragging = false
initialMouseX = e.screenX // 使用screenX/screenY获取相对于屏幕的坐标
initialMouseY = e.screenY
mouseDownTime = Date.now()
// 获取窗口初始位置
ipcRenderService.invoke('app:window:get-position').then(([x, y]: [number, number]) => {
windowInitialX = x
windowInitialY = y
document.addEventListener('mousemove', handleMouseMove)
document.addEventListener('mouseup', handleMouseUp)
})
}
// 处理鼠标移动事件
const handleMouseMove = (e: MouseEvent) => {
const deltaX = e.screenX - initialMouseX
const deltaY = e.screenY - initialMouseY
// 判断是否达到拖动阈值
if (!isDragging && (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5)) {
isDragging = true
}
if (isDragging) {
// 计算新位置
const newX = windowInitialX + deltaX
const newY = windowInitialY + deltaY
// 发送新位置到主进程
ipcRenderService.send('app:window:set-position', { x: newX, y: newY })
}
}
const handleMouseUp = () => {
document.removeEventListener('mousemove', handleMouseMove)
document.removeEventListener('mouseup', handleMouseUp)
// 如果不是拖拽且点击时间小于200ms,则触发展开/收起
if (!isDragging && (Date.now() - mouseDownTime < 200)) {
toggleExpand()
}
}
const handleMouseEnter = () => {
ipcRenderService.send('app:window:mouse-enter')
}
const handleMouseLeave = () => {
ipcRenderService.send('app:window:mouse-leave')
}
const toggleExpand = () => {
isExpanded.value = !isExpanded.value
}
const handleAction = (action: string) => {
switch (action) {
case 'restore':
ipcRenderService.send('app:window:restore-main')
break
case 'dashboard':
ipcRenderService.send('app:window:restore-main', { route: 'INDEX' })
break
case 'settings':
ipcRenderService.send('app:window:restore-main', { route: 'SETTINGS' })
break
}
isExpanded.value = false
}
// 监听主进程发来的事件
onMounted(() => {
ipcRenderService.on('app:window:mouse-enter', () => {
// 可以在这里处理鼠标进入事件
})
ipcRenderService.on('app:window:mouse-leave', () => {
// 可以在这里处理鼠标离开事件
})
})
</script>
<style lang="scss" scoped>
.mini-window {
position: relative;
margin-left: 125px;
margin-top: 109px;
width: 50px;
height: 50px;
border-radius: 25px;
background: #fff;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
transition: all 0.3s ease;
overflow: hidden;
user-select: none;
&.expanded {
width: 160px;
height: 150px;
border-radius: 12px;
transform: translate(-110px, -100px);
.mini-content {
opacity: 0;
pointer-events: none;
}
.expanded-content {
opacity: 1;
pointer-events: auto;
}
}
.mini-content {
position: absolute;
bottom: 1px;
right: 5px;
opacity: 1;
transition: opacity 0.3s;
.mini-bg {
cursor: pointer;
display: inline-block;
background: var(--app-color-gradient-blue);
width: 40px;
height: 40px;
border-radius: 20px;
}
}
.expanded-content {
position: absolute;
bottom: 0;
right: 0;
width: 160px;
height: 150px;
opacity: 0;
padding: 9px 12px;
pointer-events: none;
transition: opacity 0.3s;
.actions {
display: flex;
flex-direction: column-reverse;
gap: 8px;
.action-item {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 12px;
border-radius: 8px;
cursor: pointer;
transition: all 0.2s ease;
color: var(--ep-color-primary);
.el-icon {
font-size: 18px;
}
span {
font-size: 14px;
}
&:hover {
background-color: var(--menu-active-bg-color);
transform: scale(1.06);
outline: 1px solid var(--ep-color-primary);
}
}
}
}
}
</style>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
