| 1 | //! Owns the main window so it can be closed into the tray and opened again. |
| 2 | //! |
| 3 | //! GPUI cannot hide a window on Wayland, so on Linux "close to tray" really closes |
| 4 | //! the main window while the app (tray, notifications, calls) keeps running; the tray |
| 5 | //! or a second start opens a fresh one. Windows hides the window natively instead. |
| 6 | |
| 7 | use std::rc::Rc; |
| 8 | |
| 9 | use gpui_kit::*; |
| 10 | |
| 11 | type Opener = Rc<dyn Fn(&mut App) -> Option<AnyWindowHandle>>; |
| 12 | |
| 13 | pub struct MainWindow { |
| 14 | handle: Option<AnyWindowHandle>, |
| 15 | open: Opener, |
| 16 | hiding: bool, |
| 17 | } |
| 18 | |
| 19 | impl Global for MainWindow {} |
| 20 | |
| 21 | /// Opens the first main window with `open` and remembers how to open the next one. |
| 22 | pub fn install( |
| 23 | open: impl Fn(&mut App) -> Option<AnyWindowHandle> + 'static, |
| 24 | cx: &mut App, |
| 25 | ) -> Option<AnyWindowHandle> { |
| 26 | let open: Opener = Rc::new(open); |
| 27 | cx.set_global(MainWindow { |
| 28 | handle: None, |
| 29 | open: open.clone(), |
| 30 | hiding: false, |
| 31 | }); |
| 32 | let handle = open(cx); |
| 33 | cx.global_mut::<MainWindow>().handle = handle; |
| 34 | handle |
| 35 | } |
| 36 | |
| 37 | fn main_mut(cx: &mut App) -> Option<&mut MainWindow> { |
| 38 | cx.has_global::<MainWindow>() |
| 39 | .then(|| cx.global_mut::<MainWindow>()) |
| 40 | } |
| 41 | |
| 42 | pub fn current(cx: &App) -> Option<AnyWindowHandle> { |
| 43 | cx.try_global::<MainWindow>().and_then(|main| main.handle) |
| 44 | } |
| 45 | |
| 46 | /// The next close of the main window goes to the tray instead of quitting. |
| 47 | pub fn mark_hiding(cx: &mut App) { |
| 48 | if let Some(main) = main_mut(cx) { |
| 49 | main.hiding = true; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Closes the main window into the tray. Deferred: the window's views may be on the stack. |
| 54 | pub fn hide(cx: &mut App) { |
| 55 | let Some(handle) = current(cx) else { |
| 56 | return; |
| 57 | }; |
| 58 | mark_hiding(cx); |
| 59 | cx.defer(move |cx| { |
| 60 | if handle |
| 61 | .update(cx, |_, window, _| window.remove_window()) |
| 62 | .is_err() |
| 63 | { |
| 64 | note_closed(handle.window_id(), cx); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | /// Brings the main window to the front, opening a new one if it was closed into the tray. |
| 70 | /// Deferred: building the shell updates entities that may be on the stack. |
| 71 | pub fn show(cx: &mut App) { |
| 72 | cx.defer(|cx| { |
| 73 | if let Some(handle) = current(cx) |
| 74 | && handle |
| 75 | .update(cx, |_, window, _| window.activate_window()) |
| 76 | .is_ok() |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | let Some(open) = cx.try_global::<MainWindow>().map(|main| main.open.clone()) else { |
| 81 | return; |
| 82 | }; |
| 83 | let handle = open(cx); |
| 84 | if let Some(main) = main_mut(cx) { |
| 85 | main.handle = handle; |
| 86 | main.hiding = false; |
| 87 | } |
| 88 | if let Some(handle) = handle { |
| 89 | handle |
| 90 | .update(cx, |_, window, _| window.activate_window()) |
| 91 | .ok(); |
| 92 | } |
| 93 | cx.activate(true); |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | /// Call from `on_window_closed`. Returns true when the app should quit: the main |
| 98 | /// window closed and it was not closed into the tray. |
| 99 | pub fn note_closed(window_id: WindowId, cx: &mut App) -> bool { |
| 100 | let Some(main) = main_mut(cx) else { |
| 101 | return false; |
| 102 | }; |
| 103 | if main.handle.map(|handle| handle.window_id()) != Some(window_id) { |
| 104 | return false; |
| 105 | } |
| 106 | main.handle = None; |
| 107 | !std::mem::take(&mut main.hiding) |
| 108 | } |
| 109 | |
| 110 | /// Whether the main window is currently closed into the tray. |
| 111 | pub fn is_hidden(cx: &App) -> bool { |
| 112 | cx.try_global::<MainWindow>() |
| 113 | .is_some_and(|main| main.handle.is_none()) |
| 114 | } |