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,703 @@
1
+ """Phase 8 Pro Form Controls (Part 1): Autocomplete, TagInput, OTPInput, and Rating."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import math
6
+ from typing import Callable, Literal, Sequence
7
+
8
+ import glfw
9
+ import skia
10
+
11
+ from ..core.draw import (
12
+ draw_elevation_shadow,
13
+ draw_icon,
14
+ draw_text_centered_y,
15
+ font_for_style,
16
+ get_font,
17
+ make_rrect,
18
+ make_rrect_spec,
19
+ measure_text,
20
+ resolve_color,
21
+ )
22
+ from ..core.events import CharEvent, KeyEvent, MouseEvent
23
+ from ..core.geometry import Constraints, Rect, Size
24
+ from ..core.state import State
25
+ from ..core.widget import Widget
26
+ from ..theme import Color, Theme
27
+
28
+ FieldVariant = Literal["outlined", "filled"]
29
+
30
+
31
+ # ---------------------------------------------------------------------------
32
+ # 1. Autocomplete / ComboBox
33
+ # ---------------------------------------------------------------------------
34
+
35
+
36
+ class _AutocompleteOverlay(Widget):
37
+ interactive = True
38
+ auto_attach = False
39
+ cursor = "pointer"
40
+
41
+ ITEM_H = 40.0
42
+ PAD_Y = 6.0
43
+
44
+ def __init__(self, owner: Autocomplete) -> None:
45
+ super().__init__()
46
+ self.owner = owner
47
+ self._hover_idx: int = -1
48
+ self._menu_rect: Rect = Rect(0, 0, 0, 0)
49
+
50
+ def _compute_rect(self) -> Rect:
51
+ items = self.owner._filtered[: self.owner.max_suggestions]
52
+ h = max(self.ITEM_H, len(items) * self.ITEM_H) + self.PAD_Y * 2.0
53
+ o = self.owner.rect
54
+ y = o.y + self.owner.BOX_HEIGHT + 4.0
55
+ if self.window and y + h > self.window.height - 8.0:
56
+ y = max(8.0, o.y - h - 4.0)
57
+ return Rect(o.x, y, o.width, h)
58
+
59
+ def hit_test(self, x: float, y: float) -> Widget | None:
60
+ self._menu_rect = self._compute_rect()
61
+ return self if self._menu_rect.contains(x, y) else None
62
+
63
+ def _idx_at(self, y: float) -> int:
64
+ idx = int((y - (self._menu_rect.y + self.PAD_Y)) // self.ITEM_H)
65
+ items = self.owner._filtered[: self.owner.max_suggestions]
66
+ return idx if 0 <= idx < len(items) else -1
67
+
68
+ def on_mouse_move(self, event: MouseEvent) -> None:
69
+ idx = self._idx_at(event.y)
70
+ if idx != self._hover_idx:
71
+ self._hover_idx = idx
72
+ self.invalidate()
73
+
74
+ def on_mouse_up(self, event: MouseEvent) -> None:
75
+ super().on_mouse_up(event)
76
+ idx = self._idx_at(event.y)
77
+ items = self.owner._filtered[: self.owner.max_suggestions]
78
+ if 0 <= idx < len(items):
79
+ self.owner._select_option(items[idx])
80
+
81
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
82
+ c = theme.colors
83
+ mr = self._compute_rect()
84
+ self._menu_rect = mr
85
+ draw_elevation_shadow(canvas, mr, theme.shape.extra_small, 2, c.shadow)
86
+ rrect = make_rrect(mr, theme.shape.extra_small)
87
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=c.surface_container.to_skia()))
88
+
89
+ items = self.owner._filtered[: self.owner.max_suggestions]
90
+ font = font_for_style(theme.font_family, theme.typography.body_medium)
91
+ if not items:
92
+ draw_text_centered_y(canvas, "Sin coincidencias", mr.x + 16.0, mr.center_y, font, c.on_surface_variant)
93
+ return
94
+
95
+ for i, opt in enumerate(items):
96
+ ir = Rect(mr.x, mr.y + self.PAD_Y + i * self.ITEM_H, mr.width, self.ITEM_H)
97
+ if i == self._hover_idx or i == self.owner._active_idx:
98
+ canvas.drawRect(
99
+ skia.Rect.MakeXYWH(ir.x, ir.y, ir.width, ir.height),
100
+ skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()),
101
+ )
102
+ draw_text_centered_y(canvas, opt, ir.x + 16.0, ir.center_y, font, c.on_surface)
103
+
104
+
105
+ class Autocomplete(Widget):
106
+ """Material Design 3 Autocomplete / ComboBox with live filtering and keyboard navigation."""
107
+
108
+ interactive = True
109
+ focusable = True
110
+ manages_own_box = True
111
+ cursor = "ibeam"
112
+
113
+ BOX_HEIGHT = 56.0
114
+
115
+ def __init__(
116
+ self,
117
+ label: str,
118
+ options: Sequence[str],
119
+ *,
120
+ value: str = "",
121
+ variant: FieldVariant = "outlined",
122
+ placeholder: str = "",
123
+ max_suggestions: int = 6,
124
+ disabled: bool = False,
125
+ bind: State[str] | None = None,
126
+ on_change: Callable[[str], None] | None = None,
127
+ ) -> None:
128
+ super().__init__()
129
+ self.label = label
130
+ self.options: list[str] = list(options)
131
+ self.variant: FieldVariant = variant
132
+ self.placeholder = placeholder
133
+ self.max_suggestions = max(1, int(max_suggestions))
134
+ self.disabled = disabled
135
+ self._bind = bind
136
+ self.on_change = on_change
137
+ self._value: str = str(bind.value) if bind is not None else str(value)
138
+ self._filtered: list[str] = list(self.options)
139
+ self._active_idx: int = 0
140
+ self._overlay = _AutocompleteOverlay(self)
141
+ self._open: bool = False
142
+ self._update_filter()
143
+ if bind is not None:
144
+ bind.subscribe(self._on_bound)
145
+
146
+ def _on_bound(self, v: str) -> None:
147
+ self._value = str(v)
148
+ self._update_filter()
149
+ self.invalidate()
150
+
151
+ @property
152
+ def value(self) -> str:
153
+ return self._value
154
+
155
+ @value.setter
156
+ def value(self, val: str) -> None:
157
+ self._value = str(val)
158
+ self._update_filter()
159
+ if self._bind is not None:
160
+ self._bind.value = self._value
161
+ self.invalidate()
162
+
163
+ def _update_filter(self) -> None:
164
+ q = self._value.strip().lower()
165
+ self._filtered = [o for o in self.options if q in o.lower()] if q else list(self.options)
166
+ self._active_idx = 0
167
+
168
+ def _open_dropdown(self) -> None:
169
+ if self.disabled or self._open:
170
+ return
171
+ self._open = True
172
+ self._update_filter()
173
+ if self.window is not None:
174
+ self.window.open_overlay(self._overlay)
175
+
176
+ def _close_dropdown(self) -> None:
177
+ if not self._open:
178
+ return
179
+ self._open = False
180
+ if self.window is not None:
181
+ self.window.close_overlay(self._overlay)
182
+
183
+ def _select_option(self, opt: str) -> None:
184
+ self._value = opt
185
+ self._update_filter()
186
+ if self._bind is not None:
187
+ self._bind.value = opt
188
+ if self.on_change is not None:
189
+ self.on_change(opt)
190
+ self._close_dropdown()
191
+ self.invalidate()
192
+
193
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
194
+ w = constraints.max_width if constraints.max_width != float("inf") else 260.0
195
+ return Size(w, self.BOX_HEIGHT)
196
+
197
+ def on_focus(self) -> None:
198
+ super().on_focus()
199
+ self._open_dropdown()
200
+
201
+ def on_blur(self) -> None:
202
+ super().on_blur()
203
+ self._close_dropdown()
204
+
205
+ def on_mouse_down(self, event: MouseEvent) -> None:
206
+ super().on_mouse_down(event)
207
+ if not self.disabled:
208
+ self._open_dropdown()
209
+
210
+ def on_char(self, event: CharEvent) -> None:
211
+ if self.disabled:
212
+ return
213
+ self._value += event.char
214
+ self._update_filter()
215
+ self._open_dropdown()
216
+ if self._bind is not None:
217
+ self._bind.value = self._value
218
+ if self.on_change is not None:
219
+ self.on_change(self._value)
220
+ self.invalidate()
221
+
222
+ def on_key(self, event: KeyEvent) -> None:
223
+ if self.disabled or event.action not in (glfw.PRESS, glfw.REPEAT):
224
+ return
225
+ vis = self._filtered[: self.max_suggestions]
226
+ if event.key == glfw.KEY_BACKSPACE:
227
+ self._value = self._value[:-1]
228
+ self._update_filter()
229
+ if self._bind is not None:
230
+ self._bind.value = self._value
231
+ if self.on_change is not None:
232
+ self.on_change(self._value)
233
+ self.invalidate()
234
+ elif event.key == glfw.KEY_DOWN and vis:
235
+ self._active_idx = (self._active_idx + 1) % len(vis)
236
+ self.invalidate()
237
+ elif event.key == glfw.KEY_UP and vis:
238
+ self._active_idx = (self._active_idx - 1) % len(vis)
239
+ self.invalidate()
240
+ elif event.key in (glfw.KEY_ENTER, glfw.KEY_KP_ENTER) and vis:
241
+ self._select_option(vis[self._active_idx])
242
+ elif event.key == glfw.KEY_ESCAPE:
243
+ self._close_dropdown()
244
+
245
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
246
+ c = theme.colors
247
+ r = self.rect
248
+ rad_spec = self._shape_override if self._shape_override is not None else "extra_small"
249
+ rad = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
250
+ if self._elevation_override:
251
+ sh = resolve_color(self._shadow_color_override, theme, c.shadow)
252
+ assert sh is not None
253
+ draw_elevation_shadow(canvas, r, rad, self._elevation_override, sh)
254
+
255
+ outline_c = c.primary if self.focused else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
256
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if self.focused else 1.0)
257
+ rrect = make_rrect_spec(r, rad_spec, theme)
258
+
259
+ if self.variant == "filled":
260
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
261
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
262
+ canvas.drawLine(r.x, r.bottom - bw * 0.5, r.right, r.bottom - bw * 0.5, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), StrokeWidth=bw))
263
+ else:
264
+ if self._bg_override is not None:
265
+ bg = resolve_color(self._bg_override, theme)
266
+ if bg is not None:
267
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
268
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw))
269
+
270
+ lfont = get_font(theme.font_family, 12.0, 500 if self.focused else 400)
271
+ bfont = font_for_style(theme.font_family, theme.typography.body_large)
272
+ draw_text_centered_y(canvas, self.label, r.x + 14.0, r.y + 14.0, lfont, c.primary if self.focused else c.on_surface_variant)
273
+
274
+ shown = self._value if self._value else self.placeholder
275
+ col_txt = c.on_surface if self._value else c.on_surface_variant
276
+ if shown:
277
+ draw_text_centered_y(canvas, shown, r.x + 14.0, r.y + 36.0, bfont, col_txt)
278
+ draw_icon(canvas, "search", r.right - 22.0, r.center_y, 18.0, c.on_surface_variant)
279
+
280
+
281
+ # ---------------------------------------------------------------------------
282
+ # 2. TagInput / MultiSelect Chips Input
283
+ # ---------------------------------------------------------------------------
284
+
285
+
286
+ class TagInput(Widget):
287
+ """Material Design 3 multi-tag input displaying removable chips inline."""
288
+
289
+ interactive = True
290
+ focusable = True
291
+ manages_own_box = True
292
+ cursor = "ibeam"
293
+
294
+ def __init__(
295
+ self,
296
+ label: str = "Etiquetas",
297
+ *,
298
+ tags: Sequence[str] = (),
299
+ placeholder: str = "Escribe y pulsa Enter...",
300
+ variant: FieldVariant = "outlined",
301
+ max_tags: int | None = None,
302
+ disabled: bool = False,
303
+ bind: State[list[str]] | None = None,
304
+ on_change: Callable[[list[str]], None] | None = None,
305
+ ) -> None:
306
+ super().__init__()
307
+ self.label = label
308
+ self.placeholder = placeholder
309
+ self.variant: FieldVariant = variant
310
+ self.max_tags = max_tags
311
+ self.disabled = disabled
312
+ self._bind = bind
313
+ self.on_change = on_change
314
+ self._tags: list[str] = list(bind.value) if bind is not None else list(tags)
315
+ self._buffer: str = ""
316
+ self._chip_close_rects: list[tuple[int, Rect]] = []
317
+ if bind is not None:
318
+ bind.subscribe(self._on_bound)
319
+
320
+ def _on_bound(self, new_tags: list[str]) -> None:
321
+ self._tags = list(new_tags)
322
+ self.invalidate()
323
+
324
+ @property
325
+ def value(self) -> list[str]:
326
+ return list(self._tags)
327
+
328
+ @value.setter
329
+ def value(self, val: Sequence[str]) -> None:
330
+ self._tags = list(val)
331
+ if self._bind is not None:
332
+ self._bind.value = list(self._tags)
333
+ self.invalidate()
334
+
335
+ def add_tag(self, tag: str) -> bool:
336
+ t = tag.strip()
337
+ if not t or t in self._tags:
338
+ return False
339
+ if self.max_tags is not None and len(self._tags) >= self.max_tags:
340
+ return False
341
+ self._tags.append(t)
342
+ if self._bind is not None:
343
+ self._bind.value = list(self._tags)
344
+ if self.on_change is not None:
345
+ self.on_change(list(self._tags))
346
+ self.invalidate()
347
+ return True
348
+
349
+ def remove_tag(self, index: int) -> None:
350
+ if 0 <= index < len(self._tags):
351
+ self._tags.pop(index)
352
+ if self._bind is not None:
353
+ self._bind.value = list(self._tags)
354
+ if self.on_change is not None:
355
+ self.on_change(list(self._tags))
356
+ self.invalidate()
357
+
358
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
359
+ w = constraints.max_width if constraints.max_width != float("inf") else 320.0
360
+ return Size(w, 64.0)
361
+
362
+ def on_mouse_down(self, event: MouseEvent) -> None:
363
+ super().on_mouse_down(event)
364
+ if self.disabled:
365
+ return
366
+ for idx, cr in self._chip_close_rects:
367
+ if cr.contains(event.x, event.y):
368
+ self.remove_tag(idx)
369
+ return
370
+
371
+ def on_char(self, event: CharEvent) -> None:
372
+ if self.disabled:
373
+ return
374
+ if event.char == ",":
375
+ if self.add_tag(self._buffer):
376
+ self._buffer = ""
377
+ return
378
+ self._buffer += event.char
379
+ self.invalidate()
380
+
381
+ def on_key(self, event: KeyEvent) -> None:
382
+ if self.disabled or event.action not in (glfw.PRESS, glfw.REPEAT):
383
+ return
384
+ if event.key in (glfw.KEY_ENTER, glfw.KEY_KP_ENTER):
385
+ if self.add_tag(self._buffer):
386
+ self._buffer = ""
387
+ elif event.key == glfw.KEY_BACKSPACE:
388
+ if self._buffer:
389
+ self._buffer = self._buffer[:-1]
390
+ self.invalidate()
391
+ elif self._tags:
392
+ self.remove_tag(len(self._tags) - 1)
393
+
394
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
395
+ c = theme.colors
396
+ r = self.rect
397
+ rad_spec = self._shape_override if self._shape_override is not None else "extra_small"
398
+ rrect = make_rrect_spec(r, rad_spec, theme)
399
+ outline_c = c.primary if self.focused else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
400
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if self.focused else 1.0)
401
+
402
+ if self.variant == "filled":
403
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
404
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
405
+ canvas.drawLine(r.x, r.bottom - bw * 0.5, r.right, r.bottom - bw * 0.5, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), StrokeWidth=bw))
406
+ else:
407
+ if self._bg_override is not None:
408
+ bg = resolve_color(self._bg_override, theme)
409
+ if bg is not None:
410
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
411
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw))
412
+
413
+ lfont = get_font(theme.font_family, 11.5, 500 if self.focused else 400)
414
+ cfont = font_for_style(theme.font_family, theme.typography.label_medium)
415
+ bfont = font_for_style(theme.font_family, theme.typography.body_medium)
416
+ draw_text_centered_y(canvas, self.label, r.x + 12.0, r.y + 13.0, lfont, c.primary if self.focused else c.on_surface_variant)
417
+
418
+ self._chip_close_rects = []
419
+ x = r.x + 12.0
420
+ cy = r.y + 40.0
421
+ chip_h = 26.0
422
+
423
+ canvas.save()
424
+ canvas.clipRect(skia.Rect.MakeXYWH(r.x + 6.0, r.y + 22.0, max(0.0, r.width - 12.0), r.height - 24.0))
425
+ for i, tag in enumerate(self._tags):
426
+ tw = measure_text(cfont, tag)
427
+ cw = tw + 30.0
428
+ cr = Rect(x, cy - chip_h * 0.5, cw, chip_h)
429
+ canvas.drawRRect(make_rrect(cr, 8.0), skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()))
430
+ draw_text_centered_y(canvas, tag, cr.x + 8.0, cr.center_y, cfont, c.on_secondary_container)
431
+ close_r = Rect(cr.right - 20.0, cr.y + 3.0, 18.0, 20.0)
432
+ draw_icon(canvas, "close", close_r.center_x, close_r.center_y, 11.0, c.on_secondary_container)
433
+ self._chip_close_rects.append((i, close_r))
434
+ x += cw + 6.0
435
+
436
+ if self._buffer:
437
+ draw_text_centered_y(canvas, self._buffer + ("|" if self.focused else ""), x + 2.0, cy, bfont, c.on_surface)
438
+ elif not self._tags:
439
+ draw_text_centered_y(canvas, self.placeholder, x + 2.0, cy, bfont, c.on_surface_variant)
440
+ canvas.restore()
441
+
442
+
443
+ # ---------------------------------------------------------------------------
444
+ # 3. OTPInput / PinInput (2FA)
445
+ # ---------------------------------------------------------------------------
446
+
447
+
448
+ class OTPInput(Widget):
449
+ """Individual-box 2FA / PIN code input with auto-advance and completion callback."""
450
+
451
+ interactive = True
452
+ focusable = True
453
+ manages_own_box = True
454
+ cursor = "ibeam"
455
+
456
+ BOX_W = 46.0
457
+ BOX_H = 54.0
458
+ GAP = 10.0
459
+
460
+ def __init__(
461
+ self,
462
+ *,
463
+ length: int = 6,
464
+ value: str = "",
465
+ password: bool = False,
466
+ digits_only: bool = True,
467
+ disabled: bool = False,
468
+ bind: State[str] | None = None,
469
+ on_change: Callable[[str], None] | None = None,
470
+ on_complete: Callable[[str], None] | None = None,
471
+ ) -> None:
472
+ super().__init__()
473
+ self.length = max(2, min(10, int(length)))
474
+ self.password = password
475
+ self.digits_only = digits_only
476
+ self.disabled = disabled
477
+ self._bind = bind
478
+ self.on_change = on_change
479
+ self.on_complete = on_complete
480
+ raw = str(bind.value) if bind is not None else str(value)
481
+ self._value: str = self._sanitize(raw)
482
+ if bind is not None:
483
+ bind.subscribe(self._on_bound)
484
+
485
+ def _sanitize(self, s: str) -> str:
486
+ filtered = "".join(ch for ch in s if (ch.isdigit() if self.digits_only else ch.isalnum()))
487
+ return filtered[: self.length]
488
+
489
+ def _on_bound(self, v: str) -> None:
490
+ self._value = self._sanitize(str(v))
491
+ self.invalidate()
492
+
493
+ @property
494
+ def value(self) -> str:
495
+ return self._value
496
+
497
+ @value.setter
498
+ def value(self, val: str) -> None:
499
+ self._value = self._sanitize(str(val))
500
+ if self._bind is not None:
501
+ self._bind.value = self._value
502
+ self.invalidate()
503
+
504
+ @property
505
+ def is_complete(self) -> bool:
506
+ return len(self._value) == self.length
507
+
508
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
509
+ w = self.length * self.BOX_W + (self.length - 1) * self.GAP
510
+ return Size(w, self.BOX_H)
511
+
512
+ def _emit(self) -> None:
513
+ if self._bind is not None:
514
+ self._bind.value = self._value
515
+ if self.on_change is not None:
516
+ self.on_change(self._value)
517
+ if len(self._value) == self.length and self.on_complete is not None:
518
+ self.on_complete(self._value)
519
+ self.invalidate()
520
+
521
+ def on_char(self, event: CharEvent) -> None:
522
+ if self.disabled or len(self._value) >= self.length:
523
+ return
524
+ ch = event.char
525
+ if self.digits_only and not ch.isdigit():
526
+ return
527
+ if not ch.isalnum():
528
+ return
529
+ self._value += ch.upper()
530
+ self._emit()
531
+
532
+ def on_key(self, event: KeyEvent) -> None:
533
+ if self.disabled or event.action not in (glfw.PRESS, glfw.REPEAT):
534
+ return
535
+ if event.key == glfw.KEY_BACKSPACE and self._value:
536
+ self._value = self._value[:-1]
537
+ self._emit()
538
+ elif event.key == glfw.KEY_V and (event.mods & glfw.MOD_CONTROL):
539
+ try:
540
+ clip = glfw.get_clipboard_string(self.window._glfw_window) if self.window and self.window._glfw_window else None
541
+ if clip:
542
+ text = clip.decode("utf-8", errors="ignore") if isinstance(clip, bytes) else str(clip)
543
+ self._value = self._sanitize(text)
544
+ self._emit()
545
+ except Exception:
546
+ pass
547
+
548
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
549
+ c = theme.colors
550
+ font = font_for_style(theme.font_family, theme.typography.headline_small)
551
+ active_slot = min(len(self._value), self.length - 1) if self.focused else -1
552
+
553
+ for i in range(self.length):
554
+ bx = self.rect.x + i * (self.BOX_W + self.GAP)
555
+ br = Rect(bx, self.rect.y, self.BOX_W, self.BOX_H)
556
+ rr = make_rrect(br, theme.shape.resolve(self._shape_override or "medium"))
557
+
558
+ bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
559
+ canvas.drawRRect(rr, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
560
+
561
+ is_active = i == active_slot
562
+ bc = c.primary if is_active else (resolve_color(self._border_color_override, theme, c.outline) or c.outline)
563
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if is_active else 1.2)
564
+ canvas.drawRRect(rr, skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw))
565
+
566
+ if i < len(self._value):
567
+ ch = "•" if self.password else self._value[i]
568
+ tw = measure_text(font, ch)
569
+ fg = resolve_color(self._fg_override, theme, c.on_surface) or c.on_surface
570
+ draw_text_centered_y(canvas, ch, br.center_x - tw * 0.5, br.center_y, font, fg)
571
+
572
+
573
+ # ---------------------------------------------------------------------------
574
+ # 4. Rating (Interactive Star Selector)
575
+ # ---------------------------------------------------------------------------
576
+
577
+
578
+ def _star_path(cx: float, cy: float, outer_r: float) -> skia.Path:
579
+ inner_r = outer_r * 0.44
580
+ path = skia.Path()
581
+ for i in range(10):
582
+ ang = -math.pi * 0.5 + i * (math.pi / 5.0)
583
+ r = outer_r if i % 2 == 0 else inner_r
584
+ px = cx + math.cos(ang) * r
585
+ py = cy + math.sin(ang) * r
586
+ if i == 0:
587
+ path.moveTo(px, py)
588
+ else:
589
+ path.lineTo(px, py)
590
+ path.close()
591
+ return path
592
+
593
+
594
+ class Rating(Widget):
595
+ """Interactive GPU Star Rating widget supporting full or half-star precision."""
596
+
597
+ interactive = True
598
+ focusable = True
599
+ manages_own_box = False
600
+ cursor = "pointer"
601
+
602
+ def __init__(
603
+ self,
604
+ *,
605
+ value: float = 0.0,
606
+ max_stars: int = 5,
607
+ allow_half: bool = False,
608
+ star_size: float = 28.0,
609
+ disabled: bool = False,
610
+ bind: State[float] | None = None,
611
+ on_change: Callable[[float], None] | None = None,
612
+ ) -> None:
613
+ super().__init__()
614
+ self.max_stars = max(1, int(max_stars))
615
+ self.allow_half = allow_half
616
+ self.star_size = float(star_size)
617
+ self.disabled = disabled
618
+ self._bind = bind
619
+ self.on_change = on_change
620
+ raw = float(bind.value) if bind is not None else float(value)
621
+ self._value: float = max(0.0, min(float(self.max_stars), raw))
622
+ self._hover_val: float | None = None
623
+ if bind is not None:
624
+ bind.subscribe(self._on_bound)
625
+
626
+ def _on_bound(self, v: float) -> None:
627
+ self._value = max(0.0, min(float(self.max_stars), float(v)))
628
+ self.invalidate()
629
+
630
+ @property
631
+ def value(self) -> float:
632
+ return self._value
633
+
634
+ @value.setter
635
+ def value(self, val: float) -> None:
636
+ self._value = max(0.0, min(float(self.max_stars), float(val)))
637
+ if self._bind is not None:
638
+ self._bind.value = self._value
639
+ self.invalidate()
640
+
641
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
642
+ gap = 6.0
643
+ w = self.max_stars * self.star_size + (self.max_stars - 1) * gap
644
+ return Size(w, self.star_size + 4.0)
645
+
646
+ def _value_at(self, x: float) -> float:
647
+ step = self.star_size + 6.0
648
+ rel = max(0.0, x - self.rect.x)
649
+ idx = int(rel // step)
650
+ if idx >= self.max_stars:
651
+ return float(self.max_stars)
652
+ within = (rel - idx * step) / self.star_size
653
+ if self.allow_half and within <= 0.5:
654
+ return idx + 0.5
655
+ return float(idx + 1)
656
+
657
+ def on_mouse_move(self, event: MouseEvent) -> None:
658
+ if self.disabled:
659
+ return
660
+ v = self._value_at(event.x)
661
+ if v != self._hover_val:
662
+ self._hover_val = v
663
+ self.invalidate()
664
+
665
+ def on_mouse_leave(self) -> None:
666
+ super().on_mouse_leave()
667
+ self._hover_val = None
668
+
669
+ def on_mouse_up(self, event: MouseEvent) -> None:
670
+ was_pressed = self.pressed
671
+ super().on_mouse_up(event)
672
+ if self.disabled or not was_pressed or not self.rect.contains(event.x, event.y):
673
+ return
674
+ self._value = self._value_at(event.x)
675
+ if self._bind is not None:
676
+ self._bind.value = self._value
677
+ if self.on_change is not None:
678
+ self.on_change(self._value)
679
+ self.invalidate()
680
+
681
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
682
+ c = theme.colors
683
+ shown = self._hover_val if self._hover_val is not None else self._value
684
+ active_c = resolve_color(self._fg_override, theme, c.primary) or c.primary
685
+ if self.disabled:
686
+ active_c = c.on_surface.with_alpha(0.38)
687
+ empty_c = c.outline_variant
688
+ step = self.star_size + 6.0
689
+ rad = self.star_size * 0.48
690
+ cy = self.rect.center_y
691
+
692
+ for i in range(self.max_stars):
693
+ cx = self.rect.x + i * step + self.star_size * 0.5
694
+ star = _star_path(cx, cy, rad)
695
+ fill_amt = max(0.0, min(1.0, shown - i))
696
+ # Empty base
697
+ canvas.drawPath(star, skia.Paint(AntiAlias=True, Color=empty_c.to_skia()))
698
+ if fill_amt > 0.0:
699
+ canvas.save()
700
+ if fill_amt < 1.0:
701
+ canvas.clipRect(skia.Rect.MakeXYWH(cx - rad, cy - rad, rad * 2.0 * fill_amt, rad * 2.0))
702
+ canvas.drawPath(star, skia.Paint(AntiAlias=True, Color=active_c.to_skia()))
703
+ canvas.restore()