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
quantumgui/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""QuantumGUI — Native GPU-accelerated Python GUI library built 100% on Material Design 3."""
|
|
2
|
+
|
|
3
|
+
from . import ui
|
|
4
|
+
from .core.state import State
|
|
5
|
+
from .theme import Color, Theme
|
|
6
|
+
|
|
7
|
+
__version__ = "1.0.0"
|
|
8
|
+
|
|
9
|
+
__all__ = ["Color", "State", "Theme", "__version__", "ui"]
|
quantumgui/__main__.py
ADDED
quantumgui/cli.py
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"""Official QuantumGUI CLI (`quantumgui run --watch`, `build`, `new`, `info`).
|
|
2
|
+
|
|
3
|
+
Zero external CLI dependencies (uses Python stdlib `argparse` + `subprocess`).
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import platform
|
|
10
|
+
import shutil
|
|
11
|
+
import subprocess
|
|
12
|
+
import sys
|
|
13
|
+
import time
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Sequence
|
|
16
|
+
|
|
17
|
+
__all__ = ["main", "build_package_command", "scaffold_project", "scan_py_mtimes"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _get_version() -> str:
|
|
21
|
+
try:
|
|
22
|
+
from . import __version__
|
|
23
|
+
|
|
24
|
+
return __version__
|
|
25
|
+
except Exception:
|
|
26
|
+
return "1.0.0"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# ---------------------------------------------------------------------------
|
|
30
|
+
# 1. `quantumgui info`
|
|
31
|
+
# ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def collect_env_info() -> dict[str, str]:
|
|
35
|
+
info: dict[str, str] = {
|
|
36
|
+
"quantumgui": _get_version(),
|
|
37
|
+
"python": platform.python_version(),
|
|
38
|
+
"platform": f"{platform.system()} {platform.release()} ({platform.machine()})",
|
|
39
|
+
}
|
|
40
|
+
for mod_name in ("skia", "glfw", "OpenGL"):
|
|
41
|
+
try:
|
|
42
|
+
m = __import__(mod_name)
|
|
43
|
+
info[mod_name] = str(getattr(m, "__version__", "installed"))
|
|
44
|
+
except Exception:
|
|
45
|
+
info[mod_name] = "not installed"
|
|
46
|
+
for tool in ("pyinstaller", "nuitka"):
|
|
47
|
+
info[tool] = "available" if shutil.which(tool) else "not installed"
|
|
48
|
+
return info
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def cmd_info(_args: argparse.Namespace) -> int:
|
|
52
|
+
info = collect_env_info()
|
|
53
|
+
print(f"QuantumGUI v{info['quantumgui']} — Material Design 3 Native GPU Engine")
|
|
54
|
+
print("-" * 56)
|
|
55
|
+
for k, v in info.items():
|
|
56
|
+
if k != "quantumgui":
|
|
57
|
+
print(f" {k:14s}: {v}")
|
|
58
|
+
return 0
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# ---------------------------------------------------------------------------
|
|
62
|
+
# 2. `quantumgui new <name>`
|
|
63
|
+
# ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
_TEMPLATE_BASIC = '''"""Aplicación inicial creada con `quantumgui new`."""
|
|
66
|
+
|
|
67
|
+
from quantumgui import Theme, ui
|
|
68
|
+
|
|
69
|
+
theme = Theme(seed="#6750A4", mode="light")
|
|
70
|
+
contador = ui.state(0)
|
|
71
|
+
|
|
72
|
+
win = ui.window("{title}", width=820, height=520, theme=theme, main=True)
|
|
73
|
+
with win:
|
|
74
|
+
with ui.column(gap=16, align="stretch").padding(24):
|
|
75
|
+
with ui.row(justify="space_between", align="center").fill_width():
|
|
76
|
+
ui.text("{title}", role="headline_medium")
|
|
77
|
+
ui.switch(
|
|
78
|
+
value=False,
|
|
79
|
+
label="Modo Oscuro",
|
|
80
|
+
on_change=lambda d: setattr(theme, "mode", "dark" if d else "light"),
|
|
81
|
+
)
|
|
82
|
+
with ui.card(variant="elevated", gap=12):
|
|
83
|
+
ui.text(lambda: f"Clicks acumulados: {{contador.value}}", role="title_large")
|
|
84
|
+
with ui.row(gap=10):
|
|
85
|
+
ui.button("Sumar +1", icon="add", on_click=lambda: setattr(contador, "value", contador.value + 1))
|
|
86
|
+
ui.button("Reiniciar", variant="outlined", on_click=lambda: setattr(contador, "value", 0))
|
|
87
|
+
|
|
88
|
+
win.run()
|
|
89
|
+
'''
|
|
90
|
+
|
|
91
|
+
_TEMPLATE_ERP = '''"""Plantilla ERP empresarial creada con `quantumgui new --template erp`."""
|
|
92
|
+
|
|
93
|
+
from quantumgui import Theme, ui
|
|
94
|
+
|
|
95
|
+
theme = Theme(seed="#0061A4", mode="light")
|
|
96
|
+
registros = [
|
|
97
|
+
{{"id": 1, "cliente": "Acme Corp", "estado": "Pagado", "importe": 12450.50}},
|
|
98
|
+
{{"id": 2, "cliente": "Globex S.A.", "estado": "Pendiente", "importe": 8900.00}},
|
|
99
|
+
{{"id": 3, "cliente": "Initech", "estado": "Pagado", "importe": 15200.00}},
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
win = ui.window("{title}", width=1040, height=680, theme=theme, main=True, min_size=(800, 520))
|
|
103
|
+
with win:
|
|
104
|
+
with ui.column(gap=14, align="stretch").padding(20):
|
|
105
|
+
with ui.row(justify="space_between", align="center").fill_width():
|
|
106
|
+
ui.text("{title} — Panel ERP", role="headline_small")
|
|
107
|
+
with ui.row(gap=8):
|
|
108
|
+
ui.button("Exportar Excel", variant="tonal", icon="save", on_click=lambda: grid.export_xlsx("reporte.xlsx"))
|
|
109
|
+
ui.switch(label="Oscuro", on_change=lambda d: setattr(theme, "mode", "dark" if d else "light"))
|
|
110
|
+
|
|
111
|
+
grid = ui.data_grid(
|
|
112
|
+
source=registros,
|
|
113
|
+
column_overrides={{
|
|
114
|
+
"importe": ui.grid_column("Importe", type="currency", aggregate="sum"),
|
|
115
|
+
"estado": ui.grid_column("Estado", type="badge", badge_colors={{"Pagado": "primary", "Pendiente": "tertiary"}}),
|
|
116
|
+
}},
|
|
117
|
+
page_size=100,
|
|
118
|
+
virtualized=True,
|
|
119
|
+
editable=True,
|
|
120
|
+
height=480,
|
|
121
|
+
).fill_width()
|
|
122
|
+
|
|
123
|
+
win.run()
|
|
124
|
+
'''
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def scaffold_project(name: str, *, target_dir: str | Path = ".", template: str = "basic") -> Path:
|
|
128
|
+
"""Create a runnable QuantumGUI starter project directory."""
|
|
129
|
+
dest = Path(target_dir) / name
|
|
130
|
+
dest.mkdir(parents=True, exist_ok=True)
|
|
131
|
+
title = name.replace("_", " ").replace("-", " ").title()
|
|
132
|
+
tpl = _TEMPLATE_ERP if template == "erp" else _TEMPLATE_BASIC
|
|
133
|
+
(dest / "main.py").write_text(tpl.format(title=title), encoding="utf-8")
|
|
134
|
+
(dest / "requirements.txt").write_text("quantumgui>=1.0.0\n", encoding="utf-8")
|
|
135
|
+
(dest / "README.md").write_text(
|
|
136
|
+
f"# {title}\n\nCreado con QuantumGUI.\n\n```bash\nquantumgui run main.py --watch\n```\n",
|
|
137
|
+
encoding="utf-8",
|
|
138
|
+
)
|
|
139
|
+
return dest
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def cmd_new(args: argparse.Namespace) -> int:
|
|
143
|
+
dest = scaffold_project(args.name, target_dir=args.dir, template=args.template)
|
|
144
|
+
print(f"Proyecto creado en: {dest.resolve()}")
|
|
145
|
+
print(f"Ejecuta: quantumgui run {dest / 'main.py'} --watch")
|
|
146
|
+
return 0
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# ---------------------------------------------------------------------------
|
|
150
|
+
# 3. `quantumgui run <script.py> [--watch]` (Hot-Reload)
|
|
151
|
+
# ---------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def scan_py_mtimes(root: Path) -> dict[Path, float]:
|
|
155
|
+
"""Return `{path: mtime}` for all `.py` files under `root` (skipping venv/.git/__pycache__)."""
|
|
156
|
+
skip = {".venv", "venv", ".git", "__pycache__", "build", "dist"}
|
|
157
|
+
out: dict[Path, float] = {}
|
|
158
|
+
if root.is_file():
|
|
159
|
+
return {root: root.stat().st_mtime}
|
|
160
|
+
for p in root.rglob("*.py"):
|
|
161
|
+
if any(part in skip for part in p.parts):
|
|
162
|
+
continue
|
|
163
|
+
try:
|
|
164
|
+
out[p] = p.stat().st_mtime
|
|
165
|
+
except OSError:
|
|
166
|
+
pass
|
|
167
|
+
return out
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def cmd_run(args: argparse.Namespace) -> int:
|
|
171
|
+
script = Path(args.script).resolve()
|
|
172
|
+
if not script.is_file():
|
|
173
|
+
print(f"Error: archivo no encontrado: {script}", file=sys.stderr)
|
|
174
|
+
return 1
|
|
175
|
+
|
|
176
|
+
cmd = [sys.executable, str(script), *args.extra]
|
|
177
|
+
if not args.watch:
|
|
178
|
+
return subprocess.call(cmd)
|
|
179
|
+
|
|
180
|
+
watch_dir = script.parent
|
|
181
|
+
print(f"[QuantumGUI Hot-Reload] Observando {watch_dir} (Ctrl+C para salir)")
|
|
182
|
+
mtimes = scan_py_mtimes(watch_dir)
|
|
183
|
+
proc = subprocess.Popen(cmd)
|
|
184
|
+
try:
|
|
185
|
+
while True:
|
|
186
|
+
time.sleep(max(0.15, float(args.interval)))
|
|
187
|
+
new_mtimes = scan_py_mtimes(watch_dir)
|
|
188
|
+
if new_mtimes != mtimes:
|
|
189
|
+
mtimes = new_mtimes
|
|
190
|
+
print("[QuantumGUI Hot-Reload] Cambio detectado — reiniciando app...")
|
|
191
|
+
if proc.poll() is None:
|
|
192
|
+
proc.terminate()
|
|
193
|
+
try:
|
|
194
|
+
proc.wait(timeout=3.0)
|
|
195
|
+
except subprocess.TimeoutExpired:
|
|
196
|
+
proc.kill()
|
|
197
|
+
proc = subprocess.Popen(cmd)
|
|
198
|
+
elif proc.poll() is not None:
|
|
199
|
+
# App closed by user — exit cleanly
|
|
200
|
+
return proc.returncode or 0
|
|
201
|
+
except KeyboardInterrupt:
|
|
202
|
+
if proc.poll() is None:
|
|
203
|
+
proc.terminate()
|
|
204
|
+
return 0
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
# ---------------------------------------------------------------------------
|
|
208
|
+
# 4. `quantumgui build <script.py>` (Standalone .exe via PyInstaller / Nuitka)
|
|
209
|
+
# ---------------------------------------------------------------------------
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def build_package_command(
|
|
213
|
+
script: str | Path,
|
|
214
|
+
*,
|
|
215
|
+
backend: str = "auto",
|
|
216
|
+
name: str | None = None,
|
|
217
|
+
icon: str | Path | None = None,
|
|
218
|
+
console: bool = False,
|
|
219
|
+
onefile: bool = True,
|
|
220
|
+
) -> list[str]:
|
|
221
|
+
"""Construct the PyInstaller or Nuitka standalone executable command for a QuantumGUI app."""
|
|
222
|
+
src = Path(script)
|
|
223
|
+
app_name = name or src.stem
|
|
224
|
+
|
|
225
|
+
chosen = backend
|
|
226
|
+
if chosen == "auto":
|
|
227
|
+
chosen = "nuitka" if shutil.which("nuitka") else "pyinstaller"
|
|
228
|
+
|
|
229
|
+
if chosen == "nuitka":
|
|
230
|
+
cmd = [
|
|
231
|
+
sys.executable,
|
|
232
|
+
"-m",
|
|
233
|
+
"nuitka",
|
|
234
|
+
"--standalone",
|
|
235
|
+
f"--output-filename={app_name}",
|
|
236
|
+
"--include-package=quantumgui",
|
|
237
|
+
"--include-package=skia",
|
|
238
|
+
"--include-package=glfw",
|
|
239
|
+
"--include-package=OpenGL",
|
|
240
|
+
]
|
|
241
|
+
if onefile:
|
|
242
|
+
cmd.append("--onefile")
|
|
243
|
+
if not console and sys.platform == "win32":
|
|
244
|
+
cmd.append("--windows-console-mode=disable")
|
|
245
|
+
if icon:
|
|
246
|
+
cmd.append(f"--windows-icon-from-ico={icon}")
|
|
247
|
+
cmd.append(str(src))
|
|
248
|
+
return cmd
|
|
249
|
+
|
|
250
|
+
# PyInstaller (default)
|
|
251
|
+
cmd = [
|
|
252
|
+
sys.executable,
|
|
253
|
+
"-m",
|
|
254
|
+
"PyInstaller",
|
|
255
|
+
"--noconfirm",
|
|
256
|
+
"--clean",
|
|
257
|
+
f"--name={app_name}",
|
|
258
|
+
"--collect-all=skia",
|
|
259
|
+
"--collect-all=glfw",
|
|
260
|
+
"--collect-all=OpenGL",
|
|
261
|
+
"--collect-submodules=quantumgui",
|
|
262
|
+
]
|
|
263
|
+
cmd.append("--onefile" if onefile else "--onedir")
|
|
264
|
+
if not console:
|
|
265
|
+
cmd.append("--windowed")
|
|
266
|
+
if icon:
|
|
267
|
+
cmd.append(f"--icon={icon}")
|
|
268
|
+
cmd.append(str(src))
|
|
269
|
+
return cmd
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def cmd_build(args: argparse.Namespace) -> int:
|
|
273
|
+
src = Path(args.script)
|
|
274
|
+
if not src.is_file() and not args.dry_run:
|
|
275
|
+
print(f"Error: archivo no encontrado: {src}", file=sys.stderr)
|
|
276
|
+
return 1
|
|
277
|
+
cmd = build_package_command(
|
|
278
|
+
src,
|
|
279
|
+
backend=args.backend,
|
|
280
|
+
name=args.name,
|
|
281
|
+
icon=args.icon,
|
|
282
|
+
console=args.console,
|
|
283
|
+
onefile=not args.onedir,
|
|
284
|
+
)
|
|
285
|
+
print("Comando de compilación:")
|
|
286
|
+
print(" ", " ".join(cmd))
|
|
287
|
+
if args.dry_run:
|
|
288
|
+
return 0
|
|
289
|
+
return subprocess.call(cmd)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
# ---------------------------------------------------------------------------
|
|
293
|
+
# Entry point
|
|
294
|
+
# ---------------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
298
|
+
p = argparse.ArgumentParser(
|
|
299
|
+
prog="quantumgui",
|
|
300
|
+
description="QuantumGUI CLI — Hot-Reload, compilación standalone (.exe) y plantillas M3.",
|
|
301
|
+
)
|
|
302
|
+
p.add_argument("--version", action="version", version=f"quantumgui {_get_version()}")
|
|
303
|
+
sub = p.add_subparsers(dest="command", required=True)
|
|
304
|
+
|
|
305
|
+
p_info = sub.add_parser("info", help="Muestra información del entorno Python/Skia/GLFW.")
|
|
306
|
+
p_info.set_defaults(func=cmd_info)
|
|
307
|
+
|
|
308
|
+
p_new = sub.add_parser("new", help="Crea un proyecto inicial QuantumGUI M3.")
|
|
309
|
+
p_new.add_argument("name", help="Nombre de la carpeta del proyecto.")
|
|
310
|
+
p_new.add_argument("--dir", default=".", help="Directorio padre (por defecto '.').")
|
|
311
|
+
p_new.add_argument("--template", choices=("basic", "erp"), default="basic", help="Plantilla inicial.")
|
|
312
|
+
p_new.set_defaults(func=cmd_new)
|
|
313
|
+
|
|
314
|
+
p_run = sub.add_parser("run", help="Ejecuta una app QuantumGUI (con --watch para Hot-Reload).")
|
|
315
|
+
p_run.add_argument("script", help="Ruta al archivo Python principal.")
|
|
316
|
+
p_run.add_argument("--watch", action="store_true", help="Recarga automáticamente al guardar cambios.")
|
|
317
|
+
p_run.add_argument("--interval", type=float, default=0.5, help="Intervalo de sondeo en segundos.")
|
|
318
|
+
p_run.add_argument("extra", nargs=argparse.REMAINDER, help="Argumentos extra para el script.")
|
|
319
|
+
p_run.set_defaults(func=cmd_run)
|
|
320
|
+
|
|
321
|
+
p_build = sub.add_parser("build", help="Compila una app a ejecutable standalone (.exe).")
|
|
322
|
+
p_build.add_argument("script", help="Ruta al archivo Python principal.")
|
|
323
|
+
p_build.add_argument("--backend", choices=("auto", "pyinstaller", "nuitka"), default="auto")
|
|
324
|
+
p_build.add_argument("--name", default=None, help="Nombre del ejecutable resultante.")
|
|
325
|
+
p_build.add_argument("--icon", default=None, help="Ruta a ícono .ico/.png.")
|
|
326
|
+
p_build.add_argument("--console", action="store_true", help="Mantener consola visible.")
|
|
327
|
+
p_build.add_argument("--onedir", action="store_true", help="Generar carpeta en lugar de un único archivo.")
|
|
328
|
+
p_build.add_argument("--dry-run", action="store_true", help="Muestra el comando sin ejecutarlo.")
|
|
329
|
+
p_build.set_defaults(func=cmd_build)
|
|
330
|
+
|
|
331
|
+
return p
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
335
|
+
parser = build_parser()
|
|
336
|
+
args = parser.parse_args(argv)
|
|
337
|
+
return int(args.func(args))
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
if __name__ == "__main__":
|
|
341
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from .animation import (
|
|
2
|
+
AnimatedValue,
|
|
3
|
+
RippleController,
|
|
4
|
+
SpringPresets,
|
|
5
|
+
SpringSpec,
|
|
6
|
+
SpringValue,
|
|
7
|
+
ease_emphasized_decelerate,
|
|
8
|
+
ease_standard,
|
|
9
|
+
)
|
|
10
|
+
from .context import context
|
|
11
|
+
from .events import CharEvent, KeyEvent, MouseEvent, ScrollEvent
|
|
12
|
+
from .geometry import Constraints, EdgeInsets, Rect, Size
|
|
13
|
+
from .state import State
|
|
14
|
+
from .widget import Widget
|
|
15
|
+
from .window import Window, run_app, window_manager
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"AnimatedValue",
|
|
19
|
+
"CharEvent",
|
|
20
|
+
"Constraints",
|
|
21
|
+
"EdgeInsets",
|
|
22
|
+
"KeyEvent",
|
|
23
|
+
"MouseEvent",
|
|
24
|
+
"Rect",
|
|
25
|
+
"RippleController",
|
|
26
|
+
"ScrollEvent",
|
|
27
|
+
"Size",
|
|
28
|
+
"SpringPresets",
|
|
29
|
+
"SpringSpec",
|
|
30
|
+
"SpringValue",
|
|
31
|
+
"State",
|
|
32
|
+
"Widget",
|
|
33
|
+
"Window",
|
|
34
|
+
"context",
|
|
35
|
+
"ease_emphasized_decelerate",
|
|
36
|
+
"ease_standard",
|
|
37
|
+
"run_app",
|
|
38
|
+
"window_manager",
|
|
39
|
+
]
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""Material Design 3 Motion primitives: easing curves, AnimatedValue, and Ripple."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
import time
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def ease_standard(t: float) -> float:
|
|
11
|
+
"""Smooth Material 3 Standard easing approximation (cubic-bezier 0.2, 0, 0, 1)."""
|
|
12
|
+
t = max(0.0, min(1.0, t))
|
|
13
|
+
return 1.0 - (1.0 - t) ** 3
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def ease_emphasized_decelerate(t: float) -> float:
|
|
17
|
+
"""Material 3 Emphasized Decelerate easing."""
|
|
18
|
+
t = max(0.0, min(1.0, t))
|
|
19
|
+
return 1.0 - (1.0 - t) ** 4
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AnimatedValue:
|
|
23
|
+
"""Smoothly interpolates a float towards a target value over `duration` seconds."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, initial: float, duration: float = 0.18) -> None:
|
|
26
|
+
self.current: float = float(initial)
|
|
27
|
+
self._start: float = float(initial)
|
|
28
|
+
self._target: float = float(initial)
|
|
29
|
+
self.duration: float = duration
|
|
30
|
+
self._start_time: float = 0.0
|
|
31
|
+
self._animating: bool = False
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def target(self) -> float:
|
|
35
|
+
return self._target
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def is_animating(self) -> bool:
|
|
39
|
+
return self._animating
|
|
40
|
+
|
|
41
|
+
def animate_to(self, target: float, now: float | None = None) -> bool:
|
|
42
|
+
target = float(target)
|
|
43
|
+
if abs(target - self._target) < 1e-4:
|
|
44
|
+
return False
|
|
45
|
+
self._start = self.current
|
|
46
|
+
self._target = target
|
|
47
|
+
self._start_time = time.perf_counter() if now is None else now
|
|
48
|
+
self._animating = True
|
|
49
|
+
return True
|
|
50
|
+
|
|
51
|
+
def jump_to(self, value: float) -> None:
|
|
52
|
+
self.current = self._start = self._target = float(value)
|
|
53
|
+
self._animating = False
|
|
54
|
+
|
|
55
|
+
def update(self, now: float) -> bool:
|
|
56
|
+
if not self._animating:
|
|
57
|
+
return False
|
|
58
|
+
elapsed = now - self._start_time
|
|
59
|
+
if elapsed >= self.duration or self.duration <= 0:
|
|
60
|
+
self.current = self._target
|
|
61
|
+
self._animating = False
|
|
62
|
+
return True
|
|
63
|
+
t = ease_standard(elapsed / self.duration)
|
|
64
|
+
self.current = self._start + (self._target - self._start) * t
|
|
65
|
+
return True
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass(frozen=True, slots=True)
|
|
69
|
+
class SpringSpec:
|
|
70
|
+
"""Material 3 Expressive damped harmonic spring parameters."""
|
|
71
|
+
|
|
72
|
+
stiffness: float = 800.0
|
|
73
|
+
damping_ratio: float = 0.65
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class SpringPresets:
|
|
77
|
+
"""Official Material 3 Expressive Motion Scheme spring presets."""
|
|
78
|
+
|
|
79
|
+
EXPRESSIVE_FAST_SPATIAL = SpringSpec(stiffness=800.0, damping_ratio=0.60)
|
|
80
|
+
EXPRESSIVE_DEFAULT_SPATIAL = SpringSpec(stiffness=380.0, damping_ratio=0.75)
|
|
81
|
+
EXPRESSIVE_SLOW_SPATIAL = SpringSpec(stiffness=200.0, damping_ratio=0.80)
|
|
82
|
+
EXPRESSIVE_FAST_EFFECTS = SpringSpec(stiffness=3800.0, damping_ratio=1.0)
|
|
83
|
+
STANDARD_SPATIAL = SpringSpec(stiffness=700.0, damping_ratio=0.90)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class SpringValue:
|
|
87
|
+
"""Real damped harmonic oscillator for interruptible Material 3 Expressive motion.
|
|
88
|
+
|
|
89
|
+
Integrates `a = -k * (x - target) - c * v` with sub-stepping for unconditional
|
|
90
|
+
stability and momentum preservation when retargeted mid-flight.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def __init__(
|
|
94
|
+
self,
|
|
95
|
+
initial: float,
|
|
96
|
+
spec: SpringSpec = SpringPresets.EXPRESSIVE_FAST_SPATIAL,
|
|
97
|
+
) -> None:
|
|
98
|
+
self.current: float = float(initial)
|
|
99
|
+
self._target: float = float(initial)
|
|
100
|
+
self.velocity: float = 0.0
|
|
101
|
+
self.spec: SpringSpec = spec
|
|
102
|
+
self._last_time: float | None = None
|
|
103
|
+
self._animating: bool = False
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def target(self) -> float:
|
|
107
|
+
return self._target
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def is_animating(self) -> bool:
|
|
111
|
+
return self._animating
|
|
112
|
+
|
|
113
|
+
def animate_to(self, target: float, now: float | None = None) -> bool:
|
|
114
|
+
target = float(target)
|
|
115
|
+
if abs(target - self._target) < 1e-4 and not self._animating:
|
|
116
|
+
return False
|
|
117
|
+
self._target = target
|
|
118
|
+
if not self._animating:
|
|
119
|
+
self._last_time = time.perf_counter() if now is None else now
|
|
120
|
+
self._animating = True
|
|
121
|
+
return True
|
|
122
|
+
|
|
123
|
+
def jump_to(self, value: float) -> None:
|
|
124
|
+
self.current = self._target = float(value)
|
|
125
|
+
self.velocity = 0.0
|
|
126
|
+
self._animating = False
|
|
127
|
+
self._last_time = None
|
|
128
|
+
|
|
129
|
+
def update(self, now: float) -> bool:
|
|
130
|
+
if not self._animating:
|
|
131
|
+
return False
|
|
132
|
+
if self._last_time is None:
|
|
133
|
+
self._last_time = now
|
|
134
|
+
return True
|
|
135
|
+
|
|
136
|
+
dt = min(0.05, max(0.0, now - self._last_time))
|
|
137
|
+
self._last_time = now
|
|
138
|
+
if dt <= 0.0:
|
|
139
|
+
return True
|
|
140
|
+
|
|
141
|
+
k = self.spec.stiffness
|
|
142
|
+
c = 2.0 * self.spec.damping_ratio * math.sqrt(k)
|
|
143
|
+
|
|
144
|
+
# Sub-step at <= 2ms for rock-solid numerical stability even on stiff springs
|
|
145
|
+
steps = max(1, int(math.ceil(dt / 0.002)))
|
|
146
|
+
h = dt / steps
|
|
147
|
+
x = self.current
|
|
148
|
+
v = self.velocity
|
|
149
|
+
tgt = self._target
|
|
150
|
+
for _ in range(steps):
|
|
151
|
+
acc = -k * (x - tgt) - c * v
|
|
152
|
+
v += acc * h
|
|
153
|
+
x += v * h
|
|
154
|
+
|
|
155
|
+
self.current = x
|
|
156
|
+
self.velocity = v
|
|
157
|
+
|
|
158
|
+
if abs(x - tgt) < 1e-3 and abs(v) < 1e-2:
|
|
159
|
+
self.current = tgt
|
|
160
|
+
self.velocity = 0.0
|
|
161
|
+
self._animating = False
|
|
162
|
+
self._last_time = None
|
|
163
|
+
return True
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass
|
|
168
|
+
class RippleWave:
|
|
169
|
+
x: float
|
|
170
|
+
y: float
|
|
171
|
+
max_radius: float
|
|
172
|
+
start_time: float
|
|
173
|
+
release_time: float | None = None
|
|
174
|
+
expand_duration: float = 0.28
|
|
175
|
+
fade_duration: float = 0.22
|
|
176
|
+
|
|
177
|
+
def radius(self, now: float) -> float:
|
|
178
|
+
t = ease_emphasized_decelerate((now - self.start_time) / self.expand_duration)
|
|
179
|
+
return self.max_radius * t
|
|
180
|
+
|
|
181
|
+
def alpha(self, now: float, base_alpha: float = 0.12) -> float:
|
|
182
|
+
if self.release_time is None:
|
|
183
|
+
return base_alpha
|
|
184
|
+
fade_t = max(0.0, min(1.0, (now - self.release_time) / self.fade_duration))
|
|
185
|
+
return base_alpha * (1.0 - fade_t)
|
|
186
|
+
|
|
187
|
+
def is_done(self, now: float) -> bool:
|
|
188
|
+
return (
|
|
189
|
+
self.release_time is not None
|
|
190
|
+
and (now - self.release_time) >= self.fade_duration
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class RippleController:
|
|
195
|
+
"""Manages active Material Design 3 ink-ripple waves on an interactive widget."""
|
|
196
|
+
|
|
197
|
+
def __init__(self) -> None:
|
|
198
|
+
self.waves: list[RippleWave] = []
|
|
199
|
+
|
|
200
|
+
def start(self, x: float, y: float, width: float, height: float) -> None:
|
|
201
|
+
# Distance to farthest corner ensures full coverage
|
|
202
|
+
dx = max(x, width - x)
|
|
203
|
+
dy = max(y, height - y)
|
|
204
|
+
max_r = math.hypot(dx, dy) + 4.0
|
|
205
|
+
self.waves.append(RippleWave(x, y, max_r, time.perf_counter()))
|
|
206
|
+
|
|
207
|
+
def release(self) -> None:
|
|
208
|
+
now = time.perf_counter()
|
|
209
|
+
for w in self.waves:
|
|
210
|
+
if w.release_time is None:
|
|
211
|
+
w.release_time = now
|
|
212
|
+
|
|
213
|
+
def update(self, now: float) -> bool:
|
|
214
|
+
if not self.waves:
|
|
215
|
+
return False
|
|
216
|
+
self.waves = [w for w in self.waves if not w.is_done(now)]
|
|
217
|
+
return True
|
|
218
|
+
|
|
219
|
+
@property
|
|
220
|
+
def is_active(self) -> bool:
|
|
221
|
+
return bool(self.waves)
|