//! KVT-fork: minimaliseren, maximaliseren/herstellen en sluiten in de titelbalk. //! //! Alleen op Linux en Windows, en alleen als de instelling aan staat. Op macOS //! blijven de verkeerslichten van het systeem. Sluiten volgt dezelfde route als //! de systeemknop (inclusief sluiten naar de tray). Zie KVT-FORK.md. use gpui_kit::assets::IconName; use gpui_kit::component::button::{Button, ButtonVariants as _}; use gpui_kit::component::h_flex; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use store::Store; use crate::i18n::tr; use crate::notify::NotificationCenter; /// Kleine titelbalk-iconen, strak gegroepeerd zodat de zoekbalk ruimte houdt. const CONTROL_SIZE: f32 = 20.; const CONTROL_GAP: f32 = 0.; /// Of de eigen knoppen en client-side decorations gewenst zijn. pub fn enabled(cx: &App) -> bool { app_base::kvt_window::available() && cx .try_global::() .map(|active| active.enabled) .unwrap_or(app_base::kvt_window::DEFAULT_ENABLED) } pub fn install(store: &Store, cx: &mut App) { cx.set_global(Active { enabled: app_base::kvt_window::load(store), }); } /// Zet de instelling, vraagt de passende decorations aan en ververst de vensters. pub fn set_enabled(enabled: bool, store: &Store, window: &Window, cx: &mut App) { app_base::kvt_window::save(store, enabled); cx.set_global(Active { enabled }); apply_decorations(window, enabled); cx.refresh_windows(); } /// Decorations voor het hoofdvenster bij het openen. pub fn window_decorations(enabled: bool) -> WindowDecorations { if enabled && app_base::kvt_window::available() { WindowDecorations::Client } else { WindowDecorations::Server } } pub fn apply_decorations(window: &Window, enabled: bool) { if app_base::kvt_window::available() { window.request_decorations(window_decorations(enabled)); } } pub fn title_bar_system_window_controls(cx: &App) -> bool { !enabled(cx) } /// Knoppen rechts in de titelbalk, na «…» en de avatar, als één groep zodat /// de zoekbalk bij een smal venster ruimte houdt. Leeg als ze niet getoond /// moeten worden. pub fn title_buttons( notifications: &Entity, window: &Window, cx: &App, ) -> Vec { if !enabled(cx) { return Vec::new(); } let supported = window.window_controls(); let maximized = window.is_maximized(); let mut buttons = Vec::new(); if supported.minimize { buttons.push(control_button( "title-window-minimize", "shell/title-bar/minimize", IconName::ChevronDown, tr("kvt.window.minimize"), Action::Minimize, notifications.clone(), )); } if supported.maximize { if maximized { buttons.push(control_button( "title-window-restore", "shell/title-bar/restore", IconName::WindowRestore, tr("kvt.window.restore"), Action::Restore, notifications.clone(), )); } else { buttons.push(control_button( "title-window-maximize", "shell/title-bar/maximize", IconName::ChevronUp, tr("kvt.window.maximize"), Action::Maximize, notifications.clone(), )); } } buttons.push(control_button( "title-window-close", "shell/title-bar/close", IconName::Close, tr("kvt.window.close"), Action::Close, notifications.clone(), )); if buttons.is_empty() { return Vec::new(); } vec![ h_flex() .debug_selector(|| "shell/title-bar/window-controls".into()) .flex_none() .h_full() .items_center() .gap(px(CONTROL_GAP)) .children(buttons) .into_any_element(), ] } #[derive(Clone, Copy)] enum Action { Minimize, Maximize, Restore, Close, } impl Action { fn window_control_area(self) -> WindowControlArea { match self { Self::Minimize => WindowControlArea::Min, Self::Maximize | Self::Restore => WindowControlArea::Max, Self::Close => WindowControlArea::Close, } } } fn control_button( id: &'static str, selector: &'static str, icon: IconName, tooltip: String, action: Action, notifications: Entity, ) -> AnyElement { let is_windows = cfg!(target_os = "windows"); let is_linux = cfg!(target_os = "linux"); Button::new(id) .ghost() .compact() .p_0() .size(px(CONTROL_SIZE)) .icon(icon) .tooltip(tooltip) .debug_selector(|| selector.into()) .when(is_windows, |button| { button.window_control_area(action.window_control_area()) }) .when(is_linux || cfg!(not(any(target_os = "windows", target_os = "macos"))), |button| { button .on_mouse_down(MouseButton::Left, |_, window, cx| { window.prevent_default(); cx.stop_propagation(); }) .on_click(move |_, window, cx| { cx.stop_propagation(); match action { Action::Minimize => window.minimize_window(), Action::Maximize | Action::Restore => window.zoom_window(), Action::Close => close_main_window(¬ifications, window, cx), } }) }) .into_any_element() } /// Zelfde pad als de systeem-sluitknop: `on_window_should_close` in de shell /// (tray verbergen of echt sluiten). fn close_main_window( notifications: &Entity, window: &mut Window, cx: &mut App, ) { let keep_open = notifications.update(cx, |notifications, cx| notifications.intercept_close(cx)); if !keep_open { window.remove_window(); } } struct Active { enabled: bool, } impl Global for Active {} #[cfg(test)] mod tests { use super::window_decorations; use gpui_kit::WindowDecorations; #[test] fn client_decorations_follow_the_setting() { assert_eq!( window_decorations(true), if cfg!(target_os = "macos") { WindowDecorations::Server } else { WindowDecorations::Client } ); assert_eq!(window_decorations(false), WindowDecorations::Server); } }