BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / kvt / window_controls.rs

window_controls.rs

window_controls.rs 6.5 KiB Download Raw
1//! KVT-fork: minimaliseren, maximaliseren/herstellen en sluiten in de titelbalk.
2//!
3//! Alleen op Linux en Windows, en alleen als de instelling aan staat. Op macOS
4//! blijven de verkeerslichten van het systeem. Sluiten volgt dezelfde route als
5//! de systeemknop (inclusief sluiten naar de tray). Zie KVT-FORK.md.
6
7use gpui_kit::assets::IconName;
8use gpui_kit::component::button::{Button, ButtonVariants as _};
9use gpui_kit::component::h_flex;
10use gpui_kit::prelude::FluentBuilder as _;
11use gpui_kit::*;
12use store::Store;
13
14use crate::i18n::tr;
15use crate::notify::NotificationCenter;
16
17/// Kleine titelbalk-iconen, strak gegroepeerd zodat de zoekbalk ruimte houdt.
18const CONTROL_SIZE: f32 = 20.;
19const CONTROL_GAP: f32 = 0.;
20
21/// Of de eigen knoppen en client-side decorations gewenst zijn.
22pub fn enabled(cx: &App) -> bool {
23 app_base::kvt_window::available()
24 && cx
25 .try_global::<Active>()
26 .map(|active| active.enabled)
27 .unwrap_or(app_base::kvt_window::DEFAULT_ENABLED)
28}
29
30pub fn install(store: &Store, cx: &mut App) {
31 cx.set_global(Active {
32 enabled: app_base::kvt_window::load(store),
33 });
34}
35
36/// Zet de instelling, vraagt de passende decorations aan en ververst de vensters.
37pub fn set_enabled(enabled: bool, store: &Store, window: &Window, cx: &mut App) {
38 app_base::kvt_window::save(store, enabled);
39 cx.set_global(Active { enabled });
40 apply_decorations(window, enabled);
41 cx.refresh_windows();
42}
43
44/// Decorations voor het hoofdvenster bij het openen.
45pub fn window_decorations(enabled: bool) -> WindowDecorations {
46 if enabled && app_base::kvt_window::available() {
47 WindowDecorations::Client
48 } else {
49 WindowDecorations::Server
50 }
51}
52
53pub fn apply_decorations(window: &Window, enabled: bool) {
54 if app_base::kvt_window::available() {
55 window.request_decorations(window_decorations(enabled));
56 }
57}
58
59pub fn title_bar_system_window_controls(cx: &App) -> bool {
60 !enabled(cx)
61}
62
63/// Knoppen rechts in de titelbalk, na «…» en de avatar, als één groep zodat
64/// de zoekbalk bij een smal venster ruimte houdt. Leeg als ze niet getoond
65/// moeten worden.
66pub fn title_buttons(
67 notifications: &Entity<NotificationCenter>,
68 window: &Window,
69 cx: &App,
70) -> Vec<AnyElement> {
71 if !enabled(cx) {
72 return Vec::new();
73 }
74 let supported = window.window_controls();
75 let maximized = window.is_maximized();
76 let mut buttons = Vec::new();
77 if supported.minimize {
78 buttons.push(control_button(
79 "title-window-minimize",
80 "shell/title-bar/minimize",
81 IconName::ChevronDown,
82 tr("kvt.window.minimize"),
83 Action::Minimize,
84 notifications.clone(),
85 ));
86 }
87 if supported.maximize {
88 if maximized {
89 buttons.push(control_button(
90 "title-window-restore",
91 "shell/title-bar/restore",
92 IconName::WindowRestore,
93 tr("kvt.window.restore"),
94 Action::Restore,
95 notifications.clone(),
96 ));
97 } else {
98 buttons.push(control_button(
99 "title-window-maximize",
100 "shell/title-bar/maximize",
101 IconName::ChevronUp,
102 tr("kvt.window.maximize"),
103 Action::Maximize,
104 notifications.clone(),
105 ));
106 }
107 }
108 buttons.push(control_button(
109 "title-window-close",
110 "shell/title-bar/close",
111 IconName::Close,
112 tr("kvt.window.close"),
113 Action::Close,
114 notifications.clone(),
115 ));
116 if buttons.is_empty() {
117 return Vec::new();
118 }
119 vec![
120 h_flex()
121 .debug_selector(|| "shell/title-bar/window-controls".into())
122 .flex_none()
123 .h_full()
124 .items_center()
125 .gap(px(CONTROL_GAP))
126 .children(buttons)
127 .into_any_element(),
128 ]
129}
130
131#[derive(Clone, Copy)]
132enum Action {
133 Minimize,
134 Maximize,
135 Restore,
136 Close,
137}
138
139impl Action {
140 fn window_control_area(self) -> WindowControlArea {
141 match self {
142 Self::Minimize => WindowControlArea::Min,
143 Self::Maximize | Self::Restore => WindowControlArea::Max,
144 Self::Close => WindowControlArea::Close,
145 }
146 }
147}
148
149fn control_button(
150 id: &'static str,
151 selector: &'static str,
152 icon: IconName,
153 tooltip: String,
154 action: Action,
155 notifications: Entity<NotificationCenter>,
156) -> AnyElement {
157 let is_windows = cfg!(target_os = "windows");
158 let is_linux = cfg!(target_os = "linux");
159 Button::new(id)
160 .ghost()
161 .compact()
162 .p_0()
163 .size(px(CONTROL_SIZE))
164 .icon(icon)
165 .tooltip(tooltip)
166 .debug_selector(|| selector.into())
167 .when(is_windows, |button| {
168 button.window_control_area(action.window_control_area())
169 })
170 .when(is_linux || cfg!(not(any(target_os = "windows", target_os = "macos"))), |button| {
171 button
172 .on_mouse_down(MouseButton::Left, |_, window, cx| {
173 window.prevent_default();
174 cx.stop_propagation();
175 })
176 .on_click(move |_, window, cx| {
177 cx.stop_propagation();
178 match action {
179 Action::Minimize => window.minimize_window(),
180 Action::Maximize | Action::Restore => window.zoom_window(),
181 Action::Close => close_main_window(&notifications, window, cx),
182 }
183 })
184 })
185 .into_any_element()
186}
187
188/// Zelfde pad als de systeem-sluitknop: `on_window_should_close` in de shell
189/// (tray verbergen of echt sluiten).
190fn close_main_window(
191 notifications: &Entity<NotificationCenter>,
192 window: &mut Window,
193 cx: &mut App,
194) {
195 let keep_open = notifications.update(cx, |notifications, cx| notifications.intercept_close(cx));
196 if !keep_open {
197 window.remove_window();
198 }
199}
200
201struct Active {
202 enabled: bool,
203}
204
205impl Global for Active {}
206
207#[cfg(test)]
208mod tests {
209 use super::window_decorations;
210 use gpui_kit::WindowDecorations;
211
212 #[test]
213 fn client_decorations_follow_the_setting() {
214 assert_eq!(
215 window_decorations(true),
216 if cfg!(target_os = "macos") {
217 WindowDecorations::Server
218 } else {
219 WindowDecorations::Client
220 }
221 );
222 assert_eq!(window_decorations(false), WindowDecorations::Server);
223 }
224}