use std::cell::Cell;
use std::rc::Rc;
use windui::prelude::*;
fn solid(size: u32, hex: u32) -> Vec<u8> {
let (r, g, b) = (
((hex >> 16) & 0xff) as u8,
((hex >> 8) & 0xff) as u8,
(hex & 0xff) as u8,
);
[r, g, b, 255].repeat((size * size) as usize)
}
fn main() {
let notify_on = Rc::new(Cell::new(true));
let n2 = notify_on.clone();
let notify_sig = signal(true);
let tray = Tray::new()
.tooltip("windui 托盘示例")
.icon_rgba(16, 16, &solid(16, 0x4C8BF5))
.on_left_click(|ctx| ctx.show_window())
.on_double_click(|ctx| ctx.show_window())
.menu(vec![
TrayMenuItem::item("显示窗口", |ctx| ctx.show_window()),
TrayMenuItem::item("隐藏到托盘", |ctx| ctx.hide_window()),
TrayMenuItem::separator(),
TrayMenuItem::check("启用通知", notify_on.clone(), move |ctx| {
let next = !n2.get();
n2.set(next);
notify_sig.set(next);
if next {
ctx.notify("通知已开启", "右键菜单可再次切换");
}
}),
TrayMenuItem::item("弹个气泡", |ctx| {
ctx.notify("你好", "这是来自托盘的气泡通知")
})
.enabled(notify_sig),
TrayMenuItem::separator(),
TrayMenuItem::item("退出", |ctx| ctx.quit()),
]);
let ui = Element::col()
.fill()
.bg(Color::hex(0xFFFFFF))
.padding(24)
.spacing(10)
.child(
Element::label("系统托盘")
.font_size(22.0)
.fg(Color::hex(0x2D3436))
.height(30)
.width_match(),
)
.child(
Element::label("左键托盘图标显示窗口;右键弹原生菜单(含勾选项、分隔线、气泡通知)。")
.font_size(13.0)
.fg(Color::hex(0x636E72))
.width_match()
.weight(1.0),
);
App::new("windui — 托盘", 420, 240)
.bg(Color::hex(0xFFFFFF))
.tray(tray)
.content(ui)
.run();
}