#![cfg_attr(target_family = "wasm", no_main)]
use rgpui::{
App, Bounds, Context, Render, SharedString, TrayIconEvent, TrayMenuItem, Window, WindowBounds,
WindowOptions, actions, div, prelude::*, px, rgb, size,
};
use rgpui_platform::application;
actions!(tray, [Quit, ToggleWindow]);
struct TrayExample {
message: SharedString,
}
impl Render for TrayExample {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap_3()
.size(px(400.0))
.justify_center()
.items_center()
.text_xl()
.text_color(rgb(0x505050))
.child(div().child(self.message.clone()))
.child(
div()
.text_sm()
.text_color(rgb(0x808080))
.child("查看系统托盘以获取菜单"),
)
.child(
div()
.text_sm()
.text_color(rgb(0x808080))
.child("点击关闭按钮将最小化到托盘"),
)
}
}
fn run_example() {
application().run(|cx: &mut App| {
cx.set_keep_alive_without_windows(true);
cx.set_tray_tooltip("GPUI Tray Example");
let icon_bytes = include_bytes!("image/app-icon.png");
cx.set_tray_icon(Some(icon_bytes.as_slice()));
cx.on_tray_icon_event(|event, _cx| match event {
TrayIconEvent::LeftClick => {
eprintln!("Tray icon left-clicked");
}
TrayIconEvent::RightClick => {
eprintln!("Tray icon right-clicked");
}
TrayIconEvent::DoubleClick => {
eprintln!("Tray icon double-clicked");
}
});
cx.set_tray_menu(vec![
TrayMenuItem::Action {
label: "显示窗口".into(),
id: "show_window".into(),
},
TrayMenuItem::Separator,
TrayMenuItem::Action {
label: "退出".into(),
id: "quit".into(),
},
]);
cx.on_tray_menu_action(|id, cx| match id.as_ref() {
"quit" => {
cx.quit();
}
"show_window" => {
let existing_windows = cx.windows();
if let Some(window_handle) = existing_windows.first() {
let handle = *window_handle;
cx.update_window(handle, |_, window, _cx| {
window.activate_window();
})
.ok();
} else {
create_main_window(cx);
}
}
_ => {}
});
create_main_window(cx);
cx.activate(true);
});
}
fn create_main_window(cx: &mut App) {
let bounds = Bounds::centered(None, size(px(400.), px(300.0)), cx);
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
..Default::default()
},
|window, cx| {
let view = cx.new(|_| TrayExample {
message: "Hello from GPUI Tray!".into(),
});
window.on_window_should_close(cx, |window, _cx| {
window.hide_window();
false
});
view
},
)
.ok();
}
fn main() {
run_example();
}