diff --git a/src/gpui_shell/mod.rs b/src/gpui_shell/mod.rs index f5fd7e7..e82bf3e 100644 --- a/src/gpui_shell/mod.rs +++ b/src/gpui_shell/mod.rs @@ -153,6 +153,7 @@ pub fn run() -> Result<()> { // environment. let show_panel = show_panel || std::env::var_os("OPENMOUSE_BRIDGE_SHOW_WINDOW").is_some(); + hide_when_unfocused(cx, handle, &view); let (tray_commands, mut tray_handle) = match tray::install(cx, handle) { Some((commands, tray)) => (Some(commands), Some(tray)), None => (None, None), @@ -376,6 +377,22 @@ fn panel_wanted_after(shown: bool, api_request: bool, commands: &[tray::Command] wanted } +/// Hides the panel to the tray when a click elsewhere takes its focus, where it +/// is a popup by the tray (see [`tray::hides_on_blur`]). +fn hide_when_unfocused(cx: &mut App, handle: AnyWindowHandle, view: &Entity) { + let _ = cx.update_window(handle, |_, window, cx| { + view.update(cx, |_, cx| { + cx.observe_window_activation(window, |_, window, _| { + if !window.is_window_active() && tray::hides_on_blur() { + tracing::debug!("hiding the panel after it lost focus"); + tray::hide_on_blur(window); + } + }) + .detach(); + }); + }); +} + /// Makes the window's close button hide the panel to the tray, or quit when /// there is no tray to bring it back from. #[cfg(target_os = "linux")] @@ -431,8 +448,9 @@ fn open_layer_panel( }; let view = view.clone(); match open_window(options, cx, move |_, _| view) { - Ok((handle, _)) => { + Ok((handle, view)) => { close_hides_or_quits(cx, handle, exit); + hide_when_unfocused(cx, handle, &view); Some(handle) } Err(error) => { diff --git a/src/gpui_shell/tray.rs b/src/gpui_shell/tray.rs index 13196a5..499d344 100644 --- a/src/gpui_shell/tray.rs +++ b/src/gpui_shell/tray.rs @@ -4,9 +4,13 @@ //! //! Linux uses StatusNotifierItem over D-Bus, without a GTK event loop. -use std::sync::{ - atomic::{AtomicBool, Ordering}, - mpsc, +use std::{ + sync::{ + Mutex, + atomic::{AtomicBool, Ordering}, + mpsc, + }, + time::{Duration, Instant}, }; #[cfg(any(target_os = "macos", windows))] @@ -572,6 +576,48 @@ pub fn can_hide() -> bool { cfg!(any(target_os = "macos", windows)) } +/// When the panel last hid itself because it lost focus. +static HIDDEN_ON_BLUR_AT: Mutex> = Mutex::new(None); + +/// How soon after the panel hides on losing focus a click on the icon counts +/// as the click that took the focus: pressing the icon takes focus from the +/// panel before the click arrives, and that click should leave it closed. +const BLUR_CLICK_WINDOW: Duration = Duration::from_millis(500); + +/// Whether the panel hides when it loses focus, like a popup: where it sits by +/// the tray and the icon brings it back. Not for a normal Linux window (no +/// layer-shell), which has a taskbar entry and stays where it was put. +pub fn hides_on_blur() -> bool { + #[cfg(target_os = "linux")] + { + layer_panel() && can_hide() + } + #[cfg(not(target_os = "linux"))] + can_hide() +} + +/// Hides the panel because a click elsewhere took its focus. +pub fn hide_on_blur(window: &mut Window) { + if !shown() { + return; + } + hide(window); + if let Ok(mut at) = HIDDEN_ON_BLUR_AT.lock() { + *at = Some(Instant::now()); + } +} + +/// What a click on the icon sends: a toggle, or nothing when the panel has +/// just hidden because pressing the icon took its focus. +pub fn click_command(anchor: Option) -> Option { + let just_hidden = HIDDEN_ON_BLUR_AT + .lock() + .ok() + .and_then(|mut at| at.take()) + .is_some_and(|at| at.elapsed() < BLUR_CLICK_WINDOW); + (!just_hidden).then_some(Command::Toggle(anchor)) +} + pub fn hide(window: &mut Window) { platform::hide_window(window); #[cfg(any(target_os = "macos", windows, target_os = "linux"))] @@ -721,11 +767,14 @@ pub fn install( _ => return, }; let anchor = Some(anchor); - let _ = sender.send(if double_click { - Command::Show(anchor) + let command = if double_click { + Some(Command::Show(anchor)) } else { - Command::Toggle(anchor) - }); + click_command(anchor) + }; + if let Some(command) = command { + let _ = sender.send(command); + } } })); diff --git a/src/gpui_shell/tray_linux.rs b/src/gpui_shell/tray_linux.rs index 4d086f9..67a1c65 100644 --- a/src/gpui_shell/tray_linux.rs +++ b/src/gpui_shell/tray_linux.rs @@ -290,7 +290,9 @@ impl Tray for LinuxTray { right: f64::from(x), bottom: f64::from(y), }); - let _ = self.sender.send(Command::Toggle(anchor)); + if let Some(command) = super::click_command(anchor) { + let _ = self.sender.send(command); + } } fn menu(&self) -> Vec> { use ksni::menu::StandardItem;