quantumgui 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. quantumgui/__init__.py +9 -0
  2. quantumgui/__main__.py +6 -0
  3. quantumgui/cli.py +341 -0
  4. quantumgui/core/__init__.py +39 -0
  5. quantumgui/core/animation.py +221 -0
  6. quantumgui/core/audit.py +323 -0
  7. quantumgui/core/context.py +54 -0
  8. quantumgui/core/data_source.py +426 -0
  9. quantumgui/core/draw.py +428 -0
  10. quantumgui/core/enterprise.py +402 -0
  11. quantumgui/core/enterprise_app.py +419 -0
  12. quantumgui/core/events.py +34 -0
  13. quantumgui/core/geometry.py +108 -0
  14. quantumgui/core/native_dialogs.py +276 -0
  15. quantumgui/core/pdf_report.py +390 -0
  16. quantumgui/core/rules.py +284 -0
  17. quantumgui/core/state.py +42 -0
  18. quantumgui/core/task_runner.py +181 -0
  19. quantumgui/core/widget.py +498 -0
  20. quantumgui/core/window.py +920 -0
  21. quantumgui/core/xlsx_export.py +145 -0
  22. quantumgui/py.typed +0 -0
  23. quantumgui/theme/__init__.py +32 -0
  24. quantumgui/theme/hct.py +191 -0
  25. quantumgui/theme/theme.py +93 -0
  26. quantumgui/theme/tokens.py +423 -0
  27. quantumgui/ui.py +2142 -0
  28. quantumgui/widgets/__init__.py +168 -0
  29. quantumgui/widgets/advanced.py +499 -0
  30. quantumgui/widgets/button.py +224 -0
  31. quantumgui/widgets/charts.py +625 -0
  32. quantumgui/widgets/charts_bi.py +528 -0
  33. quantumgui/widgets/charts_extra.py +430 -0
  34. quantumgui/widgets/charts_pro.py +568 -0
  35. quantumgui/widgets/checkbox.py +199 -0
  36. quantumgui/widgets/chip.py +219 -0
  37. quantumgui/widgets/containers.py +321 -0
  38. quantumgui/widgets/data_controls.py +487 -0
  39. quantumgui/widgets/data_grid.py +956 -0
  40. quantumgui/widgets/data_table.py +551 -0
  41. quantumgui/widgets/date_picker.py +407 -0
  42. quantumgui/widgets/display_media.py +372 -0
  43. quantumgui/widgets/display_pro.py +407 -0
  44. quantumgui/widgets/drawer.py +252 -0
  45. quantumgui/widgets/enterprise_controls.py +934 -0
  46. quantumgui/widgets/fab.py +261 -0
  47. quantumgui/widgets/forms_pro.py +703 -0
  48. quantumgui/widgets/forms_pro_media.py +587 -0
  49. quantumgui/widgets/inputs.py +870 -0
  50. quantumgui/widgets/layout_pro.py +162 -0
  51. quantumgui/widgets/list_view.py +307 -0
  52. quantumgui/widgets/masked_input.py +131 -0
  53. quantumgui/widgets/master_detail.py +155 -0
  54. quantumgui/widgets/navigation.py +382 -0
  55. quantumgui/widgets/overlays.py +355 -0
  56. quantumgui/widgets/productivity.py +500 -0
  57. quantumgui/widgets/productivity_boards.py +544 -0
  58. quantumgui/widgets/radio.py +209 -0
  59. quantumgui/widgets/scroll_view.py +129 -0
  60. quantumgui/widgets/select.py +373 -0
  61. quantumgui/widgets/sheets.py +249 -0
  62. quantumgui/widgets/slider.py +209 -0
  63. quantumgui/widgets/switch.py +196 -0
  64. quantumgui/widgets/tabs.py +175 -0
  65. quantumgui/widgets/text.py +101 -0
  66. quantumgui/widgets/text_field.py +490 -0
  67. quantumgui/widgets/time_picker.py +499 -0
  68. quantumgui-1.0.0.dist-info/METADATA +137 -0
  69. quantumgui-1.0.0.dist-info/RECORD +73 -0
  70. quantumgui-1.0.0.dist-info/WHEEL +5 -0
  71. quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
  72. quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
  73. quantumgui-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,373 @@
1
+ """Material Design 3 Exposed Dropdown Menu (`Select`)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Callable, Literal, Sequence
6
+
7
+ import skia
8
+
9
+ from ..core.animation import AnimatedValue
10
+ from ..core.draw import (
11
+ draw_elevation_shadow,
12
+ draw_icon,
13
+ draw_text_centered_y,
14
+ font_for_style,
15
+ get_font,
16
+ make_rrect,
17
+ make_rrect_spec,
18
+ measure_text,
19
+ resolve_color,
20
+ )
21
+ from ..core.events import MouseEvent
22
+ from ..core.geometry import Constraints, Rect, Size
23
+ from ..core.state import State
24
+ from ..core.widget import Widget
25
+ from ..theme import Theme
26
+
27
+ SelectVariant = Literal["outlined", "filled"]
28
+
29
+
30
+ class _SelectMenuOverlay(Widget):
31
+ """Floating M3 Menu overlay rendered above all other widgets."""
32
+
33
+ interactive = True
34
+ auto_attach = False
35
+
36
+ ITEM_H = 44.0
37
+ PAD_Y = 8.0
38
+
39
+ def __init__(self, owner: Select) -> None:
40
+ super().__init__()
41
+ # Detach from any active `with` container; managed explicitly by Window overlays
42
+ if self.parent is not None:
43
+ self.parent.children.remove(self)
44
+ self.parent = None
45
+ self.owner = owner
46
+ self._hover_idx: int = -1
47
+ self._menu_rect: Rect = Rect(0.0, 0.0, 0.0, 0.0)
48
+
49
+ def layout(self, rect: Rect, theme: Theme) -> None:
50
+ self.rect = rect
51
+ anchor = self.owner.rect
52
+ menu_h = len(self.owner.options) * self.ITEM_H + self.PAD_Y * 2.0
53
+ y = anchor.y + self.owner.BOX_HEIGHT + 4.0
54
+ if y + menu_h > rect.height - 8.0 and anchor.y - menu_h - 4.0 >= 8.0:
55
+ y = anchor.y - menu_h - 4.0
56
+ self._menu_rect = Rect(anchor.x, y, anchor.width, menu_h)
57
+
58
+ def hit_test(self, x: float, y: float) -> Widget | None:
59
+ if self._menu_rect.contains(x, y):
60
+ return self
61
+ return None
62
+
63
+ def _index_at(self, y: float) -> int:
64
+ rel = y - (self._menu_rect.y + self.PAD_Y)
65
+ idx = int(rel // self.ITEM_H)
66
+ if 0 <= idx < len(self.owner.options):
67
+ return idx
68
+ return -1
69
+
70
+ def on_mouse_move(self, event: MouseEvent) -> None:
71
+ idx = self._index_at(event.y)
72
+ if idx != self._hover_idx:
73
+ self._hover_idx = idx
74
+ self.invalidate()
75
+
76
+ def on_mouse_down(self, event: MouseEvent) -> None:
77
+ self._hover_idx = self._index_at(event.y)
78
+ self.pressed = True
79
+ self.invalidate()
80
+
81
+ def on_mouse_up(self, event: MouseEvent) -> None:
82
+ self.pressed = False
83
+ idx = self._index_at(event.y)
84
+ if 0 <= idx < len(self.owner.options):
85
+ self.owner._select_option(self.owner.options[idx])
86
+ self.owner.close_menu()
87
+
88
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
89
+ c = theme.colors
90
+ mr = self._menu_rect
91
+ radius = theme.shape.resolve("extra_small")
92
+
93
+ draw_elevation_shadow(canvas, mr, radius, 2, c.shadow)
94
+ rrect = make_rrect(mr, radius)
95
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=c.surface_container.to_skia()))
96
+
97
+ font = font_for_style(theme.font_family, theme.typography.body_large)
98
+ for i, opt in enumerate(self.owner.options):
99
+ item_rect = Rect(mr.x, mr.y + self.PAD_Y + i * self.ITEM_H, mr.width, self.ITEM_H)
100
+ is_selected = opt == self.owner.value
101
+ if is_selected:
102
+ canvas.drawRect(
103
+ skia.Rect.MakeXYWH(item_rect.x, item_rect.y, item_rect.width, item_rect.height),
104
+ skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()),
105
+ )
106
+ elif i == self._hover_idx:
107
+ canvas.drawRect(
108
+ skia.Rect.MakeXYWH(item_rect.x, item_rect.y, item_rect.width, item_rect.height),
109
+ skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
110
+ )
111
+
112
+ fg = c.on_secondary_container if is_selected else c.on_surface
113
+ draw_text_centered_y(canvas, opt, item_rect.x + 16.0, item_rect.center_y, font, fg)
114
+ if is_selected:
115
+ draw_icon(canvas, "check", item_rect.right - 24.0, item_rect.center_y, 16.0, fg)
116
+
117
+
118
+ class Select(Widget):
119
+ """Official Material Design 3 Exposed Dropdown Menu (`outlined` or `filled`)."""
120
+
121
+ interactive = True
122
+ focusable = True
123
+ manages_own_box = True
124
+ cursor = "pointer"
125
+
126
+ BOX_HEIGHT = 56.0
127
+ SUPPORTING_HEIGHT = 20.0
128
+
129
+ def __init__(
130
+ self,
131
+ label: str,
132
+ options: Sequence[str],
133
+ *,
134
+ variant: SelectVariant = "outlined",
135
+ value: str | None = None,
136
+ placeholder: str = "",
137
+ supporting_text: str | None = None,
138
+ error_text: str | None = None,
139
+ leading_icon: str | None = None,
140
+ disabled: bool = False,
141
+ bind: State[str | None] | None = None,
142
+ on_change: Callable[[str], None] | None = None,
143
+ ) -> None:
144
+ super().__init__()
145
+ self.label = label
146
+ self.options = list(options)
147
+ self.variant: SelectVariant = variant
148
+ self._bind = bind
149
+ self._value: str | None = bind.value if bind is not None else value
150
+ self.placeholder = placeholder
151
+ self.supporting_text = supporting_text
152
+ self._error_text = error_text
153
+ self.leading_icon = leading_icon
154
+ self.disabled = disabled
155
+ self.on_change = on_change
156
+
157
+ self._menu_open = False
158
+ self._overlay: _SelectMenuOverlay | None = None
159
+ self._float_anim = AnimatedValue(1.0 if self._should_float() else 0.0, duration=0.16)
160
+
161
+ if bind is not None:
162
+ bind.subscribe(self._on_bound_change)
163
+
164
+ def _should_float(self) -> bool:
165
+ return bool(self._value or self.placeholder or self._menu_open)
166
+
167
+ def _on_bound_change(self, new_val: str | None) -> None:
168
+ if new_val != self._value:
169
+ self._value = new_val
170
+ self._float_anim.animate_to(1.0 if self._should_float() else 0.0)
171
+ self.invalidate()
172
+
173
+ @property
174
+ def value(self) -> str | None:
175
+ return self._value
176
+
177
+ @value.setter
178
+ def value(self, val: str | None) -> None:
179
+ if val != self._value:
180
+ self._value = val
181
+ self._float_anim.animate_to(1.0 if self._should_float() else 0.0)
182
+ if self._bind is not None and self._bind.value != val:
183
+ self._bind.value = val
184
+ self.invalidate()
185
+
186
+ @property
187
+ def error_text(self) -> str | None:
188
+ return self._error_text
189
+
190
+ @error_text.setter
191
+ def error_text(self, val: str | None) -> None:
192
+ self._error_text = val
193
+ self.invalidate()
194
+
195
+ def _select_option(self, opt: str) -> None:
196
+ if opt != self._value:
197
+ self.value = opt
198
+ if self.on_change is not None:
199
+ self.on_change(opt)
200
+
201
+ def open_menu(self) -> None:
202
+ if self.disabled or not self.options or self.window is None:
203
+ return
204
+ self._menu_open = True
205
+ self._float_anim.animate_to(1.0)
206
+ self._overlay = _SelectMenuOverlay(self)
207
+ self.window.open_overlay(self._overlay)
208
+ self.invalidate()
209
+
210
+ def close_menu(self) -> None:
211
+ self._menu_open = False
212
+ if self._overlay is not None and self.window is not None:
213
+ self.window.close_overlay(self._overlay)
214
+ self._overlay = None
215
+ self._float_anim.animate_to(1.0 if self._should_float() else 0.0)
216
+ self.invalidate()
217
+
218
+ def _has_footer(self) -> bool:
219
+ return bool(self._error_text or self.supporting_text)
220
+
221
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
222
+ h = self.BOX_HEIGHT + (self.SUPPORTING_HEIGHT if self._has_footer() else 0.0)
223
+ w = 260.0 if constraints.max_width == float("inf") else min(260.0, constraints.max_width)
224
+ return Size(w, h)
225
+
226
+ def tick(self, now: float) -> bool:
227
+ # Sync _menu_open if user clicked outside and Window closed overlays
228
+ if self._menu_open and self.window is not None and self._overlay not in self.window._overlays:
229
+ self._menu_open = False
230
+ self._overlay = None
231
+ self._float_anim.animate_to(1.0 if self._should_float() else 0.0)
232
+ return self._float_anim.update(now)
233
+
234
+ def on_mouse_up(self, event: MouseEvent) -> None:
235
+ was_pressed = self.pressed
236
+ super().on_mouse_up(event)
237
+ if self.disabled or not was_pressed or not self.rect.contains(event.x, event.y):
238
+ return
239
+ if self._menu_open:
240
+ self.close_menu()
241
+ else:
242
+ self.open_menu()
243
+
244
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
245
+ c = theme.colors
246
+ is_error = bool(self._error_text)
247
+ active = self._menu_open or self.focused
248
+ box = Rect(self.rect.x, self.rect.y, self.rect.width, self.BOX_HEIGHT)
249
+ f_t = self._float_anim.current
250
+
251
+ if self.disabled:
252
+ outline_color = c.on_surface.with_alpha(0.12)
253
+ label_color = c.on_surface.with_alpha(0.38)
254
+ text_color = c.on_surface.with_alpha(0.38)
255
+ icon_color = c.on_surface.with_alpha(0.38)
256
+ elif is_error:
257
+ outline_color = c.error
258
+ label_color = c.error
259
+ text_color = c.on_surface
260
+ icon_color = c.error
261
+ elif active:
262
+ outline_color = c.primary
263
+ label_color = c.primary
264
+ text_color = c.on_surface
265
+ icon_color = c.primary
266
+ else:
267
+ outline_color = c.on_surface if self.hovered else c.outline
268
+ label_color = c.on_surface_variant
269
+ text_color = c.on_surface
270
+ icon_color = c.on_surface_variant
271
+
272
+ pad_left = 48.0 if self.leading_icon else 16.0
273
+ if not self.disabled and not is_error and not active and self._border_color_override is not None:
274
+ outline_color = resolve_color(self._border_color_override, theme, outline_color) or outline_color
275
+ if not self.disabled and self._fg_override is not None:
276
+ text_color = resolve_color(self._fg_override, theme, text_color) or text_color
277
+ border_w = self._border_width_override if self._border_width_override is not None else (2.0 if active else 1.0)
278
+
279
+ label_size = 16.0 - 4.0 * f_t
280
+ label_font = get_font(theme.font_family, label_size, 500 if active else 400)
281
+ label_w = measure_text(label_font, self.label)
282
+
283
+ if self.variant == "filled":
284
+ if self._elevation_override:
285
+ sh = resolve_color(self._shadow_color_override, theme, c.shadow)
286
+ assert sh is not None
287
+ draw_elevation_shadow(canvas, box, 4.0, self._elevation_override, sh)
288
+ rrect = skia.RRect()
289
+ rrect.setRectRadii(
290
+ skia.Rect.MakeXYWH(box.x, box.y, box.width, box.height),
291
+ [(4.0, 4.0), (4.0, 4.0), (0.0, 0.0), (0.0, 0.0)],
292
+ )
293
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
294
+ if self.hovered and not self.disabled:
295
+ bg = bg.blend(c.on_surface, theme.state_layers.hover)
296
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
297
+ canvas.drawLine(
298
+ box.x,
299
+ box.bottom - border_w * 0.5,
300
+ box.right,
301
+ box.bottom - border_w * 0.5,
302
+ skia.Paint(AntiAlias=True, Color=outline_color.to_skia(), StrokeWidth=border_w),
303
+ )
304
+ label_x = box.x + pad_left
305
+ label_cy = box.center_y * (1.0 - f_t) + (box.y + 15.0) * f_t
306
+ text_cy = box.y + 37.0
307
+ else:
308
+ rad_spec = self._shape_override if self._shape_override is not None else "extra_small"
309
+ rad_num = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
310
+ if self._elevation_override:
311
+ sh = resolve_color(self._shadow_color_override, theme, c.shadow)
312
+ assert sh is not None
313
+ draw_elevation_shadow(canvas, box, rad_num, self._elevation_override, sh)
314
+ rrect = make_rrect_spec(box, rad_spec, theme)
315
+ if self._bg_override is not None:
316
+ bg = resolve_color(self._bg_override, theme)
317
+ if bg is not None:
318
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
319
+ canvas.save()
320
+ if f_t > 0.05 and self.label:
321
+ notch_left = box.x + 12.0
322
+ notch_right = notch_left + label_w + 8.0
323
+ canvas.clipRect(
324
+ skia.Rect.MakeXYWH(notch_left, box.y - 4.0, notch_right - notch_left, 10.0),
325
+ skia.ClipOp.kDifference,
326
+ )
327
+ canvas.drawRRect(
328
+ rrect,
329
+ skia.Paint(
330
+ AntiAlias=True,
331
+ Color=outline_color.to_skia(),
332
+ Style=skia.Paint.kStroke_Style,
333
+ StrokeWidth=border_w,
334
+ ),
335
+ )
336
+ canvas.restore()
337
+ label_x = (box.x + pad_left) * (1.0 - f_t) + (box.x + 16.0) * f_t
338
+ label_cy = box.center_y * (1.0 - f_t) + box.y * f_t
339
+ text_cy = box.center_y
340
+
341
+ if self.leading_icon:
342
+ draw_icon(canvas, self.leading_icon, box.x + 24.0, box.center_y, 20.0, icon_color)
343
+
344
+ draw_icon(
345
+ canvas,
346
+ "chevron_up" if self._menu_open else "chevron_down",
347
+ box.right - 24.0,
348
+ box.center_y,
349
+ 18.0,
350
+ icon_color,
351
+ )
352
+
353
+ if self.label:
354
+ draw_text_centered_y(canvas, self.label, label_x, label_cy, label_font, label_color)
355
+
356
+ body_font = font_for_style(theme.font_family, theme.typography.body_large)
357
+ if self._value:
358
+ draw_text_centered_y(canvas, self._value, box.x + pad_left, text_cy, body_font, text_color)
359
+ elif self.placeholder and f_t > 0.8:
360
+ draw_text_centered_y(
361
+ canvas,
362
+ self.placeholder,
363
+ box.x + pad_left,
364
+ text_cy,
365
+ body_font,
366
+ c.on_surface_variant.with_alpha(0.65),
367
+ )
368
+
369
+ if self._has_footer():
370
+ footer_font = font_for_style(theme.font_family, theme.typography.body_small)
371
+ msg = self._error_text if is_error else (self.supporting_text or "")
372
+ msg_color = c.error if is_error else c.on_surface_variant
373
+ draw_text_centered_y(canvas, msg, box.x + 16.0, box.bottom + 12.0, footer_font, msg_color)
@@ -0,0 +1,249 @@
1
+ """Material Design 3 BottomSheet and Dropdown/Context Menu overlays."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from typing import TYPE_CHECKING, Callable, Sequence
7
+
8
+ import skia
9
+
10
+ from ..core.context import context
11
+ from ..core.draw import (
12
+ draw_elevation_shadow,
13
+ draw_icon,
14
+ draw_text_centered_y,
15
+ font_for_style,
16
+ make_rrect,
17
+ measure_text,
18
+ )
19
+ from ..core.events import MouseEvent
20
+ from ..core.geometry import Constraints, Rect
21
+ from ..core.widget import Widget
22
+ from ..theme import Theme
23
+ from .containers import Column
24
+
25
+ if TYPE_CHECKING:
26
+ from ..core.window import Window
27
+
28
+
29
+ class BottomSheet(Widget):
30
+ """Official Material Design 3 Modal BottomSheet (28dp top corners + drag handle + scrim)."""
31
+
32
+ interactive = True
33
+ auto_attach = False
34
+
35
+ def __init__(
36
+ self,
37
+ title: str | None = None,
38
+ *,
39
+ max_width: float = 640.0,
40
+ dismissible: bool = True,
41
+ window: Window | None = None,
42
+ ) -> None:
43
+ super().__init__()
44
+ self._owner_window: Window | None = window or context.current_window or context.last_window
45
+ self.title = title
46
+ self.max_width = float(max_width)
47
+ self.dismiss_on_outside_click = dismissible
48
+
49
+ with context.detached():
50
+ self._body = Column(gap=14.0, align="stretch")
51
+ self.add_child(self._body)
52
+ self._sheet_rect = Rect(0.0, 0.0, 0.0, 0.0)
53
+
54
+ def __enter__(self) -> BottomSheet:
55
+ context.push_container(self._body)
56
+ return self
57
+
58
+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
59
+ context.pop_container()
60
+
61
+ def open(self) -> None:
62
+ win = self._owner_window or context.last_window
63
+ if win is not None:
64
+ self._owner_window = win
65
+ win.open_overlay(self)
66
+
67
+ def close(self) -> None:
68
+ win = self._owner_window or self.window
69
+ if win is not None:
70
+ win.close_overlay(self)
71
+
72
+ def layout(self, rect: Rect, theme: Theme) -> None:
73
+ self.rect = rect
74
+ sheet_w = min(self.max_width, rect.width)
75
+ title_h = 36.0 if self.title else 0.0
76
+ body_c = Constraints(0.0, sheet_w - 48.0, 0.0, max(120.0, rect.height * 0.7))
77
+ body_sz = self._body.measure(body_c, theme)
78
+ sheet_h = min(rect.height * 0.82, 32.0 + title_h + body_sz.height + 28.0)
79
+
80
+ sx = rect.x + (rect.width - sheet_w) * 0.5
81
+ sy = rect.bottom - sheet_h
82
+ self._sheet_rect = Rect(sx, sy, sheet_w, sheet_h)
83
+ self._body.layout(
84
+ Rect(sx + 24.0, sy + 32.0 + title_h, sheet_w - 48.0, body_sz.height),
85
+ theme,
86
+ )
87
+
88
+ def hit_test(self, x: float, y: float) -> Widget | None:
89
+ if not self.rect.contains(x, y):
90
+ return None
91
+ if self._sheet_rect.contains(x, y):
92
+ hit = self._body.hit_test(x, y)
93
+ return hit if hit is not None else self
94
+ return self
95
+
96
+ def on_mouse_up(self, event: MouseEvent) -> None:
97
+ was_pressed = self.pressed
98
+ super().on_mouse_up(event)
99
+ if was_pressed and self.dismiss_on_outside_click and not self._sheet_rect.contains(event.x, event.y):
100
+ self.close()
101
+
102
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
103
+ c = theme.colors
104
+ # Scrim
105
+ canvas.drawRect(
106
+ skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height),
107
+ skia.Paint(AntiAlias=True, Color=c.scrim.with_alpha(0.34).to_skia()),
108
+ )
109
+
110
+ sr = self._sheet_rect
111
+ rrect = skia.RRect()
112
+ rrect.setRectRadii(
113
+ skia.Rect.MakeXYWH(sr.x, sr.y, sr.width, sr.height),
114
+ [(28.0, 28.0), (28.0, 28.0), (0.0, 0.0), (0.0, 0.0)],
115
+ )
116
+ draw_elevation_shadow(canvas, sr, 28.0, 2, c.shadow)
117
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=c.surface_container_low.to_skia()))
118
+
119
+ # Official M3 32x4dp centered drag handle
120
+ handle = Rect(sr.center_x - 16.0, sr.y + 14.0, 32.0, 4.0)
121
+ canvas.drawRRect(
122
+ make_rrect(handle, 2.0),
123
+ skia.Paint(AntiAlias=True, Color=c.on_surface_variant.with_alpha(0.4).to_skia()),
124
+ )
125
+
126
+ if self.title:
127
+ tfont = font_for_style(theme.font_family, theme.typography.title_large)
128
+ draw_text_centered_y(canvas, self.title, sr.x + 24.0, sr.y + 44.0, tfont, c.on_surface)
129
+
130
+
131
+ @dataclass
132
+ class MenuItem:
133
+ label: str
134
+ icon: str | None = None
135
+ shortcut: str | None = None
136
+ on_click: Callable[[], None] | None = None
137
+ disabled: bool = False
138
+
139
+
140
+ class Menu(Widget):
141
+ """Official Material Design 3 Dropdown / Context Menu overlay."""
142
+
143
+ interactive = True
144
+ auto_attach = False
145
+ cursor = "pointer"
146
+
147
+ ITEM_H = 44.0
148
+ PAD_Y = 8.0
149
+
150
+ def __init__(
151
+ self,
152
+ items: Sequence[MenuItem | tuple[str, Callable[[], None]] | tuple[str, str, Callable[[], None]]],
153
+ *,
154
+ width: float = 220.0,
155
+ window: Window | None = None,
156
+ ) -> None:
157
+ super().__init__()
158
+ self._owner_window: Window | None = window or context.current_window or context.last_window
159
+ self.menu_width = float(width)
160
+ self.items: list[MenuItem] = []
161
+ for it in items:
162
+ if isinstance(it, MenuItem):
163
+ self.items.append(it)
164
+ elif len(it) == 2:
165
+ self.items.append(MenuItem(label=it[0], on_click=it[1]))
166
+ else:
167
+ self.items.append(MenuItem(icon=it[0], label=it[1], on_click=it[2]))
168
+
169
+ self._anchor_pos: tuple[float, float] = (40.0, 40.0)
170
+ self._menu_rect = Rect(0.0, 0.0, 0.0, 0.0)
171
+ self._hover_idx: int = -1
172
+
173
+ def open_at(self, x: float, y: float) -> None:
174
+ self._anchor_pos = (float(x), float(y))
175
+ win = self._owner_window or context.last_window
176
+ if win is not None:
177
+ self._owner_window = win
178
+ win.open_overlay(self)
179
+
180
+ def open_for(self, anchor: Widget) -> None:
181
+ self.open_at(anchor.rect.x, anchor.rect.bottom + 4.0)
182
+
183
+ def close(self) -> None:
184
+ win = self._owner_window or self.window
185
+ if win is not None:
186
+ win.close_overlay(self)
187
+
188
+ def layout(self, rect: Rect, theme: Theme) -> None:
189
+ self.rect = rect
190
+ mh = len(self.items) * self.ITEM_H + self.PAD_Y * 2.0
191
+ x = max(8.0, min(rect.width - self.menu_width - 8.0, self._anchor_pos[0]))
192
+ y = max(8.0, min(rect.height - mh - 8.0, self._anchor_pos[1]))
193
+ self._menu_rect = Rect(x, y, self.menu_width, mh)
194
+
195
+ def hit_test(self, x: float, y: float) -> Widget | None:
196
+ return self if self._menu_rect.contains(x, y) else None
197
+
198
+ def _index_at(self, y: float) -> int:
199
+ idx = int((y - (self._menu_rect.y + self.PAD_Y)) // self.ITEM_H)
200
+ return idx if 0 <= idx < len(self.items) else -1
201
+
202
+ def on_mouse_move(self, event: MouseEvent) -> None:
203
+ idx = self._index_at(event.y)
204
+ if idx != self._hover_idx:
205
+ self._hover_idx = idx
206
+ self.invalidate()
207
+
208
+ def on_mouse_up(self, event: MouseEvent) -> None:
209
+ was_pressed = self.pressed
210
+ super().on_mouse_up(event)
211
+ if not was_pressed:
212
+ return
213
+ idx = self._index_at(event.y)
214
+ if 0 <= idx < len(self.items):
215
+ item = self.items[idx]
216
+ if not item.disabled:
217
+ self.close()
218
+ if item.on_click is not None:
219
+ item.on_click()
220
+
221
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
222
+ c = theme.colors
223
+ mr = self._menu_rect
224
+ rad = theme.shape.extra_small
225
+
226
+ draw_elevation_shadow(canvas, mr, rad, 2, c.shadow)
227
+ canvas.drawRRect(make_rrect(mr, rad), skia.Paint(AntiAlias=True, Color=c.surface_container.to_skia()))
228
+
229
+ font = font_for_style(theme.font_family, theme.typography.body_large)
230
+ sc_font = font_for_style(theme.font_family, theme.typography.label_medium)
231
+
232
+ for i, item in enumerate(self.items):
233
+ ir = Rect(mr.x, mr.y + self.PAD_Y + i * self.ITEM_H, mr.width, self.ITEM_H)
234
+ if not item.disabled and i == self._hover_idx:
235
+ canvas.drawRect(
236
+ skia.Rect.MakeXYWH(ir.x, ir.y, ir.width, ir.height),
237
+ skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
238
+ )
239
+
240
+ fg = c.on_surface.with_alpha(0.38) if item.disabled else c.on_surface
241
+ ic = c.on_surface.with_alpha(0.38) if item.disabled else c.on_surface_variant
242
+ tx = ir.x + 16.0
243
+ if item.icon:
244
+ draw_icon(canvas, item.icon, ir.x + 24.0, ir.center_y, 18.0, ic)
245
+ tx = ir.x + 48.0
246
+ draw_text_centered_y(canvas, item.label, tx, ir.center_y, font, fg)
247
+ if item.shortcut:
248
+ sw = measure_text(sc_font, item.shortcut)
249
+ draw_text_centered_y(canvas, item.shortcut, ir.right - 16.0 - sw, ir.center_y, sc_font, ic)