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,587 @@
1
+ """Phase 8 Pro Form Controls (Part 2): DateRangePicker, DropZone, and SignaturePad."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import calendar
6
+ from datetime import date
7
+ from pathlib import Path
8
+ from typing import Callable, Literal, Sequence
9
+
10
+ import skia
11
+
12
+ from ..core.draw import (
13
+ draw_elevation_shadow,
14
+ draw_icon,
15
+ draw_text_centered_y,
16
+ font_for_style,
17
+ get_font,
18
+ make_rrect,
19
+ make_rrect_spec,
20
+ measure_text,
21
+ resolve_color,
22
+ )
23
+ from ..core.events import 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 Theme
28
+
29
+ FieldVariant = Literal["outlined", "filled"]
30
+
31
+ _MONTHS = (
32
+ "",
33
+ "Enero",
34
+ "Febrero",
35
+ "Marzo",
36
+ "Abril",
37
+ "Mayo",
38
+ "Junio",
39
+ "Julio",
40
+ "Agosto",
41
+ "Septiembre",
42
+ "Octubre",
43
+ "Noviembre",
44
+ "Diciembre",
45
+ )
46
+
47
+
48
+ # ---------------------------------------------------------------------------
49
+ # 5. DateRangePicker (Start -> End Calendar Band)
50
+ # ---------------------------------------------------------------------------
51
+
52
+
53
+ class _DateRangeOverlay(Widget):
54
+ interactive = True
55
+ auto_attach = False
56
+ cursor = "pointer"
57
+
58
+ W = 312.0
59
+ H = 336.0
60
+
61
+ def __init__(self, owner: DateRangePicker) -> None:
62
+ super().__init__()
63
+ self.owner = owner
64
+ today = date.today()
65
+ base = owner._start or today
66
+ self.year = base.year
67
+ self.month = base.month
68
+ self._card_rect: Rect = Rect(0, 0, self.W, self.H)
69
+ self._day_cells: list[tuple[date, Rect]] = []
70
+ self._prev_btn: Rect = Rect(0, 0, 0, 0)
71
+ self._next_btn: Rect = Rect(0, 0, 0, 0)
72
+
73
+ def _compute_rect(self) -> Rect:
74
+ o = self.owner.rect
75
+ y = o.y + self.owner.BOX_HEIGHT + 6.0
76
+ if self.window and y + self.H > self.window.height - 8.0:
77
+ y = max(8.0, o.y - self.H - 6.0)
78
+ x = o.x
79
+ if self.window and x + self.W > self.window.width - 8.0:
80
+ x = max(8.0, self.window.width - self.W - 8.0)
81
+ return Rect(x, y, self.W, self.H)
82
+
83
+ def hit_test(self, x: float, y: float) -> Widget | None:
84
+ self._card_rect = self._compute_rect()
85
+ return self if self._card_rect.contains(x, y) else None
86
+
87
+ def on_mouse_up(self, event: MouseEvent) -> None:
88
+ super().on_mouse_up(event)
89
+ if self._prev_btn.contains(event.x, event.y):
90
+ if self.month == 1:
91
+ self.month = 12
92
+ self.year -= 1
93
+ else:
94
+ self.month -= 1
95
+ self.invalidate()
96
+ return
97
+ if self._next_btn.contains(event.x, event.y):
98
+ if self.month == 12:
99
+ self.month = 1
100
+ self.year += 1
101
+ else:
102
+ self.month += 1
103
+ self.invalidate()
104
+ return
105
+ for d, dr in self._day_cells:
106
+ if dr.contains(event.x, event.y):
107
+ self.owner._pick_day(d)
108
+ return
109
+
110
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
111
+ c = theme.colors
112
+ cr = self._compute_rect()
113
+ self._card_rect = cr
114
+ draw_elevation_shadow(canvas, cr, theme.shape.large, 3, c.shadow)
115
+ canvas.drawRRect(make_rrect(cr, theme.shape.large), skia.Paint(AntiAlias=True, Color=c.surface_container_high.to_skia()))
116
+
117
+ # Header month/year + nav arrows
118
+ tfont = font_for_style(theme.font_family, theme.typography.title_small)
119
+ title = f"{_MONTHS[self.month]} {self.year}"
120
+ tw = measure_text(tfont, title)
121
+ draw_text_centered_y(canvas, title, cr.center_x - tw * 0.5, cr.y + 28.0, tfont, c.on_surface)
122
+
123
+ self._prev_btn = Rect(cr.x + 12.0, cr.y + 10.0, 34.0, 34.0)
124
+ self._next_btn = Rect(cr.right - 46.0, cr.y + 10.0, 34.0, 34.0)
125
+ draw_icon(canvas, "chevron_left", self._prev_btn.center_x, self._prev_btn.center_y, 18.0, c.on_surface_variant)
126
+ draw_icon(canvas, "chevron_right", self._next_btn.center_x, self._next_btn.center_y, 18.0, c.on_surface_variant)
127
+
128
+ # Weekday headers
129
+ wfont = font_for_style(theme.font_family, theme.typography.label_small)
130
+ cell_w = (cr.width - 24.0) / 7.0
131
+ for i, wd in enumerate(("L", "M", "X", "J", "V", "S", "D")):
132
+ wx = cr.x + 12.0 + i * cell_w + cell_w * 0.5
133
+ draw_text_centered_y(canvas, wd, wx - measure_text(wfont, wd) * 0.5, cr.y + 62.0, wfont, c.on_surface_variant)
134
+
135
+ # Day grid with continuous range highlight band
136
+ self._day_cells = []
137
+ cal = calendar.monthcalendar(self.year, self.month)
138
+ dfont = font_for_style(theme.font_family, theme.typography.body_small)
139
+ s_date, e_date = self.owner._start, self.owner._end
140
+
141
+ for row_i, week in enumerate(cal):
142
+ for col_i, day_num in enumerate(week):
143
+ if day_num == 0:
144
+ continue
145
+ d = date(self.year, self.month, day_num)
146
+ dr = Rect(cr.x + 12.0 + col_i * cell_w, cr.y + 78.0 + row_i * 38.0, cell_w, 34.0)
147
+ self._day_cells.append((d, dr))
148
+
149
+ in_band = s_date is not None and e_date is not None and s_date < d < e_date
150
+ is_endpoint = d == s_date or d == e_date
151
+ if in_band:
152
+ canvas.drawRect(
153
+ skia.Rect.MakeXYWH(dr.x, dr.y + 2.0, dr.width, dr.height - 4.0),
154
+ skia.Paint(AntiAlias=True, Color=c.primary_container.to_skia()),
155
+ )
156
+ if is_endpoint:
157
+ canvas.drawCircle(dr.center_x, dr.center_y, 15.0, skia.Paint(AntiAlias=True, Color=c.primary.to_skia()))
158
+
159
+ txt = str(day_num)
160
+ fg = c.on_primary if is_endpoint else (c.on_primary_container if in_band else c.on_surface)
161
+ draw_text_centered_y(canvas, txt, dr.center_x - measure_text(dfont, txt) * 0.5, dr.center_y, dfont, fg)
162
+
163
+
164
+ class DateRangePicker(Widget):
165
+ """Material Design 3 DateRangePicker selecting a `(start_date, end_date)` interval."""
166
+
167
+ interactive = True
168
+ focusable = True
169
+ manages_own_box = True
170
+ cursor = "pointer"
171
+
172
+ BOX_HEIGHT = 56.0
173
+
174
+ def __init__(
175
+ self,
176
+ label: str = "Rango de fechas",
177
+ *,
178
+ start: date | None = None,
179
+ end: date | None = None,
180
+ variant: FieldVariant = "outlined",
181
+ disabled: bool = False,
182
+ bind: State[tuple[date | None, date | None]] | None = None,
183
+ on_change: Callable[[tuple[date | None, date | None]], None] | None = None,
184
+ ) -> None:
185
+ super().__init__()
186
+ self.label = label
187
+ self.variant: FieldVariant = variant
188
+ self.disabled = disabled
189
+ self._bind = bind
190
+ self.on_change = on_change
191
+ if bind is not None:
192
+ self._start, self._end = bind.value
193
+ bind.subscribe(self._on_bound)
194
+ else:
195
+ self._start = start
196
+ self._end = end
197
+ self._open = False
198
+ self._overlay = _DateRangeOverlay(self)
199
+
200
+ def _on_bound(self, pair: tuple[date | None, date | None]) -> None:
201
+ self._start, self._end = pair
202
+ self.invalidate()
203
+
204
+ @property
205
+ def value(self) -> tuple[date | None, date | None]:
206
+ return (self._start, self._end)
207
+
208
+ @value.setter
209
+ def value(self, pair: tuple[date | None, date | None]) -> None:
210
+ self._start, self._end = pair
211
+ if self._bind is not None:
212
+ self._bind.value = (self._start, self._end)
213
+ self.invalidate()
214
+
215
+ def _pick_day(self, d: date) -> None:
216
+ if self._start is None or (self._start is not None and self._end is not None):
217
+ self._start = d
218
+ self._end = None
219
+ else:
220
+ if d < self._start:
221
+ self._end = self._start
222
+ self._start = d
223
+ else:
224
+ self._end = d
225
+ self._close()
226
+ if self._bind is not None:
227
+ self._bind.value = (self._start, self._end)
228
+ if self.on_change is not None:
229
+ self.on_change((self._start, self._end))
230
+ self.invalidate()
231
+
232
+ def _open_cal(self) -> None:
233
+ if self.disabled or self._open:
234
+ return
235
+ self._open = True
236
+ if self.window is not None:
237
+ self.window.open_overlay(self._overlay)
238
+
239
+ def _close(self) -> None:
240
+ if not self._open:
241
+ return
242
+ self._open = False
243
+ if self.window is not None:
244
+ self.window.close_overlay(self._overlay)
245
+
246
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
247
+ w = constraints.max_width if constraints.max_width != float("inf") else 290.0
248
+ return Size(w, self.BOX_HEIGHT)
249
+
250
+ def on_mouse_up(self, event: MouseEvent) -> None:
251
+ was_pressed = self.pressed
252
+ super().on_mouse_up(event)
253
+ if not self.disabled and was_pressed and self.rect.contains(event.x, event.y):
254
+ if self._open:
255
+ self._close()
256
+ else:
257
+ self._open_cal()
258
+
259
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
260
+ c = theme.colors
261
+ r = self.rect
262
+ rad_spec = self._shape_override if self._shape_override is not None else "extra_small"
263
+ rrect = make_rrect_spec(r, rad_spec, theme)
264
+ active = self._open or self.focused
265
+ outline_c = c.primary if active else (resolve_color(self._border_color_override, theme) or (c.on_surface if self.hovered else c.outline))
266
+ bw = self._border_width_override if self._border_width_override is not None else (2.0 if active else 1.0)
267
+
268
+ if self.variant == "filled":
269
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
270
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
271
+ 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))
272
+ else:
273
+ if self._bg_override is not None:
274
+ bg = resolve_color(self._bg_override, theme)
275
+ if bg is not None:
276
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
277
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=outline_c.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw))
278
+
279
+ lfont = get_font(theme.font_family, 12.0, 500 if active else 400)
280
+ bfont = font_for_style(theme.font_family, theme.typography.body_medium)
281
+ draw_text_centered_y(canvas, self.label, r.x + 14.0, r.y + 14.0, lfont, c.primary if active else c.on_surface_variant)
282
+
283
+ if self._start and self._end:
284
+ txt = f"{self._start.strftime('%d/%m/%Y')} → {self._end.strftime('%d/%m/%Y')}"
285
+ col = c.on_surface
286
+ elif self._start:
287
+ txt = f"{self._start.strftime('%d/%m/%Y')} → ..."
288
+ col = c.primary
289
+ else:
290
+ txt = "Desde → Hasta"
291
+ col = c.on_surface_variant
292
+ draw_text_centered_y(canvas, txt, r.x + 14.0, r.y + 37.0, bfont, col)
293
+ draw_icon(canvas, "calendar", r.right - 22.0, r.center_y, 18.0, c.on_surface_variant)
294
+
295
+
296
+ # ---------------------------------------------------------------------------
297
+ # 6. DropZone (Multi-File Upload / Attachment Area)
298
+ # ---------------------------------------------------------------------------
299
+
300
+
301
+ class DropZone(Widget):
302
+ """Material Design 3 dashed DropZone for selecting/listing multiple files."""
303
+
304
+ interactive = True
305
+ focusable = True
306
+ manages_own_box = True
307
+ cursor = "pointer"
308
+
309
+ def __init__(
310
+ self,
311
+ title: str = "Haz clic para seleccionar archivos",
312
+ *,
313
+ subtitle: str = "PNG, PDF, CSV o XLSX",
314
+ files: Sequence[str] = (),
315
+ filetypes: Sequence[tuple[str, str]] = (("Todos los archivos", "*.*"),),
316
+ multiple: bool = True,
317
+ disabled: bool = False,
318
+ bind: State[list[str]] | None = None,
319
+ on_change: Callable[[list[str]], None] | None = None,
320
+ ) -> None:
321
+ super().__init__()
322
+ self.title = title
323
+ self.subtitle = subtitle
324
+ self.filetypes = list(filetypes)
325
+ self.multiple = multiple
326
+ self.disabled = disabled
327
+ self._bind = bind
328
+ self.on_change = on_change
329
+ self._files: list[str] = list(bind.value) if bind is not None else list(files)
330
+ self._remove_rects: list[tuple[int, Rect]] = []
331
+ self._fill_width = True
332
+ if bind is not None:
333
+ bind.subscribe(self._on_bound)
334
+
335
+ def _on_bound(self, v: list[str]) -> None:
336
+ self._files = list(v)
337
+ self.invalidate()
338
+
339
+ @property
340
+ def value(self) -> list[str]:
341
+ return list(self._files)
342
+
343
+ @value.setter
344
+ def value(self, val: Sequence[str]) -> None:
345
+ self._files = list(val)
346
+ if self._bind is not None:
347
+ self._bind.value = list(self._files)
348
+ self.invalidate()
349
+
350
+ def add_files(self, paths: Sequence[str]) -> None:
351
+ for p in paths:
352
+ if p and p not in self._files:
353
+ self._files.append(p)
354
+ if self._bind is not None:
355
+ self._bind.value = list(self._files)
356
+ if self.on_change is not None:
357
+ self.on_change(list(self._files))
358
+ self.invalidate()
359
+
360
+ def remove_file(self, idx: int) -> None:
361
+ if 0 <= idx < len(self._files):
362
+ self._files.pop(idx)
363
+ if self._bind is not None:
364
+ self._bind.value = list(self._files)
365
+ if self.on_change is not None:
366
+ self.on_change(list(self._files))
367
+ self.invalidate()
368
+
369
+ def _open_native_dialog(self) -> None:
370
+ try:
371
+ from ..core.native_dialogs import open_file_dialog
372
+
373
+ chosen = open_file_dialog(
374
+ title=self.title,
375
+ filetypes=self.filetypes,
376
+ multiple=self.multiple,
377
+ )
378
+ if chosen:
379
+ self.add_files(chosen)
380
+ except Exception:
381
+ pass
382
+
383
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
384
+ w = constraints.max_width if constraints.max_width != float("inf") else 420.0
385
+ extra = len(self._files) * 30.0 + (8.0 if self._files else 0.0)
386
+ return Size(w, 108.0 + extra)
387
+
388
+ def on_mouse_up(self, event: MouseEvent) -> None:
389
+ was_pressed = self.pressed
390
+ super().on_mouse_up(event)
391
+ if self.disabled or not was_pressed or not self.rect.contains(event.x, event.y):
392
+ return
393
+ for idx, rr in self._remove_rects:
394
+ if rr.contains(event.x, event.y):
395
+ self.remove_file(idx)
396
+ return
397
+ self._open_native_dialog()
398
+
399
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
400
+ c = theme.colors
401
+ r = self.rect
402
+ rad_spec = self._shape_override if self._shape_override is not None else "large"
403
+ rrect = make_rrect_spec(r, rad_spec, theme)
404
+
405
+ bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
406
+ if self.hovered and not self.disabled:
407
+ bg = bg.blend(c.primary, 0.06)
408
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
409
+
410
+ bc = resolve_color(self._border_color_override, theme, c.primary if self.hovered else c.outline) or c.outline
411
+ bw = self._border_width_override if self._border_width_override is not None else 1.5
412
+ dash = skia.DashPathEffect.Make([6.0, 5.0], 0.0)
413
+ canvas.drawRRect(
414
+ rrect,
415
+ skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw, PathEffect=dash),
416
+ )
417
+
418
+ # Icon + text centered in top 104dp zone
419
+ canvas.drawCircle(r.center_x, r.y + 32.0, 18.0, skia.Paint(AntiAlias=True, Color=c.primary_container.to_skia()))
420
+ draw_icon(canvas, "add", r.center_x, r.y + 32.0, 18.0, c.on_primary_container)
421
+
422
+ tfont = font_for_style(theme.font_family, theme.typography.title_small)
423
+ sfont = font_for_style(theme.font_family, theme.typography.body_small)
424
+ draw_text_centered_y(canvas, self.title, r.center_x - measure_text(tfont, self.title) * 0.5, r.y + 66.0, tfont, c.on_surface)
425
+ draw_text_centered_y(canvas, self.subtitle, r.center_x - measure_text(sfont, self.subtitle) * 0.5, r.y + 86.0, sfont, c.on_surface_variant)
426
+
427
+ # Attached file chips/rows
428
+ self._remove_rects = []
429
+ y = r.y + 106.0
430
+ for i, fp in enumerate(self._files):
431
+ name = Path(fp).name or fp
432
+ row_r = Rect(r.x + 14.0, y, max(40.0, r.width - 28.0), 26.0)
433
+ canvas.drawRRect(make_rrect(row_r, 8.0), skia.Paint(AntiAlias=True, Color=c.surface_container_high.to_skia()))
434
+ draw_text_centered_y(canvas, name, row_r.x + 10.0, row_r.center_y, sfont, c.on_surface)
435
+ rem = Rect(row_r.right - 24.0, row_r.y + 3.0, 20.0, 20.0)
436
+ draw_icon(canvas, "close", rem.center_x, rem.center_y, 12.0, c.on_surface_variant)
437
+ self._remove_rects.append((i, rem))
438
+ y += 30.0
439
+
440
+
441
+ # ---------------------------------------------------------------------------
442
+ # 7. SignaturePad (GPU Vector Freehand Signature Canvas)
443
+ # ---------------------------------------------------------------------------
444
+
445
+
446
+ class SignaturePad(Widget):
447
+ """GPU-rendered vector SignaturePad with smooth strokes and PNG export."""
448
+
449
+ interactive = True
450
+ manages_own_box = True
451
+ cursor = "pointer"
452
+
453
+ def __init__(
454
+ self,
455
+ *,
456
+ label: str = "Firma",
457
+ height: float = 150.0,
458
+ stroke_width: float = 2.4,
459
+ disabled: bool = False,
460
+ on_change: Callable[[], None] | None = None,
461
+ ) -> None:
462
+ super().__init__()
463
+ self.label = label
464
+ self.pad_height = float(height)
465
+ self.stroke_width = float(stroke_width)
466
+ self.disabled = disabled
467
+ self.on_change = on_change
468
+ self._strokes: list[list[tuple[float, float]]] = []
469
+ self._drawing: bool = False
470
+ self._clear_rect: Rect = Rect(0, 0, 0, 0)
471
+ self._fill_width = True
472
+
473
+ @property
474
+ def is_empty(self) -> bool:
475
+ return not any(len(s) >= 2 for s in self._strokes)
476
+
477
+ def clear(self) -> None:
478
+ self._strokes.clear()
479
+ if self.on_change is not None:
480
+ self.on_change()
481
+ self.invalidate()
482
+
483
+ def export_png(self, path: str | Path, *, scale: float = 2.0) -> Path:
484
+ """Export the signature vector strokes to a crisp transparent PNG."""
485
+ w = max(1, int(round((self.rect.width or 360.0) * scale)))
486
+ h = max(1, int(round((self.rect.height or self.pad_height) * scale)))
487
+ surf = skia.Surface(w, h)
488
+ with surf as canvas:
489
+ canvas.scale(scale, scale)
490
+ paint = skia.Paint(
491
+ AntiAlias=True,
492
+ Color=skia.Color(29, 27, 32),
493
+ Style=skia.Paint.kStroke_Style,
494
+ StrokeWidth=self.stroke_width,
495
+ StrokeCap=skia.Paint.kRound_Cap,
496
+ StrokeJoin=skia.Paint.kRound_Join,
497
+ )
498
+ for stroke in self._strokes:
499
+ if len(stroke) < 2:
500
+ continue
501
+ p = skia.Path()
502
+ p.moveTo(stroke[0][0], stroke[0][1])
503
+ for px, py in stroke[1:]:
504
+ p.lineTo(px, py)
505
+ canvas.drawPath(p, paint)
506
+ out = Path(path)
507
+ out.parent.mkdir(parents=True, exist_ok=True)
508
+ surf.makeImageSnapshot().save(str(out))
509
+ return out
510
+
511
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
512
+ w = constraints.max_width if constraints.max_width != float("inf") else 380.0
513
+ return Size(w, self.pad_height)
514
+
515
+ def on_mouse_down(self, event: MouseEvent) -> None:
516
+ super().on_mouse_down(event)
517
+ if self.disabled:
518
+ return
519
+ if self._clear_rect.contains(event.x, event.y):
520
+ self.clear()
521
+ return
522
+ self._drawing = True
523
+ self._strokes.append([(event.x - self.rect.x, event.y - self.rect.y)])
524
+ self.invalidate()
525
+
526
+ def on_mouse_move(self, event: MouseEvent) -> None:
527
+ if self._drawing and self._strokes:
528
+ lx = max(2.0, min(self.rect.width - 2.0, event.x - self.rect.x))
529
+ ly = max(2.0, min(self.rect.height - 2.0, event.y - self.rect.y))
530
+ self._strokes[-1].append((lx, ly))
531
+ self.invalidate()
532
+
533
+ def on_mouse_up(self, event: MouseEvent) -> None:
534
+ super().on_mouse_up(event)
535
+ if self._drawing:
536
+ self._drawing = False
537
+ if self.on_change is not None:
538
+ self.on_change()
539
+
540
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
541
+ c = theme.colors
542
+ r = self.rect
543
+ rad_spec = self._shape_override if self._shape_override is not None else "medium"
544
+ rrect = make_rrect_spec(r, rad_spec, theme)
545
+ bg = resolve_color(self._bg_override, theme, c.surface_container_lowest) or c.surface_container_lowest
546
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
547
+
548
+ # Baseline guide
549
+ guide_y = r.bottom - 28.0
550
+ canvas.drawLine(
551
+ r.x + 20.0, guide_y, r.right - 20.0, guide_y,
552
+ skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0),
553
+ )
554
+ sfont = font_for_style(theme.font_family, theme.typography.label_small)
555
+ draw_text_centered_y(canvas, self.label, r.x + 20.0, r.bottom - 14.0, sfont, c.on_surface_variant)
556
+
557
+ # Clear button top-right
558
+ self._clear_rect = Rect(r.right - 68.0, r.y + 8.0, 58.0, 24.0)
559
+ canvas.drawRRect(make_rrect(self._clear_rect, 12.0), skia.Paint(AntiAlias=True, Color=c.surface_container_high.to_skia()))
560
+ cw = measure_text(sfont, "Limpiar")
561
+ draw_text_centered_y(canvas, "Limpiar", self._clear_rect.center_x - cw * 0.5, self._clear_rect.center_y, sfont, c.on_surface_variant)
562
+
563
+ # Vector strokes
564
+ ink = resolve_color(self._fg_override, theme, c.on_surface) or c.on_surface
565
+ paint = skia.Paint(
566
+ AntiAlias=True,
567
+ Color=ink.to_skia(),
568
+ Style=skia.Paint.kStroke_Style,
569
+ StrokeWidth=self.stroke_width,
570
+ StrokeCap=skia.Paint.kRound_Cap,
571
+ StrokeJoin=skia.Paint.kRound_Join,
572
+ )
573
+ canvas.save()
574
+ canvas.clipRRect(rrect, doAntiAlias=True)
575
+ for stroke in self._strokes:
576
+ if len(stroke) < 2:
577
+ continue
578
+ p = skia.Path()
579
+ p.moveTo(r.x + stroke[0][0], r.y + stroke[0][1])
580
+ for px, py in stroke[1:]:
581
+ p.lineTo(r.x + px, r.y + py)
582
+ canvas.drawPath(p, paint)
583
+ canvas.restore()
584
+
585
+ bc = resolve_color(self._border_color_override, theme, c.outline_variant) or c.outline_variant
586
+ bw = self._border_width_override if self._border_width_override is not None else 1.0
587
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw))