BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / window_zoom.rs

window_zoom.rs

window_zoom.rs 5.4 KiB Download Raw
1use gpui_kit::component::{h_flex, tooltip::Tooltip};
2use gpui_kit::prelude::FluentBuilder as _;
3use gpui_kit::*;
4
5use super::widgets::symbol;
6use crate::active_appearance;
7use crate::app_state::AppState;
8use crate::appearance::{self, DEFAULT_ZOOM};
9use crate::theme;
10
11const ZOOM_LABEL: &str = "Zoom";
12const ZOOM_IN_LABEL: &str = "Zoom in";
13const ZOOM_OUT_LABEL: &str = "Zoom out";
14const ZOOM_RESET_LABEL: &str = "Reset zoom";
15const DEFAULT_PERCENT: u32 = 100;
16const MINIMUM_TOAST_PERCENT: u32 = 60;
17const MAXIMUM_TOAST_PERCENT: u32 = 400;
18const MENU_ROUNDING: u32 = 5;
19const BUTTON_SIZE: f32 = 28.;
20const BUTTON_ICON: f32 = 18.;
21const BUTTON_RADIUS: f32 = 6.;
22const DISABLED_OPACITY: f32 = 0.4;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum WindowZoom {
26 In,
27 Out,
28 Reset,
29}
30
31impl WindowZoom {
32 fn next(self, zoom: f32) -> f32 {
33 match self {
34 WindowZoom::In => appearance::zoom_in(zoom),
35 WindowZoom::Out => appearance::zoom_out(zoom),
36 WindowZoom::Reset => DEFAULT_ZOOM,
37 }
38 }
39}
40
41pub fn toast_text(previous_percent: u32, percent: u32) -> String {
42 let state = if percent == DEFAULT_PERCENT {
43 "Default size"
44 } else if percent <= MINIMUM_TOAST_PERCENT {
45 "Minimum zoom"
46 } else if percent >= MAXIMUM_TOAST_PERCENT {
47 "Maximum zoom"
48 } else if percent > previous_percent {
49 "Zoomed in"
50 } else {
51 "Zoomed out"
52 };
53 format!("{percent}% \u{b7} {state}")
54}
55
56pub fn menu_level(zoom: f32) -> String {
57 let percent = appearance::zoom_percent(zoom);
58 let rounded = (percent + MENU_ROUNDING / 2) / MENU_ROUNDING * MENU_ROUNDING;
59 format!("({rounded})%")
60}
61
62pub fn change_zoom(app: &Entity<AppState>, change: WindowZoom, cx: &mut App) {
63 let previous = active_appearance::current(cx).zoom;
64 let next = change.next(previous);
65 let store = app.read(cx).store.clone();
66 if !active_appearance::set_zoom(next, &store, cx) {
67 return;
68 }
69 let text = toast_text(
70 appearance::zoom_percent(previous),
71 appearance::zoom_percent(next),
72 );
73 app.update(cx, |state, cx| state.raise_notice(text, None, cx));
74}
75
76fn zoom_button(
77 app: &Entity<AppState>,
78 id: &'static str,
79 glyph: &'static str,
80 label: &'static str,
81 change: WindowZoom,
82 enabled: bool,
83) -> Stateful<Div> {
84 let app = app.clone();
85 div()
86 .id(id)
87 .size(px(BUTTON_SIZE))
88 .flex_none()
89 .flex()
90 .items_center()
91 .justify_center()
92 .rounded(px(BUTTON_RADIUS))
93 .child(symbol(glyph, BUTTON_ICON, theme::text()))
94 .tooltip(move |window, cx| Tooltip::new(label).build(window, cx))
95 .when(!enabled, |button| button.opacity(DISABLED_OPACITY))
96 .when(enabled, |button| {
97 button
98 .cursor_pointer()
99 .hover(|button| button.bg(theme::popup_hover()))
100 })
101 .on_click(move |_, _, cx| {
102 cx.stop_propagation();
103 if enabled {
104 change_zoom(&app, change, cx);
105 }
106 })
107}
108
109pub fn menu_row(app: &Entity<AppState>, cx: &App) -> Div {
110 let zoom = active_appearance::current(cx).zoom;
111 let zoomed = appearance::zoom_percent(zoom) != DEFAULT_PERCENT;
112 h_flex()
113 .w_full()
114 .gap(px(8.))
115 .items_center()
116 .justify_between()
117 .child(div().text_color(theme::text()).child(ZOOM_LABEL))
118 .child(
119 h_flex()
120 .flex_none()
121 .gap(px(2.))
122 .items_center()
123 .child(zoom_button(
124 app,
125 "window-zoom-out",
126 "zoom_out",
127 ZOOM_OUT_LABEL,
128 WindowZoom::Out,
129 true,
130 ))
131 .child(
132 div()
133 .debug_selector(|| "window-zoom-level".into())
134 .text_color(theme::text_muted())
135 .child(menu_level(zoom)),
136 )
137 .child(zoom_button(
138 app,
139 "window-zoom-in",
140 "zoom_in",
141 ZOOM_IN_LABEL,
142 WindowZoom::In,
143 true,
144 ))
145 .child(zoom_button(
146 app,
147 "window-zoom-reset",
148 "refresh",
149 ZOOM_RESET_LABEL,
150 WindowZoom::Reset,
151 zoomed,
152 )),
153 )
154}
155
156#[cfg(test)]
157mod tests {
158 use super::{WindowZoom, menu_level, toast_text};
159
160 #[test]
161 fn toast_texts_follow_the_teams_zoom_toast() {
162 assert_eq!(toast_text(100, 110), "110% \u{b7} Zoomed in");
163 assert_eq!(toast_text(110, 100), "100% \u{b7} Default size");
164 assert_eq!(toast_text(90, 80), "80% \u{b7} Zoomed out");
165 assert_eq!(toast_text(67, 50), "50% \u{b7} Minimum zoom");
166 assert_eq!(toast_text(300, 400), "400% \u{b7} Maximum zoom");
167 }
168
169 #[test]
170 fn the_menu_rounds_the_level_to_five_like_teams() {
171 assert_eq!(menu_level(1.), "(100)%");
172 assert_eq!(menu_level(0.67), "(65)%");
173 assert_eq!(menu_level(1.75), "(175)%");
174 }
175
176 #[test]
177 fn reset_always_returns_to_the_default_size() {
178 assert_eq!(WindowZoom::Reset.next(2.5), 1.);
179 assert_eq!(WindowZoom::In.next(1.), 1.1);
180 assert_eq!(WindowZoom::Out.next(1.), 0.9);
181 }
182}