BlueGit

ict rusty-teams

Files

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

title_bar.rs

title_bar.rs 9.6 KiB Download Raw
1use std::sync::{Arc, OnceLock};
2
3use gpui_kit::assets::IconName;
4use gpui_kit::component::{TitleBar, h_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7
8use super::widgets::icon;
9use crate::app_state::Mode;
10use crate::appearance::AppIcon;
11use crate::theme;
12use crate::updater::UpdateStatus;
13
14pub const TITLE_BAR_HEIGHT: f32 = 40.;
15const SEARCH_WIDTH: f32 = 380.;
16const SEARCH_MIN_WIDTH: f32 = 240.;
17const SEARCH_MIN_VISIBLE_WIDTH: f32 = 36.;
18const SEARCH_HEIGHT: f32 = 28.;
19const SEARCH_GAP: f32 = 8.;
20const SHORTCUT_MIN_WIDTH: f32 = 200.;
21const SEARCH_HINT: &str = "Search chats, people and channels";
22const SHORTCUT_HINT: &str = "Ctrl K";
23const APP_ICON_SIZE: f32 = 18.;
24
25pub fn app_icon_image(app_icon: AppIcon) -> Arc<Image> {
26 static IMAGES: [OnceLock<Arc<Image>>; AppIcon::ALL.len()] =
27 [const { OnceLock::new() }; AppIcon::ALL.len()];
28 let index = AppIcon::ALL
29 .iter()
30 .position(|candidate| *candidate == app_icon)
31 .unwrap_or(0);
32 IMAGES[index]
33 .get_or_init(|| Arc::new(Image::from_bytes(ImageFormat::Png, app_icon.png().to_vec())))
34 .clone()
35}
36
37fn mode_pill(label: &'static str) -> Div {
38 div()
39 .debug_selector(|| "shell/title-bar/mode-pill".into())
40 .flex_none()
41 .px(px(8.))
42 .h(px(18.))
43 .flex()
44 .items_center()
45 .rounded_full()
46 .bg(theme::amber().opacity(0.14))
47 .text_size(px(11.))
48 .font_weight(FontWeight::SEMIBOLD)
49 .text_color(theme::amber())
50 .child(label)
51}
52
53pub fn mode_label(mode: Mode) -> Option<&'static str> {
54 if mode.demo {
55 Some("Demo")
56 } else if mode.read_only {
57 Some("Read-only")
58 } else {
59 None
60 }
61}
62
63pub fn update_button(
64 update: &UpdateStatus,
65 on_restart: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
66) -> Option<AnyElement> {
67 let installing = match update {
68 UpdateStatus::Ready => false,
69 UpdateStatus::Installing => true,
70 UpdateStatus::UpToDate | UpdateStatus::Failed(_) => return None,
71 };
72 Some(
73 div()
74 .id("title-update-restart")
75 .debug_selector(|| "shell/title-bar/update".into())
76 .flex_none()
77 .h(px(24.))
78 .px(px(10.))
79 .flex()
80 .items_center()
81 .rounded(px(6.))
82 .bg(theme::accent())
83 .text_color(theme::on_accent())
84 .text_size(px(12.))
85 .font_weight(FontWeight::SEMIBOLD)
86 .when(!installing, |button| {
87 button
88 .cursor_pointer()
89 .hover(|button| button.bg(theme::accent_text()))
90 .on_click(on_restart)
91 })
92 .when(installing, |button| button.opacity(0.7))
93 .child(if installing {
94 "Restarting..."
95 } else {
96 "Restart to update"
97 })
98 .into_any_element(),
99 )
100}
101
102#[derive(Clone, Copy, Debug, PartialEq)]
103struct SearchSpan {
104 left: f32,
105 width: f32,
106}
107
108fn search_span(window_width: f32, leading_end: f32, trailing_start: f32) -> Option<SearchSpan> {
109 let start = leading_end + SEARCH_GAP;
110 let end = trailing_start - SEARCH_GAP;
111 let room = end - start;
112 if room < SEARCH_MIN_VISIBLE_WIDTH {
113 return None;
114 }
115 let center = window_width / 2.;
116 let centered_width = (2. * (center - start).min(end - center)).min(SEARCH_WIDTH);
117 let width = centered_width.max(SEARCH_MIN_WIDTH).min(room);
118 let left = (center - width / 2.).clamp(start, end - width);
119 Some(SearchSpan { left, width })
120}
121
122#[derive(Clone, Copy, PartialEq)]
123struct Reserved {
124 leading_end: f32,
125 trailing_from_right: f32,
126}
127
128#[derive(IntoElement)]
129struct TitleBarRow {
130 app_icon: AppIcon,
131 mode_label: Option<&'static str>,
132 update: Option<AnyElement>,
133 own: Option<AnyElement>,
134 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
135}
136
137pub fn render_title_bar(
138 app_icon: AppIcon,
139 mode_label: Option<&'static str>,
140 update: Option<AnyElement>,
141 own: Option<AnyElement>,
142 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
143) -> impl IntoElement {
144 TitleBarRow {
145 app_icon,
146 mode_label,
147 update,
148 own,
149 on_search: Box::new(on_search),
150 }
151}
152
153fn search_box(
154 span: SearchSpan,
155 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
156) -> impl IntoElement {
157 h_flex()
158 .id("title-search")
159 .debug_selector(|| "shell/title-bar/search".into())
160 .occlude()
161 .absolute()
162 .top(px((TITLE_BAR_HEIGHT - SEARCH_HEIGHT) / 2.))
163 .left(px(span.left))
164 .w(px(span.width))
165 .h(px(SEARCH_HEIGHT))
166 .px(px(10.))
167 .gap(px(8.))
168 .items_center()
169 .overflow_hidden()
170 .rounded(px(6.))
171 .bg(theme::surface())
172 .cursor_pointer()
173 .text_size(px(12.))
174 .text_color(theme::text_muted())
175 .child(
176 div()
177 .flex_none()
178 .child(icon(IconName::Search, 14., theme::text_muted())),
179 )
180 .child(div().flex_1().min_w_0().truncate().child(SEARCH_HINT))
181 .when(span.width >= SHORTCUT_MIN_WIDTH, |search| {
182 search.child(
183 div()
184 .flex_none()
185 .px(px(6.))
186 .py(px(1.))
187 .rounded(px(4.))
188 .bg(theme::surface_raised())
189 .text_size(px(11.))
190 .child(SHORTCUT_HINT),
191 )
192 })
193 .on_click(move |event, window, cx| on_search(event, window, cx))
194}
195
196impl RenderOnce for TitleBarRow {
197 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
198 let reserved = window.use_keyed_state("title-bar-reserved", cx, |_, _| None::<Reserved>);
199 let window_width = f32::from(window.viewport_size().width);
200 let span = (*reserved.read(cx)).and_then(|reserved| {
201 search_span(
202 window_width,
203 reserved.leading_end,
204 window_width - reserved.trailing_from_right,
205 )
206 });
207 let leading = h_flex()
208 .debug_selector(|| "shell/title-bar/leading".into())
209 .flex_none()
210 .h_full()
211 .gap(px(12.))
212 .items_center()
213 .child(
214 img(app_icon_image(self.app_icon))
215 .size(px(APP_ICON_SIZE))
216 .flex_none(),
217 )
218 .child(
219 div()
220 .text_size(px(13.))
221 .font_weight(FontWeight::SEMIBOLD)
222 .text_color(theme::text())
223 .child(app_base::APP_NAME),
224 )
225 .children(self.mode_label.map(mode_pill));
226 let trailing = h_flex()
227 .debug_selector(|| "shell/title-bar/trailing".into())
228 .flex_none()
229 .h_full()
230 .gap(px(12.))
231 .items_center()
232 .children(self.update)
233 .children(self.own)
234 .child(div().w(px(4.)));
235 let row = h_flex()
236 .flex_1()
237 .h_full()
238 .gap(px(12.))
239 .items_center()
240 .child(leading)
241 .child(div().flex_1())
242 .child(trailing)
243 .on_children_prepainted(move |children, window, cx| {
244 let [leading, _, trailing] = children[..] else {
245 return;
246 };
247 let measured = Reserved {
248 leading_end: f32::from(leading.right()),
249 trailing_from_right: f32::from(window.viewport_size().width - trailing.left()),
250 };
251 reserved.update(cx, |current, cx| {
252 let unchanged = current.is_some_and(|current| {
253 (current.leading_end - measured.leading_end).abs() < 0.5
254 && (current.trailing_from_right - measured.trailing_from_right).abs()
255 < 0.5
256 });
257 if !unchanged {
258 *current = Some(measured);
259 cx.notify();
260 }
261 });
262 });
263 let bar = TitleBar::new()
264 .h(px(TITLE_BAR_HEIGHT))
265 .border_b_0()
266 .bg(theme::background())
267 .child(row);
268 let on_search = self.on_search;
269 div()
270 .debug_selector(|| "shell/title-bar".into())
271 .relative()
272 .flex_none()
273 .w_full()
274 .child(bar)
275 .children(span.map(|span| search_box(span, on_search)))
276 }
277}
278
279#[cfg(test)]
280mod tests {
281 use super::{SEARCH_MIN_WIDTH, SEARCH_WIDTH, search_span};
282
283 #[test]
284 fn search_stays_centered_while_it_fits() {
285 let span = search_span(1280., 200., 1080.).expect("visible");
286 assert_eq!(span.width, SEARCH_WIDTH);
287 assert_eq!(span.left + span.width / 2., 640.);
288 }
289
290 #[test]
291 fn search_shrinks_centered_before_it_shifts() {
292 let span = search_span(800., 200., 580.).expect("visible");
293 assert_eq!(span.left + span.width / 2., 400.);
294 assert_eq!(span.width, 344.);
295 let shifted = search_span(800., 200., 500.).expect("visible");
296 assert_eq!(shifted.width, SEARCH_MIN_WIDTH);
297 assert_eq!(shifted.left + shifted.width, 492.);
298 }
299
300 #[test]
301 fn search_takes_the_room_left_and_hides_without_any() {
302 let span = search_span(640., 200., 400.).expect("visible");
303 assert_eq!((span.left, span.width), (208., 184.));
304 assert_eq!(search_span(640., 300., 340.), None);
305 }
306}