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,209 @@
1
+ """Material Design 3 Slider with continuous/discrete ticks and value indicator."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Callable
6
+
7
+ import skia
8
+
9
+ from ..core.animation import AnimatedValue
10
+ from ..core.draw import draw_text_centered_y, font_for_style, make_rrect, measure_text, resolve_color
11
+ from ..core.events import MouseEvent
12
+ from ..core.geometry import Constraints, Rect, Size
13
+ from ..core.state import State
14
+ from ..core.widget import Widget
15
+ from ..theme import Theme
16
+
17
+
18
+ class Slider(Widget):
19
+ """Official Material Design 3 Slider (continuous or discrete with `step`)."""
20
+
21
+ interactive = True
22
+ focusable = True
23
+ cursor = "ew_resize"
24
+
25
+ HEIGHT = 44.0
26
+ PAD_X = 14.0
27
+
28
+ def __init__(
29
+ self,
30
+ *,
31
+ value: float = 50.0,
32
+ min: float = 0.0,
33
+ max: float = 100.0,
34
+ step: float | None = None,
35
+ label: bool = True,
36
+ disabled: bool = False,
37
+ bind: State[float] | None = None,
38
+ on_change: Callable[[float], None] | None = None,
39
+ ) -> None:
40
+ super().__init__()
41
+ self.min_val = float(min)
42
+ self.max_val = float(max) if max > min else float(min) + 1.0
43
+ self.step = float(step) if step is not None and step > 0 else None
44
+ self.show_label = label
45
+ self.disabled = disabled
46
+ self._bind = bind
47
+ self.on_change = on_change
48
+
49
+ raw = float(bind.value) if bind is not None else float(value)
50
+ self._value = self._clamp_and_snap(raw)
51
+ self._bubble_anim = AnimatedValue(0.0, duration=0.14)
52
+
53
+ if bind is not None:
54
+ bind.subscribe(self._on_bound_change)
55
+
56
+ def _clamp_and_snap(self, v: float) -> float:
57
+ v = max(self.min_val, min(self.max_val, float(v)))
58
+ if self.step is not None:
59
+ steps = round((v - self.min_val) / self.step)
60
+ v = max(self.min_val, min(self.max_val, self.min_val + steps * self.step))
61
+ return v
62
+
63
+ def _on_bound_change(self, new_val: float) -> None:
64
+ snapped = self._clamp_and_snap(new_val)
65
+ if abs(snapped - self._value) > 1e-6:
66
+ self._value = snapped
67
+ self.invalidate()
68
+
69
+ @property
70
+ def value(self) -> float:
71
+ return self._value
72
+
73
+ @value.setter
74
+ def value(self, val: float) -> None:
75
+ snapped = self._clamp_and_snap(val)
76
+ if abs(snapped - self._value) > 1e-6:
77
+ self._value = snapped
78
+ if self._bind is not None and abs(float(self._bind.value) - snapped) > 1e-6:
79
+ self._bind.value = snapped
80
+ self.invalidate()
81
+
82
+ def _ratio(self) -> float:
83
+ return (self._value - self.min_val) / (self.max_val - self.min_val)
84
+
85
+ def _update_from_x(self, px: float) -> None:
86
+ track_x = self.rect.x + self.PAD_X
87
+ track_w = max(1.0, self.rect.width - self.PAD_X * 2.0)
88
+ ratio = max(0.0, min(1.0, (px - track_x) / track_w))
89
+ new_val = self._clamp_and_snap(self.min_val + ratio * (self.max_val - self.min_val))
90
+ if abs(new_val - self._value) > 1e-6:
91
+ self.value = new_val
92
+ if self.on_change is not None:
93
+ self.on_change(self._value)
94
+
95
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
96
+ w = 240.0 if constraints.max_width == float("inf") else min(240.0, constraints.max_width)
97
+ return Size(w, self.HEIGHT)
98
+
99
+ def tick(self, now: float) -> bool:
100
+ return self._bubble_anim.update(now)
101
+
102
+ def on_mouse_down(self, event: MouseEvent) -> None:
103
+ if self.disabled:
104
+ return
105
+ super().on_mouse_down(event)
106
+ self._bubble_anim.animate_to(1.0)
107
+ self._update_from_x(event.x)
108
+
109
+ def on_mouse_move(self, event: MouseEvent) -> None:
110
+ if self.disabled or not self.pressed:
111
+ return
112
+ self._update_from_x(event.x)
113
+
114
+ def on_mouse_up(self, event: MouseEvent) -> None:
115
+ super().on_mouse_up(event)
116
+ self._bubble_anim.animate_to(0.0)
117
+
118
+ def _format_value(self) -> str:
119
+ if abs(self._value - round(self._value)) < 1e-4:
120
+ return str(int(round(self._value)))
121
+ return f"{self._value:.1f}"
122
+
123
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
124
+ c = theme.colors
125
+ track_x = self.rect.x + self.PAD_X
126
+ track_w = max(1.0, self.rect.width - self.PAD_X * 2.0)
127
+ cy = self.rect.center_y
128
+ ratio = self._ratio()
129
+ thumb_x = track_x + ratio * track_w
130
+
131
+ if self.disabled:
132
+ active_c = c.on_surface.with_alpha(0.38)
133
+ inactive_c = c.on_surface.with_alpha(0.12)
134
+ thumb_c = c.on_surface.with_alpha(0.38)
135
+ else:
136
+ accent = resolve_color(self._fg_override, theme, c.primary) or c.primary
137
+ active_c = accent
138
+ inactive_c = resolve_color(self._bg_override, theme, c.secondary_container) or c.secondary_container
139
+ thumb_c = accent
140
+
141
+ # Inactive track (6dp tall pill)
142
+ inactive_rect = Rect(track_x, cy - 3.0, track_w, 6.0)
143
+ canvas.drawRRect(
144
+ make_rrect(inactive_rect, 3.0),
145
+ skia.Paint(AntiAlias=True, Color=inactive_c.to_skia()),
146
+ )
147
+
148
+ # Active track
149
+ active_w = max(0.0, thumb_x - track_x)
150
+ if active_w > 0.5:
151
+ active_rect = Rect(track_x, cy - 3.0, active_w, 6.0)
152
+ canvas.drawRRect(
153
+ make_rrect(active_rect, 3.0),
154
+ skia.Paint(AntiAlias=True, Color=active_c.to_skia()),
155
+ )
156
+
157
+ # Discrete step ticks (official M3 subtle dots)
158
+ if self.step is not None:
159
+ count = int(round((self.max_val - self.min_val) / self.step))
160
+ if 1 <= count <= 40:
161
+ for i in range(count + 1):
162
+ r = i / count
163
+ tx = track_x + r * track_w
164
+ if abs(tx - thumb_x) < 10.0:
165
+ continue
166
+ dot_c = c.on_primary.with_alpha(0.55) if r < ratio else c.on_secondary_container.with_alpha(0.45)
167
+ canvas.drawCircle(tx, cy, 2.0, skia.Paint(AntiAlias=True, Color=dot_c.to_skia()))
168
+
169
+ # Hover / press halo
170
+ if not self.disabled and (self.hovered or self.pressed or self.focused):
171
+ sl = theme.state_layers
172
+ alpha = sl.pressed if self.pressed else (sl.hover if self.hovered else sl.focus)
173
+ canvas.drawCircle(
174
+ thumb_x,
175
+ cy,
176
+ 18.0,
177
+ skia.Paint(AntiAlias=True, Color=c.primary.with_alpha(alpha).to_skia()),
178
+ )
179
+
180
+ # Thumb (20dp circle with crisp surface ring)
181
+ canvas.drawCircle(
182
+ thumb_x,
183
+ cy,
184
+ 10.0,
185
+ skia.Paint(AntiAlias=True, Color=thumb_c.to_skia()),
186
+ )
187
+
188
+ # Floating value indicator pill (when dragging)
189
+ bt = self._bubble_anim.current
190
+ if self.show_label and bt > 0.02 and not self.disabled:
191
+ txt = self._format_value()
192
+ font = font_for_style(theme.font_family, theme.typography.label_medium)
193
+ tw = measure_text(font, txt)
194
+ bw = max(32.0, tw + 16.0)
195
+ bh = 24.0
196
+ by = cy - 14.0 - bh - (6.0 * bt)
197
+ brect = Rect(thumb_x - bw * 0.5, by, bw, bh)
198
+ canvas.drawRRect(
199
+ make_rrect(brect, bh * 0.5),
200
+ skia.Paint(AntiAlias=True, Color=c.inverse_surface.with_alpha(bt).to_skia()),
201
+ )
202
+ draw_text_centered_y(
203
+ canvas,
204
+ txt,
205
+ brect.center_x - tw * 0.5,
206
+ brect.center_y,
207
+ font,
208
+ c.inverse_on_surface.with_alpha(bt),
209
+ )
@@ -0,0 +1,196 @@
1
+ """Material Design 3 Switch with animated thumb and optional check/close icon."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Callable
6
+
7
+ import glfw
8
+ import skia
9
+
10
+ from ..core.animation import SpringPresets, SpringValue
11
+ from ..core.draw import draw_icon, draw_text_centered_y, font_for_style, make_rrect, measure_text, resolve_color
12
+ from ..core.events import KeyEvent, MouseEvent
13
+ from ..core.geometry import Constraints, Rect, Size
14
+ from ..core.state import State
15
+ from ..core.widget import Widget
16
+ from ..theme import Theme
17
+
18
+
19
+ class Switch(Widget):
20
+ """Official Material Design 3 Switch (52x32dp track, animated thumb)."""
21
+
22
+ interactive = True
23
+ focusable = True
24
+ cursor = "pointer"
25
+
26
+ TRACK_W = 52.0
27
+ TRACK_H = 32.0
28
+
29
+ def __init__(
30
+ self,
31
+ *,
32
+ value: bool = False,
33
+ label: str | None = None,
34
+ show_icon: bool = True,
35
+ disabled: bool = False,
36
+ bind: State[bool] | None = None,
37
+ on_change: Callable[[bool], None] | None = None,
38
+ ) -> None:
39
+ super().__init__()
40
+ self._bind = bind
41
+ initial = bool(bind.value) if bind is not None else bool(value)
42
+ self._value = initial
43
+ self.label = label
44
+ self.show_icon = show_icon
45
+ self.disabled = disabled
46
+ self.on_change = on_change
47
+
48
+ self._pos = SpringValue(1.0 if initial else 0.0, SpringPresets.EXPRESSIVE_FAST_SPATIAL)
49
+ self._thumb_r = SpringValue(self._target_thumb_radius(), SpringPresets.EXPRESSIVE_FAST_SPATIAL)
50
+
51
+ if bind is not None:
52
+ bind.subscribe(self._on_bound_change)
53
+
54
+ def _on_bound_change(self, new_val: bool) -> None:
55
+ b = bool(new_val)
56
+ if b != self._value:
57
+ self._value = b
58
+ self._pos.animate_to(1.0 if b else 0.0)
59
+ self._thumb_r.animate_to(self._target_thumb_radius())
60
+ self.invalidate()
61
+
62
+ def _target_thumb_radius(self) -> float:
63
+ if self.pressed and not self.disabled:
64
+ return 14.0 # 28dp diameter on press
65
+ if self._value or self.show_icon:
66
+ return 12.0 # 24dp diameter selected / with icon
67
+ return 8.0 # 16dp diameter unselected without icon
68
+
69
+ @property
70
+ def value(self) -> bool:
71
+ return self._value
72
+
73
+ @value.setter
74
+ def value(self, val: bool) -> None:
75
+ b = bool(val)
76
+ if b != self._value:
77
+ self._value = b
78
+ self._pos.animate_to(1.0 if b else 0.0)
79
+ self._thumb_r.animate_to(self._target_thumb_radius())
80
+ if self._bind is not None and self._bind.value != b:
81
+ self._bind.value = b
82
+ self.invalidate()
83
+
84
+ def toggle(self) -> None:
85
+ if self.disabled:
86
+ return
87
+ self.value = not self._value
88
+ if self.on_change is not None:
89
+ self.on_change(self._value)
90
+
91
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
92
+ w = self.TRACK_W
93
+ h = self.TRACK_H
94
+ if self.label:
95
+ font = font_for_style(theme.font_family, theme.typography.body_large)
96
+ w += 12.0 + measure_text(font, self.label)
97
+ h = max(h, theme.typography.body_large.line_height)
98
+ return Size(w, h)
99
+
100
+ def tick(self, now: float) -> bool:
101
+ a = self._pos.update(now)
102
+ b = self._thumb_r.update(now)
103
+ return a or b
104
+
105
+ def on_mouse_down(self, event: MouseEvent) -> None:
106
+ if self.disabled:
107
+ return
108
+ super().on_mouse_down(event)
109
+ self._thumb_r.animate_to(self._target_thumb_radius())
110
+
111
+ def on_mouse_up(self, event: MouseEvent) -> None:
112
+ was_pressed = self.pressed
113
+ super().on_mouse_up(event)
114
+ if not self.disabled and was_pressed and self.rect.contains(event.x, event.y):
115
+ self.toggle()
116
+ self._thumb_r.animate_to(self._target_thumb_radius())
117
+
118
+ def on_key(self, event: KeyEvent) -> None:
119
+ if not self.disabled and event.action == glfw.PRESS and event.key in (
120
+ glfw.KEY_SPACE,
121
+ glfw.KEY_ENTER,
122
+ glfw.KEY_KP_ENTER,
123
+ ):
124
+ self.toggle()
125
+
126
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
127
+ c = theme.colors
128
+ t = max(0.0, min(1.0, self._pos.current))
129
+ t_spatial = self._pos.current
130
+ track_y = self.rect.center_y - self.TRACK_H * 0.5
131
+ track_rect = Rect(self.rect.x, track_y, self.TRACK_W, self.TRACK_H)
132
+ track_rrect = make_rrect(track_rect, self.TRACK_H * 0.5)
133
+
134
+ if self.disabled:
135
+ track_bg = c.on_surface.with_alpha(0.12) if self._value else c.surface_container_highest.with_alpha(0.12)
136
+ border_color = c.on_surface.with_alpha(0.12)
137
+ thumb_color = c.surface if self._value else c.on_surface.with_alpha(0.38)
138
+ icon_color = c.on_surface.with_alpha(0.38)
139
+ else:
140
+ accent = resolve_color(self._fg_override, theme, c.primary) or c.primary
141
+ track_bg = c.surface_container_highest.blend(accent, t)
142
+ border_color = c.outline.blend(accent, t)
143
+ thumb_color = c.outline.blend(c.on_primary, t)
144
+ icon_color = c.surface_container_highest.blend(c.on_primary_container, t)
145
+
146
+ canvas.drawRRect(track_rrect, skia.Paint(AntiAlias=True, Color=track_bg.to_skia()))
147
+
148
+ if t < 0.99:
149
+ canvas.drawRRect(
150
+ track_rrect,
151
+ skia.Paint(
152
+ AntiAlias=True,
153
+ Color=border_color.with_alpha(1.0 - t).to_skia(),
154
+ Style=skia.Paint.kStroke_Style,
155
+ StrokeWidth=2.0,
156
+ ),
157
+ )
158
+
159
+ # Thumb center travels from x + 16 to x + 36 (with subtle M3 Expressive spring bounce!)
160
+ thumb_cx = track_rect.x + 16.0 + t_spatial * 20.0
161
+ thumb_cy = track_rect.center_y
162
+
163
+ # State layer halo (40dp diameter)
164
+ if not self.disabled and (self.hovered or self.pressed or self.focused):
165
+ sl = theme.state_layers
166
+ alpha = sl.pressed if self.pressed else (sl.hover if self.hovered else sl.focus)
167
+ halo_color = c.primary if self._value else c.on_surface
168
+ canvas.drawCircle(
169
+ thumb_cx,
170
+ thumb_cy,
171
+ 20.0,
172
+ skia.Paint(AntiAlias=True, Color=halo_color.with_alpha(alpha).to_skia()),
173
+ )
174
+
175
+ canvas.drawCircle(
176
+ thumb_cx,
177
+ thumb_cy,
178
+ self._thumb_r.current,
179
+ skia.Paint(AntiAlias=True, Color=thumb_color.to_skia()),
180
+ )
181
+
182
+ if self.show_icon:
183
+ icon_name = "check" if self._value else "close"
184
+ draw_icon(canvas, icon_name, thumb_cx, thumb_cy, 14.0, icon_color)
185
+
186
+ if self.label:
187
+ font = font_for_style(theme.font_family, theme.typography.body_large)
188
+ fg = c.on_surface.with_alpha(0.38) if self.disabled else c.on_surface
189
+ draw_text_centered_y(
190
+ canvas,
191
+ self.label,
192
+ track_rect.right + 12.0,
193
+ self.rect.center_y,
194
+ font,
195
+ fg,
196
+ )
@@ -0,0 +1,175 @@
1
+ """Material Design 3 Primary & Secondary Tabs with animated indicator."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Callable, Literal, Sequence
6
+
7
+ import skia
8
+
9
+ from ..core.animation import SpringPresets, SpringValue
10
+ from ..core.draw import draw_icon, draw_text_centered_y, font_for_style, make_rrect, measure_text, resolve_color
11
+ from ..core.events import MouseEvent
12
+ from ..core.geometry import Constraints, Rect, Size
13
+ from ..core.state import State
14
+ from ..core.widget import Widget
15
+ from ..theme import Theme
16
+
17
+ TabsVariant = Literal["primary", "secondary"]
18
+
19
+
20
+ class Tabs(Widget):
21
+ """Official Material Design 3 Tabs (`primary` or `secondary`).
22
+
23
+ `items` may be a list of labels (`["General", "Avanzado"]`) or `(icon, label)` tuples.
24
+ """
25
+
26
+ interactive = True
27
+ focusable = True
28
+ cursor = "pointer"
29
+
30
+ def __init__(
31
+ self,
32
+ items: Sequence[str | tuple[str, str]],
33
+ *,
34
+ selected: int = 0,
35
+ variant: TabsVariant = "primary",
36
+ bind: State[int] | None = None,
37
+ on_change: Callable[[int], None] | None = None,
38
+ ) -> None:
39
+ super().__init__()
40
+ self.items: list[tuple[str | None, str]] = [
41
+ (it[0], it[1]) if isinstance(it, tuple) else (None, str(it))
42
+ for it in items
43
+ ]
44
+ self.variant: TabsVariant = variant
45
+ self._bind = bind
46
+ self._selected = int(bind.value) if bind is not None else int(selected)
47
+ self.on_change = on_change
48
+ self._hover_idx: int = -1
49
+ self._indicator_x = SpringValue(float(self._selected), SpringPresets.EXPRESSIVE_DEFAULT_SPATIAL)
50
+ self._fill_width = True
51
+
52
+ if bind is not None:
53
+ bind.subscribe(self._on_bound_change)
54
+
55
+ def _on_bound_change(self, idx: int) -> None:
56
+ i = int(idx)
57
+ if i != self._selected:
58
+ self._selected = i
59
+ self._indicator_x.animate_to(float(i))
60
+ self.invalidate()
61
+
62
+ @property
63
+ def selected(self) -> int:
64
+ return self._selected
65
+
66
+ @selected.setter
67
+ def selected(self, idx: int) -> None:
68
+ i = max(0, min(len(self.items) - 1, int(idx))) if self.items else 0
69
+ if i != self._selected:
70
+ self._selected = i
71
+ self._indicator_x.animate_to(float(i))
72
+ if self._bind is not None and self._bind.value != i:
73
+ self._bind.value = i
74
+ self.invalidate()
75
+
76
+ def _has_icons(self) -> bool:
77
+ return any(icon is not None for icon, _ in self.items)
78
+
79
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
80
+ h = 64.0 if (self._has_icons() and self.variant == "primary") else 48.0
81
+ w = constraints.max_width if constraints.max_width != float("inf") else max(240.0, len(self.items) * 120.0)
82
+ return Size(w, h)
83
+
84
+ def tick(self, now: float) -> bool:
85
+ return self._indicator_x.update(now)
86
+
87
+ def _index_at(self, x: float) -> int:
88
+ if not self.items or self.rect.width <= 0:
89
+ return -1
90
+ slot_w = self.rect.width / len(self.items)
91
+ idx = int((x - self.rect.x) // slot_w)
92
+ return idx if 0 <= idx < len(self.items) else -1
93
+
94
+ def on_mouse_move(self, event: MouseEvent) -> None:
95
+ idx = self._index_at(event.x)
96
+ if idx != self._hover_idx:
97
+ self._hover_idx = idx
98
+ self.invalidate()
99
+
100
+ def on_mouse_leave(self) -> None:
101
+ super().on_mouse_leave()
102
+ self._hover_idx = -1
103
+
104
+ def on_mouse_up(self, event: MouseEvent) -> None:
105
+ was_pressed = self.pressed
106
+ super().on_mouse_up(event)
107
+ if was_pressed:
108
+ idx = self._index_at(event.x)
109
+ if idx != -1 and idx != self._selected:
110
+ self.selected = idx
111
+ if self.on_change is not None:
112
+ self.on_change(idx)
113
+
114
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
115
+ c = theme.colors
116
+ if not self.items:
117
+ return
118
+
119
+ # Surface background + bottom divider
120
+ bg = resolve_color(self._bg_override, theme, c.surface) or c.surface
121
+ canvas.drawRect(
122
+ skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height),
123
+ skia.Paint(AntiAlias=True, Color=bg.to_skia()),
124
+ )
125
+ canvas.drawLine(
126
+ self.rect.x,
127
+ self.rect.bottom - 0.5,
128
+ self.rect.right,
129
+ self.rect.bottom - 0.5,
130
+ skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0),
131
+ )
132
+
133
+ n = len(self.items)
134
+ slot_w = self.rect.width / n
135
+ font = font_for_style(theme.font_family, theme.typography.title_small)
136
+ stacked = self._has_icons() and self.variant == "primary"
137
+
138
+ for i, (icon_name, label) in enumerate(self.items):
139
+ slot = Rect(self.rect.x + i * slot_w, self.rect.y, slot_w, self.rect.height)
140
+ is_sel = i == self._selected
141
+ if i == self._hover_idx:
142
+ canvas.drawRect(
143
+ skia.Rect.MakeXYWH(slot.x, slot.y, slot.width, slot.height),
144
+ skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
145
+ )
146
+
147
+ fg = c.primary if (is_sel and self.variant == "primary") else (
148
+ c.on_surface if is_sel else c.on_surface_variant
149
+ )
150
+ lw = measure_text(font, label)
151
+
152
+ if stacked and icon_name:
153
+ draw_icon(canvas, icon_name, slot.center_x, slot.y + 20.0, 20.0, fg)
154
+ draw_text_centered_y(canvas, label, slot.center_x - lw * 0.5, slot.y + 44.0, font, fg)
155
+ elif icon_name:
156
+ total_w = 18.0 + 8.0 + lw
157
+ sx = slot.center_x - total_w * 0.5
158
+ draw_icon(canvas, icon_name, sx + 9.0, slot.center_y, 18.0, fg)
159
+ draw_text_centered_y(canvas, label, sx + 26.0, slot.center_y, font, fg)
160
+ else:
161
+ draw_text_centered_y(canvas, label, slot.center_x - lw * 0.5, slot.center_y, font, fg)
162
+
163
+ # Animated active indicator bar
164
+ ind_center = self.rect.x + (self._indicator_x.current + 0.5) * slot_w
165
+ if self.variant == "primary":
166
+ _, sel_label = self.items[self._selected]
167
+ ind_w = max(28.0, min(slot_w - 16.0, measure_text(font, sel_label) + 12.0))
168
+ ind_rect = Rect(ind_center - ind_w * 0.5, self.rect.bottom - 3.0, ind_w, 3.0)
169
+ canvas.drawRRect(make_rrect(ind_rect, 1.5), skia.Paint(AntiAlias=True, Color=c.primary.to_skia()))
170
+ else:
171
+ ind_rect = Rect(ind_center - slot_w * 0.5, self.rect.bottom - 2.0, slot_w, 2.0)
172
+ canvas.drawRect(
173
+ skia.Rect.MakeXYWH(ind_rect.x, ind_rect.y, ind_rect.width, ind_rect.height),
174
+ skia.Paint(AntiAlias=True, Color=c.primary.to_skia()),
175
+ )
@@ -0,0 +1,101 @@
1
+ """Material Design 3 Text widget with official Typography roles and custom overrides."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Callable, Literal
6
+
7
+ import skia
8
+
9
+ from ..core.draw import ColorSpec, draw_text_centered_y, get_font, measure_text, resolve_color
10
+ from ..core.geometry import Constraints, Size
11
+ from ..core.state import State
12
+ from ..core.widget import Widget
13
+ from ..theme import Color, Theme, TypographyRole
14
+
15
+ TextColorRole = Literal[
16
+ "on_surface",
17
+ "on_surface_variant",
18
+ "primary",
19
+ "secondary",
20
+ "tertiary",
21
+ "error",
22
+ ]
23
+ TextAlign = Literal["left", "center", "right"]
24
+
25
+
26
+ class Text(Widget):
27
+ """Material Design 3 Typography text widget with optional size/weight/align/underline."""
28
+
29
+ def __init__(
30
+ self,
31
+ text: str | State[str] | Callable[[], str],
32
+ *,
33
+ role: TypographyRole = "body_medium",
34
+ color: TextColorRole | ColorSpec = "on_surface",
35
+ size: float | None = None,
36
+ weight: int | None = None,
37
+ align: TextAlign = "left",
38
+ underline: bool = False,
39
+ ) -> None:
40
+ super().__init__()
41
+ self._source = text
42
+ self.role: TypographyRole = role
43
+ self.color: TextColorRole | ColorSpec = color
44
+ self.font_size = float(size) if size is not None else None
45
+ self.font_weight = int(weight) if weight is not None else None
46
+ self.align: TextAlign = align
47
+ self.underline = underline
48
+
49
+ if isinstance(text, State):
50
+ text.subscribe(lambda _: self.invalidate())
51
+
52
+ @property
53
+ def text(self) -> str:
54
+ if isinstance(self._source, State):
55
+ return str(self._source.value)
56
+ if callable(self._source):
57
+ return str(self._source())
58
+ return str(self._source)
59
+
60
+ @text.setter
61
+ def text(self, value: str) -> None:
62
+ if isinstance(self._source, State):
63
+ self._source.value = value
64
+ else:
65
+ self._source = value
66
+ self.invalidate()
67
+
68
+ def _resolve_font(self, theme: Theme) -> tuple[skia.Font, float]:
69
+ style = theme.typography.get(self.role)
70
+ sz = self.font_size if self.font_size is not None else style.size
71
+ wt = self.font_weight if self.font_weight is not None else style.weight
72
+ lh = max(sz * 1.3, style.line_height) if self.font_size is not None else style.line_height
73
+ return get_font(theme.font_family, sz, wt), lh
74
+
75
+ def _resolve_color(self, theme: Theme) -> Color:
76
+ spec = self._fg_override if self._fg_override is not None else self.color
77
+ col = resolve_color(spec, theme, theme.colors.on_surface)
78
+ assert col is not None
79
+ return col
80
+
81
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
82
+ font, lh = self._resolve_font(theme)
83
+ w = measure_text(font, self.text)
84
+ return Size(w + self._padding.horizontal, lh + self._padding.vertical)
85
+
86
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
87
+ font, _ = self._resolve_font(theme)
88
+ inner = self.rect.inset(self._padding)
89
+ txt = self.text
90
+ tw = measure_text(font, txt)
91
+ if self.align == "center":
92
+ tx = inner.center_x - tw * 0.5
93
+ elif self.align == "right":
94
+ tx = inner.right - tw
95
+ else:
96
+ tx = inner.x
97
+ fg = self._resolve_color(theme)
98
+ draw_text_centered_y(canvas, txt, tx, inner.center_y, font, fg)
99
+ if self.underline and tw > 0:
100
+ uy = inner.center_y + (self.font_size or theme.typography.get(self.role).size) * 0.48
101
+ canvas.drawLine(tx, uy, tx + tw, uy, skia.Paint(AntiAlias=True, Color=fg.to_skia(), StrokeWidth=1.0))