-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevents.py
More file actions
65 lines (45 loc) · 2.16 KB
/
Copy pathevents.py
File metadata and controls
65 lines (45 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
events.py — App and Window events, plus Python-to-JS event emission.
Shows:
- AppEvent.ReadyEvent — fires once when the app is ready
- AppEvent.AppCloseEvent — fires on graceful shutdown
- WindowEvent.TitleChangedEvent — fires when the page title changes
- win.emit() — push events from Python to JS (window.lumiview.listen)
Run:
python examples/events.py
"""
from lumiview import App, AppEvent, Window, WindowEvent, WindowOptions
app = App(name="EventsDemo")
# ── App-level events ─────────────────────────────────────────────────────────
@app.on(AppEvent.ReadyEvent)
async def on_ready(event: AppEvent.ReadyEvent):
print("[AppEvent] Ready — app is running.")
@app.on(AppEvent.AppCloseEvent)
async def on_close(event: AppEvent.AppCloseEvent):
print("[AppEvent] Close — shutting down gracefully.")
async def main():
win = await Window.create(
title="Events Demo",
url="https://www.example.com",
width=800,
height=600,
devtools=True,
)
# ── Window-level event: TitleChanged ────────────────────────────────
@win.on(WindowEvent.TitleChangedEvent)
async def on_title_changed(event: WindowEvent.TitleChangedEvent):
print(f"[WindowEvent] Title changed → '{event.title}'")
# Trigger a title change to fire the hook
await win.eval_js("document.title = 'LumiView Events!'")
# ── Push events from Python to JS ───────────────────────────────────
# Register a JS-side listener
await win.eval_js(
"window.lumiview.listen('my.event', (p) => { window.__evt = p.data; });"
)
await win.emit("my.event", {"data": "Hello from Python!", "ts": 42})
print("[emit] Sent 'my.event' to JS.")
received = await win.eval_js("window.__evt")
print(f"[emit] JS received payload: {received!r}")
print("App running — close the window or press Ctrl+C.")
if __name__ == "__main__":
app.run(main)