BlueGit

ict rusty-teams

Files

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

shortcuts_dialog.rs

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