-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.py
More file actions
529 lines (440 loc) · 18.1 KB
/
widgets.py
File metadata and controls
529 lines (440 loc) · 18.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/python3
# Widget classes for ovolay: scroll gradient boxes and volume slider rows.
import math
import cairo
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk, GLib # noqa
def _parse_color(color):
"""Convert color to (r, g, b) float tuple.
Accepts a float tuple, byte tuple (0-255), or hex string.
"""
if isinstance(color, str):
h = color.lstrip('#')
return tuple(int(h[i:i + 2], 16) / 255.0 for i in (0, 2, 4))
if any(v > 1.0 for v in color):
return tuple(v / 255.0 for v in color)
return tuple(color)
def _suppress_overshoot(scrolled_window):
"""Hide the built-in overshoot highlight on a ScrolledWindow."""
scrolled_window.add_css_class("scroll-no-overshoot")
class _ScrollGradientBase(Gtk.Overlay):
"""Shared base for scroll gradient overlay boxes."""
# #1c1f26 as floats matching .overlay-window background
GRADIENT_SIZE = 30
BG = (0.11, 0.122, 0.149)
FLASH = (0.3, 0.36, 0.47)
def __init__(
self, child, gradient_size=None,
bg_color=None, flash_color=None):
super().__init__()
self._gradient_size = (
gradient_size if gradient_size is not None
else self.GRADIENT_SIZE
)
self._bg_color = (
_parse_color(bg_color) if bg_color else self.BG
)
self._flash_color = (
_parse_color(flash_color) if flash_color else self.FLASH
)
self._flash_opacity = 0.0
self._flash_dir = 0
self._anim_id = None
self.set_overflow(Gtk.Overflow.HIDDEN)
self._scroll = self._make_scroll()
self._scroll.set_child(child)
self.set_child(self._scroll)
self._canvas = Gtk.DrawingArea()
self._canvas.set_can_target(False)
self._canvas.set_draw_func(self._draw)
self.add_overlay(self._canvas)
adj = self._get_adjustment()
adj.connect(
"value-changed", lambda *_: self._canvas.queue_draw())
adj.connect(
"changed", lambda *_: self._canvas.queue_draw())
self._scroll_controller = Gtk.EventControllerScroll.new(
Gtk.EventControllerScrollFlags.BOTH_AXES)
self._scroll_controller.connect(
"scroll", self._on_scroll_event)
self._scroll.add_controller(self._scroll_controller)
def _on_scroll_event(self, _controller, dx, dy):
# Use dy for vertical boxes, dx for horizontal
delta = dy if hasattr(self, '_sw_height') else dx
if delta == 0:
return False
adj = self._get_adjustment()
val = adj.get_value()
max_val = adj.get_upper() - adj.get_page_size()
if max_val <= 0:
return False
if delta < 0 and val <= 0:
self._start_flash(-1)
elif delta > 0 and val >= max_val - 0.1:
self._start_flash(1)
return False
def _make_scroll(self):
raise NotImplementedError
def _get_adjustment(self):
raise NotImplementedError
def _start_flash(self, direction):
"""Animate a brief edge-flash to signal an overscroll attempt."""
if self._anim_id:
GLib.source_remove(self._anim_id)
self._flash_opacity = 0.7
self._flash_dir = direction
def _fade():
self._flash_opacity -= 0.05
if self._flash_opacity <= 0.0:
self._flash_opacity = 0.0
self._anim_id = None
self._canvas.queue_draw()
return False
self._canvas.queue_draw()
return True
self._anim_id = GLib.timeout_add(16, _fade)
def _rounded_rect(self, cr, x, y, w, h, r):
"""Trace a rounded rectangle path."""
cr.new_sub_path()
cr.arc(x + r, y + r, r, math.pi, 3 * math.pi / 2)
cr.arc(x + w - r, y + r, r, 3 * math.pi / 2, 2 * math.pi)
cr.arc(x + w - r, y + h - r, r, 0, math.pi / 2)
cr.arc(x + r, y + h - r, r, math.pi / 2, math.pi)
cr.close_path()
def _draw(self, _area, cr, width, height, *_args):
raise NotImplementedError
class VScrollGradientBox(_ScrollGradientBase):
"""Wrap a child in a vertical ScrolledWindow with gradient edges.
Provides edge-fade gradients and an overscroll flash effect.
"""
def __init__(
self, child, height=0, max_height=None, width=0,
gradient_size=None, bg_color=None, flash_color=None):
# Store before super().__init__() calls _make_scroll()
self._sw_height = height
self._max_height = max_height
self._sw_width = width
super().__init__(
child, gradient_size=gradient_size,
bg_color=bg_color, flash_color=flash_color)
def _make_scroll(self):
sw = Gtk.ScrolledWindow(hexpand=True)
sw.set_overflow(Gtk.Overflow.HIDDEN)
sw.set_policy(
Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
sw.set_propagate_natural_height(True)
sw.set_kinetic_scrolling(False)
_suppress_overshoot(sw)
if self._sw_width > 0:
sw.set_min_content_width(self._sw_width)
sw.set_max_content_width(self._sw_width)
sw.set_propagate_natural_width(False)
self.set_size_request(self._sw_width, -1)
if self._sw_height > 0:
sw.set_min_content_height(self._sw_height)
sw.set_max_content_height(self._sw_height)
if self._max_height is not None:
sw.set_vexpand(True)
sw.set_max_content_height(self._max_height)
return sw
def _get_adjustment(self):
return self._scroll.get_vadjustment()
def _draw(self, _area, cr, width, height, *_args):
adj = self._get_adjustment()
val = adj.get_value()
upper = adj.get_upper()
page = adj.get_page_size()
gs = self._gradient_size
fade_px = 40.0
r, g, b = self._bg_color
fr, fg, fb = self._flash_color
radius = 0
top_op = min(val / fade_px, 1.0)
bottom_op = min((upper - page - val) / fade_px, 1.0)
cr.save()
self._rounded_rect(cr, 0, 0, width, height, radius)
cr.clip()
if top_op > 0:
pat = cairo.LinearGradient(0, 0, 0, gs)
pat.add_color_stop_rgba(0, r, g, b, top_op)
pat.add_color_stop_rgba(1, r, g, b, 0.0)
cr.rectangle(0, 0, width, gs)
cr.set_source(pat)
cr.fill()
if bottom_op > 0:
pat = cairo.LinearGradient(0, height - gs, 0, height)
pat.add_color_stop_rgba(0, r, g, b, 0.0)
pat.add_color_stop_rgba(1, r, g, b, bottom_op)
cr.rectangle(0, height - gs, width, gs)
cr.set_source(pat)
cr.fill()
if self._flash_opacity > 0:
if self._flash_dir == -1:
pat = cairo.LinearGradient(0, 0, 0, gs)
pat.add_color_stop_rgba(
0, fr, fg, fb, self._flash_opacity)
pat.add_color_stop_rgba(1, fr, fg, fb, 0.0)
cr.rectangle(0, 0, width, gs)
cr.set_source(pat)
cr.fill()
elif self._flash_dir == 1:
pat = cairo.LinearGradient(
0, height - gs, 0, height)
pat.add_color_stop_rgba(0, fr, fg, fb, 0.0)
pat.add_color_stop_rgba(
1, fr, fg, fb, self._flash_opacity)
cr.rectangle(0, height - gs, width, gs)
cr.set_source(pat)
cr.fill()
cr.restore()
class VolumeSliderRow(Gtk.Box):
"""A volume row with a progress bar background and drag/scroll input."""
def __init__(self, title, subtitle, index, initial_volume,
is_muted, set_volume_cb, set_mute_cb,
is_default=False, set_default_cb=None,
scroll_to_adjust=True):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
self.index = index
self.set_volume_cb = set_volume_cb
self.set_mute_cb = set_mute_cb
self.set_default_cb = set_default_cb
self.is_muted = bool(is_muted)
self.is_selected_item = False
self.scroll_to_adjust = scroll_to_adjust
self.add_css_class("volume-row")
self.set_hexpand(True)
# Overlay puts content over the progress bar background
overlay = Gtk.Overlay()
# Background progress bar showing volume level
self.progress_bar = Gtk.ProgressBar()
self.progress_bar.set_fraction(initial_volume / 100.0)
self.progress_bar.add_css_class("volume-progress")
overlay.set_child(self.progress_bar)
# Horizontal content box
content_box = Gtk.Box(
orientation=Gtk.Orientation.HORIZONTAL, spacing=20)
content_box.add_css_class("volume-row-content")
# Vertical box for title and optional subtitle
title_box = Gtk.Box(
orientation=Gtk.Orientation.VERTICAL, spacing=0)
title_box.set_hexpand(True)
title_box.set_valign(Gtk.Align.CENTER)
title_label = Gtk.Label()
title_label.set_text(title)
title_label.set_halign(Gtk.Align.START)
title_label.set_ellipsize(3)
title_label.set_max_width_chars(30)
title_label.add_css_class("title-label")
title_box.append(title_label)
if subtitle and subtitle != title:
subtitle_label = Gtk.Label()
subtitle_label.set_text(subtitle)
subtitle_label.set_halign(Gtk.Align.START)
subtitle_label.set_ellipsize(3)
subtitle_label.set_max_width_chars(35)
subtitle_label.add_css_class("subtitle-label")
title_box.append(subtitle_label)
content_box.append(title_box)
# Default device indicator; hidden when not the default
self.default_icon = Gtk.Image.new_from_icon_name(
"object-select-symbolic")
self.default_icon.set_valign(Gtk.Align.CENTER)
self.default_icon.add_css_class("default-icon")
self.default_icon.set_visible(is_default)
content_box.append(self.default_icon)
overlay.add_overlay(content_box)
self.append(overlay)
self.adjustment = Gtk.Adjustment(
value=initial_volume, lower=0, upper=100,
step_increment=1, page_increment=10
)
self.adjustment.connect("value-changed", self.on_volume_changed)
# Scroll controller for volume adjustment
sc = Gtk.EventControllerScroll.new(
Gtk.EventControllerScrollFlags.VERTICAL)
sc.connect("scroll", self.on_scroll)
self.add_controller(sc)
# Left-click drag to set volume
self.dragging = False
self.drag_gesture = Gtk.GestureDrag.new()
self.drag_gesture.set_button(1)
self.drag_gesture.connect("drag-begin", self.on_drag_begin)
self.drag_gesture.connect("drag-update", self.on_drag_update)
self.drag_gesture.connect("drag-end", self.on_drag_end)
self.add_controller(self.drag_gesture)
# Right-click to toggle mute
self.right_click_gesture = Gtk.GestureClick.new()
self.right_click_gesture.set_button(3)
self.right_click_gesture.connect("pressed", self.on_right_click)
self.add_controller(self.right_click_gesture)
# Middle-click to set as default device
self.middle_click_gesture = Gtk.GestureClick.new()
self.middle_click_gesture.set_button(2)
self.middle_click_gesture.connect(
"pressed", self.on_middle_click)
self.add_controller(self.middle_click_gesture)
self.update_ui()
def update_volume_from_x(self, x):
width = self.get_width()
if width > 0:
volume = max(0, min(100, (x / width) * 100))
self.adjustment.set_value(volume)
def on_drag_begin(self, gesture, start_x, start_y):
self.dragging = True
self.update_volume_from_x(start_x)
def on_drag_update(self, gesture, offset_x, offset_y):
success, start_x, start_y = gesture.get_start_point()
if success:
self.update_volume_from_x(start_x + offset_x)
def on_drag_end(self, gesture, offset_x, offset_y):
self.dragging = False
def on_right_click(self, gesture, n_press, x, y):
self.toggle_mute()
def on_middle_click(self, gesture, n_press, x, y):
"""Set this device as the default on middle-click."""
if self.set_default_cb:
self.set_default_cb(self.index)
def on_scroll(self, controller, dx, dy):
if not self.scroll_to_adjust:
# Let the event bubble up to the parent ScrolledWindow
return False
self.adjust_volume(-dy * 2)
return True
def set_selected(self, selected):
self.is_selected_item = selected
self.update_ui()
def update_ui(self):
volume_percent = self.adjustment.get_value()
self.progress_bar.set_fraction(volume_percent / 100.0)
if self.is_selected_item:
self.add_css_class("selected")
else:
self.remove_css_class("selected")
if self.is_muted:
self.progress_bar.add_css_class("muted")
else:
self.progress_bar.remove_css_class("muted")
def on_volume_changed(self, adjustment):
# Forward new volume (0.0-1.0) to PulseAudio via callback
self.set_volume_cb(self.index, adjustment.get_value() / 100.0)
self.update_ui()
def adjust_volume(self, delta):
current = self.adjustment.get_value()
self.adjustment.set_value(max(0, min(100, current + delta)))
def toggle_mute(self):
self.is_muted = not self.is_muted
self.set_mute_cb(self.index, self.is_muted)
self.update_ui()
def set_is_default(self, value):
"""Show or hide the default device indicator."""
self.default_icon.set_visible(bool(value))
class PillSlider(Gtk.DrawingArea):
"""Horizontal pill-shaped slider with no visible handle.
Visually resembles a level bar: a fully-rounded trough with a
rounded filled region that grows from the left. Supports click
and drag to set position. Value range is always 0.0–1.0.
"""
def __init__(
self, value=0.0, height=8,
on_change=None, width=-1,
scroll_step=0.05, scroll_to_adjust=True):
super().__init__()
# Current normalised value [0.0, 1.0]
self._value = max(0.0, min(1.0, value))
self._on_change = on_change
self._height = height
self._scroll_step = scroll_step
self.set_draw_func(self._draw)
self.set_size_request(width, height)
self.set_hexpand(width == -1)
self.set_valign(Gtk.Align.CENTER)
self.set_focusable(False)
# Click to set position
click = Gtk.GestureClick.new()
click.set_button(1)
click.connect('pressed', self._on_press)
self.add_controller(click)
# Drag to adjust
drag = Gtk.GestureDrag.new()
drag.set_button(1)
drag.connect('drag-begin', self._on_drag_begin)
drag.connect('drag-update', self._on_drag_update)
self.add_controller(drag)
# Optional vertical scroll to adjust value
if scroll_to_adjust:
scroll = Gtk.EventControllerScroll.new(
Gtk.EventControllerScrollFlags.VERTICAL)
scroll.connect('scroll', self._on_scroll)
self.add_controller(scroll)
def get_value(self):
"""Return the current value in [0.0, 1.0]."""
return self._value
def set_value(self, value):
"""Set value (0.0–1.0) and redraw; does not fire on_change."""
self._value = max(0.0, min(1.0, value))
self.queue_draw()
def _set_from_x(self, x):
"""Update value from an x pixel coordinate and fire callback."""
w = self.get_width()
if w <= 0:
return
new_val = max(0.0, min(1.0, x / w))
if new_val == self._value:
return
self._value = new_val
self.queue_draw()
if self._on_change:
self._on_change(self._value)
def _on_scroll(self, _controller, _dx, dy):
"""Adjust value by scroll_step on vertical scroll."""
self._value = max(0.0, min(1.0, self._value - dy * self._scroll_step))
self.queue_draw()
if self._on_change:
self._on_change(self._value)
return True
def _on_press(self, gesture, _n, x, _y):
self._set_from_x(x)
def _on_drag_begin(self, gesture, x, _y):
self._set_from_x(x)
def _on_drag_update(self, gesture, dx, _dy):
ok, start_x, _ = gesture.get_start_point()
if ok:
self._set_from_x(start_x + dx)
def _draw(self, _area, cr, width, height, *_args):
"""Paint trough and filled highlight as rounded pills."""
r = height / 2.0
fill_w = width * self._value
# Retrieve theme colours via a StyleContext snapshot
style = self.get_style_context()
fg = style.get_color()
tr, tg, tb = fg.red, fg.green, fg.blue
# Trough (full width, low opacity)
cr.save()
self._pill(cr, 0, 0, width, height, r)
cr.set_source_rgba(tr, tg, tb, 0.15)
cr.fill()
cr.restore()
# Filled region (accent colour from CSS variable if available)
if fill_w > 0:
cr.save()
# Clip to a pill the width of the fill so the right edge
# stays rounded even when partially filled
clip_w = max(fill_w, height)
self._pill(cr, 0, 0, clip_w, height, r)
cr.clip()
# Draw a full-width pill and let the clip cut the right side
self._pill(cr, 0, 0, width, height, r)
cr.set_source_rgba(tr, tg, tb, 0.7)
cr.fill()
cr.restore()
@staticmethod
def _pill(cr, x, y, w, h, r):
"""Trace a fully-rounded pill path."""
r = min(r, w / 2.0, h / 2.0)
cr.new_sub_path()
cr.arc(x + r, y + r, r, math.pi, 3 * math.pi / 2)
cr.arc(x + w - r, y + r, r, 3 * math.pi / 2, 2 * math.pi)
cr.arc(x + w - r, y + h - r, r, 0, math.pi / 2)
cr.arc(x + r, y + h - r, r, math.pi / 2, math.pi)
cr.close_path()