BlueGit

ict rusty-teams

Files

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

shortcuts_dialog.rs

shortcuts_dialog.rs 4.8 KiB Download Raw
1use gpui_kit::component::scroll::ScrollableElement as _;
2use gpui_kit::component::{h_flex, v_flex};
3use gpui_kit::*;
4
5use super::shortcuts::{Group, shortcuts_in};
6use super::widgets::symbol;
7use crate::i18n::tr;
8use crate::theme::{self, ElevatedSurface as _};
9
10const CARD_WIDTH: f32 = 520.;
11const CARD_RADIUS: f32 = 10.;
12const CARD_PADDING: f32 = 16.;
13const CARD_MARGIN: f32 = 32.;
14const SECTION_GAP: f32 = 12.;
15const ROW_GAP: f32 = 6.;
16const BACKDROP_OPACITY: f32 = 0.55;
17const CLOSE_SIZE: f32 = 28.;
18
19pub enum ShortcutsDialogEvent {
20 Close,
21}
22
23pub struct ShortcutsDialog {
24 focus_handle: FocusHandle,
25}
26
27impl EventEmitter<ShortcutsDialogEvent> for ShortcutsDialog {}
28
29impl ShortcutsDialog {
30 pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
31 let focus_handle = cx.focus_handle();
32 window.focus(&focus_handle, cx);
33 ShortcutsDialog { focus_handle }
34 }
35
36 fn on_key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
37 if event.keystroke.key != "escape" {
38 return;
39 }
40 cx.emit(ShortcutsDialogEvent::Close);
41 cx.stop_propagation();
42 }
43
44 fn header(&self, cx: &mut Context<Self>) -> Div {
45 h_flex()
46 .w_full()
47 .items_center()
48 .gap(px(SECTION_GAP))
49 .child(
50 div()
51 .flex_1()
52 .text_size(px(15.))
53 .font_weight(FontWeight::SEMIBOLD)
54 .child(tr("shortcuts.title")),
55 )
56 .child(
57 div()
58 .id("shortcuts-close")
59 .size(px(CLOSE_SIZE))
60 .flex_none()
61 .flex()
62 .items_center()
63 .justify_center()
64 .rounded_full()
65 .cursor_pointer()
66 .hover(|button| button.bg(theme::popup_hover()))
67 .on_click(cx.listener(|_, _, _, cx| cx.emit(ShortcutsDialogEvent::Close)))
68 .child(symbol("close", 16., theme::text_muted())),
69 )
70 }
71}
72
73fn key_chip(keys: String) -> Div {
74 div()
75 .flex_none()
76 .px(px(8.))
77 .py(px(2.))
78 .rounded(px(6.))
79 .bg(theme::popup_hover())
80 .text_size(px(12.))
81 .text_color(theme::text_muted())
82 .child(keys)
83}
84
85fn group_block(group: Group) -> Div {
86 let rows = shortcuts_in(group).map(|shortcut| {
87 h_flex()
88 .w_full()
89 .items_center()
90 .justify_between()
91 .gap(px(SECTION_GAP))
92 .text_size(px(13.))
93 .child(div().flex_1().min_w_0().child(shortcut.label))
94 .child(key_chip(shortcut.display_keys()))
95 });
96 v_flex()
97 .w_full()
98 .gap(px(ROW_GAP))
99 .child(
100 div()
101 .text_size(px(12.))
102 .font_weight(FontWeight::SEMIBOLD)
103 .text_color(theme::text_muted())
104 .child(group.title()),
105 )
106 .children(rows)
107}
108
109impl Render for ShortcutsDialog {
110 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
111 div()
112 .id("shortcuts-layer")
113 .absolute()
114 .top_0()
115 .left_0()
116 .size_full()
117 .flex()
118 .items_center()
119 .justify_center()
120 .child(
121 div()
122 .id("shortcuts-backdrop")
123 .absolute()
124 .top_0()
125 .left_0()
126 .size_full()
127 .bg(black().opacity(BACKDROP_OPACITY))
128 .occlude()
129 .on_click(cx.listener(|_, _, _, cx| cx.emit(ShortcutsDialogEvent::Close))),
130 )
131 .child(
132 v_flex()
133 .id("shortcuts-card")
134 .track_focus(&self.focus_handle)
135 .w(px(CARD_WIDTH))
136 .max_w(relative(1.))
137 .max_h(relative(1.))
138 .m(px(CARD_MARGIN))
139 .p(px(CARD_PADDING))
140 .gap(px(SECTION_GAP))
141 .rounded(px(CARD_RADIUS))
142 .dialog_surface()
143 .text_color(theme::text())
144 .occlude()
145 .capture_key_down(cx.listener(Self::on_key_down))
146 .child(self.header(cx))
147 .child(
148 v_flex()
149 .id("shortcuts-body")
150 .min_h_0()
151 .gap(px(SECTION_GAP))
152 .overflow_y_scrollbar()
153 .children(Group::ALL.into_iter().map(group_block)),
154 ),
155 )
156 }
157}