Skip to repository content150 lines · 5.6 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T04:44:43.522Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
system_notifications.rs
1#![cfg_attr(target_family = "wasm", no_main)]
2
3//! Demonstrates posting, replacing, dismissing, and responding to system notifications.
4
5use gpui::{
6 App, Bounds, Context, Div, SharedString, Stateful, SystemNotification,
7 SystemNotificationAction, SystemNotificationResponse, Window, WindowBounds, WindowOptions, div,
8 prelude::*, px, rgb, size,
9};
10use gpui_platform::application;
11
12const NOTIFICATION_TAG: &str = "gpui-system-notification-example";
13
14struct SystemNotificationExample {
15 revision: usize,
16 status: SharedString,
17}
18
19impl Render for SystemNotificationExample {
20 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
21 div()
22 .flex()
23 .flex_col()
24 .size_full()
25 .gap_4()
26 .p_8()
27 .bg(rgb(0x18181b))
28 .text_color(rgb(0xf4f4f5))
29 .child(div().text_2xl().child("GPUI system notifications"))
30 .child(div().text_sm().text_color(rgb(0xa1a1aa)).child(
31 "Post repeatedly to replace the notification with the same tag. Click the \
32 notification body or an action button to send a response back to GPUI.",
33 ))
34 .child(
35 div()
36 .flex()
37 .gap_3()
38 .child(button("show", "Show or replace").on_click(cx.listener(
39 |this, _, _, cx| {
40 this.revision += 1;
41 let revision = this.revision;
42 cx.show_system_notification(SystemNotification {
43 tag: NOTIFICATION_TAG.into(),
44 title: format!("Example notification {revision}").into(),
45 body: "This notification was posted by the GPUI example.".into(),
46 actions: vec![
47 SystemNotificationAction {
48 id: "open".into(),
49 label: "Open".into(),
50 },
51 SystemNotificationAction {
52 id: "snooze".into(),
53 label: "Snooze".into(),
54 },
55 ],
56 });
57 this.status = format!("Posted notification revision {revision}").into();
58 cx.notify();
59 },
60 )))
61 .child(
62 button("dismiss", "Dismiss").on_click(cx.listener(|this, _, _, cx| {
63 cx.dismiss_system_notification(NOTIFICATION_TAG);
64 this.status = "Dismissed the notification".into();
65 cx.notify();
66 })),
67 ),
68 )
69 .child(
70 div()
71 .mt_2()
72 .p_4()
73 .rounded_md()
74 .bg(rgb(0x27272a))
75 .child(self.status.clone()),
76 )
77 .when(cfg!(target_os = "macos"), |this| {
78 this.child(div().mt_2().text_xs().text_color(rgb(0x71717a)).child(
79 "macOS only delivers notifications when this example runs from an app bundle.",
80 ))
81 })
82 }
83}
84
85fn button(id: &'static str, label: &'static str) -> Stateful<Div> {
86 div()
87 .id(id)
88 .px_4()
89 .py_2()
90 .rounded_md()
91 .bg(rgb(0x3f3f46))
92 .hover(|style| style.bg(rgb(0x52525b)))
93 .active(|style| style.bg(rgb(0x71717a)))
94 .cursor_pointer()
95 .child(label)
96}
97
98fn run_example() {
99 application().run(|cx: &mut App| {
100 cx.set_app_identity("dev.zed.gpui.system-notifications", "GPUI Notifications");
101
102 let view = cx.new(|_| SystemNotificationExample {
103 revision: 0,
104 status: "No notification posted yet".into(),
105 });
106 cx.on_system_notification_response({
107 let view = view.clone();
108 move |response, cx| {
109 let SystemNotificationResponse { tag, action_id } = response;
110 view.update(cx, |this, cx| {
111 this.status = match action_id {
112 Some(action_id) => {
113 format!("Received action '{action_id}' for tag '{tag}'").into()
114 }
115 None => format!("Notification body clicked for tag '{tag}'").into(),
116 };
117 cx.notify();
118 });
119 }
120 });
121
122 let bounds = Bounds::centered(None, size(px(560.), px(360.)), cx);
123 cx.open_window(
124 WindowOptions {
125 window_bounds: Some(WindowBounds::Windowed(bounds)),
126 titlebar: Some(gpui::TitlebarOptions {
127 title: Some("System Notifications Example".into()),
128 ..Default::default()
129 }),
130 ..Default::default()
131 },
132 move |_, _| view,
133 )
134 .expect("failed to open system notifications example window");
135 cx.activate(true);
136 });
137}
138
139#[cfg(not(target_family = "wasm"))]
140fn main() {
141 run_example();
142}
143
144#[cfg(target_family = "wasm")]
145#[wasm_bindgen::prelude::wasm_bindgen(start)]
146pub fn start() {
147 gpui_platform::web_init();
148 run_example();
149}
150