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