BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / src / questionnaire / keyboard.rs

keyboard.rs

eef6c0a4 History
keyboard.rs 4.0 KiB Download Raw
1use gpui::{App, Entity, KeyDownEvent, Window};
2
3use super::QuestionnaireState;
4
5/// Routes a key press to the questionnaire's behavior and reports whether it
6/// was consumed. The skin installs it on the root; the contract — arrows move
7/// between answers and items, Enter confirms a filled answer, a bare letter or
8/// digit activates a shortcut — lives here so a different skin keeps it.
9pub fn handle_key_down(
10 state: &Entity<QuestionnaireState>,
11 event: &KeyDownEvent,
12 window: &mut Window,
13 cx: &mut App,
14) {
15 if window.default_prevented()
16 || event.is_held
17 || event.prefer_character_input
18 || event.keystroke.is_ime_in_progress()
19 {
20 return;
21 }
22
23 let modifiers = event.keystroke.modifiers;
24 let key = event.keystroke.key.as_str();
25 let input_focused = state.read(cx).is_current_input_focused(window);
26 let input_has_text = input_focused && state.read(cx).current_input_has_text(cx);
27 let single_radio_focused = {
28 let state = state.read(cx);
29 state
30 .current_item()
31 .and_then(|item| state.item_state(item))
32 .is_some_and(|item| !item.is_multiple())
33 && state.focused_current_choice(window).is_some()
34 };
35
36 let handled = if key == "enter" && modifiers.secondary() && modifiers.number_of_modifiers() == 1
37 {
38 state.update(cx, |state, cx| state.confirm_current(window, cx))
39 } else if modifiers.number_of_modifiers() != 0 {
40 false
41 } else if input_focused {
42 match key {
43 "enter" if focused_answer_is_filled(state, window, cx) => {
44 state.update(cx, |state, cx| state.confirm_current(window, cx))
45 }
46 "up" if !input_has_text => {
47 state.update(cx, |state, cx| state.focus_previous_answer(window, cx))
48 }
49 "down" if !input_has_text => {
50 state.update(cx, |state, cx| state.focus_next_answer(window, cx))
51 }
52 _ => false,
53 }
54 } else {
55 match key {
56 "up" => {
57 state.update(cx, |state, cx| state.focus_previous_answer(window, cx))
58 || (single_radio_focused
59 && state.update(cx, |state, cx| state.move_current_radio(-1, window, cx)))
60 }
61 "down" => {
62 state.update(cx, |state, cx| state.focus_next_answer(window, cx))
63 || (single_radio_focused
64 && state.update(cx, |state, cx| state.move_current_radio(1, window, cx)))
65 }
66 "left" if single_radio_focused => {
67 state.update(cx, |state, cx| state.move_current_radio(-1, window, cx))
68 }
69 "right" if single_radio_focused => {
70 state.update(cx, |state, cx| state.move_current_radio(1, window, cx))
71 }
72 "left" => state.update(cx, |state, cx| state.go_previous(window, cx)),
73 "right" if state.read(cx).navigation_state().is_confirmable() => {
74 state.update(cx, |state, cx| state.go_next(window, cx))
75 }
76 "right" => false,
77 "enter" if focused_answer_is_filled(state, window, cx) => {
78 state.update(cx, |state, cx| state.confirm_current(window, cx))
79 }
80 "enter" => false,
81 _ => state.update(cx, |state, cx| state.activate_shortcut(key, window, cx)),
82 }
83 };
84
85 if handled {
86 window.prevent_default();
87 }
88}
89
90fn focused_answer_is_filled(state: &Entity<QuestionnaireState>, window: &Window, cx: &App) -> bool {
91 let state = state.read(cx);
92 let Some(item) = state.current_item() else {
93 return false;
94 };
95 if state.is_current_input_focused(window) {
96 return state
97 .answer(item)
98 .is_some_and(|answer| answer.freeform().is_some());
99 }
100 state
101 .focused_current_choice(window)
102 .and_then(|value| state.choice_state(item, value))
103 .is_some_and(|choice| choice.is_selected())
104}