Skip to content

Commit

Permalink
Handle left mouse click on the icon on Windows to show an app
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Aug 30, 2024
1 parent a18d94e commit 7d71043
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/frontend/tray_icon/windows_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::any::Any;
use std::error::Error;
use tracing::warn;
use tray_icon::menu::{Menu, MenuEvent, MenuItem};
use tray_icon::{TrayIcon, TrayIconBuilder};
use tray_icon::{MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent};

pub(in super::super) async fn spawn(sender: &AsyncComponentSender<App>) -> Option<Box<dyn Any>> {
let init_result: Result<TrayIcon, Box<dyn Error>> = try {
Expand All @@ -30,9 +30,9 @@ pub(in super::super) async fn spawn(sender: &AsyncComponentSender<App>) -> Optio
.build()
.map_err(|error| format!("Failed to create tray icon: {error}"))?;

let menu_event = MenuEvent::receiver();
let menu_events = MenuEvent::receiver();
sender.spawn_command(move |sender| {
while let Ok(event) = menu_event.recv() {
while let Ok(event) = menu_events.recv() {
let output = if event.id == menu_open_id {
AppCommandOutput::ShowWindow
} else if event.id == menu_close_id {
Expand All @@ -48,6 +48,27 @@ pub(in super::super) async fn spawn(sender: &AsyncComponentSender<App>) -> Optio
}
});

let tray_icon_events = TrayIconEvent::receiver();
sender.spawn_command(move |sender| {
while let Ok(event) = tray_icon_events.recv() {
let output = if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event
{
AppCommandOutput::ShowWindow
} else {
continue;
};

if let Err(error) = sender.send(output) {
warn!(?error, "Failed to send tray icon notification");
break;
}
}
});

icon
};

Expand Down

0 comments on commit 7d71043

Please sign in to comment.