Repository navigation
如何实现系统托盘?
#1077
Replies: 3 comments
|
还没做过,GPUI 和 GPUI Component 没有这样的功能。这个可以试试用一些 Native API 实现。 |
0 replies
|
有人之前在GPUI提过PR,但GPUI暂时拒绝了,因为没有人提过这个需求. 但我现在很需要系统托盘的功能. |
0 replies
|
托盘库:trayicon #[cfg(target_os = "windows")]
std::thread::spawn(move || {
use windows::Win32::UI::WindowsAndMessaging::*;
let mut msg = MSG::default();
loop {
unsafe {
GetMessageW(&mut msg, None, 0, 0).as_bool().then(|| {
let _ = TranslateMessage(&msg);
let _ = DispatchMessageA(&msg);
});
}
}
});run方法里增加订阅trayicon发出的事件 app.run(move |cx| {
#[cfg(target_os = "windows")]
{
let (tx, mut rx) = tokio::sync::mpsc::channel::<TrayEvent>(10);
let mut tray = tray::start_tray().unwrap();
let _sub = CrossRuntimeBridge::global().subscribe(move |event: &TrayEvent| {
tx.try_send(event.clone()).ok();
});
cx.spawn(async move |app| {
let _subscription = _sub;
while let Some(event) = rx.recv().await {
// println!("Tray event2: {:?}", event);
match event {
TrayEvent::DoubleClickTrayIcon => {
// println!("Double clicked");
}
TrayEvent::LeftClickTrayIcon => {
app.update(|app| {
// println!("Left clicked");
app.windows().iter().for_each(|handle| {
handle
.update(app, |_, window, _| {
#[cfg(target_os = "windows")]
{
window.show();
}
window.activate_window();
})
.ok();
});
app.activate(true);
})
.ok();
}
TrayEvent::RightClickTrayIcon => {
tray.show_menu().ok();
// println!("Right clicked");
}
TrayEvent::Exit => {
app.update(|app| {
app.quit();
})
.ok();
}
TrayEvent::Open => {
app.update(|app| {
// println!("Open clicked");
app.windows().iter().for_each(|handle| {
handle
.update(app, |_, window, _| {
#[cfg(target_os = "windows")]
{
window.show();
}
window.activate_window();
})
.ok();
});
app.activate(true);
})
.ok();
}
TrayEvent::Hide => {
app.update(|app| {
// println!("Hide clicked");
app.hide();
#[cfg(target_os = "windows")]
{
app.windows().iter().for_each(|handle| {
handle
.update(app, |_, window, _| {
window.hide();
})
.ok();
});
}
app.activate(false);
})
.ok();
}
}
}
})
.detach();
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
如何实现系统托盘?
All reactions