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,168 @@
1
+ from .advanced import Badge, BadgeColor, Image, ImageFit, SearchBar, SegmentedButton
2
+ from .button import Button, ButtonSize, ButtonVariant
3
+ from .charts import BarChart, ChartColorRole, DonutChart, KPICard, LineChart
4
+ from .charts_bi import BoxPlotChart, SankeyChart, Sparkline, TreemapChart, WaterfallChart
5
+ from .charts_extra import CandlestickChart, FunnelChart, GanttChart, HorizontalBarChart
6
+ from .charts_pro import GaugeChart, Heatmap, MultiLineChart, RadarChart, ScatterChart
7
+ from .checkbox import Checkbox
8
+ from .chip import Chip, ChipVariant
9
+ from .containers import Card, CardVariant, Column, CrossAlign, MainJustify, Row
10
+ from .data_controls import ExpansionPanel, Pagination, Skeleton, TreeNode, TreeView
11
+ from .data_grid import AggregateOp, DataGrid, GridColAlign, GridColType, GridColumn
12
+ from .data_table import BadgeTone, ColumnAlign, ColumnType, DataTable, TableColumn
13
+ from .date_picker import DatePicker, DatePickerVariant
14
+ from .display_media import Carousel, CarouselItem, CodeBlock, MarkdownView, VectorCanvas
15
+ from .display_pro import Alert, AlertSeverity, Avatar, AvatarGroup, EmptyState, PresenceStatus, StatusBar
16
+ from .drawer import DrawerItem, NavigationDrawer
17
+ from .fab import FAB, FABSize, FABVariant, IconButton
18
+ from .forms_pro import Autocomplete, OTPInput, Rating, TagInput
19
+ from .forms_pro_media import DateRangePicker, DropZone, SignaturePad
20
+ from .inputs import ColorPicker, FilePicker, NumberInput, RangeSlider, TextArea
21
+ from .layout_pro import Grid, SplitView
22
+ from .list_view import ListItem, ListVariant, ListView
23
+ from .masked_input import MASK_PRESETS, MaskedInput
24
+ from .navigation import Divider, NavigationBar, NavigationRail, TopAppBar
25
+ from .overlays import Dialog, ProgressIndicator, Snackbar
26
+ from .productivity import Breadcrumbs, CommandItem, CommandPalette, Stepper, Timeline, TimelineItem
27
+ from .productivity_boards import CalendarEvent, CalendarView, KanbanBoard, KanbanCard, PropertyGrid, PropertyItem
28
+ from .radio import Radio, RadioDirection, RadioGroup
29
+ from .scroll_view import ScrollView
30
+ from .select import Select, SelectVariant
31
+ from .sheets import BottomSheet, Menu, MenuItem
32
+ from .slider import Slider
33
+ from .switch import Switch
34
+ from .tabs import Tabs, TabsVariant
35
+ from .text import Text, TextAlign, TextColorRole
36
+ from .text_field import TextField, TextFieldVariant
37
+ from .time_picker import TimePicker, TimePickerVariant
38
+
39
+ __all__ = [
40
+ "AggregateOp",
41
+ "Alert",
42
+ "AlertSeverity",
43
+ "Autocomplete",
44
+ "Avatar",
45
+ "AvatarGroup",
46
+ "Badge",
47
+ "BadgeColor",
48
+ "BadgeTone",
49
+ "BarChart",
50
+ "BottomSheet",
51
+ "BoxPlotChart",
52
+ "Breadcrumbs",
53
+ "Button",
54
+ "ButtonVariant",
55
+ "CalendarEvent",
56
+ "CalendarView",
57
+ "CandlestickChart",
58
+ "Card",
59
+ "CardVariant",
60
+ "Carousel",
61
+ "CarouselItem",
62
+ "ChartColorRole",
63
+ "Checkbox",
64
+ "Chip",
65
+ "ChipVariant",
66
+ "CodeBlock",
67
+ "ColorPicker",
68
+ "Column",
69
+ "ColumnAlign",
70
+ "ColumnType",
71
+ "CommandItem",
72
+ "CommandPalette",
73
+ "CrossAlign",
74
+ "DataGrid",
75
+ "DataTable",
76
+ "GridColAlign",
77
+ "GridColType",
78
+ "GridColumn",
79
+ "TableColumn",
80
+ "DatePicker",
81
+ "DatePickerVariant",
82
+ "DateRangePicker",
83
+ "Dialog",
84
+ "Divider",
85
+ "DonutChart",
86
+ "DrawerItem",
87
+ "DropZone",
88
+ "EmptyState",
89
+ "ExpansionPanel",
90
+ "FAB",
91
+ "FABSize",
92
+ "FABVariant",
93
+ "FilePicker",
94
+ "FunnelChart",
95
+ "GanttChart",
96
+ "GaugeChart",
97
+ "Grid",
98
+ "Heatmap",
99
+ "HorizontalBarChart",
100
+ "IconButton",
101
+ "Image",
102
+ "ImageFit",
103
+ "KPICard",
104
+ "KanbanBoard",
105
+ "KanbanCard",
106
+ "LineChart",
107
+ "ListItem",
108
+ "ListVariant",
109
+ "ListView",
110
+ "MASK_PRESETS",
111
+ "MaskedInput",
112
+ "MainJustify",
113
+ "MarkdownView",
114
+ "Menu",
115
+ "MenuItem",
116
+ "MultiLineChart",
117
+ "NavigationBar",
118
+ "NavigationDrawer",
119
+ "NavigationRail",
120
+ "NumberInput",
121
+ "OTPInput",
122
+ "Pagination",
123
+ "PresenceStatus",
124
+ "ProgressIndicator",
125
+ "PropertyGrid",
126
+ "PropertyItem",
127
+ "RadarChart",
128
+ "Radio",
129
+ "RadioDirection",
130
+ "RadioGroup",
131
+ "RangeSlider",
132
+ "Rating",
133
+ "Row",
134
+ "SankeyChart",
135
+ "ScatterChart",
136
+ "ScrollView",
137
+ "SearchBar",
138
+ "SegmentedButton",
139
+ "Select",
140
+ "SelectVariant",
141
+ "SignaturePad",
142
+ "Skeleton",
143
+ "Slider",
144
+ "Snackbar",
145
+ "Sparkline",
146
+ "SplitView",
147
+ "StatusBar",
148
+ "Stepper",
149
+ "Switch",
150
+ "Tabs",
151
+ "TabsVariant",
152
+ "TagInput",
153
+ "Text",
154
+ "TextArea",
155
+ "TextColorRole",
156
+ "TextField",
157
+ "TextFieldVariant",
158
+ "TimePicker",
159
+ "TimePickerVariant",
160
+ "Timeline",
161
+ "TimelineItem",
162
+ "TopAppBar",
163
+ "TreeNode",
164
+ "TreeView",
165
+ "TreemapChart",
166
+ "VectorCanvas",
167
+ "WaterfallChart",
168
+ ]
@@ -0,0 +1,499 @@
1
+ """Material Design 3 Phase 5 widgets: Image, Badge, SegmentedButton, and SearchBar."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from typing import Any, 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
+ make_rrect,
17
+ make_rrect_spec,
18
+ measure_text,
19
+ resolve_color,
20
+ )
21
+ from ..core.events import CharEvent, KeyEvent, MouseEvent
22
+ from ..core.geometry import Constraints, Rect, Size
23
+ from ..core.state import State
24
+ from ..core.widget import Widget
25
+ from ..theme import Color, ShapeToken, Theme
26
+ from .data_table import _draw_image_cover, _resolve_skia_image
27
+
28
+ ImageFit = Literal["cover", "contain", "fill"]
29
+ BadgeColor = Literal["error", "primary", "secondary", "tertiary"]
30
+
31
+
32
+ class Image(Widget):
33
+ """GPU-rendered Image widget supporting file paths, bytes, or `skia.Image`."""
34
+
35
+ manages_own_box = True
36
+
37
+ def __init__(
38
+ self,
39
+ source: Any,
40
+ *,
41
+ fit: ImageFit = "cover",
42
+ radius: ShapeToken | float = "medium",
43
+ ) -> None:
44
+ super().__init__()
45
+ self._source = source
46
+ self.fit: ImageFit = fit
47
+ self._shape_override = radius
48
+
49
+ @property
50
+ def source(self) -> Any:
51
+ return self._source
52
+
53
+ @source.setter
54
+ def source(self, val: Any) -> None:
55
+ self._source = val
56
+ self.invalidate()
57
+
58
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
59
+ img = _resolve_skia_image(self._source)
60
+ if img is not None:
61
+ return Size(float(img.width()), float(img.height()))
62
+ return Size(120.0, 120.0)
63
+
64
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
65
+ c = theme.colors
66
+ rad_spec = self._shape_override if self._shape_override is not None else "medium"
67
+ rad = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
68
+ if self._elevation_override:
69
+ sh = resolve_color(self._shadow_color_override, theme, c.shadow)
70
+ assert sh is not None
71
+ draw_elevation_shadow(canvas, self.rect, rad, self._elevation_override, sh)
72
+ rrect = make_rrect_spec(self.rect, rad_spec, theme)
73
+ img = _resolve_skia_image(self._source)
74
+
75
+ canvas.save()
76
+ canvas.clipRRect(rrect, doAntiAlias=True)
77
+ if img is None:
78
+ bg = resolve_color(self._bg_override, theme, c.surface_container_highest) or c.surface_container_highest
79
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
80
+ elif self.fit == "cover":
81
+ _draw_image_cover(canvas, img, self.rect)
82
+ elif self.fit == "fill":
83
+ dst = skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height)
84
+ sampling = skia.SamplingOptions(skia.FilterMode.kLinear, skia.MipmapMode.kLinear)
85
+ canvas.drawImageRect(img, dst, sampling, skia.Paint(AntiAlias=True))
86
+ else:
87
+ # contain
88
+ bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
89
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
90
+ iw, ih = float(img.width()), float(img.height())
91
+ if iw > 0 and ih > 0:
92
+ scale = min(self.rect.width / iw, self.rect.height / ih)
93
+ dw, dh = iw * scale, ih * scale
94
+ dx = self.rect.center_x - dw * 0.5
95
+ dy = self.rect.center_y - dh * 0.5
96
+ dst = skia.Rect.MakeXYWH(dx, dy, dw, dh)
97
+ sampling = skia.SamplingOptions(skia.FilterMode.kLinear, skia.MipmapMode.kLinear)
98
+ canvas.drawImageRect(img, dst, sampling, skia.Paint(AntiAlias=True))
99
+ canvas.restore()
100
+
101
+ if self._border_color_override is not None:
102
+ bc = resolve_color(self._border_color_override, theme)
103
+ if bc is not None:
104
+ bw = self._border_width_override if self._border_width_override is not None else 1.0
105
+ canvas.drawRRect(
106
+ rrect,
107
+ skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
108
+ )
109
+
110
+
111
+ class Badge(Widget):
112
+ """Official Material Design 3 Small (6dp dot) or Large (pill with label) Badge."""
113
+
114
+ manages_own_box = True
115
+
116
+ def __init__(
117
+ self,
118
+ value: str | int | None = None,
119
+ *,
120
+ color: BadgeColor = "error",
121
+ ) -> None:
122
+ super().__init__()
123
+ self._value = value
124
+ self.color: BadgeColor = color
125
+
126
+ @property
127
+ def value(self) -> str | int | None:
128
+ return self._value
129
+
130
+ @value.setter
131
+ def value(self, val: str | int | None) -> None:
132
+ self._value = val
133
+ self.invalidate()
134
+
135
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
136
+ if self._value is None:
137
+ return Size(8.0, 8.0)
138
+ font = font_for_style(theme.font_family, theme.typography.label_small)
139
+ tw = measure_text(font, str(self._value))
140
+ return Size(max(18.0, tw + 10.0), 18.0)
141
+
142
+ def _colors(self, theme: Theme) -> tuple[Color, Color]:
143
+ c = theme.colors
144
+ if self.color == "primary":
145
+ return (c.primary, c.on_primary)
146
+ if self.color == "secondary":
147
+ return (c.secondary, c.on_secondary)
148
+ if self.color == "tertiary":
149
+ return (c.tertiary, c.on_tertiary)
150
+ return (c.error, c.on_error)
151
+
152
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
153
+ bg, fg = self._colors(theme)
154
+ if self._bg_override is not None:
155
+ bg = resolve_color(self._bg_override, theme, bg) or bg
156
+ if self._fg_override is not None:
157
+ fg = resolve_color(self._fg_override, theme, fg) or fg
158
+ if self._value is None:
159
+ canvas.drawCircle(self.rect.center_x, self.rect.center_y, 3.5, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
160
+ if self._border_color_override is not None:
161
+ bc = resolve_color(self._border_color_override, theme)
162
+ if bc is not None:
163
+ bw = self._border_width_override if self._border_width_override is not None else 1.0
164
+ canvas.drawCircle(
165
+ self.rect.center_x, self.rect.center_y, 3.5,
166
+ skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
167
+ )
168
+ return
169
+ rad_spec = self._shape_override if self._shape_override is not None else self.rect.height * 0.5
170
+ rrect = make_rrect_spec(self.rect, rad_spec, theme)
171
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
172
+ if self._border_color_override is not None:
173
+ bc = resolve_color(self._border_color_override, theme)
174
+ if bc is not None:
175
+ bw = self._border_width_override if self._border_width_override is not None else 1.0
176
+ canvas.drawRRect(
177
+ rrect,
178
+ skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
179
+ )
180
+ font = font_for_style(theme.font_family, theme.typography.label_small)
181
+ txt = str(self._value)
182
+ tw = measure_text(font, txt)
183
+ draw_text_centered_y(canvas, txt, self.rect.center_x - tw * 0.5, self.rect.center_y, font, fg)
184
+
185
+
186
+ class SegmentedButton(Widget):
187
+ """Official Material Design 3 SegmentedButton (single or multi-select, 40dp pill)."""
188
+
189
+ interactive = True
190
+ focusable = True
191
+ manages_own_box = True
192
+ cursor = "pointer"
193
+
194
+ HEIGHT = 40.0
195
+
196
+ def __init__(
197
+ self,
198
+ options: Sequence[str],
199
+ *,
200
+ selected: str | set[str] | None = None,
201
+ multi_select: bool = False,
202
+ disabled: bool = False,
203
+ bind: State[Any] | None = None,
204
+ on_change: Callable[[Any], None] | None = None,
205
+ ) -> None:
206
+ super().__init__()
207
+ self.options = list(options)
208
+ self.multi_select = multi_select
209
+ self.disabled = disabled
210
+ self._bind = bind
211
+ self.on_change = on_change
212
+ self._hover_idx: int = -1
213
+
214
+ raw = bind.value if bind is not None else selected
215
+ if multi_select:
216
+ self._selected_set: set[str] = set(raw) if raw is not None else set()
217
+ else:
218
+ val = raw if raw is not None else (self.options[0] if self.options else None)
219
+ self._selected_set = {val} if val is not None else set()
220
+
221
+ if bind is not None:
222
+ bind.subscribe(self._on_bound_change)
223
+
224
+ def _on_bound_change(self, new_val: Any) -> None:
225
+ if self.multi_select:
226
+ self._selected_set = set(new_val) if new_val is not None else set()
227
+ else:
228
+ self._selected_set = {new_val} if new_val is not None else set()
229
+ self.invalidate()
230
+
231
+ @property
232
+ def value(self) -> Any:
233
+ if self.multi_select:
234
+ return set(self._selected_set)
235
+ return next(iter(self._selected_set), None)
236
+
237
+ @value.setter
238
+ def value(self, val: Any) -> None:
239
+ if self.multi_select:
240
+ self._selected_set = set(val) if val is not None else set()
241
+ else:
242
+ self._selected_set = {val} if val is not None else set()
243
+ if self._bind is not None:
244
+ self._bind.value = self.value
245
+ self.invalidate()
246
+
247
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
248
+ font = font_for_style(theme.font_family, theme.typography.label_large)
249
+ max_seg = max((measure_text(font, o) + 46.0 for o in self.options), default=80.0)
250
+ return Size(max_seg * max(1, len(self.options)), self.HEIGHT)
251
+
252
+ def _index_at(self, x: float) -> int:
253
+ if not self.options or self.rect.width <= 0:
254
+ return -1
255
+ seg_w = self.rect.width / len(self.options)
256
+ idx = int((x - self.rect.x) // seg_w)
257
+ return idx if 0 <= idx < len(self.options) else -1
258
+
259
+ def on_mouse_move(self, event: MouseEvent) -> None:
260
+ idx = self._index_at(event.x)
261
+ if idx != self._hover_idx:
262
+ self._hover_idx = idx
263
+ self.invalidate()
264
+
265
+ def on_mouse_leave(self) -> None:
266
+ super().on_mouse_leave()
267
+ self._hover_idx = -1
268
+
269
+ def on_mouse_up(self, event: MouseEvent) -> None:
270
+ was_pressed = self.pressed
271
+ super().on_mouse_up(event)
272
+ if self.disabled or not was_pressed or not self.rect.contains(event.x, event.y):
273
+ return
274
+ idx = self._index_at(event.x)
275
+ if idx == -1:
276
+ return
277
+ opt = self.options[idx]
278
+ if self.multi_select:
279
+ if opt in self._selected_set:
280
+ self._selected_set.remove(opt)
281
+ else:
282
+ self._selected_set.add(opt)
283
+ else:
284
+ self._selected_set = {opt}
285
+ if self._bind is not None:
286
+ self._bind.value = self.value
287
+ if self.on_change is not None:
288
+ self.on_change(self.value)
289
+ self.invalidate()
290
+
291
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
292
+ if not self.options:
293
+ return
294
+ c = theme.colors
295
+ rad_spec = self._shape_override if self._shape_override is not None else self.rect.height * 0.5
296
+ radius = rad_spec[0] if isinstance(rad_spec, tuple) else theme.shape.resolve(rad_spec)
297
+ if self._elevation_override:
298
+ sh = resolve_color(self._shadow_color_override, theme, c.shadow)
299
+ assert sh is not None
300
+ draw_elevation_shadow(canvas, self.rect, radius, self._elevation_override, sh)
301
+ rrect = make_rrect_spec(self.rect, rad_spec, theme)
302
+ n = len(self.options)
303
+ seg_w = self.rect.width / n
304
+ font = font_for_style(theme.font_family, theme.typography.label_large)
305
+
306
+ border_c = c.on_surface.with_alpha(0.12) if self.disabled else (
307
+ resolve_color(self._border_color_override, theme, c.outline) or c.outline
308
+ )
309
+ border_w = self._border_width_override if self._border_width_override is not None else 1.0
310
+
311
+ canvas.save()
312
+ canvas.clipRRect(rrect, doAntiAlias=True)
313
+
314
+ if self._bg_override is not None:
315
+ base_bg = resolve_color(self._bg_override, theme)
316
+ if base_bg is not None:
317
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=base_bg.to_skia()))
318
+
319
+ for i, opt in enumerate(self.options):
320
+ sr = Rect(self.rect.x + i * seg_w, self.rect.y, seg_w, self.rect.height)
321
+ is_sel = opt in self._selected_set
322
+ if is_sel:
323
+ bg = c.on_surface.with_alpha(0.12) if self.disabled else c.secondary_container
324
+ canvas.drawRect(skia.Rect.MakeXYWH(sr.x, sr.y, sr.width, sr.height), skia.Paint(AntiAlias=True, Color=bg.to_skia()))
325
+ elif not self.disabled and i == self._hover_idx:
326
+ canvas.drawRect(
327
+ skia.Rect.MakeXYWH(sr.x, sr.y, sr.width, sr.height),
328
+ skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
329
+ )
330
+
331
+ fg = c.on_surface.with_alpha(0.38) if self.disabled else (
332
+ resolve_color(self._fg_override, theme) or (c.on_secondary_container if is_sel else c.on_surface)
333
+ )
334
+ tw = measure_text(font, opt)
335
+ icon_w = 22.0 if is_sel else 0.0
336
+ sx = sr.center_x - (tw + icon_w) * 0.5
337
+ if is_sel:
338
+ draw_icon(canvas, "check", sx + 8.0, sr.center_y, 15.0, fg)
339
+ sx += icon_w
340
+ draw_text_centered_y(canvas, opt, sx, sr.center_y, font, fg)
341
+
342
+ if i > 0:
343
+ canvas.drawLine(
344
+ sr.x, sr.y, sr.x, sr.bottom,
345
+ skia.Paint(AntiAlias=True, Color=border_c.to_skia(), StrokeWidth=border_w),
346
+ )
347
+
348
+ canvas.restore()
349
+
350
+ canvas.drawRRect(
351
+ rrect,
352
+ skia.Paint(
353
+ AntiAlias=True,
354
+ Color=border_c.to_skia(),
355
+ Style=skia.Paint.kStroke_Style,
356
+ StrokeWidth=border_w,
357
+ ),
358
+ )
359
+
360
+
361
+ class SearchBar(Widget):
362
+ """Official Material Design 3 SearchBar (`filled`, `outlined`, or `elevated`)."""
363
+
364
+ interactive = True
365
+ focusable = True
366
+ manages_own_box = True
367
+ cursor = "ibeam"
368
+
369
+ HEIGHT = 52.0
370
+
371
+ def __init__(
372
+ self,
373
+ placeholder: str = "Buscar...",
374
+ *,
375
+ value: str = "",
376
+ variant: Literal["filled", "outlined", "elevated"] = "filled",
377
+ leading_icon: str = "search",
378
+ disabled: bool = False,
379
+ bind: State[str] | None = None,
380
+ on_change: Callable[[str], None] | None = None,
381
+ on_submit: Callable[[str], None] | None = None,
382
+ ) -> None:
383
+ super().__init__()
384
+ self.placeholder = placeholder
385
+ self.variant = variant
386
+ self.leading_icon = leading_icon
387
+ self.disabled = disabled
388
+ self._bind = bind
389
+ self._value = str(bind.value) if bind is not None else str(value)
390
+ self.on_change = on_change
391
+ self.on_submit = on_submit
392
+ self._blink_start = time.perf_counter()
393
+ self._fill_width = True
394
+
395
+ if bind is not None:
396
+ bind.subscribe(self._on_bound_change)
397
+
398
+ def _on_bound_change(self, new_val: str) -> None:
399
+ s = str(new_val)
400
+ if s != self._value:
401
+ self._value = s
402
+ self.invalidate()
403
+
404
+ @property
405
+ def value(self) -> str:
406
+ return self._value
407
+
408
+ @value.setter
409
+ def value(self, val: str) -> None:
410
+ s = str(val)
411
+ if s != self._value:
412
+ self._value = s
413
+ if self._bind is not None and self._bind.value != s:
414
+ self._bind.value = s
415
+ self.invalidate()
416
+
417
+ def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
418
+ w = 320.0 if constraints.max_width == float("inf") else min(360.0, constraints.max_width)
419
+ return Size(w, self.HEIGHT)
420
+
421
+ def tick(self, now: float) -> bool:
422
+ return self.focused
423
+
424
+ def _set_text(self, s: str) -> None:
425
+ self._value = s
426
+ self._blink_start = time.perf_counter()
427
+ if self._bind is not None and self._bind.value != s:
428
+ self._bind.value = s
429
+ if self.on_change is not None:
430
+ self.on_change(s)
431
+ self.invalidate()
432
+
433
+ def on_mouse_up(self, event: MouseEvent) -> None:
434
+ was_pressed = self.pressed
435
+ super().on_mouse_up(event)
436
+ if not self.disabled and was_pressed and self._value and event.x >= self.rect.right - 44.0:
437
+ self._set_text("")
438
+
439
+ def on_char(self, event: CharEvent) -> None:
440
+ if self.disabled or not self.focused:
441
+ return
442
+ self._set_text(self._value + event.char)
443
+
444
+ def on_key(self, event: KeyEvent) -> None:
445
+ if self.disabled or not self.focused or event.action == glfw.RELEASE:
446
+ return
447
+ if event.key == glfw.KEY_BACKSPACE and self._value:
448
+ self._set_text(self._value[:-1])
449
+ elif event.key in (glfw.KEY_ENTER, glfw.KEY_KP_ENTER):
450
+ if self.on_submit is not None:
451
+ self.on_submit(self._value)
452
+
453
+ def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
454
+ c = theme.colors
455
+ radius = (
456
+ self._shape_override[0]
457
+ if isinstance(self._shape_override, tuple)
458
+ else (theme.shape.resolve(self._shape_override) if self._shape_override is not None else self.rect.height * 0.5)
459
+ )
460
+ rrect = make_rrect(self.rect, radius)
461
+ elev = self._elevation_override if self._elevation_override is not None else (1 if self.variant == "elevated" else 0)
462
+ if elev > 0:
463
+ draw_elevation_shadow(canvas, self.rect, radius, elev, c.shadow)
464
+
465
+ default_bg = c.surface if self.variant == "outlined" else (c.surface_container_low if self.variant == "elevated" else c.surface_container_high)
466
+ bg = resolve_color(self._bg_override, theme, default_bg) or default_bg
467
+ if self.hovered and not self.disabled:
468
+ bg = bg.blend(c.on_surface, theme.state_layers.hover * 0.6)
469
+ canvas.drawRRect(rrect, skia.Paint(AntiAlias=True, Color=bg.to_skia()))
470
+
471
+ bc = resolve_color(self._border_color_override, theme)
472
+ if self.focused:
473
+ bc = c.primary
474
+ elif bc is None and self.variant == "outlined":
475
+ bc = c.outline
476
+ if bc is not None:
477
+ bw = self._border_width_override if self._border_width_override is not None else (1.5 if self.focused else 1.0)
478
+ canvas.drawRRect(
479
+ rrect,
480
+ skia.Paint(AntiAlias=True, Color=bc.to_skia(), Style=skia.Paint.kStroke_Style, StrokeWidth=bw),
481
+ )
482
+
483
+ draw_icon(canvas, self.leading_icon, self.rect.x + 26.0, self.rect.center_y, 20.0, c.on_surface_variant)
484
+
485
+ font = font_for_style(theme.font_family, theme.typography.body_large)
486
+ text_x = self.rect.x + 52.0
487
+ if self._value:
488
+ draw_text_centered_y(canvas, self._value, text_x, self.rect.center_y, font, c.on_surface)
489
+ draw_icon(canvas, "close", self.rect.right - 24.0, self.rect.center_y, 18.0, c.on_surface_variant)
490
+ else:
491
+ draw_text_centered_y(canvas, self.placeholder, text_x, self.rect.center_y, font, c.on_surface_variant)
492
+
493
+ if self.focused and not self.disabled:
494
+ if int((now - self._blink_start) / 0.53) % 2 == 0:
495
+ cx = text_x + measure_text(font, self._value)
496
+ canvas.drawLine(
497
+ cx, self.rect.center_y - 10.0, cx, self.rect.center_y + 10.0,
498
+ skia.Paint(AntiAlias=True, Color=c.primary.to_skia(), StrokeWidth=1.8),
499
+ )