Coverage Report

Created: 2026-12-05 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
super-voice-gpui/src/menus.rs
Line
Count
Source
1
// SPDX-License-Identifier: GPL-3.0-only
2
//! The menu bar on macOS: the app's menu (About, Settings, Hide, Quit), Edit
3
//! and Window.
4
//!
5
//! Only macOS gets one. On Linux GPUI keeps the menus and draws nothing, so
6
//! the app sets none there, and binds none of their keys.
7
//!
8
//! Edit's items are the kit's text field actions, under the system's own
9
//! selectors (cut:, copy:, paste:, selectAll:): macOS enables each only
10
//! while a text field can take it. Quit and Close go the way of the
11
//! window's close button ([`crate::shell::Shell::close`]): a note being
12
//! recorded is saved first, and closing the window quits the app.
13
14
use gpui_kit::component::input as text;
15
use gpui_kit::{App, Context, KeyBinding, Menu, MenuItem, OsAction, Window, actions};
16
17
use crate::pages::Page;
18
use crate::shell::{self, Shell};
19
use crate::t;
20
21
actions!(
22
    super_voice_menu,
23
    [
24
        /// Show the About page.
25
        ShowAbout,
26
        /// Show the Settings page.
27
        ShowSettings,
28
        /// Hide the app.
29
        HideApp,
30
        /// Hide the other apps.
31
        HideOthers,
32
        /// Show every app again.
33
        ShowAll,
34
        /// Close the window, which quits the app.
35
        Quit,
36
        /// Minimize the window.
37
        Minimize,
38
        /// Zoom the window.
39
        Zoom,
40
        /// Close the window, which quits the app.
41
        CloseWindow,
42
    ]
43
);
44
45
/// The menu bar's message keys.
46
#[cfg(test)]
47
pub const KEYS: &[&str] = &[
48
    "app-title",
49
    "about-super-voice",
50
    "menu-settings",
51
    "menu-hide-app",
52
    "menu-hide-others",
53
    "menu-show-all",
54
    "menu-quit-app",
55
    "menu-edit",
56
    "menu-undo",
57
    "menu-redo",
58
    "menu-cut",
59
    "copy",
60
    "menu-paste",
61
    "menu-select-all",
62
    "menu-window",
63
    "menu-minimize",
64
    "menu-zoom",
65
    "menu-close",
66
];
67
68
/// Set up the menu bar, on macOS. Call it after [`crate::setup`], so the
69
/// kit's text field keys are bound: each item shows the keys bound to it.
70
0
pub fn install(cx: &mut App) {
71
0
    if !cfg!(target_os = "macos") {
72
0
        return;
73
0
    }
74
0
    cx.on_action(|_: &ShowAbout, cx| show(Page::About, cx));
75
0
    cx.on_action(|_: &ShowSettings, cx| show(Page::Settings, cx));
76
0
    cx.on_action(|_: &HideApp, cx| cx.hide());
77
0
    cx.on_action(|_: &HideOthers, cx| cx.hide_other_apps());
78
0
    cx.on_action(|_: &ShowAll, cx| cx.unhide_other_apps());
79
0
    cx.on_action(|_: &Quit, cx| close(cx));
80
0
    cx.on_action(|_: &CloseWindow, cx| close(cx));
81
0
    cx.on_action(|_: &Minimize, cx| {
82
0
        on_shell(cx, |_, window, _| window.minimize_window());
83
0
    });
84
0
    cx.on_action(|_: &Zoom, cx| {
85
0
        on_shell(cx, |_, window, _| window.zoom_window());
86
0
    });
87
0
    cx.bind_keys([
88
0
        KeyBinding::new("cmd-,", ShowSettings, None),
89
0
        KeyBinding::new("cmd-h", HideApp, None),
90
0
        KeyBinding::new("alt-cmd-h", HideOthers, None),
91
0
        KeyBinding::new("cmd-q", Quit, None),
92
0
        KeyBinding::new("cmd-m", Minimize, None),
93
0
        KeyBinding::new("cmd-w", CloseWindow, None),
94
0
    ]);
95
0
    cx.set_menus(bar());
96
0
}
97
98
/// The menus, left to right. macOS shows the first under the app's name.
99
1
fn bar() -> Vec<Menu> {
100
1
    vec![
101
1
        Menu::new(t("app-title")).items([
102
1
            MenuItem::action(t("about-super-voice"), ShowAbout),
103
1
            MenuItem::separator(),
104
1
            MenuItem::action(t("menu-settings"), ShowSettings),
105
1
            MenuItem::separator(),
106
1
            MenuItem::action(t("menu-hide-app"), HideApp),
107
1
            MenuItem::action(t("menu-hide-others"), HideOthers),
108
1
            MenuItem::action(t("menu-show-all"), ShowAll),
109
1
            MenuItem::separator(),
110
1
            MenuItem::action(t("menu-quit-app"), Quit),
111
1
        ]),
112
1
        Menu::new(t("menu-edit")).items([
113
1
            MenuItem::os_action(t("menu-undo"), text::Undo, OsAction::Undo),
114
1
            MenuItem::os_action(t("menu-redo"), text::Redo, OsAction::Redo),
115
1
            MenuItem::separator(),
116
1
            MenuItem::os_action(t("menu-cut"), text::Cut, OsAction::Cut),
117
1
            MenuItem::os_action(t("copy"), text::Copy, OsAction::Copy),
118
1
            MenuItem::os_action(t("menu-paste"), text::Paste, OsAction::Paste),
119
1
            MenuItem::os_action(t("menu-select-all"), text::SelectAll, OsAction::SelectAll),
120
1
        ]),
121
1
        Menu::new(t("menu-window")).items([
122
1
            MenuItem::action(t("menu-minimize"), Minimize),
123
1
            MenuItem::action(t("menu-zoom"), Zoom),
124
1
            MenuItem::separator(),
125
1
            MenuItem::action(t("menu-close"), CloseWindow),
126
1
        ]),
127
    ]
128
1
}
129
130
/// Run `f` on the window's shell once the action's dispatch is done.
131
///
132
/// A menu item or its keys reach these handlers while GPUI is still
133
/// dispatching the action in the window, which it holds until then, so
134
/// the window can't be updated from here; deferred, it can.
135
0
fn on_shell(cx: &mut App, f: impl FnOnce(&mut Shell, &mut Window, &mut Context<Shell>) + 'static) {
136
0
    cx.defer(move |cx| {
137
0
        shell::with_current(cx, f);
138
0
    });
139
0
}
140
141
/// Show `page` in the window.
142
0
fn show(page: Page, cx: &mut App) {
143
0
    on_shell(cx, move |shell, window, cx| shell.go(page, window, cx));
144
0
}
145
146
/// Close the window as its close button does, which quits the app; quit at
147
/// once when no window is open. Deferred, like [`on_shell`]. Quit and Close
148
/// here both come this way, so a take being recorded is saved first.
149
0
pub(crate) fn close(cx: &mut App) {
150
0
    cx.defer(|cx| {
151
0
        if !shell::with_current(cx, Shell::close_window) {
152
0
            cx.quit();
153
0
        }
154
0
    });
155
0
}
156
157
#[cfg(test)]
158
mod tests {
159
    use super::*;
160
161
    /// Every word the menu bar shows is in the English catalog.
162
    #[test]
163
1
    fn every_item_has_its_words() {
164
1
        for key in KEYS {
165
18
            assert!(
166
18
                super_voice_app_core::i18n::LANGUAGE_LOADER.has(key),
167
                "no message {key}"
168
            );
169
        }
170
1
    }
171
172
    /// The bar is the app's menu, Edit and Window, each with its items.
173
    #[test]
174
1
    fn the_bar_has_three_menus() {
175
1
        let bar = bar();
176
3
        let names: Vec<&str> = 
bar.iter()1
.map(|menu| menu.name.as_ref()).collect();
177
1
        assert_eq!(names, ["Super Voice", "Edit", "Window"]);
178
3
        let actions = |menu: &Menu| {
179
3
            menu.items
180
3
                .iter()
181
20
                .filter(|item| matches!(item, MenuItem::Action { .. }))
182
                .count()
183
3
        };
184
1
        assert_eq!(bar.iter().map(actions).collect::<Vec<_>>(), [6, 6, 3]);
185
1
    }
186
}