Skip to content

Commit

Permalink
feat: Enable double clicking on system tray icon to open the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
PRTTMPRPHT committed Jan 13, 2025
1 parent d588d8b commit 676e5b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ fn main() -> anyhow::Result<()> {
.icon_as_template(true)
.menu(&menu)
.menu_on_left_click(true)
.on_menu_event(system_tray.get_event_handler())
.on_menu_event(system_tray.get_menu_event_handler())
.on_tray_icon_event(system_tray.get_tray_icon_event_handler())
.build(app)?;

info!("Setup done");
Expand Down
21 changes: 19 additions & 2 deletions desktop/src-tauri/src/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{util, workspaces::WorkspacesState, AppHandle, AppState, UiMessage};
use log::{error, warn};
use tauri::{
menu::{Menu, MenuBuilder, MenuEvent, MenuItem, Submenu, SubmenuBuilder},
tray::{TrayIcon, TrayIconEvent},
tray::{TrayIcon, TrayIconEvent, MouseButton},
EventLoopMessage, Manager, State, Wry,
};
use util::QUIT_EXIT_CODE;
Expand Down Expand Up @@ -67,7 +67,7 @@ impl SystemTray {
// TauriSystemTray::new().with_menu(tray_menu)
// }

pub fn get_event_handler(&self) -> impl Fn(&AppHandle, MenuEvent) + Send + Sync {
pub fn get_menu_event_handler(&self) -> impl Fn(&AppHandle, MenuEvent) + Send + Sync {
|app, event| match event.id.as_ref() {
Self::QUIT_ID => {
app.exit(QUIT_EXIT_CODE)
Expand Down Expand Up @@ -98,4 +98,21 @@ impl SystemTray {
}
}
}

pub fn get_tray_icon_event_handler(&self) -> impl Fn(&TrayIcon, TrayIconEvent) + Send + Sync {
|icon, event| match event {
TrayIconEvent::DoubleClick { button, .. }=> {
if button == MouseButton::Left {
let app_state = icon.app_handle().state::<AppState>();

tauri::async_runtime::block_on(async move {
if let Err(err) = app_state.ui_messages.send(UiMessage::ShowDashboard).await {
error!("Failed to broadcast show dashboard message: {}", err);
};
});
}
}
_ => {}
}
}
}

0 comments on commit 676e5b2

Please sign in to comment.