-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
282 lines (211 loc) · 10.4 KB
/
Copy pathui.py
File metadata and controls
282 lines (211 loc) · 10.4 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import sys
import math
import random
from PySide6.QtCore import Qt, QTimer, QPropertyAnimation, QEasingCurve, QRect, Property
from PySide6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QLabel, QApplication, QGraphicsDropShadowEffect, QStackedLayout
from PySide6.QtGui import QPainter, QColor, QPen, QBrush, QRadialGradient, QLinearGradient, QFont
class ShadowWidget(QWidget):
"""Static widget solely responsible for casting the drop shadow."""
def __init__(self, parent_ui, parent=None):
super().__init__(parent)
self.parent_ui = parent_ui
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
w = self.width() - 2
h = self.height() - 2
radius = h / 2.0
alpha = int(255 * self.parent_ui._ui_opacity)
if alpha > 0:
painter.setBrush(QBrush(QColor(0, 0, 0, alpha)))
painter.setPen(Qt.NoPen)
painter.drawRoundedRect(1, 1, w, h, radius, radius)
painter.end()
class FlowPillWidget(QWidget):
def __init__(self, parent_ui, parent=None):
super().__init__(parent)
self.parent_ui = parent_ui
self.volume = 0.0
self.max_seen_volume = 0.01
self.phase = 0.0
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_animation)
self.timer.start(30)
def update_volume(self, volume):
self.volume = volume
def update_animation(self):
if self.parent_ui.state == "listening":
self.phase += 0.25
self.max_seen_volume = max(self.max_seen_volume * 0.98, self.volume, 0.005)
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
ui_op = self.parent_ui._ui_opacity
if ui_op <= 0.01:
painter.end()
return
w = self.width() - 2
h = self.height() - 2
radius = h / 2.0
bg_alpha = int(230 * ui_op)
border_alpha = int(25 * ui_op)
bg_color = QColor(15, 15, 18, bg_alpha)
border_color = QColor(255, 255, 255, border_alpha)
if self.parent_ui.state == "transcribing":
border_color = QColor(0, 200, 255, int(150 * ui_op))
elif self.parent_ui.state == "cleaning":
border_color = QColor(160, 60, 255, int(150 * ui_op))
elif self.parent_ui.state == "done":
border_color = QColor(50, 220, 100, int(150 * ui_op))
painter.setBrush(QBrush(bg_color))
painter.setPen(QPen(border_color, 1.5))
painter.drawRoundedRect(1, 1, w, h, radius, radius)
if self.parent_ui.state == "listening":
norm_vol = min(self.volume / self.max_seen_volume, 1.0)
num_stems = 15
stem_width = 3
spacing = 4
total_w = num_stems * stem_width + (num_stems - 1) * spacing
start_x = (w - total_w) / 2.0 + 1
cy = h / 2.0 + 1
# Vibrant Neon Linear Gradient
grad = QLinearGradient(start_x, cy, start_x + total_w, cy)
intensity = min(1.0, norm_vol * 2.5)
r1, g1, b1 = 200, 200, 255
r2, g2, b2 = 255, 255, 255
r3, g3, b3 = 255, 200, 255
vr1, vg1, vb1 = 0, 255, 255
vr2, vg2, vb2 = 255, 0, 255
vr3, vg3, vb3 = 150, 50, 255
c1 = QColor(int(r1 + (vr1 - r1)*intensity), int(g1 + (vg1 - g1)*intensity), int(b1 + (vb1 - b1)*intensity), int(255 * ui_op))
c2 = QColor(int(r2 + (vr2 - r2)*intensity), int(g2 + (vg2 - g2)*intensity), int(b2 + (vb2 - b2)*intensity), int(255 * ui_op))
c3 = QColor(int(r3 + (vr3 - r3)*intensity), int(g3 + (vg3 - g3)*intensity), int(b3 + (vb3 - b3)*intensity), int(255 * ui_op))
grad.setColorAt(0.0, c1)
grad.setColorAt(0.5, c2)
grad.setColorAt(1.0, c3)
painter.setBrush(QBrush(grad))
painter.setPen(Qt.NoPen)
for i in range(num_stems):
dist_from_center = abs(i - (num_stems - 1) / 2.0)
envelope = max(0, 1.0 - (dist_from_center / (num_stems / 2.0))**1.5)
base_h = 8 + 8 * math.sin(self.phase + i * 0.7) * envelope
vol_h = 45 * norm_vol * envelope * (0.5 + 0.8 * random.random())
stem_h = base_h + vol_h
stem_h = min(stem_h, h - 4) # Allow stems to reach almost the absolute edge of the pill
stem_h = max(stem_h, 4)
x = start_x + i * (stem_width + spacing)
y = cy - stem_h / 2.0
painter.drawRoundedRect(x, y, stem_width, stem_h, 1.5, 1.5)
painter.end()
class FlowUI(QWidget):
def __init__(self):
super().__init__()
self.setWindowFlags(
Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint |
Qt.Tool |
Qt.SubWindow |
Qt.WindowDoesNotAcceptFocus
)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self._ui_opacity = 0.0
self.main_layout = QVBoxLayout(self)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.setSpacing(0)
self.outer_container = QWidget(self)
self.main_layout.addWidget(self.outer_container)
self.shadow_margin = 30
self.outer_layout = QStackedLayout(self.outer_container)
self.outer_layout.setStackingMode(QStackedLayout.StackAll)
self.outer_layout.setContentsMargins(self.shadow_margin, self.shadow_margin, self.shadow_margin, self.shadow_margin)
self.shadow_bg = ShadowWidget(self, self.outer_container)
self.shadow_effect = QGraphicsDropShadowEffect(self.shadow_bg)
self.shadow_effect.setBlurRadius(25)
self.shadow_effect.setColor(QColor(0, 0, 0, 160))
self.shadow_effect.setOffset(0, 6)
self.shadow_bg.setGraphicsEffect(self.shadow_effect)
self.pill = FlowPillWidget(self, self.outer_container)
self.outer_layout.addWidget(self.shadow_bg)
self.outer_layout.addWidget(self.pill)
self.layout = QHBoxLayout(self.pill)
self.layout.setContentsMargins(16, 0, 16, 0)
self.layout.setSpacing(0)
self.layout.setAlignment(Qt.AlignCenter)
self.status_label = QLabel("", self.pill)
self.status_label.setStyleSheet("color: rgba(255, 255, 255, 0); font-size: 14px; font-weight: 600; font-family: 'SF Pro Text', 'Inter', 'Segoe UI', -apple-system, sans-serif; border: none; background: transparent;")
self.status_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.status_label)
self.state = "idle"
self.geom_anim = QPropertyAnimation(self, b"geometry")
self.geom_anim.setDuration(450)
self.geom_anim.setEasingCurve(QEasingCurve.OutBack)
self.opacity_anim = QPropertyAnimation(self, b"ui_opacity")
self.opacity_anim.setDuration(300)
self.opacity_anim.setEasingCurve(QEasingCurve.InOutQuad)
def get_ui_opacity(self):
return self._ui_opacity
def set_ui_opacity(self, value):
self._ui_opacity = value
self.shadow_bg.update()
self.pill.update()
alpha = int(255 * value)
self.status_label.setStyleSheet(f"color: rgba(255, 255, 255, {alpha}); font-size: 14px; font-weight: 600; font-family: 'SF Pro Text', 'Inter', 'Segoe UI', -apple-system, sans-serif; border: none; background: transparent;")
ui_opacity = Property(float, get_ui_opacity, set_ui_opacity)
def _get_target_geometry(self, width, height):
w_window = width + 2 * self.shadow_margin
h_window = height + 2 * self.shadow_margin
screen = QApplication.primaryScreen()
screen_geom = screen.geometry()
x = screen_geom.x() + (screen_geom.width() - w_window) // 2
y = screen_geom.y() + 10 - self.shadow_margin
return x, y, w_window, h_window
def show_centered_top(self):
width = 80
height = 28
x, y, w, h = self._get_target_geometry(width, height)
self.setGeometry(x, y, w, h)
self.show()
self.set_state("idle")
def animate_geometry(self, width, height, opacity_val):
x, y, w, h = self._get_target_geometry(width, height)
self.geom_anim.stop()
self.geom_anim.setStartValue(self.geometry())
self.geom_anim.setEndValue(QRect(x, y, w, h))
self.geom_anim.start()
self.opacity_anim.stop()
self.opacity_anim.setStartValue(self.ui_opacity)
self.opacity_anim.setEndValue(opacity_val)
self.opacity_anim.start()
def update_volume(self, volume):
if self.state == "listening":
self.pill.update_volume(volume)
def set_state(self, state):
self.state = state
# Enforce absolute Z-order to prevent overlapping issues
self.shadow_bg.lower()
self.pill.raise_()
self.pill.update()
self.shadow_bg.update()
if state == "idle":
self.status_label.hide()
self.animate_geometry(80, 28, 0.6)
elif state == "listening":
self.status_label.hide()
self.animate_geometry(180, 40, 1.0)
elif state == "transcribing":
self.status_label.setText("Transcribing...")
self.status_label.show()
self.status_label.raise_() # Ensure text is always on top when visible
self.animate_geometry(160, 40, 1.0)
elif state == "cleaning":
self.status_label.setText("Polishing...")
self.status_label.show()
self.status_label.raise_()
self.animate_geometry(150, 40, 1.0)
elif state == "done":
self.status_label.setText("Pasted!")
self.status_label.show()
self.status_label.raise_()
self.animate_geometry(120, 40, 1.0)
QTimer.singleShot(1500, lambda: self.set_state("idle") if self.state == "done" else None)