forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

blur.rs237 lines, 10.1 KiBHistoryRawDownload
1//! Leaving a text field: a click on anything that is not text, or a chord that is not a
2//! text-editing one, takes the keyboard back to the room instead of letting the field keep it.
3
4use crate::*;
5
6/// Whether a stroke belongs to the app rather than to a field being typed into: any Ctrl or Alt
7/// chord except the editing ones (Ctrl+A, C, V, X, Z on their own).
8pub(crate) fn is_app_chord(key: &str, ctrl: bool, shift: bool, alt: bool) -> bool {
9 if !ctrl && !alt {
10 return false;
11 }
12 !(ctrl && !alt && !shift && matches!(key, "a" | "c" | "v" | "x" | "z"))
13}
14
15/// Where a stroke goes while a text field is open: into the field, or out of it to the room's
16/// keymap. Every stroke is the field's except an app chord (Ctrl+K, Alt+T, Ctrl+Y ...), and an
17/// editing chord (Ctrl+A, C, V, X, Z) is the field's too.
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub(crate) enum Route {
20 Field,
21 Room,
22}
23
24/// The dispatch rule for an open field: one place, so every field (current and future) takes its
25/// keys the same way. A field that is not open never takes a stroke.
26pub(crate) fn route(field_open: bool, key: &str, ctrl: bool, shift: bool, alt: bool) -> Route {
27 match field_open && !is_app_chord(key, ctrl, shift, alt) {
28 true => Route::Field,
29 false => Route::Room,
30 }
31}
32
33/// Escape in a search box: clears the words when there are some (the box stays open for the next
34/// words), and leaves the box when it is empty. Never passed on.
35pub(crate) fn search_escape_clears(text: &str) -> bool {
36 !text.is_empty()
37}
38
39impl Player {
40 /// Whether any text field (a rename, a path, a filter, a hex, a number) is taking keys.
41 pub(crate) fn any_text_field(&self) -> bool {
42 (self.export_open && self.export_sheet.field.is_some())
43 || self.cue_edit.is_some()
44 || self.cue_num.is_some()
45 || self.still_num.is_some()
46 || self.transition_edit.is_some()
47 || self.marker_edit.is_some()
48 || self.seq_edit.is_some()
49 || self.title_draft.is_some()
50 || self.title_hex.is_some()
51 || self.gen_hex.is_some()
52 || self.accent_hex.is_some()
53 || self.lane_edit.is_some()
54 || self.time_edit.is_some()
55 || self.look_name.is_some()
56 || self.bin_edit.is_some()
57 || crate::ui::cards::numedit::typing()
58 || self.tools.as_ref().is_some_and(|t| t.typing)
59 || self.dock_filter_edit
60 || self.transcript.search_edit
61 }
62
63 /// The one door every open text field takes the keyboard through, ahead of the palette, the
64 /// chords, the cards and the keymap. `true` when a field took the stroke; `false` when no
65 /// field is open or an app chord left the field (the field is closed and the chord runs).
66 pub(crate) fn text_field_key(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) -> bool {
67 let key = event.keystroke.key.as_str();
68 let (ctrl, shift, alt) = (event.keystroke.modifiers.control, event.keystroke.modifiers.shift, event.keystroke.modifiers.alt);
69 let key_char = event.keystroke.key_char.as_deref();
70 if route(self.any_text_field(), key, ctrl, shift, alt) == Route::Room {
71 if self.any_text_field() {
72 self.blur_text_fields(window, cx);
73 }
74 return false;
75 }
76 if self.export_open && self.export_sheet.field.is_some() {
77 self.export_field_key(key, ctrl, alt, key_char, cx);
78 } else if self.cue_edit.is_some() {
79 self.cue_edit_key(key, ctrl, shift, alt, key_char, cx);
80 } else if self.time_edit.is_some() {
81 self.time_edit_key(key, ctrl, alt, key_char, cx);
82 } else if self.look_name.is_some() {
83 self.look_name_key(key, ctrl, alt, key_char, cx);
84 } else if self.transition_edit.is_some() {
85 self.transition_key(key, cx);
86 } else if self.cue_num.is_some() {
87 self.cue_num_key(key, ctrl, alt, key_char, cx);
88 } else if self.still_num.is_some() {
89 self.still_num_key(key, ctrl, alt, key_char, cx);
90 } else if self.marker_edit.is_some() {
91 self.marker_edit_key(key, ctrl, alt, key_char, cx);
92 } else if self.seq_edit.is_some() {
93 self.seq_edit_key(key, ctrl, alt, key_char, cx);
94 } else if self.title_draft.is_some() || self.title_hex.is_some() {
95 self.title_key(key, ctrl, shift, alt, key_char, cx);
96 } else if self.gen_hex.is_some() {
97 self.gen_key(key, ctrl, alt, key_char, cx);
98 } else if self.accent_hex.is_some() {
99 self.accent_key(key, ctrl, alt, key_char, cx);
100 } else if self.lane_edit.is_some() {
101 self.lane_edit_key(key, ctrl, alt, key_char, cx);
102 } else if self.bin_edit.is_some() {
103 self.bin_edit_key(key, ctrl, alt, key_char, cx);
104 } else if crate::ui::cards::numedit::typing() {
105 crate::ui::cards::numedit::key(self, key, ctrl, alt, key_char, cx);
106 } else if self.tools.as_ref().is_some_and(|t| t.typing) {
107 self.tools_type_key(key, ctrl, alt, key_char, cx);
108 } else if self.dock_filter_edit {
109 self.dock_filter_key(key, ctrl, alt, key_char, window, cx);
110 } else if self.transcript.search_edit {
111 self.transcript_key(key, ctrl, shift, alt, true, cx);
112 }
113 cx.notify();
114 true
115 }
116
117 /// The Sources search box: letters narrow the list, Escape clears the words and then leaves
118 /// the box, and no stroke is passed on.
119 fn dock_filter_key(
120 &mut self,
121 key: &str,
122 ctrl: bool,
123 alt: bool,
124 key_char: Option<&str>,
125 window: &mut Window,
126 cx: &mut Context<Self>,
127 ) {
128 match key {
129 "escape" if search_escape_clears(&self.dock_filter) => self.dock_filter.clear(),
130 "escape" | "enter" => {
131 self.dock_filter_edit = false;
132 window.focus(&self.focus);
133 }
134 "backspace" => {
135 self.dock_filter.pop();
136 }
137 _ => {
138 if let Some(chars) = key_char.filter(|_| !ctrl && !alt) {
139 self.dock_filter.extend(chars.chars().filter(|c| !c.is_control()));
140 }
141 }
142 }
143 cx.notify();
144 }
145
146 /// Takes every open field's text as it stands and gives the keyboard back to the room. An
147 /// export name or folder that is refused stays open with its note: the typed text is never
148 /// dropped, and the export is blocked until it is fixed (see `commit_pending_field`).
149 pub(crate) fn blur_text_fields(&mut self, window: &mut Window, cx: &mut Context<Self>) {
150 if self.export_sheet.field.is_some() {
151 self.commit_field();
152 }
153 if self.cue_edit.is_some() {
154 self.commit_cue_edit(cx);
155 self.cue_edit = None;
156 }
157 if self.marker_edit.is_some() {
158 self.marker_edit_key("enter", false, false, None, cx);
159 self.marker_edit = None;
160 }
161 if self.seq_edit.is_some() {
162 self.seq_edit_key("enter", false, false, None, cx);
163 self.seq_edit = None;
164 }
165 // A click away commits the words, as enter does: they are written as one undo step.
166 if self.title_draft.is_some() || self.title_hex.is_some() {
167 self.commit_title_draft(cx);
168 self.title_hex = None;
169 }
170 if self.gen_hex.is_some() {
171 self.gen_key("enter", false, false, None, cx);
172 self.gen_hex = None;
173 }
174 self.commit_accent_hex(cx);
175 if self.lane_edit.is_some() {
176 self.lane_edit_key("enter", false, false, None, cx);
177 self.lane_edit = None;
178 }
179 if self.bin_edit.is_some() {
180 self.bin_edit_key("enter", false, false, None, cx);
181 self.bin_edit = None;
182 }
183 // Go to time is cancelled by a click elsewhere: the click then does its own job (a
184 // timeline click seeks), and nothing is typed into a time that was not entered.
185 self.time_edit = None;
186 // The look name stays open: its Save and Cancel buttons sit under the card, and a click on
187 // Save must still find the name there.
188 if let Some(t) = self.tools.as_mut() {
189 t.typing = false;
190 }
191 self.dock_filter_edit = false;
192 self.transcript.search_edit = false;
193 window.focus(&self.focus);
194 cx.notify();
195 }
196}
197
198#[cfg(test)]
199mod tests {
200 use super::*;
201
202 #[test]
203 fn an_open_field_takes_every_stroke_but_an_app_chord() {
204 // Letters, the arrows, Escape, Backspace and the editing chords are the field's.
205 for key in ["f", "l", "backspace", "delete", "left", "home", "escape", "enter"] {
206 assert_eq!(route(true, key, false, false, false), Route::Field, "{key}");
207 }
208 for key in ["a", "c", "v", "x", "z"] {
209 assert_eq!(route(true, key, true, false, false), Route::Field, "ctrl+{key}");
210 }
211 // An app chord leaves the field and runs.
212 assert_eq!(route(true, "k", true, false, false), Route::Room);
213 assert_eq!(route(true, "t", false, false, true), Route::Room);
214 assert_eq!(route(true, "y", true, false, false), Route::Room);
215 // No open field: nothing is taken, so the room's keymap sees every stroke.
216 assert_eq!(route(false, "f", false, false, false), Route::Room);
217 assert_eq!(route(false, "a", true, false, false), Route::Room);
218 }
219
220 #[test]
221 fn escape_in_a_search_box_clears_words_first_and_only_then_leaves() {
222 assert!(search_escape_clears("fractal"));
223 assert!(!search_escape_clears(""));
224 }
225
226 #[test]
227 fn only_text_editing_chords_stay_with_a_field() {
228 assert!(!is_app_chord("k", false, false, false));
229 assert!(is_app_chord("k", true, false, false));
230 assert!(is_app_chord("t", false, false, true));
231 assert!(is_app_chord("z", true, true, false));
232 assert!(is_app_chord("y", true, false, false));
233 for k in ["a", "c", "v", "x", "z"] {
234 assert!(!is_app_chord(k, true, false, false));
235 }
236 }
237}