diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 18:22:18 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-14 18:23:14 -0400 |
commit | d5466b503e0ba0f88682654dc7ce28cf2f4393c8 (patch) | |
tree | 9ce09bb4100b3f12480f6db2eb951ec48e9795b4 /src/ui | |
parent | 032258fcd42761719561a43e52c1a264b92cf067 (diff) |
rework input event thread handling
- this fixes issues where joshuto steals the inputs of
terminal applications after file operations have been done
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/widgets/tui_menu.rs | 29 | ||||
-rw-r--r-- | src/ui/widgets/tui_prompt.rs | 6 | ||||
-rw-r--r-- | src/ui/widgets/tui_textfield.rs | 138 |
3 files changed, 96 insertions, 77 deletions
diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs index 5f628cc..4088d6d 100644 --- a/src/ui/widgets/tui_menu.rs +++ b/src/ui/widgets/tui_menu.rs @@ -32,6 +32,7 @@ impl TuiCommandMenu { ) -> Option<&'a Box<dyn JoshutoCommand>> { let mut map: &JoshutoCommandMapping = &m; let terminal = backend.terminal_mut(); + context.events.flush(); loop { terminal.draw(|mut frame| { @@ -73,20 +74,26 @@ impl TuiCommandMenu { TuiMenu::new(&display_str).render(&mut frame, menu_rect); } }); + if let Ok(event) = context.events.next() { match event { - Event::Input(Key::Esc) => { - return None; - } - Event::Input(key) => match map.get(&key) { - Some(CommandKeybind::SimpleKeybind(s)) => { - return Some(s); + Event::Input(key) => { + match key { + Key::Esc => return None, + key => { + match map.get(&key) { + Some(CommandKeybind::SimpleKeybind(s)) => { + return Some(s); + } + Some(CommandKeybind::CompositeKeybind(m)) => { + map = m; + } + None => return None, + } + } } - Some(CommandKeybind::CompositeKeybind(m)) => { - map = m; - } - None => return None, - }, + context.events.flush(); + } _ => {} } } diff --git a/src/ui/widgets/tui_prompt.rs b/src/ui/widgets/tui_prompt.rs index dd947a9..1f4826f 100644 --- a/src/ui/widgets/tui_prompt.rs +++ b/src/ui/widgets/tui_prompt.rs @@ -21,6 +21,8 @@ impl<'a> TuiPrompt<'a> { pub fn get_key(&mut self, backend: &mut TuiBackend, context: &JoshutoContext) -> Key { let terminal = backend.terminal_mut(); + + context.events.flush(); loop { terminal.draw(|mut frame| { let f_size = frame.size(); @@ -52,7 +54,9 @@ impl<'a> TuiPrompt<'a> { if let Ok(event) = context.events.next() { match event { - Event::Input(key) => return key, + Event::Input(key) => { + return key; + } _ => {} }; } diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs index 602fa5d..c663b95 100644 --- a/src/ui/widgets/tui_textfield.rs +++ b/src/ui/widgets/tui_textfield.rs @@ -64,6 +64,8 @@ impl<'a> TuiTextField<'a> { backend: &mut TuiBackend, context: &JoshutoContext, ) -> Option<String> { + context.events.flush(); + let mut line_buffer = line_buffer::LineBuffer::with_capacity(255); let completer = FilenameCompleter::new(); @@ -140,74 +142,80 @@ impl<'a> TuiTextField<'a> { if let Ok(event) = context.events.next() { match event { - Event::Input(Key::Backspace) => { - if line_buffer.backspace(1) { - completion_tracker.take(); - } - } - Event::Input(Key::Left) => { - if line_buffer.move_backward(1) { - completion_tracker.take(); - } - } - Event::Input(Key::Right) => { - if line_buffer.move_forward(1) { - completion_tracker.take(); - } - } - Event::Input(Key::Delete) => { - if line_buffer.delete(1).is_some() { - completion_tracker.take(); - } - } - Event::Input(Key::Home) => { - line_buffer.move_home(); - completion_tracker.take(); - } - Event::Input(Key::End) => { - line_buffer.move_end(); - completion_tracker.take(); - } - Event::Input(Key::Up) => {} - Event::Input(Key::Down) => {} - Event::Input(Key::Esc) => { - terminal.hide_cursor(); - return None; - } - Event::Input(Key::Char('\t')) => { - if completion_tracker.is_none() { - let res = - completer.complete_path(line_buffer.as_str(), line_buffer.pos()); - if let Ok((pos, mut candidates)) = res { - candidates.sort_by(|x, y| { - x.display() - .partial_cmp(y.display()) - .unwrap_or(std::cmp::Ordering::Less) - }); - let ct = CompletionTracker::new( - pos, - candidates, - String::from(line_buffer.as_str()), - ); - completion_tracker = Some(ct); + Event::Input(key) => { + match key { + Key::Backspace => { + if line_buffer.backspace(1) { + completion_tracker.take(); + } } - } - - if let Some(ref mut s) = completion_tracker { - if s.index < s.candidates.len() { - let candidate = &s.candidates[s.index]; - completer.update(&mut line_buffer, s.pos, candidate.replacement()); - s.index += 1; + Key::Left => { + if line_buffer.move_backward(1) { + completion_tracker.take(); + } } + Key::Right => { + if line_buffer.move_forward(1) { + completion_tracker.take(); + } + } + Key::Delete => { + if line_buffer.delete(1).is_some() { + completion_tracker.take(); + } + } + Key::Home => { + line_buffer.move_home(); + completion_tracker.take(); + } + Key::End => { + line_buffer.move_end(); + completion_tracker.take(); + } + Key::Up => {} + Key::Down => {} + Key::Esc => { + terminal.hide_cursor(); + return None; + } + Key::Char('\t') => { + if completion_tracker.is_none() { + let res = + completer.complete_path(line_buffer.as_str(), line_buffer.pos()); + if let Ok((pos, mut candidates)) = res { + candidates.sort_by(|x, y| { + x.display() + .partial_cmp(y.display()) + .unwrap_or(std::cmp::Ordering::Less) + }); + let ct = CompletionTracker::new( + pos, + candidates, + String::from(line_buffer.as_str()), + ); + completion_tracker = Some(ct); + } + } + + if let Some(ref mut s) = completion_tracker { + if s.index < s.candidates.len() { + let candidate = &s.candidates[s.index]; + completer.update(&mut line_buffer, s.pos, candidate.display()); + s.index += 1; + } + } + } + Key::Char('\n') => { + break; + } + Key::Char(c) => { + if line_buffer.insert(c, 1).is_some() { + completion_tracker.take(); + } + } + _ => {} } - } - Event::Input(Key::Char('\n')) => { - break; - } - Event::Input(Key::Char(c)) => { - if line_buffer.insert(c, 1).is_some() { - completion_tracker.take(); - } + context.events.flush(); } _ => {} }; |