Skip to main content

fc_ui/capabilities/navigation/
keyboard_shortcuts.rs

1//! Keyboard shortcuts manager component with categorized shortcuts and platform-specific key display.
2
3use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6#[derive(Clone, Debug)]
7pub struct ShortcutItem {
8    pub description: SharedString,
9    pub keys: SharedString,
10    pub icon: Option<SharedString>,
11}
12
13impl ShortcutItem {
14    pub fn new(description: impl Into<SharedString>, keys: impl Into<SharedString>) -> Self {
15        Self {
16            description: description.into(),
17            keys: keys.into(),
18            icon: None,
19        }
20    }
21
22    pub fn with_icon(mut self, icon: impl Into<SharedString>) -> Self {
23        self.icon = Some(icon.into());
24        self
25    }
26
27    pub fn formatted_keys(&self) -> String {
28        let keys = self.keys.to_string();
29
30        #[cfg(target_os = "macos")]
31        {
32            keys.replace("cmd", "⌘")
33                .replace("shift", "⇧")
34                .replace("alt", "⌥")
35                .replace("ctrl", "⌃")
36                .replace("-", " ")
37        }
38
39        #[cfg(not(target_os = "macos"))]
40        {
41            keys.replace("cmd", "Ctrl")
42                .replace("shift", "Shift")
43                .replace("alt", "Alt")
44                .replace("ctrl", "Ctrl")
45                .replace("-", "+")
46        }
47    }
48}
49
50#[derive(Clone, Debug)]
51pub struct ShortcutCategory {
52    pub name: SharedString,
53    pub shortcuts: Vec<ShortcutItem>,
54    pub expanded: bool,
55}
56
57impl ShortcutCategory {
58    pub fn new(name: impl Into<SharedString>, shortcuts: Vec<ShortcutItem>) -> Self {
59        Self {
60            name: name.into(),
61            shortcuts,
62            expanded: true,
63        }
64    }
65}
66
67pub struct KeyboardShortcuts {
68    categories: Vec<ShortcutCategory>,
69    show_icons: bool,
70    style: StyleRefinement,
71}
72
73impl KeyboardShortcuts {
74    pub fn new() -> Self {
75        Self {
76            categories: Vec::new(),
77            show_icons: false,
78            style: StyleRefinement::default(),
79        }
80    }
81
82    pub fn category(mut self, name: impl Into<SharedString>, shortcuts: Vec<ShortcutItem>) -> Self {
83        self.categories.push(ShortcutCategory::new(name, shortcuts));
84        self
85    }
86
87    pub fn add_category(mut self, category: ShortcutCategory) -> Self {
88        self.categories.push(category);
89        self
90    }
91
92    pub fn show_icons(mut self, show: bool) -> Self {
93        self.show_icons = show;
94        self
95    }
96}
97
98impl Default for KeyboardShortcuts {
99    fn default() -> Self {
100        Self::new()
101    }
102}
103
104impl Styled for KeyboardShortcuts {
105    fn style(&mut self) -> &mut StyleRefinement {
106        &mut self.style
107    }
108}
109
110impl Render for KeyboardShortcuts {
111    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
112        let theme = use_theme(cx);
113        let user_style = self.style.clone();
114
115        div()
116            .flex()
117            .flex_col()
118            .gap(px(24.0))
119            .children(self.categories.iter().map(|category| {
120                div()
121                    .flex()
122                    .flex_col()
123                    .gap(px(12.0))
124                    .child(
125                        div()
126                            .text_size(px(18.0))
127                            .font_weight(FontWeight::SEMIBOLD)
128                            .text_color(theme.tokens.foreground)
129                            .child(category.name.clone()),
130                    )
131                    .child(div().flex().flex_col().gap(px(8.0)).children(
132                        category.shortcuts.iter().map(|shortcut| {
133                            div()
134                                .flex()
135                                .items_center()
136                                .justify_between()
137                                .px(px(12.0))
138                                .py(px(8.0))
139                                .rounded(theme.tokens.radius_sm)
140                                .hover(|style| style.bg(theme.tokens.muted.opacity(0.5)))
141                                .child(
142                                    div().flex().items_center().gap(px(8.0)).child(
143                                        div()
144                                            .text_size(px(14.0))
145                                            .text_color(theme.tokens.foreground)
146                                            .child(shortcut.description.clone()),
147                                    ),
148                                )
149                                .child(
150                                    div().flex().items_center().gap(px(4.0)).child(
151                                        div()
152                                            .px(px(8.0))
153                                            .py(px(4.0))
154                                            .rounded(theme.tokens.radius_sm)
155                                            .bg(theme.tokens.muted)
156                                            .border_1()
157                                            .border_color(theme.tokens.border)
158                                            .text_size(px(12.0))
159                                            .font_family("monospace")
160                                            .text_color(theme.tokens.muted_foreground)
161                                            .child(shortcut.formatted_keys()),
162                                    ),
163                                )
164                                .into_any_element()
165                        }),
166                    ))
167                    .into_any_element()
168            }))
169            .map(|this| {
170                let mut div = this;
171                div.style().refine(&user_style);
172                div
173            })
174    }
175}