super-voice-gpui/src/shell/title_bar.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The title bar: the kit's `TitleBar`, which draws the window's own buttons |
3 | | //! on Linux (client decorations) and leaves room for the traffic lights on |
4 | | //! macOS. |
5 | | //! |
6 | | //! - Wide: the app's icon and name. |
7 | | //! - Compact: the same, and "Go to or run…" on the right, which the rail has |
8 | | //! no room for. |
9 | | //! - Narrow: the drawer's button, the page's name (the page drops its own |
10 | | //! header), and a search button for the palette. |
11 | | //! |
12 | | //! In full screen on macOS, the traffic lights are hidden. A wide window's |
13 | | //! bar then holds nothing the sidebar doesn't show, so it goes ([`shown`]); |
14 | | //! a narrower one keeps it for its buttons, at the window's edge, without |
15 | | //! the room kept for the traffic lights. |
16 | | |
17 | | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
18 | | use gpui_kit::component::{ |
19 | | ActiveTheme as _, Icon, IconName, Selectable as _, Sizable as _, TitleBar, h_flex, |
20 | | }; |
21 | | use gpui_kit::prelude::FluentBuilder as _; |
22 | | use gpui_kit::{Context, FontWeight, IntoElement, ParentElement as _, Styled, div, img, px}; |
23 | | |
24 | | use super::Shell; |
25 | | use super::sidebar::palette_field; |
26 | | use crate::assets::app_icon; |
27 | | use crate::t; |
28 | | use crate::ui::width::WindowSize; |
29 | | |
30 | | /// Whether the window shows the bar: always, except a wide window in full |
31 | | /// screen on macOS. |
32 | | #[must_use] |
33 | 0 | pub fn shown(size: WindowSize, full_screen: bool) -> bool { |
34 | 0 | !(cfg!(target_os = "macos") && full_screen && size == WindowSize::Wide) |
35 | 0 | } |
36 | | |
37 | 0 | pub fn render( |
38 | 0 | shell: &Shell, |
39 | 0 | size: WindowSize, |
40 | 0 | full_screen: bool, |
41 | 0 | cx: &mut Context<Shell>, |
42 | 0 | ) -> impl IntoElement { |
43 | 0 | let left = if size == WindowSize::Narrow { |
44 | 0 | narrow_title(shell, cx).into_any_element() |
45 | | } else { |
46 | 0 | h_flex() |
47 | 0 | .gap_2() |
48 | 0 | .items_center() |
49 | 0 | .child(img(app_icon(px(16.))).size_4()) |
50 | 0 | .child( |
51 | 0 | div() |
52 | 0 | .text_sm() |
53 | 0 | .font_weight(FontWeight::MEDIUM) |
54 | 0 | .child(t("app-title")), |
55 | 0 | ) |
56 | 0 | .into_any_element() |
57 | | }; |
58 | 0 | let weak = cx.weak_entity(); |
59 | 0 | let right = match size { |
60 | 0 | WindowSize::Wide => None, |
61 | 0 | WindowSize::Compact => Some( |
62 | 0 | div() |
63 | 0 | .w(px(240.)) |
64 | 0 | .child(palette_field("title-palette", weak, cx).small()) |
65 | 0 | .into_any_element(), |
66 | 0 | ), |
67 | | WindowSize::Narrow => Some( |
68 | 0 | Button::new("title-search") |
69 | 0 | .ghost() |
70 | 0 | .small() |
71 | 0 | .icon(IconName::Search) |
72 | 0 | .tooltip(t("go-to-or-run")) |
73 | 0 | .on_click(move |_, window, cx| { |
74 | 0 | _ = weak.update(cx, |shell, cx| shell.open_palette(window, cx)); |
75 | 0 | }) |
76 | | .into_any_element(), |
77 | | ), |
78 | | }; |
79 | | // The close button asks the pages first: one with work in flight holds |
80 | | // the window until it is done (`Shell::close`). |
81 | 0 | let shell = cx.weak_entity(); |
82 | 0 | TitleBar::new() |
83 | | // The kit keeps 80 px for the traffic lights, full screen or not. |
84 | 0 | .when(cfg!(target_os = "macos") && full_screen, Styled::pl_0) |
85 | 0 | .on_close_window(move |_, window, cx| { |
86 | 0 | let close = shell |
87 | 0 | .update(cx, |shell, cx| shell.close(window, cx)) |
88 | 0 | .unwrap_or(true); |
89 | 0 | if close { |
90 | 0 | window.remove_window(); |
91 | 0 | } |
92 | 0 | }) |
93 | 0 | .child(left) |
94 | 0 | .child(h_flex().pr_1().gap_1().items_center().children(right)) |
95 | 0 | } |
96 | | |
97 | | /// The drawer's button and the page's name: "Speech to text › Notes", "General › Settings", or |
98 | | /// "Super Voice" while the Connection page shows. |
99 | 0 | fn narrow_title(shell: &Shell, cx: &mut Context<Shell>) -> impl IntoElement + use<> { |
100 | 0 | let open = shell.drawer_open; |
101 | 0 | let weak = cx.weak_entity(); |
102 | 0 | let muted = cx.theme().muted_foreground; |
103 | 0 | let toggle = Button::new("drawer") |
104 | 0 | .ghost() |
105 | 0 | .small() |
106 | 0 | .selected(open) |
107 | 0 | .icon(if open { |
108 | 0 | IconName::PanelLeftClose |
109 | | } else { |
110 | 0 | IconName::PanelLeftOpen |
111 | | }) |
112 | 0 | .tooltip(t(if open { "hide-pages" } else { "show-pages" })) |
113 | 0 | .on_click(move |_, window, cx| { |
114 | 0 | _ = weak.update(cx, |shell, cx| shell.toggle_drawer(window, cx)); |
115 | 0 | }); |
116 | 0 | let name = match shell.page_shown(cx) { |
117 | 0 | None => h_flex().child(t("app-title")), |
118 | 0 | Some(page) => h_flex() |
119 | 0 | .gap_1() |
120 | 0 | .items_center() |
121 | 0 | .min_w_0() |
122 | 0 | .child( |
123 | 0 | div() |
124 | 0 | .text_color(muted) |
125 | 0 | .whitespace_nowrap() |
126 | 0 | .child(t(page.section().key())), |
127 | 0 | ) |
128 | 0 | .child(Icon::new(IconName::ChevronRight).size_3().text_color(muted)) |
129 | 0 | .child(div().truncate().child(t(page.key()))), |
130 | | }; |
131 | 0 | h_flex() |
132 | 0 | .gap_1() |
133 | 0 | .items_center() |
134 | 0 | .min_w_0() |
135 | 0 | .child(toggle) |
136 | 0 | .child(name.text_sm().font_weight(FontWeight::MEDIUM).min_w_0()) |
137 | 0 | } |