-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
114 lines (98 loc) · 3.7 KB
/
Copy pathapp.py
File metadata and controls
114 lines (98 loc) · 3.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import argparse
import asyncio
import os
from pathlib import Path
ROOT = Path(__file__).resolve().parent
os.environ["WINAPPSDK_BOOTSTRAP_DLL_PATH"] = str(
ROOT / ".runtime" / "Microsoft.WindowsAppRuntime.Bootstrap.dll"
)
from dynwinrt import RoApartment, init_winappsdk, projected_lifetime_scope
from generated.microsoft.windows.app_notifications import (
AppNotification,
AppNotificationActivatedEventArgs,
AppNotificationManager,
)
from generated.microsoft.windows.app_notifications.builder import (
AppNotificationBuilder,
)
TAG = "dynwinrt-python-sample"
def build_notification() -> AppNotification:
builder = AppNotificationBuilder()
builder.add_text("Hello from dynwinrt")
builder.add_text("This notification was created by generated Python bindings.")
builder.add_argument("source", "dynwinrt-python-sample")
builder.set_tag(TAG)
notification = builder.build_notification()
if notification is None:
raise RuntimeError("AppNotificationBuilder returned no notification")
return notification
async def run(
smoke: bool,
timeout: int,
major: int,
minor: int,
) -> None:
runtime = init_winappsdk(major, minor)
try:
with RoApartment(), projected_lifetime_scope():
supported = AppNotificationManager.is_supported()
notification = build_notification()
if smoke:
print(
"python-app-notification-ok",
{
"supported": supported,
"payload_length": len(notification.payload),
},
)
return
if not supported:
raise RuntimeError("App notifications are not supported")
manager = AppNotificationManager.get_default()
if manager is None:
raise RuntimeError("AppNotificationManager is unavailable")
loop = asyncio.get_running_loop()
invoked = asyncio.Event()
def on_invoked(
_sender: AppNotificationManager | None,
args: AppNotificationActivatedEventArgs | None,
) -> None:
argument = "" if args is None else args.argument
print("Notification activated:", argument)
loop.call_soon_threadsafe(invoked.set)
unsubscribe = manager.subscribe_notification_invoked(on_invoked)
registered = False
try:
manager.register()
registered = True
manager.show(notification)
print(f"Notification shown. Click it within {timeout} seconds.")
try:
await asyncio.wait_for(invoked.wait(), timeout=timeout)
except TimeoutError:
print("No activation was received before the timeout.")
finally:
try:
unsubscribe()
finally:
if registered:
try:
await manager.remove_by_tag_async(TAG)
finally:
manager.unregister_all()
finally:
del runtime
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--smoke",
action="store_true",
help="Build the notification without registering or displaying it.",
)
parser.add_argument("--timeout", type=int, default=30)
parser.add_argument("--major", type=int, default=2)
parser.add_argument("--minor", type=int, default=3)
args = parser.parse_args()
asyncio.run(run(args.smoke, args.timeout, args.major, args.minor))
if __name__ == "__main__":
main()