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,920 @@
|
|
|
1
|
+
"""Native GPU-accelerated multi-window GLFW + OpenGL + Skia engine."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ctypes
|
|
6
|
+
import queue
|
|
7
|
+
import sys
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Callable, Self
|
|
11
|
+
|
|
12
|
+
import glfw
|
|
13
|
+
import skia
|
|
14
|
+
from OpenGL import GL
|
|
15
|
+
|
|
16
|
+
from ..theme import Color, Theme
|
|
17
|
+
from .context import context
|
|
18
|
+
from .draw import ColorSpec, draw_text_centered_y, font_for_style, make_rrect, measure_text, resolve_color
|
|
19
|
+
from .enterprise import ShortcutManager, export_pdf as _export_pdf
|
|
20
|
+
from .events import CharEvent, KeyEvent, MouseEvent, ScrollEvent
|
|
21
|
+
from .geometry import Constraints, Rect
|
|
22
|
+
from .widget import Widget
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _WindowManager:
|
|
26
|
+
"""Coordinates the single GLFW lifecycle across any number of native GPU windows."""
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None:
|
|
29
|
+
self._glfw_initialized: bool = False
|
|
30
|
+
self._running: bool = False
|
|
31
|
+
self.windows: list[Window] = []
|
|
32
|
+
self._main_window: Window | None = None
|
|
33
|
+
self._ui_queue: queue.SimpleQueue[Callable[[], None]] = queue.SimpleQueue()
|
|
34
|
+
|
|
35
|
+
def call_soon(self, fn: Callable[[], None]) -> None:
|
|
36
|
+
"""Thread-safely enqueue `fn` to run on the main UI thread."""
|
|
37
|
+
self._ui_queue.put(fn)
|
|
38
|
+
if self._glfw_initialized:
|
|
39
|
+
try:
|
|
40
|
+
glfw.post_empty_event()
|
|
41
|
+
except Exception:
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
def drain_pending(self) -> bool:
|
|
45
|
+
"""Execute all queued UI callbacks on the main thread; return True if any ran."""
|
|
46
|
+
ran = False
|
|
47
|
+
while True:
|
|
48
|
+
try:
|
|
49
|
+
fn = self._ui_queue.get_nowait()
|
|
50
|
+
except queue.Empty:
|
|
51
|
+
break
|
|
52
|
+
ran = True
|
|
53
|
+
fn()
|
|
54
|
+
return ran
|
|
55
|
+
|
|
56
|
+
def ensure_glfw(self) -> None:
|
|
57
|
+
if not self._glfw_initialized:
|
|
58
|
+
if not glfw.init():
|
|
59
|
+
raise RuntimeError("Failed to initialize GLFW.")
|
|
60
|
+
self._glfw_initialized = True
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def main_window(self) -> Window | None:
|
|
64
|
+
return self._main_window
|
|
65
|
+
|
|
66
|
+
def set_main_window(self, win: Window | None) -> None:
|
|
67
|
+
"""Designate `win` as the application's main window (closing it exits the app)."""
|
|
68
|
+
if self._main_window is not None and self._main_window is not win:
|
|
69
|
+
self._main_window.is_main = False
|
|
70
|
+
self._main_window = win
|
|
71
|
+
if win is not None:
|
|
72
|
+
win.is_main = True
|
|
73
|
+
|
|
74
|
+
def close_all(self) -> None:
|
|
75
|
+
"""Close all open windows and terminate the application loop cleanly."""
|
|
76
|
+
for w in list(self.windows):
|
|
77
|
+
w._closed_requested = True
|
|
78
|
+
if w._glfw_window is not None:
|
|
79
|
+
glfw.set_window_should_close(w._glfw_window, True)
|
|
80
|
+
else:
|
|
81
|
+
if w in self.windows:
|
|
82
|
+
self.windows.remove(w)
|
|
83
|
+
if self._glfw_initialized:
|
|
84
|
+
try:
|
|
85
|
+
glfw.post_empty_event()
|
|
86
|
+
except Exception:
|
|
87
|
+
pass
|
|
88
|
+
|
|
89
|
+
def register(self, win: Window) -> None:
|
|
90
|
+
if win not in self.windows:
|
|
91
|
+
self.windows.append(win)
|
|
92
|
+
if win.is_main:
|
|
93
|
+
self.set_main_window(win)
|
|
94
|
+
|
|
95
|
+
def unregister(self, win: Window) -> None:
|
|
96
|
+
was_main = win is self._main_window or win.is_main
|
|
97
|
+
if win in self.windows:
|
|
98
|
+
self.windows.remove(win)
|
|
99
|
+
if was_main:
|
|
100
|
+
self._main_window = None
|
|
101
|
+
self.close_all()
|
|
102
|
+
|
|
103
|
+
def open_window(self, win: Window) -> None:
|
|
104
|
+
self.register(win)
|
|
105
|
+
if self._running and win._glfw_window is None:
|
|
106
|
+
self.ensure_glfw()
|
|
107
|
+
win._open_native()
|
|
108
|
+
|
|
109
|
+
def run(self, initial_window: Window | None = None) -> None:
|
|
110
|
+
if initial_window is not None:
|
|
111
|
+
self.register(initial_window)
|
|
112
|
+
if not self.windows:
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
self.ensure_glfw()
|
|
116
|
+
self._running = True
|
|
117
|
+
try:
|
|
118
|
+
for w in list(self.windows):
|
|
119
|
+
if w._glfw_window is None:
|
|
120
|
+
w._open_native()
|
|
121
|
+
|
|
122
|
+
while self.windows:
|
|
123
|
+
now = time.perf_counter()
|
|
124
|
+
any_animating = False
|
|
125
|
+
any_redraw = self.drain_pending()
|
|
126
|
+
|
|
127
|
+
for w in list(self.windows):
|
|
128
|
+
if w._glfw_window is None:
|
|
129
|
+
w._open_native()
|
|
130
|
+
if w._closed_requested or glfw.window_should_close(w._glfw_window):
|
|
131
|
+
w._destroy_native()
|
|
132
|
+
self.unregister(w)
|
|
133
|
+
continue
|
|
134
|
+
|
|
135
|
+
anim, redrew = w._step_frame(now)
|
|
136
|
+
if anim:
|
|
137
|
+
any_animating = True
|
|
138
|
+
if redrew:
|
|
139
|
+
any_redraw = True
|
|
140
|
+
|
|
141
|
+
if not self.windows:
|
|
142
|
+
break
|
|
143
|
+
|
|
144
|
+
if any_animating or any_redraw:
|
|
145
|
+
glfw.poll_events()
|
|
146
|
+
else:
|
|
147
|
+
glfw.wait_events_timeout(0.05)
|
|
148
|
+
finally:
|
|
149
|
+
self._running = False
|
|
150
|
+
for w in list(self.windows):
|
|
151
|
+
w._destroy_native()
|
|
152
|
+
self.windows.clear()
|
|
153
|
+
if self._glfw_initialized:
|
|
154
|
+
glfw.terminate()
|
|
155
|
+
self._glfw_initialized = False
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
window_manager = _WindowManager()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class Window:
|
|
162
|
+
"""Native OS window rendered with Skia on the GPU (OpenGL backend)."""
|
|
163
|
+
|
|
164
|
+
def __init__(
|
|
165
|
+
self,
|
|
166
|
+
title: str = "QuantumGUI",
|
|
167
|
+
width: int = 960,
|
|
168
|
+
height: int = 680,
|
|
169
|
+
theme: Theme | None = None,
|
|
170
|
+
resizable: bool = True,
|
|
171
|
+
*,
|
|
172
|
+
frameless: bool = False,
|
|
173
|
+
transparent: bool = False,
|
|
174
|
+
always_on_top: bool = False,
|
|
175
|
+
maximized: bool = False,
|
|
176
|
+
fullscreen: bool = False,
|
|
177
|
+
center: bool = True,
|
|
178
|
+
position: tuple[int, int] | None = None,
|
|
179
|
+
min_size: tuple[int, int] | None = None,
|
|
180
|
+
max_size: tuple[int, int] | None = None,
|
|
181
|
+
opacity: float = 1.0,
|
|
182
|
+
icon: str | Path | skia.Image | None = None,
|
|
183
|
+
titlebar_color: ColorSpec | None = None,
|
|
184
|
+
main: bool = False,
|
|
185
|
+
on_close: Callable[[], bool | None] | None = None,
|
|
186
|
+
on_resize: Callable[[int, int], None] | None = None,
|
|
187
|
+
on_focus: Callable[[bool], None] | None = None,
|
|
188
|
+
on_file_drop: Callable[[list[str]], None] | None = None,
|
|
189
|
+
) -> None:
|
|
190
|
+
self.title = title
|
|
191
|
+
self.width = width
|
|
192
|
+
self.height = height
|
|
193
|
+
self.theme = theme or Theme()
|
|
194
|
+
self.resizable = resizable
|
|
195
|
+
self.frameless = frameless
|
|
196
|
+
self.transparent = transparent
|
|
197
|
+
self.always_on_top = always_on_top
|
|
198
|
+
self.maximized = maximized
|
|
199
|
+
self.fullscreen = fullscreen
|
|
200
|
+
self.center = center
|
|
201
|
+
self.position = position
|
|
202
|
+
self.min_size = min_size
|
|
203
|
+
self.max_size = max_size
|
|
204
|
+
self._window_opacity: float = max(0.05, min(1.0, float(opacity)))
|
|
205
|
+
self._icon_source = icon
|
|
206
|
+
self.titlebar_color = titlebar_color
|
|
207
|
+
self.is_main: bool = bool(main)
|
|
208
|
+
if self.is_main:
|
|
209
|
+
window_manager.set_main_window(self)
|
|
210
|
+
|
|
211
|
+
self.on_close = on_close
|
|
212
|
+
self.on_resize = on_resize
|
|
213
|
+
self.on_focus = on_focus
|
|
214
|
+
self.on_file_drop = on_file_drop
|
|
215
|
+
self._saved_windowed_bounds: tuple[int, int, int, int] = (100, 100, width, height)
|
|
216
|
+
|
|
217
|
+
self.root: Widget | None = None
|
|
218
|
+
self._overlays: list[Widget] = []
|
|
219
|
+
self._glfw_window = None
|
|
220
|
+
self._closed_requested: bool = False
|
|
221
|
+
self._cursors: dict[str, object] = {}
|
|
222
|
+
self._active_cursor: str = "default"
|
|
223
|
+
self._gr_context: skia.GrDirectContext | None = None
|
|
224
|
+
self._backend_render_target: skia.GrBackendRenderTarget | None = None
|
|
225
|
+
self._surface: skia.Surface | None = None
|
|
226
|
+
self._fb_size: tuple[int, int] = (0, 0)
|
|
227
|
+
self._scale: float = 1.0
|
|
228
|
+
|
|
229
|
+
self._needs_redraw: bool = True
|
|
230
|
+
self._hovered_widget: Widget | None = None
|
|
231
|
+
self._hover_since: float = 0.0
|
|
232
|
+
self._pressed_widget: Widget | None = None
|
|
233
|
+
self._focused_widget: Widget | None = None
|
|
234
|
+
self._mouse_pos: tuple[float, float] = (0.0, 0.0)
|
|
235
|
+
self._scroll_y: float = 0.0
|
|
236
|
+
self._content_height: float = 0.0
|
|
237
|
+
self._shortcuts = ShortcutManager()
|
|
238
|
+
self.on_error: Callable[[Exception], None] | None = None
|
|
239
|
+
|
|
240
|
+
self.theme.subscribe(self._on_theme_changed)
|
|
241
|
+
|
|
242
|
+
def _on_theme_changed(self) -> None:
|
|
243
|
+
self._sync_native_titlebar()
|
|
244
|
+
self.request_redraw()
|
|
245
|
+
|
|
246
|
+
# --- Runtime Window Customization Controls ---
|
|
247
|
+
|
|
248
|
+
def set_as_main(self) -> Self:
|
|
249
|
+
"""Designate this window as the main window (closing it closes all windows and exits)."""
|
|
250
|
+
window_manager.set_main_window(self)
|
|
251
|
+
return self
|
|
252
|
+
|
|
253
|
+
def set_title(self, title: str) -> Self:
|
|
254
|
+
self.title = str(title)
|
|
255
|
+
if self._glfw_window is not None:
|
|
256
|
+
glfw.set_window_title(self._glfw_window, self.title)
|
|
257
|
+
return self
|
|
258
|
+
|
|
259
|
+
def set_size(self, width: int, height: int) -> Self:
|
|
260
|
+
self.width = int(width)
|
|
261
|
+
self.height = int(height)
|
|
262
|
+
if self._glfw_window is not None:
|
|
263
|
+
glfw.set_window_size(self._glfw_window, self.width, self.height)
|
|
264
|
+
self.request_redraw()
|
|
265
|
+
return self
|
|
266
|
+
|
|
267
|
+
def set_position(self, x: int, y: int) -> Self:
|
|
268
|
+
self.position = (int(x), int(y))
|
|
269
|
+
if self._glfw_window is not None:
|
|
270
|
+
glfw.set_window_pos(self._glfw_window, int(x), int(y))
|
|
271
|
+
return self
|
|
272
|
+
|
|
273
|
+
def move_by(self, dx: int, dy: int) -> Self:
|
|
274
|
+
"""Move window by `(dx, dy)` screen pixels (useful for custom frameless titlebars)."""
|
|
275
|
+
if self._glfw_window is not None:
|
|
276
|
+
x, y = glfw.get_window_pos(self._glfw_window)
|
|
277
|
+
self.set_position(x + int(dx), y + int(dy))
|
|
278
|
+
elif self.position is not None:
|
|
279
|
+
self.position = (self.position[0] + int(dx), self.position[1] + int(dy))
|
|
280
|
+
return self
|
|
281
|
+
|
|
282
|
+
def center_on_screen(self) -> Self:
|
|
283
|
+
if self._glfw_window is not None:
|
|
284
|
+
mon = glfw.get_primary_monitor()
|
|
285
|
+
if mon is not None:
|
|
286
|
+
wx, wy, ww, wh = glfw.get_monitor_workarea(mon)
|
|
287
|
+
cx = wx + max(0, (ww - self.width) // 2)
|
|
288
|
+
cy = wy + max(0, (wh - self.height) // 2)
|
|
289
|
+
self.set_position(cx, cy)
|
|
290
|
+
return self
|
|
291
|
+
|
|
292
|
+
def set_opacity(self, alpha: float) -> Self:
|
|
293
|
+
self._window_opacity = max(0.05, min(1.0, float(alpha)))
|
|
294
|
+
if self._glfw_window is not None:
|
|
295
|
+
glfw.set_window_opacity(self._glfw_window, self._window_opacity)
|
|
296
|
+
return self
|
|
297
|
+
|
|
298
|
+
def set_always_on_top(self, enabled: bool) -> Self:
|
|
299
|
+
self.always_on_top = bool(enabled)
|
|
300
|
+
if self._glfw_window is not None:
|
|
301
|
+
glfw.set_window_attrib(self._glfw_window, glfw.FLOATING, glfw.TRUE if self.always_on_top else glfw.FALSE)
|
|
302
|
+
return self
|
|
303
|
+
|
|
304
|
+
def set_titlebar_color(self, color: ColorSpec | None) -> Self:
|
|
305
|
+
self.titlebar_color = color
|
|
306
|
+
self._sync_native_titlebar()
|
|
307
|
+
return self
|
|
308
|
+
|
|
309
|
+
def set_icon(self, source: str | Path | skia.Image) -> Self:
|
|
310
|
+
self._icon_source = source
|
|
311
|
+
if self._glfw_window is not None:
|
|
312
|
+
self._apply_native_icon()
|
|
313
|
+
return self
|
|
314
|
+
|
|
315
|
+
def minimize(self) -> Self:
|
|
316
|
+
if self._glfw_window is not None:
|
|
317
|
+
glfw.iconify_window(self._glfw_window)
|
|
318
|
+
return self
|
|
319
|
+
|
|
320
|
+
def maximize(self) -> Self:
|
|
321
|
+
self.maximized = True
|
|
322
|
+
if self._glfw_window is not None:
|
|
323
|
+
glfw.maximize_window(self._glfw_window)
|
|
324
|
+
return self
|
|
325
|
+
|
|
326
|
+
def restore(self) -> Self:
|
|
327
|
+
self.maximized = False
|
|
328
|
+
if self._glfw_window is not None:
|
|
329
|
+
glfw.restore_window(self._glfw_window)
|
|
330
|
+
return self
|
|
331
|
+
|
|
332
|
+
def set_fullscreen(self, enabled: bool) -> Self:
|
|
333
|
+
self.fullscreen = bool(enabled)
|
|
334
|
+
if self._glfw_window is None:
|
|
335
|
+
return self
|
|
336
|
+
mon = glfw.get_primary_monitor()
|
|
337
|
+
if self.fullscreen and mon is not None:
|
|
338
|
+
x, y = glfw.get_window_pos(self._glfw_window)
|
|
339
|
+
w, h = glfw.get_window_size(self._glfw_window)
|
|
340
|
+
self._saved_windowed_bounds = (x, y, w, h)
|
|
341
|
+
mode = glfw.get_video_mode(mon)
|
|
342
|
+
glfw.set_window_monitor(
|
|
343
|
+
self._glfw_window, mon, 0, 0, mode.size.width, mode.size.height, mode.refresh_rate
|
|
344
|
+
)
|
|
345
|
+
else:
|
|
346
|
+
x, y, w, h = self._saved_windowed_bounds
|
|
347
|
+
glfw.set_window_monitor(self._glfw_window, None, x, y, w, h, 0)
|
|
348
|
+
return self
|
|
349
|
+
|
|
350
|
+
def _sync_native_titlebar(self) -> None:
|
|
351
|
+
"""Sync Windows 10/11 native titlebar Dark Mode & optional custom caption color via DWM."""
|
|
352
|
+
if sys.platform != "win32" or self._glfw_window is None:
|
|
353
|
+
return
|
|
354
|
+
try:
|
|
355
|
+
hwnd = glfw.get_win32_window(self._glfw_window)
|
|
356
|
+
if not hwnd:
|
|
357
|
+
return
|
|
358
|
+
dwmapi = ctypes.windll.dwmapi
|
|
359
|
+
dark = ctypes.c_int(1 if self.theme.mode == "dark" else 0)
|
|
360
|
+
# DWMWA_USE_IMMERSIVE_DARK_MODE = 20 (Win11 / Win10 20H1+), fallback 19
|
|
361
|
+
res = dwmapi.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 20, ctypes.byref(dark), ctypes.sizeof(dark))
|
|
362
|
+
if res != 0:
|
|
363
|
+
dwmapi.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 19, ctypes.byref(dark), ctypes.sizeof(dark))
|
|
364
|
+
|
|
365
|
+
if self.titlebar_color is not None:
|
|
366
|
+
col = resolve_color(self.titlebar_color, self.theme)
|
|
367
|
+
if col is not None:
|
|
368
|
+
# Win32 COLORREF is 0x00BBGGRR
|
|
369
|
+
colorref = ctypes.c_uint32((col.b << 16) | (col.g << 8) | col.r)
|
|
370
|
+
dwmapi.DwmSetWindowAttribute(
|
|
371
|
+
ctypes.c_void_p(hwnd), 35, ctypes.byref(colorref), ctypes.sizeof(colorref)
|
|
372
|
+
)
|
|
373
|
+
except Exception:
|
|
374
|
+
pass
|
|
375
|
+
|
|
376
|
+
@staticmethod
|
|
377
|
+
def _decode_icon_rgba(source: str | Path | skia.Image) -> tuple[int, int, bytes] | None:
|
|
378
|
+
"""Decode PNG/JPG/ICO/skia.Image into `(width, height, rgba_bytes)` with zero Pillow dependency."""
|
|
379
|
+
img: skia.Image | None
|
|
380
|
+
if isinstance(source, skia.Image):
|
|
381
|
+
img = source
|
|
382
|
+
else:
|
|
383
|
+
p = Path(source)
|
|
384
|
+
if not p.is_file():
|
|
385
|
+
return None
|
|
386
|
+
data = skia.Data.MakeFromFileName(str(p))
|
|
387
|
+
img = skia.Image.MakeFromEncoded(data) if data is not None else None
|
|
388
|
+
if img is None:
|
|
389
|
+
return None
|
|
390
|
+
w, h = img.width(), img.height()
|
|
391
|
+
info = skia.ImageInfo.Make(w, h, skia.kRGBA_8888_ColorType, skia.kUnpremul_AlphaType)
|
|
392
|
+
buf = bytearray(w * h * 4)
|
|
393
|
+
if not img.readPixels(info, buf, w * 4, 0, 0):
|
|
394
|
+
return None
|
|
395
|
+
return (w, h, bytes(buf))
|
|
396
|
+
|
|
397
|
+
def _apply_native_icon(self) -> None:
|
|
398
|
+
if self._glfw_window is None or self._icon_source is None:
|
|
399
|
+
return
|
|
400
|
+
decoded = self._decode_icon_rgba(self._icon_source)
|
|
401
|
+
if decoded is None:
|
|
402
|
+
return
|
|
403
|
+
w, h, rgba = decoded
|
|
404
|
+
try:
|
|
405
|
+
# pyglfw accepts a list of [width, height, pixels] tuples/lists or PIL images
|
|
406
|
+
glfw.set_window_icon(self._glfw_window, 1, [(w, h, rgba)])
|
|
407
|
+
except Exception:
|
|
408
|
+
pass
|
|
409
|
+
|
|
410
|
+
def _dispatch_file_drop(self, paths: list[str]) -> None:
|
|
411
|
+
"""Deliver OS dropped file paths to `on_file_drop` and any DropZone under the cursor."""
|
|
412
|
+
if self.on_file_drop is not None:
|
|
413
|
+
self.on_file_drop(list(paths))
|
|
414
|
+
x, y = self._mouse_pos
|
|
415
|
+
node = self._hit_test_all(x, y)
|
|
416
|
+
while node is not None:
|
|
417
|
+
if hasattr(node, "add_files") and callable(getattr(node, "add_files")):
|
|
418
|
+
getattr(node, "add_files")(paths)
|
|
419
|
+
self.request_redraw()
|
|
420
|
+
break
|
|
421
|
+
node = node.parent
|
|
422
|
+
|
|
423
|
+
# --- Enterprise: Shortcuts, Error Guard & Vector PDF Export ---
|
|
424
|
+
|
|
425
|
+
def shortcut(self, combo: str, callback: Callable[[], None]) -> Self:
|
|
426
|
+
"""Register a global window keyboard shortcut (e.g. `'Ctrl+S'`, `'F2'`, `'Ctrl+Shift+P'`)."""
|
|
427
|
+
self._shortcuts.register(combo, callback)
|
|
428
|
+
return self
|
|
429
|
+
|
|
430
|
+
def remove_shortcut(self, combo: str) -> Self:
|
|
431
|
+
self._shortcuts.unregister(combo)
|
|
432
|
+
return self
|
|
433
|
+
|
|
434
|
+
def trigger_key(self, key: int, mods: int = 0) -> bool:
|
|
435
|
+
"""Dispatch a key press against registered window shortcuts; returns True if handled."""
|
|
436
|
+
try:
|
|
437
|
+
return self._shortcuts.dispatch(key, mods)
|
|
438
|
+
except Exception as exc: # noqa: BLE001
|
|
439
|
+
self._handle_error(exc)
|
|
440
|
+
return True
|
|
441
|
+
|
|
442
|
+
def _handle_error(self, exc: Exception) -> None:
|
|
443
|
+
if self.on_error is not None:
|
|
444
|
+
self.on_error(exc)
|
|
445
|
+
else:
|
|
446
|
+
from ..widgets.overlays import Snackbar
|
|
447
|
+
|
|
448
|
+
Snackbar(f"Error: {exc}", action="OK", duration=5.0, window=self).show()
|
|
449
|
+
|
|
450
|
+
def export_pdf(
|
|
451
|
+
self,
|
|
452
|
+
path: str | Path | None = None,
|
|
453
|
+
*,
|
|
454
|
+
page_size: str | tuple[float, float] | None = None,
|
|
455
|
+
margin: float = 24.0,
|
|
456
|
+
) -> Path | bytes:
|
|
457
|
+
"""Export this window's current UI as a crisp vector PDF using Skia's native PDF backend."""
|
|
458
|
+
return _export_pdf(self, path, page_size=page_size, margin=margin)
|
|
459
|
+
|
|
460
|
+
# --- NiceGUI-style `with ui.window(...)` ---
|
|
461
|
+
|
|
462
|
+
def __enter__(self) -> Self:
|
|
463
|
+
context.push_window(self)
|
|
464
|
+
return self
|
|
465
|
+
|
|
466
|
+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
|
|
467
|
+
context.pop_window()
|
|
468
|
+
|
|
469
|
+
def set_root(self, widget: Widget) -> None:
|
|
470
|
+
self.root = widget
|
|
471
|
+
widget._set_window_recursive(self)
|
|
472
|
+
self.request_redraw()
|
|
473
|
+
|
|
474
|
+
def open_overlay(self, overlay: Widget) -> None:
|
|
475
|
+
if overlay not in self._overlays:
|
|
476
|
+
overlay._set_window_recursive(self)
|
|
477
|
+
self._overlays.append(overlay)
|
|
478
|
+
self.request_redraw()
|
|
479
|
+
|
|
480
|
+
def close_overlay(self, overlay: Widget | None = None) -> None:
|
|
481
|
+
if overlay is None:
|
|
482
|
+
if self._overlays:
|
|
483
|
+
self._overlays.clear()
|
|
484
|
+
self.request_redraw()
|
|
485
|
+
elif overlay in self._overlays:
|
|
486
|
+
self._overlays.remove(overlay)
|
|
487
|
+
self.request_redraw()
|
|
488
|
+
|
|
489
|
+
def request_redraw(self) -> None:
|
|
490
|
+
self._needs_redraw = True
|
|
491
|
+
if self._glfw_window is not None:
|
|
492
|
+
glfw.post_empty_event()
|
|
493
|
+
|
|
494
|
+
def set_focus(self, widget: Widget | None) -> None:
|
|
495
|
+
if self._focused_widget is widget:
|
|
496
|
+
return
|
|
497
|
+
if self._focused_widget is not None:
|
|
498
|
+
self._focused_widget.on_blur()
|
|
499
|
+
self._focused_widget = widget
|
|
500
|
+
if self._focused_widget is not None:
|
|
501
|
+
self._focused_widget.on_focus()
|
|
502
|
+
self.request_redraw()
|
|
503
|
+
|
|
504
|
+
def get_clipboard(self) -> str:
|
|
505
|
+
if self._glfw_window is None:
|
|
506
|
+
return ""
|
|
507
|
+
text = glfw.get_clipboard_string(self._glfw_window)
|
|
508
|
+
if isinstance(text, bytes):
|
|
509
|
+
return text.decode("utf-8", errors="ignore")
|
|
510
|
+
return text or ""
|
|
511
|
+
|
|
512
|
+
def set_clipboard(self, text: str) -> None:
|
|
513
|
+
if self._glfw_window is not None:
|
|
514
|
+
glfw.set_clipboard_string(self._glfw_window, text)
|
|
515
|
+
|
|
516
|
+
def _update_cursor(self, widget: Widget | None) -> None:
|
|
517
|
+
if self._glfw_window is None:
|
|
518
|
+
return
|
|
519
|
+
desired = "default"
|
|
520
|
+
if widget is not None and not getattr(widget, "disabled", False):
|
|
521
|
+
desired = widget.cursor
|
|
522
|
+
if desired != self._active_cursor:
|
|
523
|
+
self._active_cursor = desired
|
|
524
|
+
cur = self._cursors.get(desired) or self._cursors.get("default")
|
|
525
|
+
if cur is not None:
|
|
526
|
+
glfw.set_cursor(self._glfw_window, cur)
|
|
527
|
+
|
|
528
|
+
def _collect_focusable(self, node: Widget | None, out: list[Widget]) -> None:
|
|
529
|
+
if node is None or not node._visible:
|
|
530
|
+
return
|
|
531
|
+
if node.focusable and not getattr(node, "disabled", False):
|
|
532
|
+
out.append(node)
|
|
533
|
+
for child in node.children:
|
|
534
|
+
self._collect_focusable(child, out)
|
|
535
|
+
|
|
536
|
+
def cycle_focus(self, reverse: bool = False) -> None:
|
|
537
|
+
scope: Widget | None = self.root
|
|
538
|
+
for ov in reversed(self._overlays):
|
|
539
|
+
if ov.dismiss_on_outside_click:
|
|
540
|
+
scope = ov
|
|
541
|
+
break
|
|
542
|
+
focusables: list[Widget] = []
|
|
543
|
+
self._collect_focusable(scope, focusables)
|
|
544
|
+
if not focusables:
|
|
545
|
+
return
|
|
546
|
+
if self._focused_widget in focusables:
|
|
547
|
+
idx = focusables.index(self._focused_widget)
|
|
548
|
+
nxt = (idx - 1) % len(focusables) if reverse else (idx + 1) % len(focusables)
|
|
549
|
+
else:
|
|
550
|
+
nxt = len(focusables) - 1 if reverse else 0
|
|
551
|
+
self.set_focus(focusables[nxt])
|
|
552
|
+
|
|
553
|
+
# --- Multi-window open / close / run ---
|
|
554
|
+
|
|
555
|
+
def open(self) -> Self:
|
|
556
|
+
"""Open this window (can be called dynamically while another window is running)."""
|
|
557
|
+
self._closed_requested = False
|
|
558
|
+
window_manager.open_window(self)
|
|
559
|
+
return self
|
|
560
|
+
|
|
561
|
+
def close(self) -> None:
|
|
562
|
+
"""Request closing this native window cleanly."""
|
|
563
|
+
self._closed_requested = True
|
|
564
|
+
if self._glfw_window is not None:
|
|
565
|
+
glfw.set_window_should_close(self._glfw_window, True)
|
|
566
|
+
glfw.post_empty_event()
|
|
567
|
+
else:
|
|
568
|
+
window_manager.unregister(self)
|
|
569
|
+
|
|
570
|
+
def run(self) -> None:
|
|
571
|
+
"""Open this window and start the native GPU event/render loop."""
|
|
572
|
+
self._closed_requested = False
|
|
573
|
+
window_manager.run(self)
|
|
574
|
+
|
|
575
|
+
# --- Layout & Painting ---
|
|
576
|
+
|
|
577
|
+
def _layout_root(self, logical_w: float, logical_h: float) -> None:
|
|
578
|
+
if self.root is None:
|
|
579
|
+
self._content_height = 0.0
|
|
580
|
+
return
|
|
581
|
+
constraints = Constraints(
|
|
582
|
+
min_width=logical_w,
|
|
583
|
+
max_width=logical_w,
|
|
584
|
+
min_height=0.0,
|
|
585
|
+
max_height=float("inf"),
|
|
586
|
+
)
|
|
587
|
+
measured = self.root.measure(constraints, self.theme)
|
|
588
|
+
self._content_height = max(logical_h, measured.height)
|
|
589
|
+
max_scroll = max(0.0, self._content_height - logical_h)
|
|
590
|
+
self._scroll_y = max(0.0, min(self._scroll_y, max_scroll))
|
|
591
|
+
self.root.layout(Rect(0.0, -self._scroll_y, logical_w, self._content_height), self.theme)
|
|
592
|
+
|
|
593
|
+
def _paint_frame(self, canvas: skia.Canvas, logical_w: float, logical_h: float, scale: float, now: float) -> None:
|
|
594
|
+
self._layout_root(logical_w, logical_h)
|
|
595
|
+
for ov in self._overlays:
|
|
596
|
+
ov.layout(Rect(0.0, 0.0, logical_w, logical_h), self.theme)
|
|
597
|
+
canvas.save()
|
|
598
|
+
canvas.scale(scale, scale)
|
|
599
|
+
if self.transparent:
|
|
600
|
+
canvas.clear(Color(0, 0, 0, 0).to_skia())
|
|
601
|
+
else:
|
|
602
|
+
canvas.clear(self.theme.colors.surface.to_skia())
|
|
603
|
+
if self.root is not None:
|
|
604
|
+
self.root.paint(canvas, self.theme, now)
|
|
605
|
+
for ov in self._overlays:
|
|
606
|
+
ov.paint(canvas, self.theme, now)
|
|
607
|
+
self._paint_tooltip(canvas, logical_w, logical_h, now)
|
|
608
|
+
canvas.restore()
|
|
609
|
+
|
|
610
|
+
def _paint_tooltip(self, canvas: skia.Canvas, logical_w: float, logical_h: float, now: float) -> None:
|
|
611
|
+
w = self._hovered_widget
|
|
612
|
+
if w is None or not w._tooltip_text or self._pressed_widget is not None:
|
|
613
|
+
return
|
|
614
|
+
if (now - self._hover_since) < 0.45:
|
|
615
|
+
return
|
|
616
|
+
c = self.theme.colors
|
|
617
|
+
font = font_for_style(self.theme.font_family, self.theme.typography.body_small)
|
|
618
|
+
tw = measure_text(font, w._tooltip_text)
|
|
619
|
+
box_w = tw + 16.0
|
|
620
|
+
box_h = 26.0
|
|
621
|
+
tx = max(8.0, min(logical_w - box_w - 8.0, w.rect.center_x - box_w * 0.5))
|
|
622
|
+
ty = w.rect.bottom + 8.0
|
|
623
|
+
if ty + box_h > logical_h - 8.0:
|
|
624
|
+
ty = max(8.0, w.rect.y - box_h - 8.0)
|
|
625
|
+
trect = Rect(tx, ty, box_w, box_h)
|
|
626
|
+
canvas.drawRRect(
|
|
627
|
+
make_rrect(trect, self.theme.shape.extra_small),
|
|
628
|
+
skia.Paint(AntiAlias=True, Color=c.inverse_surface.to_skia()),
|
|
629
|
+
)
|
|
630
|
+
draw_text_centered_y(canvas, w._tooltip_text, trect.x + 8.0, trect.center_y, font, c.inverse_on_surface)
|
|
631
|
+
|
|
632
|
+
def _hit_test_all(self, x: float, y: float) -> Widget | None:
|
|
633
|
+
for ov in reversed(self._overlays):
|
|
634
|
+
hit = ov.hit_test(x, y)
|
|
635
|
+
if hit is not None:
|
|
636
|
+
return hit
|
|
637
|
+
if self.root is not None:
|
|
638
|
+
return self.root.hit_test(x, y)
|
|
639
|
+
return None
|
|
640
|
+
|
|
641
|
+
def render_to_image(self, scale: float = 1.0) -> skia.Image:
|
|
642
|
+
"""Render the current widget tree offscreen on CPU (useful for tests & snapshots)."""
|
|
643
|
+
w = int(self.width * scale)
|
|
644
|
+
h = int(self.height * scale)
|
|
645
|
+
surface = skia.Surface(w, h)
|
|
646
|
+
with surface as canvas:
|
|
647
|
+
self._paint_frame(canvas, float(self.width), float(self.height), scale, time.perf_counter())
|
|
648
|
+
return surface.makeImageSnapshot()
|
|
649
|
+
|
|
650
|
+
# --- Native GLFW + OpenGL + Skia per-window lifecycle ---
|
|
651
|
+
|
|
652
|
+
def _create_surface(self, fb_w: int, fb_h: int) -> None:
|
|
653
|
+
if self._gr_context is None:
|
|
654
|
+
self._gr_context = skia.GrDirectContext.MakeGL()
|
|
655
|
+
self._backend_render_target = skia.GrBackendRenderTarget(
|
|
656
|
+
fb_w,
|
|
657
|
+
fb_h,
|
|
658
|
+
0,
|
|
659
|
+
0,
|
|
660
|
+
skia.GrGLFramebufferInfo(0, GL.GL_RGBA8),
|
|
661
|
+
)
|
|
662
|
+
self._surface = skia.Surface.MakeFromBackendRenderTarget(
|
|
663
|
+
self._gr_context,
|
|
664
|
+
self._backend_render_target,
|
|
665
|
+
skia.kBottomLeft_GrSurfaceOrigin,
|
|
666
|
+
skia.kRGBA_8888_ColorType,
|
|
667
|
+
skia.ColorSpace.MakeSRGB(),
|
|
668
|
+
)
|
|
669
|
+
if self._surface is None:
|
|
670
|
+
raise RuntimeError("Failed to create Skia GPU surface from OpenGL framebuffer.")
|
|
671
|
+
self._fb_size = (fb_w, fb_h)
|
|
672
|
+
|
|
673
|
+
def _install_callbacks(self, win) -> None:
|
|
674
|
+
def on_resize(_w, width: int, height: int) -> None:
|
|
675
|
+
if width > 0 and height > 0:
|
|
676
|
+
self.width = width
|
|
677
|
+
self.height = height
|
|
678
|
+
self._needs_redraw = True
|
|
679
|
+
if self.on_resize is not None:
|
|
680
|
+
self.on_resize(width, height)
|
|
681
|
+
|
|
682
|
+
def on_cursor_pos(_w, x: float, y: float) -> None:
|
|
683
|
+
self._mouse_pos = (x, y)
|
|
684
|
+
if self._pressed_widget is not None:
|
|
685
|
+
self._pressed_widget.on_mouse_move(MouseEvent(x=x, y=y))
|
|
686
|
+
self._needs_redraw = True
|
|
687
|
+
hit = self._hit_test_all(x, y)
|
|
688
|
+
if hit is not self._hovered_widget:
|
|
689
|
+
if self._hovered_widget is not None:
|
|
690
|
+
self._hovered_widget.on_mouse_leave()
|
|
691
|
+
self._hovered_widget = hit
|
|
692
|
+
self._hover_since = time.perf_counter()
|
|
693
|
+
if self._hovered_widget is not None:
|
|
694
|
+
self._hovered_widget.on_mouse_enter()
|
|
695
|
+
self._update_cursor(hit)
|
|
696
|
+
self._needs_redraw = True
|
|
697
|
+
elif hit is not None and hit is not self._pressed_widget:
|
|
698
|
+
hit.on_mouse_move(MouseEvent(x=x, y=y))
|
|
699
|
+
|
|
700
|
+
def on_mouse_button(_w, button: int, action: int, mods: int) -> None:
|
|
701
|
+
x, y = self._mouse_pos
|
|
702
|
+
ev = MouseEvent(x=x, y=y, button=button, mods=mods)
|
|
703
|
+
try:
|
|
704
|
+
if action == glfw.PRESS:
|
|
705
|
+
hit = self._hit_test_all(x, y)
|
|
706
|
+
if self._overlays:
|
|
707
|
+
for ov in list(self._overlays):
|
|
708
|
+
if ov.dismiss_on_outside_click and ov.hit_test(x, y) is None:
|
|
709
|
+
self.close_overlay(ov)
|
|
710
|
+
self._pressed_widget = hit
|
|
711
|
+
if hit is not None and hit.focusable:
|
|
712
|
+
self.set_focus(hit)
|
|
713
|
+
else:
|
|
714
|
+
self.set_focus(None)
|
|
715
|
+
if hit is not None:
|
|
716
|
+
hit.on_mouse_down(ev)
|
|
717
|
+
self._needs_redraw = True
|
|
718
|
+
elif action == glfw.RELEASE:
|
|
719
|
+
if self._pressed_widget is not None:
|
|
720
|
+
self._pressed_widget.on_mouse_up(ev)
|
|
721
|
+
self._pressed_widget = None
|
|
722
|
+
self._needs_redraw = True
|
|
723
|
+
except Exception as exc: # noqa: BLE001
|
|
724
|
+
self._pressed_widget = None
|
|
725
|
+
self._handle_error(exc)
|
|
726
|
+
|
|
727
|
+
def on_scroll(_w, dx: float, dy: float) -> None:
|
|
728
|
+
x, y = self._mouse_pos
|
|
729
|
+
ev = ScrollEvent(x=x, y=y, dx=dx, dy=dy)
|
|
730
|
+
node = self._hit_test_all(x, y)
|
|
731
|
+
while node is not None:
|
|
732
|
+
if node.on_scroll(ev):
|
|
733
|
+
self._needs_redraw = True
|
|
734
|
+
return
|
|
735
|
+
node = node.parent
|
|
736
|
+
|
|
737
|
+
max_scroll = max(0.0, self._content_height - self.height)
|
|
738
|
+
if max_scroll > 0.0:
|
|
739
|
+
self._scroll_y = max(0.0, min(self._scroll_y - dy * 48.0, max_scroll))
|
|
740
|
+
self._needs_redraw = True
|
|
741
|
+
|
|
742
|
+
def on_key(_w, key: int, scancode: int, action: int, mods: int) -> None:
|
|
743
|
+
if key == glfw.KEY_ESCAPE and action == glfw.PRESS and self._overlays:
|
|
744
|
+
for ov in reversed(list(self._overlays)):
|
|
745
|
+
if ov.dismiss_on_outside_click:
|
|
746
|
+
self.close_overlay(ov)
|
|
747
|
+
self._needs_redraw = True
|
|
748
|
+
return
|
|
749
|
+
if action == glfw.PRESS and self.trigger_key(key, mods):
|
|
750
|
+
self._needs_redraw = True
|
|
751
|
+
return
|
|
752
|
+
if key == glfw.KEY_TAB and action in (glfw.PRESS, glfw.REPEAT):
|
|
753
|
+
self.cycle_focus(reverse=bool(mods & glfw.MOD_SHIFT))
|
|
754
|
+
self._needs_redraw = True
|
|
755
|
+
return
|
|
756
|
+
if self._focused_widget is not None:
|
|
757
|
+
try:
|
|
758
|
+
self._focused_widget.on_key(KeyEvent(key, scancode, action, mods))
|
|
759
|
+
except Exception as exc: # noqa: BLE001
|
|
760
|
+
self._handle_error(exc)
|
|
761
|
+
self._needs_redraw = True
|
|
762
|
+
|
|
763
|
+
def on_char(_w, codepoint: int) -> None:
|
|
764
|
+
if self._focused_widget is not None:
|
|
765
|
+
self._focused_widget.on_char(CharEvent(chr(codepoint)))
|
|
766
|
+
self._needs_redraw = True
|
|
767
|
+
|
|
768
|
+
def on_close_req(w) -> None:
|
|
769
|
+
if self.on_close is not None:
|
|
770
|
+
try:
|
|
771
|
+
allow = self.on_close()
|
|
772
|
+
if allow is False:
|
|
773
|
+
glfw.set_window_should_close(w, False)
|
|
774
|
+
except Exception as exc: # noqa: BLE001
|
|
775
|
+
self._handle_error(exc)
|
|
776
|
+
|
|
777
|
+
def on_focus_cb(_w, focused: int) -> None:
|
|
778
|
+
if self.on_focus is not None:
|
|
779
|
+
self.on_focus(bool(focused))
|
|
780
|
+
|
|
781
|
+
def on_drop_cb(_w, paths: list[str]) -> None:
|
|
782
|
+
self._dispatch_file_drop(list(paths))
|
|
783
|
+
|
|
784
|
+
glfw.set_window_size_callback(win, on_resize)
|
|
785
|
+
glfw.set_window_close_callback(win, on_close_req)
|
|
786
|
+
glfw.set_window_focus_callback(win, on_focus_cb)
|
|
787
|
+
glfw.set_drop_callback(win, on_drop_cb)
|
|
788
|
+
glfw.set_cursor_pos_callback(win, on_cursor_pos)
|
|
789
|
+
glfw.set_mouse_button_callback(win, on_mouse_button)
|
|
790
|
+
glfw.set_scroll_callback(win, on_scroll)
|
|
791
|
+
glfw.set_key_callback(win, on_key)
|
|
792
|
+
glfw.set_char_callback(win, on_char)
|
|
793
|
+
|
|
794
|
+
def _open_native(self) -> None:
|
|
795
|
+
glfw.default_window_hints()
|
|
796
|
+
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
|
|
797
|
+
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
|
|
798
|
+
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
|
|
799
|
+
glfw.window_hint(glfw.RESIZABLE, glfw.TRUE if self.resizable else glfw.FALSE)
|
|
800
|
+
glfw.window_hint(glfw.DECORATED, glfw.FALSE if self.frameless else glfw.TRUE)
|
|
801
|
+
glfw.window_hint(glfw.FLOATING, glfw.TRUE if self.always_on_top else glfw.FALSE)
|
|
802
|
+
glfw.window_hint(glfw.MAXIMIZED, glfw.TRUE if self.maximized else glfw.FALSE)
|
|
803
|
+
glfw.window_hint(glfw.TRANSPARENT_FRAMEBUFFER, glfw.TRUE if self.transparent else glfw.FALSE)
|
|
804
|
+
glfw.window_hint(glfw.STENCIL_BITS, 8)
|
|
805
|
+
|
|
806
|
+
monitor = glfw.get_primary_monitor() if self.fullscreen else None
|
|
807
|
+
create_w, create_h = self.width, self.height
|
|
808
|
+
if monitor is not None:
|
|
809
|
+
mode = glfw.get_video_mode(monitor)
|
|
810
|
+
create_w, create_h = mode.size.width, mode.size.height
|
|
811
|
+
|
|
812
|
+
win = glfw.create_window(create_w, create_h, self.title, monitor, None)
|
|
813
|
+
if not win:
|
|
814
|
+
raise RuntimeError(f"Failed to create GLFW window {self.title!r}.")
|
|
815
|
+
|
|
816
|
+
self._glfw_window = win
|
|
817
|
+
if self.min_size is not None or self.max_size is not None:
|
|
818
|
+
min_w = self.min_size[0] if self.min_size else glfw.DONT_CARE
|
|
819
|
+
min_h = self.min_size[1] if self.min_size else glfw.DONT_CARE
|
|
820
|
+
max_w = self.max_size[0] if self.max_size else glfw.DONT_CARE
|
|
821
|
+
max_h = self.max_size[1] if self.max_size else glfw.DONT_CARE
|
|
822
|
+
glfw.set_window_size_limits(win, min_w, min_h, max_w, max_h)
|
|
823
|
+
|
|
824
|
+
if self._window_opacity < 0.999:
|
|
825
|
+
glfw.set_window_opacity(win, self._window_opacity)
|
|
826
|
+
|
|
827
|
+
if not self.fullscreen and not self.maximized:
|
|
828
|
+
if self.position is not None:
|
|
829
|
+
glfw.set_window_pos(win, self.position[0], self.position[1])
|
|
830
|
+
elif self.center:
|
|
831
|
+
self.center_on_screen()
|
|
832
|
+
|
|
833
|
+
self._apply_native_icon()
|
|
834
|
+
self._sync_native_titlebar()
|
|
835
|
+
|
|
836
|
+
self._cursors = {
|
|
837
|
+
"default": glfw.create_standard_cursor(glfw.ARROW_CURSOR),
|
|
838
|
+
"pointer": glfw.create_standard_cursor(glfw.HAND_CURSOR),
|
|
839
|
+
"ibeam": glfw.create_standard_cursor(glfw.IBEAM_CURSOR),
|
|
840
|
+
"ew_resize": glfw.create_standard_cursor(glfw.HRESIZE_CURSOR),
|
|
841
|
+
}
|
|
842
|
+
self._active_cursor = "default"
|
|
843
|
+
glfw.make_context_current(win)
|
|
844
|
+
glfw.swap_interval(1)
|
|
845
|
+
self._install_callbacks(win)
|
|
846
|
+
self._needs_redraw = True
|
|
847
|
+
|
|
848
|
+
def _step_frame(self, now: float) -> tuple[bool, bool]:
|
|
849
|
+
"""Advance animations & render this window if needed. Returns (animating, redrew)."""
|
|
850
|
+
win = self._glfw_window
|
|
851
|
+
if win is None:
|
|
852
|
+
return (False, False)
|
|
853
|
+
|
|
854
|
+
animating = self.root.tick(now) if self.root is not None else False
|
|
855
|
+
for ov in list(self._overlays):
|
|
856
|
+
if ov.tick(now):
|
|
857
|
+
animating = True
|
|
858
|
+
if (
|
|
859
|
+
self._hovered_widget is not None
|
|
860
|
+
and self._hovered_widget._tooltip_text
|
|
861
|
+
and (now - self._hover_since) < 0.55
|
|
862
|
+
):
|
|
863
|
+
animating = True
|
|
864
|
+
|
|
865
|
+
fb_w, fb_h = glfw.get_framebuffer_size(win)
|
|
866
|
+
win_w, win_h = glfw.get_window_size(win)
|
|
867
|
+
if fb_w <= 0 or fb_h <= 0 or win_w <= 0 or win_h <= 0:
|
|
868
|
+
return (animating, False)
|
|
869
|
+
|
|
870
|
+
self._scale = fb_w / float(win_w)
|
|
871
|
+
needs_surface = self._surface is None or self._fb_size != (fb_w, fb_h)
|
|
872
|
+
if not (self._needs_redraw or animating or needs_surface):
|
|
873
|
+
return (False, False)
|
|
874
|
+
|
|
875
|
+
glfw.make_context_current(win)
|
|
876
|
+
if needs_surface:
|
|
877
|
+
self._create_surface(fb_w, fb_h)
|
|
878
|
+
|
|
879
|
+
self._needs_redraw = False
|
|
880
|
+
GL.glViewport(0, 0, fb_w, fb_h)
|
|
881
|
+
with self._surface as canvas:
|
|
882
|
+
self._paint_frame(canvas, float(win_w), float(win_h), self._scale, now)
|
|
883
|
+
self._surface.flushAndSubmit()
|
|
884
|
+
glfw.swap_buffers(win)
|
|
885
|
+
return (animating, True)
|
|
886
|
+
|
|
887
|
+
def _destroy_native(self) -> None:
|
|
888
|
+
if self._glfw_window is not None:
|
|
889
|
+
glfw.make_context_current(self._glfw_window)
|
|
890
|
+
if self._surface is not None:
|
|
891
|
+
self._surface = None
|
|
892
|
+
self._backend_render_target = None
|
|
893
|
+
if self._gr_context is not None:
|
|
894
|
+
self._gr_context.abandonContext()
|
|
895
|
+
self._gr_context = None
|
|
896
|
+
for cur in self._cursors.values():
|
|
897
|
+
if cur is not None:
|
|
898
|
+
glfw.destroy_cursor(cur)
|
|
899
|
+
self._cursors.clear()
|
|
900
|
+
if self._glfw_window is not None:
|
|
901
|
+
glfw.destroy_window(self._glfw_window)
|
|
902
|
+
self._glfw_window = None
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
def set_main_window(win: Window | None) -> Window | None:
|
|
906
|
+
"""Designate `win` as the main application window: closing it closes all other windows and exits."""
|
|
907
|
+
window_manager.set_main_window(win)
|
|
908
|
+
return win
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
def quit_app() -> None:
|
|
912
|
+
"""Close all open windows and exit the QuantumGUI event loop cleanly."""
|
|
913
|
+
window_manager.close_all()
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
def run_app(*windows: Window) -> None:
|
|
917
|
+
"""Run one or more native QuantumGUI windows simultaneously."""
|
|
918
|
+
for w in windows:
|
|
919
|
+
window_manager.register(w)
|
|
920
|
+
window_manager.run()
|