BlueGit

ict rusty-teams

Files

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

shortcuts_dialog.rs

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