Coverage Report

Created: 2026-12-05 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
super-voice-gpui/src/shell/sidebar.rs
Line
Count
Source
1
// SPDX-License-Identifier: GPL-3.0-only
2
//! The pages, grouped per feature: Speech to text, Text to speech, General.
3
//!
4
//! In a wide window, the kit's `Sidebar` with labelled groups, each feature's
5
//! readiness a pill beside its heading, and "Go to or run…" under the app's
6
//! name. In a compact window, the same sidebar collapsed to a 48 px rail of
7
//! icons: the headings hide, so each feature's readiness is a bare dot on its
8
//! Models icon (the status bar says it in words) and each tooltip names the
9
//! feature ("Text to speech › Models"). In a narrow window, the expanded
10
//! groups in a drawer.
11
12
use std::rc::Rc;
13
14
use gpui_kit::component::badge::Badge;
15
use gpui_kit::component::button::Button;
16
use gpui_kit::component::kbd::Kbd;
17
use gpui_kit::component::sidebar::{
18
    Sidebar, SidebarCollapsible, SidebarHeader, SidebarItem, SidebarMenu, SidebarMenuItem,
19
};
20
use gpui_kit::component::tooltip::Tooltip;
21
use gpui_kit::component::{ActiveTheme as _, Collapsible, Icon, IconName, h_flex, v_flex};
22
use gpui_kit::prelude::FluentBuilder as _;
23
use gpui_kit::{
24
    AnyElement, App, Context, ElementId, FontWeight, InteractiveElement as _, IntoElement,
25
    Keystroke, ParentElement as _, SharedString, StatefulInteractiveElement as _, Styled as _,
26
    WeakEntity, Window, div, img, px,
27
};
28
use super_voice_app_core::pages::readiness::{Feature, Readiness};
29
30
use super::{Shell, version};
31
use crate::assets::app_icon;
32
use crate::pages::{Page, Section, SidebarMark};
33
use crate::t;
34
use crate::ui::status::{Dot, StatusDot, StatusPill, dot_color};
35
use crate::ui::width::WindowSize;
36
37
/// What the pages' list shows, read from the shell.
38
#[derive(Clone)]
39
pub struct Nav {
40
    /// The page shown; `None` while the Connection page is.
41
    shown: Option<Page>,
42
    /// The daemon is down: the pages that need it are dimmed.
43
    down: bool,
44
    readiness: [(Feature, Readiness); 2],
45
    marks: Vec<(Page, SidebarMark)>,
46
    /// Finish setup is listed: something macOS has to allow is missing.
47
    setup_row: bool,
48
}
49
50
impl Nav {
51
0
    pub fn read(shell: &Shell, cx: &App) -> Self {
52
0
        let readiness = shell.shared.readiness.read(cx);
53
        Self {
54
0
            setup_row: shell.setup_row_shown(cx),
55
0
            shown: shell.page_shown(cx),
56
0
            down: shell.shared.connection.read(cx).is_down(),
57
0
            readiness: Feature::ALL.map(|feature| (feature, readiness.of(feature))),
58
0
            marks: shell
59
0
                .pages
60
0
                .iter()
61
0
                .filter_map(|(page, handle)| Some((*page, handle.sidebar_mark(cx)?)))
62
                .collect(),
63
        }
64
0
    }
65
66
0
    fn readiness(&self, feature: Feature) -> Readiness {
67
0
        self.readiness
68
0
            .iter()
69
0
            .find(|(f, _)| *f == feature)
70
0
            .map_or(Readiness::Unknown, |(_, r)| *r)
71
0
    }
72
73
0
    fn mark(&self, page: Page) -> Option<SidebarMark> {
74
0
        self.marks
75
0
            .iter()
76
0
            .find(|(p, _)| *p == page)
77
0
            .map(|(_, mark)| *mark)
78
0
    }
79
80
    /// The sidebar's groups, each item showing its page through `pick`.
81
    /// Finish setup's, which has no heading, only while it is listed.
82
0
    fn groups(&self, pick: &Pick) -> Vec<NavGroup> {
83
0
        Section::ALL
84
0
            .into_iter()
85
0
            .filter(|section| *section != Section::Setup || self.setup_row)
86
            .map(|section| NavGroup {
87
0
                label: t(section.key()),
88
0
                heading: section != Section::Setup,
89
0
                feature: section
90
0
                    .feature()
91
0
                    .map(|feature| (feature, self.readiness(feature))),
92
0
                collapsed: false,
93
0
                items: section
94
0
                    .pages()
95
                    .map(|page| NavItem {
96
0
                        page,
97
0
                        active: self.shown == Some(page),
98
0
                        disabled: self.down && page.needs_daemon(),
99
0
                        dot: page.feature().map(|feature| self.readiness(feature).dot()),
100
0
                        mark: self.mark(page),
101
0
                        pick: pick.clone(),
102
0
                    })
103
                    .collect(),
104
0
            })
105
            .collect()
106
0
    }
107
}
108
109
/// What picking a page does.
110
type Pick = Rc<dyn Fn(Page, &mut Window, &mut App)>;
111
112
/// The sidebar of a wide window, or its rail in a compact one.
113
0
pub fn sidebar(shell: &Shell, size: WindowSize, cx: &mut Context<Shell>) -> impl IntoElement {
114
0
    let nav = Nav::read(shell, cx);
115
0
    let collapsed = size == WindowSize::Compact;
116
0
    let weak = cx.weak_entity();
117
0
    let pick: Pick = Rc::new(move |page, window, cx| {
118
0
        _ = weak.update(cx, |shell, cx| shell.go(page, window, cx));
119
0
    });
120
0
    let header = if collapsed {
121
0
        SidebarHeader::new().child(
122
0
            div()
123
0
                .w_full()
124
0
                .flex()
125
0
                .justify_center()
126
0
                .child(img(app_icon(px(32.))).size_8()),
127
0
        )
128
    } else {
129
0
        let weak = cx.weak_entity();
130
0
        SidebarHeader::new().child(
131
0
            v_flex()
132
0
                .w_full()
133
0
                .gap_3()
134
0
                .child(brand(cx))
135
0
                .child(palette_field("sidebar-palette", weak, cx)),
136
0
        )
137
    };
138
0
    Sidebar::new("pages")
139
0
        .collapsible(SidebarCollapsible::Icon)
140
0
        .collapsed(collapsed)
141
0
        .header(header)
142
0
        .children(nav.groups(&pick))
143
0
}
144
145
/// The drawer's list of pages (narrow windows): the sidebar's groups,
146
/// expanded, without its header, which the drawer's title holds.
147
0
pub fn drawer(nav: &Nav, shell: WeakEntity<Shell>, _: &App) -> impl IntoElement {
148
0
    let pick: Pick = Rc::new(move |page, window, cx| {
149
0
        _ = shell.update(cx, |shell, cx| shell.go(page, window, cx));
150
0
    });
151
0
    Sidebar::new("drawer-pages")
152
0
        .collapsible(SidebarCollapsible::None)
153
0
        .w_full()
154
0
        .border_0()
155
0
        .children(nav.groups(&pick))
156
0
}
157
158
/// The app's icon, name and version.
159
0
pub fn brand(cx: &App) -> impl IntoElement + use<> {
160
0
    h_flex()
161
0
        .gap_2()
162
0
        .items_center()
163
0
        .min_w_0()
164
0
        .child(img(app_icon(px(32.))).size_8().flex_shrink_0())
165
0
        .child(
166
0
            v_flex()
167
0
                .min_w_0()
168
0
                .child(
169
0
                    div()
170
0
                        .text_sm()
171
0
                        .font_weight(FontWeight::SEMIBOLD)
172
0
                        .child(t("app-title")),
173
0
                )
174
0
                .child(
175
0
                    // Normal weight, also in the drawer's title, which is
176
0
                    // semibold.
177
0
                    div()
178
0
                        .text_xs()
179
0
                        .font_weight(FontWeight::NORMAL)
180
0
                        .text_color(cx.theme().muted_foreground)
181
0
                        .child(version()),
182
0
                ),
183
0
        )
184
0
}
185
186
/// "Go to or run…" with its keys: opens the palette.
187
0
pub fn palette_field(id: &'static str, shell: WeakEntity<Shell>, cx: &App) -> Button {
188
0
    let muted = cx.theme().muted_foreground;
189
0
    Button::new(id)
190
0
        .w_full()
191
0
        .child(
192
0
            h_flex()
193
0
                .w_full()
194
0
                .gap_2()
195
0
                .items_center()
196
0
                .text_color(muted)
197
0
                .child(Icon::new(IconName::Search).size_3p5())
198
0
                .child(div().flex_1().child(t("go-to-or-run")))
199
0
                .child(Kbd::new(
200
0
                    Keystroke::parse(super::PALETTE_KEYS).expect("the palette's keys"),
201
0
                )),
202
0
        )
203
0
        .on_click(move |_, window, cx| {
204
0
            _ = shell.update(cx, |shell, cx| shell.open_palette(window, cx));
205
0
        })
206
0
}
207
208
/// A group of the sidebar: a feature's pages, or General's.
209
#[derive(Clone)]
210
struct NavGroup {
211
    label: SharedString,
212
    /// Whether the group shows its heading: Finish setup's has none.
213
    heading: bool,
214
    /// The feature and its readiness: its icon before the heading, its pill
215
    /// after.
216
    feature: Option<(Feature, Readiness)>,
217
    collapsed: bool,
218
    items: Vec<NavItem>,
219
}
220
221
/// A page in the sidebar.
222
#[derive(Clone)]
223
struct NavItem {
224
    page: Page,
225
    active: bool,
226
    disabled: bool,
227
    /// The feature's dot, on a Models icon in the rail.
228
    dot: Option<Dot>,
229
    mark: Option<SidebarMark>,
230
    pick: Pick,
231
}
232
233
impl NavItem {
234
    /// The kit's item, as the expanded sidebar has it.
235
0
    fn menu_item(self) -> SidebarMenuItem {
236
0
        let pick = self.pick.clone();
237
0
        let page = self.page;
238
0
        SidebarMenuItem::new(t(page.key()))
239
0
            .icon(page.icon())
240
0
            .accessibility_label(page.full_name())
241
0
            .active(self.active)
242
0
            .disable(self.disabled)
243
0
            .on_click(move |_, window, cx| pick(page, window, cx))
244
0
            .when_some(self.mark, |item, mark| {
245
0
                item.suffix(move |_, cx| mark_element(mark, cx))
246
0
            })
247
0
            .when(self.mark.is_none() && page.is_preview(), |item| {
248
0
                item.suffix(|_, _| crate::ui::page_header::preview_tag().into_any_element())
249
0
            })
250
0
    }
251
252
    /// The rail's icon: the kit's collapsed item, drawn here so the feature's
253
    /// dot can sit on its icon. Same size, colours and tooltip.
254
0
    fn rail_item(self, id: ElementId, cx: &App) -> AnyElement {
255
0
        let theme = cx.theme();
256
0
        let pick = self.pick.clone();
257
0
        let page = self.page;
258
0
        let name = page.full_name();
259
0
        let icon: Icon = page.icon();
260
0
        let badge = match (self.dot, self.mark) {
261
0
            (Some(dot), _) => Some(dot_color(dot, cx)),
262
0
            (None, Some(SidebarMark::Update)) => Some(theme.info),
263
0
            (None, Some(SidebarMark::NeedsSetup)) => Some(dot_color(Dot::Yellow, cx)),
264
0
            _ => None,
265
        };
266
0
        let icon = match badge {
267
0
            Some(color) => Badge::new()
268
0
                .dot()
269
0
                .color(color)
270
0
                .child(icon)
271
0
                .into_any_element(),
272
0
            None => icon.into_any_element(),
273
        };
274
0
        let accent = theme.tokens.sidebar_accent;
275
0
        let accent_text = theme.sidebar_accent_foreground;
276
0
        let muted = theme.muted_foreground;
277
0
        div()
278
0
            .id(id)
279
0
            .w_full()
280
            .child(
281
0
                h_flex()
282
0
                    .id("item")
283
0
                    .w_full()
284
0
                    .h(px(28.))
285
0
                    .justify_center()
286
0
                    .rounded(theme.radius)
287
0
                    .text_sm()
288
0
                    .child(icon)
289
0
                    .when(self.active, |row| row.bg(accent).text_color(accent_text))
290
0
                    .when(!self.active && !self.disabled, |row| {
291
0
                        row.hover(|row| row.bg(accent.opacity(0.8)).text_color(accent_text))
292
0
                    })
293
0
                    .when(self.disabled, |row| row.text_color(muted))
294
0
                    .when(!self.disabled, |row| {
295
0
                        row.on_click(move |_, window, cx| pick(page, window, cx))
296
0
                    })
297
0
                    .tooltip(move |window, cx| Tooltip::new(name.clone()).build(window, cx)),
298
            )
299
            .into_any_element()
300
0
    }
301
}
302
303
/// A page's mark in the expanded sidebar: its count, muted, or a dot.
304
0
fn mark_element(mark: SidebarMark, cx: &App) -> AnyElement {
305
0
    match mark {
306
0
        SidebarMark::Count(0) => div().into_any_element(),
307
0
        SidebarMark::Count(count) => div()
308
0
            .text_xs()
309
0
            .text_color(cx.theme().muted_foreground)
310
0
            .child(count.to_string())
311
0
            .into_any_element(),
312
0
        SidebarMark::Update => div()
313
0
            .flex_shrink_0()
314
0
            .size(px(7.))
315
0
            .rounded_full()
316
0
            .bg(cx.theme().info)
317
0
            .into_any_element(),
318
0
        SidebarMark::NeedsSetup => StatusDot::new(Dot::Yellow).into_any_element(),
319
    }
320
0
}
321
322
impl Collapsible for NavGroup {
323
0
    fn is_collapsed(&self) -> bool {
324
0
        self.collapsed
325
0
    }
326
327
0
    fn collapsed(mut self, collapsed: bool) -> Self {
328
0
        self.collapsed = collapsed;
329
0
        self
330
0
    }
331
}
332
333
impl SidebarItem for NavGroup {
334
0
    fn render(
335
0
        self,
336
0
        id: impl Into<ElementId>,
337
0
        window: &mut Window,
338
0
        cx: &mut App,
339
0
    ) -> impl IntoElement {
340
0
        let id = id.into();
341
0
        if self.collapsed {
342
            // 12 px between the rail's groups, as between the sidebar's.
343
0
            return v_flex()
344
0
                .pb_3()
345
0
                .gap(px(1.))
346
0
                .children(self.items.into_iter().enumerate().map(|(ix, item)| {
347
0
                    item.rail_item(ElementId::Name(format!("{id}-{ix}").into()), cx)
348
0
                }))
349
                .into_any_element();
350
0
        }
351
        // The kit's `SidebarGroup` header, with the feature's icon before
352
        // the heading, which the kit's takes only words for.
353
0
        let theme = cx.theme();
354
0
        let header = h_flex()
355
0
            .flex_shrink_0()
356
0
            .h_8()
357
0
            .px_2()
358
0
            .gap_2()
359
0
            .items_center()
360
0
            .text_xs()
361
0
            .text_color(theme.sidebar_foreground.opacity(0.7))
362
0
            .when_some(self.feature, |row, (feature, _)| {
363
0
                row.child(super::status_bar::feature_icon(feature).size_3())
364
0
            })
365
            .child(
366
0
                div()
367
0
                    .flex_1()
368
0
                    .min_w_0()
369
0
                    .overflow_hidden()
370
0
                    .whitespace_nowrap()
371
0
                    .text_ellipsis()
372
0
                    .child(self.label),
373
            )
374
0
            .when_some(self.feature, |row, (_, readiness)| {
375
0
                row.child(StatusPill::new(readiness.dot(), t(readiness.key())))
376
0
            });
377
0
        let menu = SidebarMenu::new()
378
0
            .gap_0p5()
379
0
            .children(self.items.into_iter().map(NavItem::menu_item));
380
0
        v_flex()
381
0
            .when(self.heading, |group| group.child(header))
382
0
            .child(menu.render(id, window, cx))
383
            .into_any_element()
384
0
    }
385
}