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,870 @@
1
+ """Material Design 3 Advanced Inputs: TextArea, RangeSlider, NumberInput, ColorPicker, FilePicker."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from pathlib import Path
7
+ from typing import Callable, Literal, Sequence
8
+
9
+ import glfw
10
+ import skia
11
+
12
+ from ..core.animation import AnimatedValue
13
+ from ..core.draw import (
14
+ draw_elevation_shadow,
15
+ draw_icon,
16
+ draw_text_centered_y,
17
+ font_for_style,
18
+ get_font,
19
+ make_rrect,
20
+ measure_text,
21
+ resolve_color,
22
+ )
23
+ from ..core.events import CharEvent, KeyEvent, MouseEvent
24
+ from ..core.geometry import Constraints, Rect, Size
25
+ from ..core.state import State
26
+ from ..core.widget import Widget
27
+ from ..theme import Color, Theme
28
+ from ..theme.hct import color_to_lch, lch_to_color
29
+
30
+
31
+ class TextArea(Widget):
32
+ """Material Design 3 multiline TextArea (`outlined` or `filled`)."""
33
+
34
+ interactive = True
35
+ focusable = True
36
+ manages_own_box = True
37
+ cursor = "ibeam"
38
+
39
+ LINE_H = 22.0
40
+
41
+ def __init__(
42
+ self,
43
+ label: str,
44
+ *,
45
+ variant: Literal["outlined", "filled"] = "outlined",
46
+ value: str = "",
47
+ rows: int = 4,
48
+ placeholder: str = "",
49
+ supporting_text: str | None = None,
50
+ error_text: str | None = None,
51
+ max_length: int | None = None,
52
+ disabled: bool = False,
53
+ bind: State[str] | None = None,
54
+ on_change: Callable[[str], None] | None = None,
55
+ ) -> None:
56
+ super().__init__()
57
+ self.label = label
58
+ self.variant = variant
59
+ self.rows = max(2, int(rows))
60
+ self.placeholder = placeholder
61
+ self.supporting_text = supporting_text
62
+ self._error_text = error_text
63
+ self.max_length = max_length
64
+ self.disabled = disabled
65
+ self._bind = bind
66
+ self._value = str(bind.value) if bind is not None else str(value)
67
+ self.on_change = on_change
68
+ self._cursor = len(self._value)
69
+ self._blink_start = time.perf_counter()
70
+ self._float_anim = AnimatedValue(1.0 if (self._value or self.placeholder) else 0.0, duration=0.16)
71
+ self._fill_width = True
72
+
73
+ if bind is not None:
74
+ bind.subscribe(self._on_bound_change)
75
+
76
+ def _on_bound_change(self, new_val: str) -> None:
77
+ s = str(new_val)
78
+ if s != self._value:
79
+ self._value = s
80
+ self._cursor = min(self._cursor, len(s))
81
+ self._float_anim.animate_to(1.0 if (self._value or self.placeholder or self.focused) else 0.0)
82
+ self.invalidate()
83
+
84
+ @property
85
+ def value(self) -> str:
86
+ return self._value
87
+
88
+ @value.setter
89
+ def value(self, val: str) -> None:
90
+ s = str(val)
91
+ if self.max_length is not None:
92
+ s = s[: self.max_length]
93
+ if s != self._value:
94
+ self._value = s
95
+ self._cursor = min(self._cursor, len(s))
96
+ self._float_anim.animate_to(1.0 if (self._value or self.placeholder or self.focused) else 0.0)
97
+ if self._bind is not None and self._bind.value != s:
98
+ self._bind.value = s
99
+ self.invalidate()
100
+
101
+ def _box_h(self) -> float:
102
+ return self.rows * self.LINE_H + 24.0
103
+
104
+ def _has_footer(self) -> bool:
105
+ return bool(self._error_text or self.supporting_text or self.max_length is not None)
106
+
107
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
108
+ w = 320.0 if constraints.max_width == float("inf") else min(360.0, constraints.max_width)
109
+ h = self._box_h() + (20.0 if self._has_footer() else 0.0)
110
+ return Size(w, h)
111
+
112
+ def tick(self, now: float) -> bool:
113
+ return self._float_anim.update(now) or self.focused
114
+
115
+ def on_focus(self) -> None:
116
+ super().on_focus()
117
+ self._blink_start = time.perf_counter()
118
+ self._float_anim.animate_to(1.0)
119
+
120
+ def on_blur(self) -> None:
121
+ super().on_blur()
122
+ self._float_anim.animate_to(1.0 if (self._value or self.placeholder) else 0.0)
123
+
124
+ def _commit(self, txt: str, cur: int) -> None:
125
+ if self.max_length is not None:
126
+ txt = txt[: self.max_length]
127
+ self._value = txt
128
+ self._cursor = max(0, min(cur, len(txt)))
129
+ self._blink_start = time.perf_counter()
130
+ if self._bind is not None and self._bind.value != txt:
131
+ self._bind.value = txt
132
+ if self.on_change is not None:
133
+ self.on_change(txt)
134
+ self.invalidate()
135
+
136
+ def on_char(self, event: CharEvent) -> None:
137
+ if self.disabled or not self.focused:
138
+ return
139
+ self._commit(self._value[: self._cursor] + event.char + self._value[self._cursor :], self._cursor + len(event.char))
140
+
141
+ def on_key(self, event: KeyEvent) -> None:
142
+ if self.disabled or not self.focused or event.action == glfw.RELEASE:
143
+ return
144
+ ctrl = bool(event.mods & glfw.MOD_CONTROL)
145
+ if ctrl and event.key == glfw.KEY_V and self.window:
146
+ clip = self.window.get_clipboard().replace("\r", "")
147
+ self._commit(self._value[: self._cursor] + clip + self._value[self._cursor :], self._cursor + len(clip))
148
+ return
149
+ if ctrl and event.key == glfw.KEY_C and self.window:
150
+ self.window.set_clipboard(self._value)
151
+ return
152
+ if event.key in (glfw.KEY_ENTER, glfw.KEY_KP_ENTER):
153
+ self._commit(self._value[: self._cursor] + "\n" + self._value[self._cursor :], self._cursor + 1)
154
+ elif event.key == glfw.KEY_BACKSPACE and self._cursor > 0:
155
+ self._commit(self._value[: self._cursor - 1] + self._value[self._cursor :], self._cursor - 1)
156
+ elif event.key == glfw.KEY_DELETE and self._cursor < len(self._value):
157
+ self._commit(self._value[: self._cursor] + self._value[self._cursor + 1 :], self._cursor)
158
+ elif event.key == glfw.KEY_LEFT:
159
+ self._cursor = max(0, self._cursor - 1)
160
+ self._blink_start = time.perf_counter()
161
+ elif event.key == glfw.KEY_RIGHT:
162
+ self._cursor = min(len(self._value), self._cursor + 1)
163
+ self._blink_start = time.perf_counter()
164
+
165
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
166
+ c = theme.colors
167
+ is_err = bool(self._error_text)
168
+ box = Rect(self.rect.x, self.rect.y, self.rect.width, self._box_h())
169
+ f_t = self._float_anim.current
170
+
171
+ outline_c = c.error if is_err else (
172
+ c.primary if self.focused else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
173
+ )
174
+ label_c = c.error if is_err else (c.primary if self.focused else c.on_surface_variant)
175
+ border_w = self._border_width_override if self._border_width_override is not None else (2.0 if self.focused else 1.0)
176
+
177
+ label_font = get_font(theme.font_family, 16.0 - 4.0 * f_t, 500 if self.focused else 400)
178
+ lw = measure_text(label_font, self.label)
179
+
180
+ if self.variant == "filled":
181
+ rrect = skia.RRect()
182
+ rrect.setRectRadii(
183
+ skia.Rect.MakeXYWH(box.x, box.y, box.width, box.height),
184
+ [(4.0, 4.0), (4.0, 4.0), (0.0, 0.0), (0.0, 0.0)],
185
+ )
186
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
187
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
188
+ canvas.drawLine(box.x, box.bottom - border_w * 0.5, box.right, box.bottom - border_w * 0.5, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), StrokeWidth=border_w))
189
+ else:
190
+ rad = self._shape_override[0] if isinstance(self._shape_override, tuple) else theme.shape.resolve(self._shape_override or "extra_small")
191
+ rrect = make_rrect(box, rad)
192
+ if self._bg_override is not None:
193
+ bg = resolve_color(self._bg_override, theme)
194
+ if bg is not None:
195
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
196
+ canvas.save()
197
+ if f_t > 0.05 and self.label:
198
+ canvas.clipRect(skia.Rect.MakeXYWH(box.x + 12.0, box.y - 4.0, lw + 8.0, 10.0), skia.ClipOp.kDifference)
199
+ canvas.drawRRect(
200
+ rrect,
201
+ skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=border_w),
202
+ )
203
+ canvas.restore()
204
+
205
+ if self.label:
206
+ lcy = (box.y + 24.0) * (1.0 - f_t) + box.y * f_t
207
+ draw_text_centered_y(canvas, self.label, box.x + 16.0, lcy, label_font, label_c)
208
+
209
+ body_font = font_for_style(theme.font_family, theme.typography.body_large)
210
+ canvas.save()
211
+ canvas.clipRect(skia.Rect.MakeXYWH(box.x + 12.0, box.y + 8.0, max(0.0, box.width - 24.0), max(0.0, box.height - 16.0)))
212
+
213
+ lines = self._value.split("\n")
214
+ if self._value:
215
+ for i, ln in enumerate(lines[: self.rows]):
216
+ draw_text_centered_y(canvas, ln, box.x + 16.0, box.y + 22.0 + i * self.LINE_H, body_font, c.on_surface)
217
+ elif self.placeholder and (self.focused or f_t > 0.8):
218
+ draw_text_centered_y(canvas, self.placeholder, box.x + 16.0, box.y + 22.0, body_font, c.on_surface_variant.with_alpha(0.65))
219
+
220
+ if self.focused and not self.disabled and int((now - self._blink_start) / 0.53) % 2 == 0:
221
+ before = self._value[: self._cursor]
222
+ row_idx = before.count("\n")
223
+ col_txt = before.rsplit("\n", 1)[-1]
224
+ cx = box.x + 16.0 + measure_text(body_font, col_txt)
225
+ cy = box.y + 22.0 + min(row_idx, self.rows - 1) * self.LINE_H
226
+ canvas.drawLine(cx, cy - 9.0, cx, cy + 9.0, skia.Paint(AntiAlias=True, Color=c.primary.to_skia(), StrokeWidth=1.8))
227
+
228
+ canvas.restore()
229
+
230
+ if self._has_footer():
231
+ ff = font_for_style(theme.font_family, theme.typography.body_small)
232
+ msg = self._error_text if is_err else (self.supporting_text or "")
233
+ mc = c.error if is_err else c.on_surface_variant
234
+ if msg:
235
+ draw_text_centered_y(canvas, msg, box.x + 16.0, box.bottom + 12.0, ff, mc)
236
+ if self.max_length is not None:
237
+ cnt = f"{len(self._value)}/{self.max_length}"
238
+ draw_text_centered_y(canvas, cnt, box.right - 16.0 - measure_text(ff, cnt), box.bottom + 12.0, ff, mc)
239
+
240
+
241
+ class RangeSlider(Widget):
242
+ """Material Design 3 dual-thumb RangeSlider returning `(start, end)`."""
243
+
244
+ interactive = True
245
+ focusable = True
246
+ cursor = "ew_resize"
247
+
248
+ HEIGHT = 44.0
249
+ PAD_X = 14.0
250
+
251
+ def __init__(
252
+ self,
253
+ *,
254
+ start: float = 20.0,
255
+ end: float = 80.0,
256
+ min: float = 0.0,
257
+ max: float = 100.0,
258
+ step: float | None = None,
259
+ disabled: bool = False,
260
+ bind: State[tuple[float, float]] | None = None,
261
+ on_change: Callable[[tuple[float, float]], None] | None = None,
262
+ ) -> None:
263
+ super().__init__()
264
+ self.min_val = float(min)
265
+ self.max_val = float(max) if max > min else float(min) + 1.0
266
+ self.step = float(step) if step is not None and step > 0 else None
267
+ self.disabled = disabled
268
+ self._bind = bind
269
+ self.on_change = on_change
270
+ self._active_thumb: int | None = None # 0=start, 1=end
271
+
272
+ raw_s, raw_e = bind.value if bind is not None else (start, end)
273
+ s_val = self._snap(raw_s)
274
+ e_val = self._snap(raw_e)
275
+ self._start = s_val if s_val <= e_val else e_val
276
+ self._end = e_val if e_val >= s_val else s_val
277
+ self._fill_width = True
278
+
279
+ if bind is not None:
280
+ bind.subscribe(self._on_bound_change)
281
+
282
+ def _snap(self, v: float) -> float:
283
+ v = max(self.min_val, min(self.max_val, float(v)))
284
+ if self.step is not None:
285
+ v = self.min_val + round((v - self.min_val) / self.step) * self.step
286
+ v = max(self.min_val, min(self.max_val, v))
287
+ return v
288
+
289
+ def _on_bound_change(self, val: tuple[float, float]) -> None:
290
+ s, e = self._snap(val[0]), self._snap(val[1])
291
+ self._start, self._end = min(s, e), max(s, e)
292
+ self.invalidate()
293
+
294
+ @property
295
+ def value(self) -> tuple[float, float]:
296
+ return (self._start, self._end)
297
+
298
+ @value.setter
299
+ def value(self, val: tuple[float, float]) -> None:
300
+ s, e = self._snap(val[0]), self._snap(val[1])
301
+ self._start, self._end = min(s, e), max(s, e)
302
+ if self._bind is not None and self._bind.value != (self._start, self._end):
303
+ self._bind.value = (self._start, self._end)
304
+ self.invalidate()
305
+
306
+ def _x_for(self, v: float) -> float:
307
+ tw = max(1.0, self.rect.width - self.PAD_X * 2.0)
308
+ return self.rect.x + self.PAD_X + ((v - self.min_val) / (self.max_val - self.min_val)) * tw
309
+
310
+ def _val_for(self, px: float) -> float:
311
+ tw = max(1.0, self.rect.width - self.PAD_X * 2.0)
312
+ r = max(0.0, min(1.0, (px - (self.rect.x + self.PAD_X)) / tw))
313
+ return self._snap(self.min_val + r * (self.max_val - self.min_val))
314
+
315
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
316
+ w = 260.0 if constraints.max_width == float("inf") else min(280.0, constraints.max_width)
317
+ return Size(w, self.HEIGHT)
318
+
319
+ def on_mouse_down(self, event: MouseEvent) -> None:
320
+ if self.disabled:
321
+ return
322
+ super().on_mouse_down(event)
323
+ d0 = abs(event.x - self._x_for(self._start))
324
+ d1 = abs(event.x - self._x_for(self._end))
325
+ self._active_thumb = 0 if d0 <= d1 else 1
326
+ self._drag(event.x)
327
+
328
+ def on_mouse_move(self, event: MouseEvent) -> None:
329
+ if not self.disabled and self.pressed and self._active_thumb is not None:
330
+ self._drag(event.x)
331
+
332
+ def on_mouse_up(self, event: MouseEvent) -> None:
333
+ super().on_mouse_up(event)
334
+ self._active_thumb = None
335
+
336
+ def _drag(self, px: float) -> None:
337
+ v = self._val_for(px)
338
+ prev = (self._start, self._end)
339
+ if self._active_thumb == 0:
340
+ self._start = min(v, self._end)
341
+ else:
342
+ self._end = max(v, self._start)
343
+ if (self._start, self._end) != prev:
344
+ if self._bind is not None:
345
+ self._bind.value = (self._start, self._end)
346
+ if self.on_change is not None:
347
+ self.on_change((self._start, self._end))
348
+ self.invalidate()
349
+
350
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
351
+ c = theme.colors
352
+ tx = self.rect.x + self.PAD_X
353
+ tw = max(1.0, self.rect.width - self.PAD_X * 2.0)
354
+ cy = self.rect.center_y
355
+ x0 = self._x_for(self._start)
356
+ x1 = self._x_for(self._end)
357
+
358
+ accent = resolve_color(self._fg_override, theme, c.primary) or c.primary
359
+ track_bg = resolve_color(self._bg_override, theme, c.secondary_container) or c.secondary_container
360
+ canvas.drawRRect(make_rrect(Rect(tx, cy - 3.0, tw, 6.0), 3.0), skia.Paint(AntiAlias=True, Color=track_bg.to_skia()))
361
+ canvas.drawRRect(
362
+ make_rrect(Rect(x0, cy - 3.0, max(2.0, x1 - x0), 6.0), 3.0),
363
+ skia.Paint(AntiAlias=True, Color=accent.to_skia()),
364
+ )
365
+
366
+ for idx, cx in ((0, x0), (1, x1)):
367
+ if self._active_thumb == idx:
368
+ canvas.drawCircle(cx, cy, 18.0, skia.Paint(AntiAlias=True, Color=accent.with_alpha(0.14).to_skia()))
369
+ canvas.drawCircle(cx, cy, 10.0, skia.Paint(AntiAlias=True, Color=accent.to_skia()))
370
+
371
+
372
+ class NumberInput(Widget):
373
+ """Material Design 3 numeric input (`outlined` or `filled`) with `-` and `+` buttons."""
374
+
375
+ interactive = True
376
+ focusable = True
377
+ manages_own_box = True
378
+ cursor = "pointer"
379
+
380
+ BOX_H = 56.0
381
+
382
+ def __init__(
383
+ self,
384
+ label: str,
385
+ *,
386
+ variant: Literal["outlined", "filled"] = "outlined",
387
+ value: float = 0,
388
+ min: float | None = None,
389
+ max: float | None = None,
390
+ step: float = 1,
391
+ decimals: int = 0,
392
+ disabled: bool = False,
393
+ bind: State[float] | None = None,
394
+ on_change: Callable[[float], None] | None = None,
395
+ ) -> None:
396
+ super().__init__()
397
+ self.label = label
398
+ self.variant = variant
399
+ self.min_val = min
400
+ self.max_val = max
401
+ self.step = float(step)
402
+ self.decimals = int(decimals)
403
+ self.disabled = disabled
404
+ self._bind = bind
405
+ self.on_change = on_change
406
+ raw = float(bind.value) if bind is not None else float(value)
407
+ self._value = self._clamp(raw)
408
+
409
+ if bind is not None:
410
+ bind.subscribe(self._on_bound_change)
411
+
412
+ def _clamp(self, v: float) -> float:
413
+ if self.min_val is not None:
414
+ v = max(float(self.min_val), v)
415
+ if self.max_val is not None:
416
+ v = min(float(self.max_val), v)
417
+ return round(v, self.decimals) if self.decimals > 0 else float(round(v))
418
+
419
+ def _on_bound_change(self, val: float) -> None:
420
+ c = self._clamp(val)
421
+ if abs(c - self._value) > 1e-6:
422
+ self._value = c
423
+ self.invalidate()
424
+
425
+ @property
426
+ def value(self) -> float:
427
+ return self._value
428
+
429
+ @value.setter
430
+ def value(self, val: float) -> None:
431
+ c = self._clamp(val)
432
+ if abs(c - self._value) > 1e-6:
433
+ self._value = c
434
+ if self._bind is not None and abs(float(self._bind.value) - c) > 1e-6:
435
+ self._bind.value = c
436
+ self.invalidate()
437
+
438
+ def _bump(self, delta: float) -> None:
439
+ if self.disabled:
440
+ return
441
+ prev = self._value
442
+ self.value = self._value + delta
443
+ if self._value != prev and self.on_change is not None:
444
+ self.on_change(self._value)
445
+
446
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
447
+ return Size(190.0, self.BOX_H)
448
+
449
+ def on_mouse_up(self, event: MouseEvent) -> None:
450
+ was_pressed = self.pressed
451
+ super().on_mouse_up(event)
452
+ if self.disabled or not was_pressed or not self.rect.contains(event.x, event.y):
453
+ return
454
+ if event.x <= self.rect.x + 44.0:
455
+ self._bump(-self.step)
456
+ elif event.x >= self.rect.right - 44.0:
457
+ self._bump(self.step)
458
+
459
+ def on_key(self, event: KeyEvent) -> None:
460
+ if self.disabled or event.action == glfw.RELEASE:
461
+ return
462
+ if event.key == glfw.KEY_UP:
463
+ self._bump(self.step)
464
+ elif event.key == glfw.KEY_DOWN:
465
+ self._bump(-self.step)
466
+
467
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
468
+ c = theme.colors
469
+ box = self.rect
470
+ outline = c.primary if self.focused else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
471
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if self.focused else 1.0)
472
+ lfont = get_font(theme.font_family, 12.0, 500 if self.focused else 400)
473
+ lw = measure_text(lfont, self.label)
474
+
475
+ if self.variant == "filled":
476
+ rrect = skia.RRect()
477
+ rrect.setRectRadii(skia.Rect.MakeXYWH(box.x, box.y, box.width, box.height), [(4.0, 4.0), (4.0, 4.0), (0.0, 0.0), (0.0, 0.0)])
478
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
479
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
480
+ canvas.drawLine(box.x, box.bottom - bw * 0.5, box.right, box.bottom - bw * 0.5, skia.Paint(AntiAlias=True, Color=outline.to_skia(), StrokeWidth=bw))
481
+ lcy = box.y + 13.0
482
+ else:
483
+ rad = self._shape_override[0] if isinstance(self._shape_override, tuple) else theme.shape.resolve(self._shape_override or "extra_small")
484
+ rrect = make_rrect(box, rad)
485
+ if self._bg_override is not None:
486
+ bg_c = resolve_color(self._bg_override, theme)
487
+ if bg_c is not None:
488
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg_c.to_skia()))
489
+ canvas.save()
490
+ if self.label:
491
+ canvas.clipRect(skia.Rect.MakeXYWH(box.x + 12.0, box.y - 4.0, lw + 8.0, 10.0), skia.ClipOp.kDifference)
492
+ canvas.drawRRect(
493
+ rrect,
494
+ skia.Paint(AntiAlias=True, Color=outline.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
495
+ )
496
+ canvas.restore()
497
+ lcy = box.y
498
+
499
+ if self.label:
500
+ draw_text_centered_y(canvas, self.label, box.x + 16.0, lcy, lfont, c.primary if self.focused else c.on_surface_variant)
501
+
502
+ draw_icon(canvas, "minus", box.x + 24.0, box.center_y, 16.0, c.primary)
503
+ draw_icon(canvas, "add", box.right - 24.0, box.center_y, 16.0, c.primary)
504
+
505
+ txt = f"{int(self._value)}" if self.decimals == 0 else f"{self._value:.{self.decimals}f}"
506
+ bf = font_for_style(theme.font_family, theme.typography.body_large)
507
+ tw = measure_text(bf, txt)
508
+ draw_text_centered_y(canvas, txt, box.center_x - tw * 0.5, box.center_y, bf, c.on_surface)
509
+
510
+
511
+ _DEFAULT_SWATCHES = (
512
+ "#6750A4",
513
+ "#0061A4",
514
+ "#006D3B",
515
+ "#9C4146",
516
+ "#825500",
517
+ "#006874",
518
+ "#984061",
519
+ "#4B5320",
520
+ )
521
+
522
+
523
+ class _ColorPickerOverlay(Widget):
524
+ interactive = True
525
+ auto_attach = False
526
+ cursor = "pointer"
527
+
528
+ POPUP_W = 280.0
529
+ POPUP_H = 196.0
530
+
531
+ def __init__(self, owner: ColorPicker) -> None:
532
+ super().__init__()
533
+ self.owner = owner
534
+ l, _c, h = color_to_lch(Color.from_hex(owner.value))
535
+ self._hue = h
536
+ self._tone = l
537
+ self._popup_rect = Rect(0.0, 0.0, 0.0, 0.0)
538
+ self._swatch_rects: list[tuple[str, Rect]] = []
539
+ self._hue_rect = Rect(0.0, 0.0, 0.0, 0.0)
540
+
541
+ def layout(self, rect: Rect, theme: Theme) -> None:
542
+ self.rect = rect
543
+ a = self.owner.rect
544
+ x = max(8.0, min(rect.width - self.POPUP_W - 8.0, a.x))
545
+ y = a.y + a.height + 6.0
546
+ if y + self.POPUP_H > rect.height - 8.0:
547
+ y = max(8.0, a.y - self.POPUP_H - 6.0)
548
+ self._popup_rect = Rect(x, y, self.POPUP_W, self.POPUP_H)
549
+ self._hue_rect = Rect(x + 18.0, y + 136.0, self.POPUP_W - 36.0, 18.0)
550
+
551
+ self._swatch_rects = []
552
+ for i, hx in enumerate(self.owner.swatches):
553
+ col, row = i % 4, i // 4
554
+ sx = x + 20.0 + col * 62.0
555
+ sy = y + 36.0 + row * 44.0
556
+ self._swatch_rects.append((hx, Rect(sx, sy, 48.0, 32.0)))
557
+
558
+ def hit_test(self, x: float, y: float) -> Widget | None:
559
+ return self if self._popup_rect.contains(x, y) else None
560
+
561
+ def _pick_hue_at(self, px: float) -> None:
562
+ hr = self._hue_rect
563
+ r = max(0.0, min(1.0, (px - hr.x) / max(1.0, hr.width)))
564
+ self._hue = r * 360.0
565
+ col = lch_to_color(42.0, 56.0, self._hue)
566
+ self.owner._select_hex(col.to_hex())
567
+
568
+ def on_mouse_down(self, event: MouseEvent) -> None:
569
+ super().on_mouse_down(event)
570
+ if self._hue_rect.contains(event.x, event.y):
571
+ self._pick_hue_at(event.x)
572
+
573
+ def on_mouse_move(self, event: MouseEvent) -> None:
574
+ if self.pressed and self._hue_rect.x - 10 <= event.x <= self._hue_rect.right + 10:
575
+ if abs(event.y - self._hue_rect.center_y) < 28.0:
576
+ self._pick_hue_at(event.x)
577
+
578
+ def on_mouse_up(self, event: MouseEvent) -> None:
579
+ was_pressed = self.pressed
580
+ super().on_mouse_up(event)
581
+ if not was_pressed:
582
+ return
583
+ for hx, sr in self._swatch_rects:
584
+ if sr.contains(event.x, event.y):
585
+ self.owner._select_hex(hx)
586
+ self.owner.close_picker()
587
+ return
588
+
589
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
590
+ c = theme.colors
591
+ pr = self._popup_rect
592
+ draw_elevation_shadow(canvas, pr, 16.0, 3, c.shadow)
593
+ canvas.drawRRect(make_rrect(pr, 16.0), skia.Paint(AntiAlias=True, Color=c.surface_container_high.to_skia()))
594
+
595
+ lf = font_for_style(theme.font_family, theme.typography.label_medium)
596
+ draw_text_centered_y(canvas, "Paleta rápida M3", pr.x + 18.0, pr.y + 20.0, lf, c.on_surface_variant)
597
+
598
+ for hx, sr in self._swatch_rects:
599
+ col = Color.from_hex(hx)
600
+ canvas.drawRRect(make_rrect(sr, 8.0), skia.Paint(AntiAlias=True, Color=col.to_skia()))
601
+ if hx.upper() == self.owner.value.upper():
602
+ canvas.drawRRect(
603
+ make_rrect(sr, 8.0),
604
+ skia.Paint(AntiAlias=True, Color=c.on_surface.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.5),
605
+ )
606
+
607
+ # Rainbow HCT Hue bar
608
+ hr = self._hue_rect
609
+ stops = [lch_to_color(50.0, 64.0, h).to_skia() for h in (0, 60, 120, 180, 240, 300, 360)]
610
+ grad = skia.GradientShader.MakeLinear([(hr.x, hr.y), (hr.right, hr.y)], stops)
611
+ canvas.drawRRect(make_rrect(hr, 9.0), skia.Paint(AntiAlias=True, Shader=grad))
612
+ kx = hr.x + (self._hue / 360.0) * hr.width
613
+ canvas.drawCircle(kx, hr.center_y, 10.0, skia.Paint(AntiAlias=True, Color=Color.from_hex(self.owner.value).to_skia()))
614
+ canvas.drawCircle(
615
+ kx, hr.center_y, 10.0,
616
+ skia.Paint(AntiAlias=True, Color=c.surface.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=2.5),
617
+ )
618
+ draw_text_centered_y(canvas, f"Hex: {self.owner.value}", pr.x + 18.0, pr.bottom - 18.0, lf, c.on_surface)
619
+
620
+
621
+ class ColorPicker(Widget):
622
+ """Material Design 3 ColorPicker (`outlined` or `filled`) with live swatch preview."""
623
+
624
+ interactive = True
625
+ focusable = True
626
+ manages_own_box = True
627
+ cursor = "pointer"
628
+
629
+ BOX_H = 56.0
630
+
631
+ def __init__(
632
+ self,
633
+ label: str = "Color",
634
+ *,
635
+ variant: Literal["outlined", "filled"] = "outlined",
636
+ value: str = "#6750A4",
637
+ swatches: Sequence[str] = _DEFAULT_SWATCHES,
638
+ disabled: bool = False,
639
+ bind: State[str] | None = None,
640
+ on_change: Callable[[str], None] | None = None,
641
+ ) -> None:
642
+ super().__init__()
643
+ self.label = label
644
+ self.variant = variant
645
+ self.swatches = list(swatches)
646
+ self.disabled = disabled
647
+ self._bind = bind
648
+ self._value = str(bind.value) if bind is not None else str(value)
649
+ self.on_change = on_change
650
+ self._open = False
651
+ self._overlay: _ColorPickerOverlay | None = None
652
+
653
+ if bind is not None:
654
+ bind.subscribe(lambda v: self._set_hex(str(v)))
655
+
656
+ def _set_hex(self, hx: str) -> None:
657
+ if hx != self._value:
658
+ self._value = hx
659
+ self.invalidate()
660
+
661
+ @property
662
+ def value(self) -> str:
663
+ return self._value
664
+
665
+ @value.setter
666
+ def value(self, hx: str) -> None:
667
+ self._set_hex(hx)
668
+ if self._bind is not None and self._bind.value != hx:
669
+ self._bind.value = hx
670
+
671
+ def _select_hex(self, hx: str) -> None:
672
+ self.value = hx
673
+ if self.on_change is not None:
674
+ self.on_change(hx)
675
+
676
+ def open_picker(self) -> None:
677
+ if self.disabled or self.window is None:
678
+ return
679
+ self._open = True
680
+ self._overlay = _ColorPickerOverlay(self)
681
+ self.window.open_overlay(self._overlay)
682
+ self.invalidate()
683
+
684
+ def close_picker(self) -> None:
685
+ self._open = False
686
+ if self._overlay is not None and self.window is not None:
687
+ self.window.close_overlay(self._overlay)
688
+ self._overlay = None
689
+ self.invalidate()
690
+
691
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
692
+ return Size(210.0, self.BOX_H)
693
+
694
+ def on_mouse_up(self, event: MouseEvent) -> None:
695
+ was_pressed = self.pressed
696
+ super().on_mouse_up(event)
697
+ if not self.disabled and was_pressed and self.rect.contains(event.x, event.y):
698
+ if self._open:
699
+ self.close_picker()
700
+ else:
701
+ self.open_picker()
702
+
703
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
704
+ c = theme.colors
705
+ box = self.rect
706
+ active = self._open or self.focused
707
+ outline = c.primary if active else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
708
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if active else 1.0)
709
+ lf = get_font(theme.font_family, 12.0, 500 if active else 400)
710
+ lw = measure_text(lf, self.label)
711
+
712
+ if self.variant == "filled":
713
+ rrect = skia.RRect()
714
+ rrect.setRectRadii(skia.Rect.MakeXYWH(box.x, box.y, box.width, box.height), [(4.0, 4.0), (4.0, 4.0), (0.0, 0.0), (0.0, 0.0)])
715
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
716
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
717
+ canvas.drawLine(box.x, box.bottom - bw * 0.5, box.right, box.bottom - bw * 0.5, skia.Paint(AntiAlias=True, Color=outline.to_skia(), StrokeWidth=bw))
718
+ lcy = box.y + 13.0
719
+ else:
720
+ rad = self._shape_override[0] if isinstance(self._shape_override, tuple) else theme.shape.resolve(self._shape_override or "extra_small")
721
+ rrect = make_rrect(box, rad)
722
+ if self._bg_override is not None:
723
+ bg_c = resolve_color(self._bg_override, theme)
724
+ if bg_c is not None:
725
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg_c.to_skia()))
726
+ canvas.save()
727
+ if self.label:
728
+ canvas.clipRect(skia.Rect.MakeXYWH(box.x + 12.0, box.y - 4.0, lw + 8.0, 10.0), skia.ClipOp.kDifference)
729
+ canvas.drawRRect(
730
+ rrect,
731
+ skia.Paint(AntiAlias=True, Color=outline.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
732
+ )
733
+ canvas.restore()
734
+ lcy = box.y
735
+
736
+ if self.label:
737
+ draw_text_centered_y(canvas, self.label, box.x + 16.0, lcy, lf, c.primary if active else c.on_surface_variant)
738
+
739
+ sw_rect = Rect(box.x + 14.0, box.center_y - 12.0, 24.0, 24.0)
740
+ canvas.drawRRect(make_rrect(sw_rect, 6.0), skia.Paint(AntiAlias=True, Color=Color.from_hex(self._value).to_skia()))
741
+ bf = font_for_style(theme.font_family, theme.typography.body_large)
742
+ draw_text_centered_y(canvas, self._value.upper(), sw_rect.right + 12.0, box.center_y, bf, c.on_surface)
743
+ draw_icon(canvas, "palette", box.right - 24.0, box.center_y, 18.0, c.on_surface_variant)
744
+
745
+
746
+ class FilePicker(Widget):
747
+ """Material Design 3 FilePicker (`outlined` or `filled`) invoking the native OS dialog."""
748
+
749
+ interactive = True
750
+ focusable = True
751
+ manages_own_box = True
752
+ cursor = "pointer"
753
+
754
+ BOX_H = 56.0
755
+
756
+ def __init__(
757
+ self,
758
+ label: str = "Archivo",
759
+ *,
760
+ variant: Literal["outlined", "filled"] = "outlined",
761
+ mode: Literal["open", "save", "folder"] = "open",
762
+ filetypes: Sequence[tuple[str, str]] = (("Todos los archivos", "*.*"),),
763
+ value: str = "",
764
+ disabled: bool = False,
765
+ bind: State[str] | None = None,
766
+ on_change: Callable[[str], None] | None = None,
767
+ ) -> None:
768
+ super().__init__()
769
+ self.label = label
770
+ self.variant = variant
771
+ self.mode = mode
772
+ self.filetypes = list(filetypes)
773
+ self.disabled = disabled
774
+ self._bind = bind
775
+ self._value = str(bind.value) if bind is not None else str(value)
776
+ self.on_change = on_change
777
+ self._fill_width = True
778
+
779
+ if bind is not None:
780
+ bind.subscribe(lambda v: self._set_path(str(v)))
781
+
782
+ def _set_path(self, p: str) -> None:
783
+ if p != self._value:
784
+ self._value = p
785
+ self.invalidate()
786
+
787
+ @property
788
+ def value(self) -> str:
789
+ return self._value
790
+
791
+ @value.setter
792
+ def value(self, p: str) -> None:
793
+ self._set_path(p)
794
+ if self._bind is not None and self._bind.value != p:
795
+ self._bind.value = p
796
+
797
+ def pick(self) -> str:
798
+ """Open native OS file/folder dialog (zero tkinter) and return chosen path."""
799
+ from ..core.native_dialogs import open_file_dialog, open_folder_dialog, save_file_dialog
800
+
801
+ if self.mode == "folder":
802
+ chosen = open_folder_dialog(title=self.label)
803
+ elif self.mode == "save":
804
+ chosen = save_file_dialog(title=self.label, filetypes=self.filetypes)
805
+ else:
806
+ files = open_file_dialog(title=self.label, filetypes=self.filetypes, multiple=False)
807
+ chosen = files[0] if files else ""
808
+
809
+ if chosen:
810
+ self.value = chosen
811
+ if self.on_change is not None:
812
+ self.on_change(chosen)
813
+ return chosen or ""
814
+
815
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
816
+ w = 300.0 if constraints.max_width == float("inf") else min(340.0, constraints.max_width)
817
+ return Size(w, self.BOX_H)
818
+
819
+ def on_mouse_up(self, event: MouseEvent) -> None:
820
+ was_pressed = self.pressed
821
+ super().on_mouse_up(event)
822
+ if not self.disabled and was_pressed and self.rect.contains(event.x, event.y):
823
+ self.pick()
824
+
825
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
826
+ c = theme.colors
827
+ box = self.rect
828
+ outline = c.primary if self.focused else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
829
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if self.focused else 1.0)
830
+ lf = get_font(theme.font_family, 12.0, 500 if self.focused else 400)
831
+ lw = measure_text(lf, self.label)
832
+
833
+ if self.variant == "filled":
834
+ rrect = skia.RRect()
835
+ rrect.setRectRadii(skia.Rect.MakeXYWH(box.x, box.y, box.width, box.height), [(4.0, 4.0), (4.0, 4.0), (0.0, 0.0), (0.0, 0.0)])
836
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
837
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
838
+ canvas.drawLine(box.x, box.bottom - bw * 0.5, box.right, box.bottom - bw * 0.5, skia.Paint(AntiAlias=True, Color=outline.to_skia(), StrokeWidth=bw))
839
+ lcy = box.y + 13.0
840
+ else:
841
+ rad = self._shape_override[0] if isinstance(self._shape_override, tuple) else theme.shape.resolve(self._shape_override or "extra_small")
842
+ rrect = make_rrect(box, rad)
843
+ if self._bg_override is not None:
844
+ bg_c = resolve_color(self._bg_override, theme)
845
+ if bg_c is not None:
846
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg_c.to_skia()))
847
+ canvas.save()
848
+ if self.label:
849
+ canvas.clipRect(skia.Rect.MakeXYWH(box.x + 12.0, box.y - 4.0, lw + 8.0, 10.0), skia.ClipOp.kDifference)
850
+ canvas.drawRRect(
851
+ rrect,
852
+ skia.Paint(AntiAlias=True, Color=outline.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
853
+ )
854
+ canvas.restore()
855
+ lcy = box.y
856
+
857
+ if self.label:
858
+ draw_text_centered_y(canvas, self.label, box.x + 16.0, lcy, lf, c.primary if self.focused else c.on_surface_variant)
859
+
860
+ bf = font_for_style(theme.font_family, theme.typography.body_medium)
861
+ disp = Path(self._value).name if self._value else "Elegir archivo..."
862
+ fg = c.on_surface if self._value else c.on_surface_variant
863
+ draw_text_centered_y(canvas, disp, box.x + 16.0, box.center_y, bf, fg)
864
+
865
+ btn_r = Rect(box.right - 92.0, box.center_y - 15.0, 80.0, 30.0)
866
+ canvas.drawRRect(make_rrect(btn_r, 15.0), skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()))
867
+ btf = font_for_style(theme.font_family, theme.typography.label_medium)
868
+ btxt = "Explorar"
869
+ btw = measure_text(btf, btxt)
870
+ draw_text_centered_y(canvas, btxt, btn_r.center_x - btw * 0.5, btn_r.center_y, btf, c.on_secondary_container)