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.
- quantumgui/__init__.py +9 -0
- quantumgui/__main__.py +6 -0
- quantumgui/cli.py +341 -0
- quantumgui/core/__init__.py +39 -0
- quantumgui/core/animation.py +221 -0
- quantumgui/core/audit.py +323 -0
- quantumgui/core/context.py +54 -0
- quantumgui/core/data_source.py +426 -0
- quantumgui/core/draw.py +428 -0
- quantumgui/core/enterprise.py +402 -0
- quantumgui/core/enterprise_app.py +419 -0
- quantumgui/core/events.py +34 -0
- quantumgui/core/geometry.py +108 -0
- quantumgui/core/native_dialogs.py +276 -0
- quantumgui/core/pdf_report.py +390 -0
- quantumgui/core/rules.py +284 -0
- quantumgui/core/state.py +42 -0
- quantumgui/core/task_runner.py +181 -0
- quantumgui/core/widget.py +498 -0
- quantumgui/core/window.py +920 -0
- quantumgui/core/xlsx_export.py +145 -0
- quantumgui/py.typed +0 -0
- quantumgui/theme/__init__.py +32 -0
- quantumgui/theme/hct.py +191 -0
- quantumgui/theme/theme.py +93 -0
- quantumgui/theme/tokens.py +423 -0
- quantumgui/ui.py +2142 -0
- quantumgui/widgets/__init__.py +168 -0
- quantumgui/widgets/advanced.py +499 -0
- quantumgui/widgets/button.py +224 -0
- quantumgui/widgets/charts.py +625 -0
- quantumgui/widgets/charts_bi.py +528 -0
- quantumgui/widgets/charts_extra.py +430 -0
- quantumgui/widgets/charts_pro.py +568 -0
- quantumgui/widgets/checkbox.py +199 -0
- quantumgui/widgets/chip.py +219 -0
- quantumgui/widgets/containers.py +321 -0
- quantumgui/widgets/data_controls.py +487 -0
- quantumgui/widgets/data_grid.py +956 -0
- quantumgui/widgets/data_table.py +551 -0
- quantumgui/widgets/date_picker.py +407 -0
- quantumgui/widgets/display_media.py +372 -0
- quantumgui/widgets/display_pro.py +407 -0
- quantumgui/widgets/drawer.py +252 -0
- quantumgui/widgets/enterprise_controls.py +934 -0
- quantumgui/widgets/fab.py +261 -0
- quantumgui/widgets/forms_pro.py +703 -0
- quantumgui/widgets/forms_pro_media.py +587 -0
- quantumgui/widgets/inputs.py +870 -0
- quantumgui/widgets/layout_pro.py +162 -0
- quantumgui/widgets/list_view.py +307 -0
- quantumgui/widgets/masked_input.py +131 -0
- quantumgui/widgets/master_detail.py +155 -0
- quantumgui/widgets/navigation.py +382 -0
- quantumgui/widgets/overlays.py +355 -0
- quantumgui/widgets/productivity.py +500 -0
- quantumgui/widgets/productivity_boards.py +544 -0
- quantumgui/widgets/radio.py +209 -0
- quantumgui/widgets/scroll_view.py +129 -0
- quantumgui/widgets/select.py +373 -0
- quantumgui/widgets/sheets.py +249 -0
- quantumgui/widgets/slider.py +209 -0
- quantumgui/widgets/switch.py +196 -0
- quantumgui/widgets/tabs.py +175 -0
- quantumgui/widgets/text.py +101 -0
- quantumgui/widgets/text_field.py +490 -0
- quantumgui/widgets/time_picker.py +499 -0
- quantumgui-1.0.0.dist-info/METADATA +137 -0
- quantumgui-1.0.0.dist-info/RECORD +73 -0
- quantumgui-1.0.0.dist-info/WHEEL +5 -0
- quantumgui-1.0.0.dist-info/entry_points.txt +2 -0
- quantumgui-1.0.0.dist-info/licenses/LICENSE +32 -0
- quantumgui-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Reactive Enterprise Master-Detail container (`ui.master_detail`).
|
|
2
|
+
|
|
3
|
+
Automatically synchronizes a Master `DataGrid` (e.g. Invoices / Customers) with a Detail
|
|
4
|
+
`DataGrid` (e.g. Invoice Items / Movements) filtered by `master_key -> detail_foreign_key`
|
|
5
|
+
using either SQL queries (`SQLDataSource`), `list[dict]`, or a custom `detail_loader`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any, Callable, Mapping, Sequence
|
|
11
|
+
|
|
12
|
+
from ..core.data_source import DataSource, SQLDataSource, _quote_ident, coerce_data_source
|
|
13
|
+
from .containers import Column
|
|
14
|
+
from .data_grid import DataGrid, GridColumn
|
|
15
|
+
from .text import Text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MasterDetail(Column):
|
|
19
|
+
"""Reactive Master-Detail ERP layout pairing two synchronized `DataGrid` widgets."""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
master_source: Any,
|
|
24
|
+
detail_source: Any,
|
|
25
|
+
*,
|
|
26
|
+
master_key: str = "id",
|
|
27
|
+
detail_foreign_key: str = "master_id",
|
|
28
|
+
master_title: str = "Maestro",
|
|
29
|
+
detail_title: str = "Detalle",
|
|
30
|
+
master_height: float = 240.0,
|
|
31
|
+
detail_height: float = 220.0,
|
|
32
|
+
page_size: int | None = 100,
|
|
33
|
+
virtualized: bool = True,
|
|
34
|
+
editable: bool = False,
|
|
35
|
+
master_overrides: Mapping[str, GridColumn] | None = None,
|
|
36
|
+
detail_overrides: Mapping[str, GridColumn] | None = None,
|
|
37
|
+
detail_loader: Callable[[dict[str, Any]], Sequence[Sequence[Any]]] | None = None,
|
|
38
|
+
on_master_select: Callable[[dict[str, Any], list[list[Any]]], None] | None = None,
|
|
39
|
+
) -> None:
|
|
40
|
+
super().__init__(gap=10.0, align="stretch")
|
|
41
|
+
self._fill_width = True
|
|
42
|
+
self._master_src: DataSource = coerce_data_source(master_source)
|
|
43
|
+
self._detail_src: DataSource = coerce_data_source(detail_source)
|
|
44
|
+
self.master_key = master_key
|
|
45
|
+
self.detail_foreign_key = detail_foreign_key
|
|
46
|
+
self.detail_loader = detail_loader
|
|
47
|
+
self.on_master_select = on_master_select
|
|
48
|
+
self._selected_master: dict[str, Any] | None = None
|
|
49
|
+
|
|
50
|
+
self._master_header = Text(master_title, role="title_small")
|
|
51
|
+
self.add_child(self._master_header)
|
|
52
|
+
|
|
53
|
+
self.master_grid = DataGrid(
|
|
54
|
+
source=self._master_src,
|
|
55
|
+
column_overrides=master_overrides,
|
|
56
|
+
page_size=page_size,
|
|
57
|
+
virtualized=virtualized,
|
|
58
|
+
editable=editable,
|
|
59
|
+
height=master_height,
|
|
60
|
+
on_row_click=self._handle_master_click,
|
|
61
|
+
)
|
|
62
|
+
self.add_child(self.master_grid)
|
|
63
|
+
|
|
64
|
+
self._detail_base_title = detail_title
|
|
65
|
+
self._detail_header = Text(detail_title, role="title_small")
|
|
66
|
+
self.add_child(self._detail_header)
|
|
67
|
+
|
|
68
|
+
# Build detail columns from detail_source, starting empty until a master row is selected
|
|
69
|
+
detail_cols = self._build_detail_columns(detail_overrides)
|
|
70
|
+
self.detail_grid = DataGrid(
|
|
71
|
+
columns=detail_cols,
|
|
72
|
+
rows=[],
|
|
73
|
+
virtualized=virtualized,
|
|
74
|
+
editable=editable,
|
|
75
|
+
height=detail_height,
|
|
76
|
+
)
|
|
77
|
+
self.add_child(self.detail_grid)
|
|
78
|
+
|
|
79
|
+
# Auto-select first master row if available
|
|
80
|
+
if self.master_grid.rows:
|
|
81
|
+
self.select_master(0)
|
|
82
|
+
|
|
83
|
+
def _build_detail_columns(
|
|
84
|
+
self, overrides: Mapping[str, GridColumn] | None
|
|
85
|
+
) -> list[GridColumn]:
|
|
86
|
+
ov = dict(overrides) if overrides else {}
|
|
87
|
+
types = self._detail_src.column_types
|
|
88
|
+
cols: list[GridColumn] = []
|
|
89
|
+
for field_name in self._detail_src.columns:
|
|
90
|
+
if field_name in ov:
|
|
91
|
+
cols.append(ov[field_name])
|
|
92
|
+
else:
|
|
93
|
+
inferred = types.get(field_name, "text")
|
|
94
|
+
col_type = inferred if inferred in ("text", "int", "float", "bool") else "text"
|
|
95
|
+
title = field_name.replace("_", " ").strip().title() or field_name
|
|
96
|
+
cols.append(GridColumn(title=title, type=col_type)) # type: ignore[arg-type]
|
|
97
|
+
return cols
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def selected_master(self) -> dict[str, Any] | None:
|
|
101
|
+
return dict(self._selected_master) if self._selected_master is not None else None
|
|
102
|
+
|
|
103
|
+
def _row_to_dict(self, row: Sequence[Any]) -> dict[str, Any]:
|
|
104
|
+
fields = self._master_src.columns
|
|
105
|
+
return {fields[i]: (row[i] if i < len(row) else None) for i in range(len(fields))}
|
|
106
|
+
|
|
107
|
+
def _fetch_detail_for_key(self, key_val: Any, master_dict: dict[str, Any]) -> list[list[Any]]:
|
|
108
|
+
if self.detail_loader is not None:
|
|
109
|
+
return [list(r) for r in self.detail_loader(master_dict)]
|
|
110
|
+
|
|
111
|
+
if isinstance(self._detail_src, SQLDataSource):
|
|
112
|
+
cur = self._detail_src._conn.cursor()
|
|
113
|
+
try:
|
|
114
|
+
base = self._detail_src._base_select()
|
|
115
|
+
ph = self._detail_src._placeholder
|
|
116
|
+
sql = f"SELECT * FROM ({base}) AS _qgui_det WHERE {_quote_ident(self.detail_foreign_key)} = {ph}"
|
|
117
|
+
cur.execute(sql, self._detail_src.params + (key_val,))
|
|
118
|
+
return [list(r) for r in cur.fetchall()]
|
|
119
|
+
finally:
|
|
120
|
+
cur.close()
|
|
121
|
+
|
|
122
|
+
# In-memory / CSV / DataFrame detail filtering
|
|
123
|
+
cols = self._detail_src.columns
|
|
124
|
+
if self.detail_foreign_key not in cols:
|
|
125
|
+
return []
|
|
126
|
+
fk_idx = cols.index(self.detail_foreign_key)
|
|
127
|
+
all_rows = self._detail_src.fetch()
|
|
128
|
+
return [r for r in all_rows if fk_idx < len(r) and r[fk_idx] == key_val]
|
|
129
|
+
|
|
130
|
+
def _handle_master_click(self, real_row: int, row: list[Any]) -> None:
|
|
131
|
+
master_dict = self._row_to_dict(row)
|
|
132
|
+
self._selected_master = master_dict
|
|
133
|
+
key_val = master_dict.get(self.master_key)
|
|
134
|
+
detail_rows = self._fetch_detail_for_key(key_val, master_dict)
|
|
135
|
+
self.detail_grid.rows = detail_rows
|
|
136
|
+
self._detail_header._content = f"{self._detail_base_title} ({len(detail_rows)})"
|
|
137
|
+
self._detail_header.invalidate()
|
|
138
|
+
if self.on_master_select is not None:
|
|
139
|
+
self.on_master_select(master_dict, detail_rows)
|
|
140
|
+
|
|
141
|
+
def select_master(self, row_index: int) -> bool:
|
|
142
|
+
"""Programmatically select a master row and synchronize the detail grid."""
|
|
143
|
+
if not (0 <= row_index < len(self.master_grid.rows)):
|
|
144
|
+
return False
|
|
145
|
+
self._handle_master_click(row_index, self.master_grid.rows[row_index])
|
|
146
|
+
return True
|
|
147
|
+
|
|
148
|
+
def reload(self) -> None:
|
|
149
|
+
"""Reload master grid and re-sync active detail rows."""
|
|
150
|
+
self.master_grid.reload()
|
|
151
|
+
if self.master_grid.rows:
|
|
152
|
+
self.select_master(0)
|
|
153
|
+
else:
|
|
154
|
+
self._selected_master = None
|
|
155
|
+
self.detail_grid.rows = []
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"""Material Design 3 Navigation components: TopAppBar, NavigationRail, NavigationBar, Divider."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Callable, Literal, Sequence
|
|
6
|
+
|
|
7
|
+
import skia
|
|
8
|
+
|
|
9
|
+
from ..core.draw import (
|
|
10
|
+
draw_icon,
|
|
11
|
+
draw_text_centered_y,
|
|
12
|
+
font_for_style,
|
|
13
|
+
make_rrect,
|
|
14
|
+
measure_text,
|
|
15
|
+
resolve_color,
|
|
16
|
+
)
|
|
17
|
+
from ..core.events import MouseEvent
|
|
18
|
+
from ..core.geometry import Constraints, Rect, Size
|
|
19
|
+
from ..core.state import State
|
|
20
|
+
from ..core.widget import Widget
|
|
21
|
+
from ..theme import Theme
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Divider(Widget):
|
|
25
|
+
"""Official Material Design 3 1dp Divider (horizontal or vertical)."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, *, vertical: bool = False, inset: float = 0.0) -> None:
|
|
28
|
+
super().__init__()
|
|
29
|
+
self.vertical = vertical
|
|
30
|
+
self.inset_px = float(inset)
|
|
31
|
+
if not vertical:
|
|
32
|
+
self._fill_width = True
|
|
33
|
+
else:
|
|
34
|
+
self._fill_height = True
|
|
35
|
+
|
|
36
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
37
|
+
if self.vertical:
|
|
38
|
+
h = constraints.max_height if constraints.max_height != float("inf") else 24.0
|
|
39
|
+
return Size(1.0, h)
|
|
40
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 100.0
|
|
41
|
+
return Size(w, 1.0)
|
|
42
|
+
|
|
43
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
44
|
+
paint = skia.Paint(
|
|
45
|
+
AntiAlias=True,
|
|
46
|
+
Color=theme.colors.outline_variant.to_skia(),
|
|
47
|
+
StrokeWidth=1.0,
|
|
48
|
+
)
|
|
49
|
+
if self.vertical:
|
|
50
|
+
x = self.rect.center_x
|
|
51
|
+
canvas.drawLine(x, self.rect.y + self.inset_px, x, self.rect.bottom - self.inset_px, paint)
|
|
52
|
+
else:
|
|
53
|
+
y = self.rect.center_y
|
|
54
|
+
canvas.drawLine(self.rect.x + self.inset_px, y, self.rect.right - self.inset_px, y, paint)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class TopAppBar(Widget):
|
|
58
|
+
"""Official Material Design 3 TopAppBar (64dp height).
|
|
59
|
+
|
|
60
|
+
Use with `with ui.top_app_bar("Title"):` to place trailing action widgets on the right.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
interactive = True
|
|
64
|
+
HEIGHT = 64.0
|
|
65
|
+
|
|
66
|
+
def __init__(
|
|
67
|
+
self,
|
|
68
|
+
title: str,
|
|
69
|
+
*,
|
|
70
|
+
subtitle: str | None = None,
|
|
71
|
+
leading_icon: str | None = None,
|
|
72
|
+
on_leading_click: Callable[[], None] | None = None,
|
|
73
|
+
variant: Literal["small", "center_aligned"] = "small",
|
|
74
|
+
) -> None:
|
|
75
|
+
super().__init__()
|
|
76
|
+
self.title = title
|
|
77
|
+
self.subtitle = subtitle
|
|
78
|
+
self.leading_icon = leading_icon
|
|
79
|
+
self.on_leading_click = on_leading_click
|
|
80
|
+
self.variant = variant
|
|
81
|
+
self._fill_width = True
|
|
82
|
+
self._leading_hovered = False
|
|
83
|
+
|
|
84
|
+
def _leading_rect(self) -> Rect:
|
|
85
|
+
return Rect(self.rect.x + 12.0, self.rect.center_y - 20.0, 40.0, 40.0)
|
|
86
|
+
|
|
87
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
88
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 480.0
|
|
89
|
+
return Size(w, self.HEIGHT)
|
|
90
|
+
|
|
91
|
+
def _layout_children(self, content_rect: Rect, theme: Theme) -> None:
|
|
92
|
+
visible = [c for c in self.children if c._visible]
|
|
93
|
+
if not visible:
|
|
94
|
+
return
|
|
95
|
+
# Lay out trailing actions from right to left
|
|
96
|
+
x = content_rect.right - 16.0
|
|
97
|
+
for child in reversed(visible):
|
|
98
|
+
sz = child.measure(Constraints(0.0, content_rect.width, 0.0, content_rect.height), theme)
|
|
99
|
+
x -= sz.width
|
|
100
|
+
y = content_rect.center_y - sz.height * 0.5
|
|
101
|
+
child.layout(Rect(x, y, sz.width, sz.height), theme)
|
|
102
|
+
x -= 8.0
|
|
103
|
+
|
|
104
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
105
|
+
if self.leading_icon:
|
|
106
|
+
hov = self._leading_rect().contains(event.x, event.y)
|
|
107
|
+
if hov != self._leading_hovered:
|
|
108
|
+
self._leading_hovered = hov
|
|
109
|
+
self.invalidate()
|
|
110
|
+
|
|
111
|
+
def on_mouse_leave(self) -> None:
|
|
112
|
+
super().on_mouse_leave()
|
|
113
|
+
self._leading_hovered = False
|
|
114
|
+
|
|
115
|
+
def on_mouse_up(self, event: MouseEvent) -> None:
|
|
116
|
+
was_pressed = self.pressed
|
|
117
|
+
super().on_mouse_up(event)
|
|
118
|
+
if was_pressed and self.leading_icon and self._leading_rect().contains(event.x, event.y):
|
|
119
|
+
if self.on_leading_click is not None:
|
|
120
|
+
self.on_leading_click()
|
|
121
|
+
|
|
122
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
123
|
+
c = theme.colors
|
|
124
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container) or c.surface_container
|
|
125
|
+
canvas.drawRect(
|
|
126
|
+
skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height),
|
|
127
|
+
skia.Paint(AntiAlias=True, Color=bg.to_skia()),
|
|
128
|
+
)
|
|
129
|
+
# Subtle bottom hairline
|
|
130
|
+
canvas.drawLine(
|
|
131
|
+
self.rect.x,
|
|
132
|
+
self.rect.bottom - 0.5,
|
|
133
|
+
self.rect.right,
|
|
134
|
+
self.rect.bottom - 0.5,
|
|
135
|
+
skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0),
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
if self.leading_icon:
|
|
139
|
+
lr = self._leading_rect()
|
|
140
|
+
if self._leading_hovered:
|
|
141
|
+
canvas.drawCircle(
|
|
142
|
+
lr.center_x,
|
|
143
|
+
lr.center_y,
|
|
144
|
+
20.0,
|
|
145
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
|
|
146
|
+
)
|
|
147
|
+
draw_icon(canvas, self.leading_icon, lr.center_x, lr.center_y, 20.0, c.on_surface)
|
|
148
|
+
|
|
149
|
+
title_font = font_for_style(theme.font_family, theme.typography.title_large)
|
|
150
|
+
if self.variant == "center_aligned":
|
|
151
|
+
tw = measure_text(title_font, self.title)
|
|
152
|
+
tx = self.rect.center_x - tw * 0.5
|
|
153
|
+
else:
|
|
154
|
+
tx = self.rect.x + (64.0 if self.leading_icon else 20.0)
|
|
155
|
+
|
|
156
|
+
if self.subtitle:
|
|
157
|
+
draw_text_centered_y(canvas, self.title, tx, self.rect.center_y - 9.0, title_font, c.on_surface)
|
|
158
|
+
sub_font = font_for_style(theme.font_family, theme.typography.body_small)
|
|
159
|
+
draw_text_centered_y(canvas, self.subtitle, tx, self.rect.center_y + 13.0, sub_font, c.on_surface_variant)
|
|
160
|
+
else:
|
|
161
|
+
draw_text_centered_y(canvas, self.title, tx, self.rect.center_y, title_font, c.on_surface)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class NavigationRail(Widget):
|
|
165
|
+
"""Official Material Design 3 NavigationRail (80dp wide vertical sidebar)."""
|
|
166
|
+
|
|
167
|
+
interactive = True
|
|
168
|
+
cursor = "pointer"
|
|
169
|
+
WIDTH = 84.0
|
|
170
|
+
ITEM_H = 64.0
|
|
171
|
+
|
|
172
|
+
def __init__(
|
|
173
|
+
self,
|
|
174
|
+
items: Sequence[tuple[str, str]],
|
|
175
|
+
*,
|
|
176
|
+
selected: int = 0,
|
|
177
|
+
bind: State[int] | None = None,
|
|
178
|
+
on_change: Callable[[int], None] | None = None,
|
|
179
|
+
) -> None:
|
|
180
|
+
super().__init__()
|
|
181
|
+
self.items: list[tuple[str, str]] = list(items)
|
|
182
|
+
self._bind = bind
|
|
183
|
+
self._selected: int = int(bind.value) if bind is not None else int(selected)
|
|
184
|
+
self.on_change = on_change
|
|
185
|
+
self._hover_idx: int = -1
|
|
186
|
+
|
|
187
|
+
if bind is not None:
|
|
188
|
+
bind.subscribe(self._on_bound_change)
|
|
189
|
+
|
|
190
|
+
def _on_bound_change(self, idx: int) -> None:
|
|
191
|
+
i = int(idx)
|
|
192
|
+
if i != self._selected:
|
|
193
|
+
self._selected = i
|
|
194
|
+
self.invalidate()
|
|
195
|
+
|
|
196
|
+
@property
|
|
197
|
+
def selected(self) -> int:
|
|
198
|
+
return self._selected
|
|
199
|
+
|
|
200
|
+
@selected.setter
|
|
201
|
+
def selected(self, idx: int) -> None:
|
|
202
|
+
i = max(0, min(len(self.items) - 1, int(idx))) if self.items else 0
|
|
203
|
+
if i != self._selected:
|
|
204
|
+
self._selected = i
|
|
205
|
+
if self._bind is not None and self._bind.value != i:
|
|
206
|
+
self._bind.value = i
|
|
207
|
+
self.invalidate()
|
|
208
|
+
|
|
209
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
210
|
+
min_h = max(len(self.items) * self.ITEM_H + 32.0, 240.0)
|
|
211
|
+
h = constraints.max_height if constraints.max_height != float("inf") else min_h
|
|
212
|
+
return Size(self.WIDTH, max(min_h, h))
|
|
213
|
+
|
|
214
|
+
def _item_rect(self, idx: int) -> Rect:
|
|
215
|
+
return Rect(self.rect.x, self.rect.y + 16.0 + idx * self.ITEM_H, self.rect.width, self.ITEM_H)
|
|
216
|
+
|
|
217
|
+
def _index_at(self, y: float) -> int:
|
|
218
|
+
idx = int((y - (self.rect.y + 16.0)) // self.ITEM_H)
|
|
219
|
+
return idx if 0 <= idx < len(self.items) else -1
|
|
220
|
+
|
|
221
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
222
|
+
idx = self._index_at(event.y)
|
|
223
|
+
if idx != self._hover_idx:
|
|
224
|
+
self._hover_idx = idx
|
|
225
|
+
self.invalidate()
|
|
226
|
+
|
|
227
|
+
def on_mouse_leave(self) -> None:
|
|
228
|
+
super().on_mouse_leave()
|
|
229
|
+
self._hover_idx = -1
|
|
230
|
+
|
|
231
|
+
def on_mouse_up(self, event: MouseEvent) -> None:
|
|
232
|
+
was_pressed = self.pressed
|
|
233
|
+
super().on_mouse_up(event)
|
|
234
|
+
if was_pressed:
|
|
235
|
+
idx = self._index_at(event.y)
|
|
236
|
+
if idx != -1 and idx != self._selected:
|
|
237
|
+
self.selected = idx
|
|
238
|
+
if self.on_change is not None:
|
|
239
|
+
self.on_change(idx)
|
|
240
|
+
|
|
241
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
242
|
+
c = theme.colors
|
|
243
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container_low) or c.surface_container_low
|
|
244
|
+
canvas.drawRect(
|
|
245
|
+
skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height),
|
|
246
|
+
skia.Paint(AntiAlias=True, Color=bg.to_skia()),
|
|
247
|
+
)
|
|
248
|
+
canvas.drawLine(
|
|
249
|
+
self.rect.right - 0.5,
|
|
250
|
+
self.rect.y,
|
|
251
|
+
self.rect.right - 0.5,
|
|
252
|
+
self.rect.bottom,
|
|
253
|
+
skia.Paint(AntiAlias=True, Color=c.outline_variant.to_skia(), StrokeWidth=1.0),
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
label_font = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
257
|
+
for i, (icon_name, label) in enumerate(self.items):
|
|
258
|
+
ir = self._item_rect(i)
|
|
259
|
+
is_sel = i == self._selected
|
|
260
|
+
pill = Rect(ir.center_x - 28.0, ir.y + 4.0, 56.0, 32.0)
|
|
261
|
+
pill_rrect = make_rrect(pill, 16.0)
|
|
262
|
+
|
|
263
|
+
if is_sel:
|
|
264
|
+
canvas.drawRRect(pill_rrect, skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()))
|
|
265
|
+
elif i == self._hover_idx:
|
|
266
|
+
canvas.drawRRect(
|
|
267
|
+
pill_rrect,
|
|
268
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
icon_c = c.on_secondary_container if is_sel else c.on_surface_variant
|
|
272
|
+
text_c = c.on_surface if is_sel else c.on_surface_variant
|
|
273
|
+
draw_icon(canvas, icon_name, pill.center_x, pill.center_y, 20.0, icon_c)
|
|
274
|
+
|
|
275
|
+
lw = measure_text(label_font, label)
|
|
276
|
+
draw_text_centered_y(canvas, label, ir.center_x - lw * 0.5, pill.bottom + 12.0, label_font, text_c)
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class NavigationBar(Widget):
|
|
280
|
+
"""Official Material Design 3 Bottom NavigationBar (80dp tall)."""
|
|
281
|
+
|
|
282
|
+
interactive = True
|
|
283
|
+
cursor = "pointer"
|
|
284
|
+
HEIGHT = 80.0
|
|
285
|
+
|
|
286
|
+
def __init__(
|
|
287
|
+
self,
|
|
288
|
+
items: Sequence[tuple[str, str]],
|
|
289
|
+
*,
|
|
290
|
+
selected: int = 0,
|
|
291
|
+
bind: State[int] | None = None,
|
|
292
|
+
on_change: Callable[[int], None] | None = None,
|
|
293
|
+
) -> None:
|
|
294
|
+
super().__init__()
|
|
295
|
+
self.items: list[tuple[str, str]] = list(items)
|
|
296
|
+
self._bind = bind
|
|
297
|
+
self._selected: int = int(bind.value) if bind is not None else int(selected)
|
|
298
|
+
self.on_change = on_change
|
|
299
|
+
self._hover_idx: int = -1
|
|
300
|
+
self._fill_width = True
|
|
301
|
+
|
|
302
|
+
if bind is not None:
|
|
303
|
+
bind.subscribe(self._on_bound_change)
|
|
304
|
+
|
|
305
|
+
def _on_bound_change(self, idx: int) -> None:
|
|
306
|
+
i = int(idx)
|
|
307
|
+
if i != self._selected:
|
|
308
|
+
self._selected = i
|
|
309
|
+
self.invalidate()
|
|
310
|
+
|
|
311
|
+
@property
|
|
312
|
+
def selected(self) -> int:
|
|
313
|
+
return self._selected
|
|
314
|
+
|
|
315
|
+
@selected.setter
|
|
316
|
+
def selected(self, idx: int) -> None:
|
|
317
|
+
i = max(0, min(len(self.items) - 1, int(idx))) if self.items else 0
|
|
318
|
+
if i != self._selected:
|
|
319
|
+
self._selected = i
|
|
320
|
+
if self._bind is not None and self._bind.value != i:
|
|
321
|
+
self._bind.value = i
|
|
322
|
+
self.invalidate()
|
|
323
|
+
|
|
324
|
+
def _measure_content(self, constraints: Constraints, theme: Theme) -> Size:
|
|
325
|
+
w = constraints.max_width if constraints.max_width != float("inf") else 480.0
|
|
326
|
+
return Size(w, self.HEIGHT)
|
|
327
|
+
|
|
328
|
+
def _index_at(self, x: float) -> int:
|
|
329
|
+
if not self.items or self.rect.width <= 0:
|
|
330
|
+
return -1
|
|
331
|
+
slot_w = self.rect.width / len(self.items)
|
|
332
|
+
idx = int((x - self.rect.x) // slot_w)
|
|
333
|
+
return idx if 0 <= idx < len(self.items) else -1
|
|
334
|
+
|
|
335
|
+
def on_mouse_move(self, event: MouseEvent) -> None:
|
|
336
|
+
idx = self._index_at(event.x)
|
|
337
|
+
if idx != self._hover_idx:
|
|
338
|
+
self._hover_idx = idx
|
|
339
|
+
self.invalidate()
|
|
340
|
+
|
|
341
|
+
def on_mouse_leave(self) -> None:
|
|
342
|
+
super().on_mouse_leave()
|
|
343
|
+
self._hover_idx = -1
|
|
344
|
+
|
|
345
|
+
def on_mouse_up(self, event: MouseEvent) -> None:
|
|
346
|
+
was_pressed = self.pressed
|
|
347
|
+
super().on_mouse_up(event)
|
|
348
|
+
if was_pressed:
|
|
349
|
+
idx = self._index_at(event.x)
|
|
350
|
+
if idx != -1 and idx != self._selected:
|
|
351
|
+
self.selected = idx
|
|
352
|
+
if self.on_change is not None:
|
|
353
|
+
self.on_change(idx)
|
|
354
|
+
|
|
355
|
+
def _paint_self(self, canvas: skia.Canvas, theme: Theme, now: float) -> None:
|
|
356
|
+
c = theme.colors
|
|
357
|
+
bg = resolve_color(self._bg_override, theme, c.surface_container) or c.surface_container
|
|
358
|
+
canvas.drawRect(
|
|
359
|
+
skia.Rect.MakeXYWH(self.rect.x, self.rect.y, self.rect.width, self.rect.height),
|
|
360
|
+
skia.Paint(AntiAlias=True, Color=bg.to_skia()),
|
|
361
|
+
)
|
|
362
|
+
if not self.items:
|
|
363
|
+
return
|
|
364
|
+
slot_w = self.rect.width / len(self.items)
|
|
365
|
+
label_font = font_for_style(theme.font_family, theme.typography.label_medium)
|
|
366
|
+
for i, (icon_name, label) in enumerate(self.items):
|
|
367
|
+
cx = self.rect.x + slot_w * (i + 0.5)
|
|
368
|
+
is_sel = i == self._selected
|
|
369
|
+
pill = Rect(cx - 32.0, self.rect.y + 12.0, 64.0, 32.0)
|
|
370
|
+
pill_rrect = make_rrect(pill, 16.0)
|
|
371
|
+
if is_sel:
|
|
372
|
+
canvas.drawRRect(pill_rrect, skia.Paint(AntiAlias=True, Color=c.secondary_container.to_skia()))
|
|
373
|
+
elif i == self._hover_idx:
|
|
374
|
+
canvas.drawRRect(
|
|
375
|
+
pill_rrect,
|
|
376
|
+
skia.Paint(AntiAlias=True, Color=c.on_surface.with_alpha(theme.state_layers.hover).to_skia()),
|
|
377
|
+
)
|
|
378
|
+
icon_c = c.on_secondary_container if is_sel else c.on_surface_variant
|
|
379
|
+
text_c = c.on_surface if is_sel else c.on_surface_variant
|
|
380
|
+
draw_icon(canvas, icon_name, pill.center_x, pill.center_y, 20.0, icon_c)
|
|
381
|
+
lw = measure_text(label_font, label)
|
|
382
|
+
draw_text_centered_y(canvas, label, cx - lw * 0.5, pill.bottom + 14.0, label_font, text_c)
|