From b54e67f757ec5c985a9df26f9d3d974aa376a59f Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 11 Dec 2025 10:21:06 +0100 Subject: [PATCH] lib/ui: Improve input request functions --- bin/commands/inbox/list.rs | 54 +++++++++++++++---------------- bin/commands/patch/list.rs | 61 +++++++++++++++++------------------- bin/commands/patch/review.rs | 14 ++++----- bin/ui/im.rs | 6 ++-- examples/hello.rs | 2 +- examples/selection.rs | 4 +-- src/ui.rs | 7 +---- src/ui/im.rs | 8 ++--- src/ui/im/widget.rs | 10 +++--- 9 files changed, 75 insertions(+), 91 deletions(-) diff --git a/bin/commands/inbox/list.rs b/bin/commands/inbox/list.rs index dccd6b9e..bfa82eb3 100644 --- a/bin/commands/inbox/list.rs +++ b/bin/commands/inbox/list.rs @@ -3,7 +3,6 @@ use std::sync::{Arc, Mutex}; use anyhow::Result; -use radicle_tui::ui::Spacing; use termion::event::Key; use ratatui::layout::Constraint; @@ -25,7 +24,7 @@ use tui::task::{Process, Task}; use tui::ui::im; use tui::ui::im::widget::{ContainerState, TableState, TextEditState, TextViewState, Window}; use tui::ui::im::{Borders, Show}; -use tui::ui::{BufferedValue, Column}; +use tui::ui::{BufferedValue, Column, Spacing}; use tui::{Channel, Exit}; use crate::ui::items::filter::Filter; @@ -302,6 +301,22 @@ impl Show for App { } }, ); + if ui.has_input(|key| key == Key::Char('?')) { + ui.send_message(Message::Changed(Change::Page { page: Page::Help })); + } + if ui.has_input(|key| key == Key::Char('\n')) { + ui.send_message(Message::Exit { + operation: Some(InboxOperation::Show), + }); + } + if ui.has_input(|key| key == Key::Char('c')) { + ui.send_message(Message::Exit { + operation: Some(InboxOperation::Clear), + }); + } + if ui.has_input(|key| key == Key::Char('r')) { + ui.send_message(Message::Reload); + } } Page::Help => { @@ -319,15 +334,15 @@ impl Show for App { ui.shortcuts(frame, &[("?", "close")], '∙'); }); - if ui.input_global(|key| key == Key::Char('?')) { + if ui.has_input(|key| key == Key::Char('?')) { ui.send_message(Message::Changed(Change::Page { page: Page::Main })); } - if ui.input_global(|key| key == Key::Char('q')) { - ui.send_message(Message::Quit); - } } } - if ui.input_global(|key| key == Key::Ctrl('c')) { + if ui.has_input(|key| key == Key::Char('q')) { + ui.send_message(Message::Quit); + } + if ui.has_input(|key| key == Key::Ctrl('c')) { ui.send_message(Message::Quit); } }); @@ -390,7 +405,7 @@ impl App { } // TODO(erikli): Should only work if table has focus - if ui.input_global(|key| key == Key::Char('/')) { + if ui.has_input(|key| key == Key::Char('/')) { ui.send_message(Message::ShowSearch); } }, @@ -402,25 +417,6 @@ impl App { self.show_browser_context(frame, ui); self.show_browser_shortcuts(frame, ui); }); - if ui.input_global(|key| key == Key::Char('q')) { - ui.send_message(Message::Quit); - } - if ui.input_global(|key| key == Key::Char('?')) { - ui.send_message(Message::Changed(Change::Page { page: Page::Help })); - } - if ui.input_global(|key| key == Key::Char('\n')) { - ui.send_message(Message::Exit { - operation: Some(InboxOperation::Show), - }); - } - if ui.input_global(|key| key == Key::Char('c')) { - ui.send_message(Message::Exit { - operation: Some(InboxOperation::Clear), - }); - } - if ui.input_global(|key| key == Key::Char('r')) { - ui.send_message(Message::Reload); - } } pub fn show_browser_search(&self, frame: &mut Frame, ui: &mut im::Ui) { @@ -446,10 +442,10 @@ impl App { ui.send_message(Message::Changed(Change::Search { search })); } - if ui.input_global(|key| key == Key::Esc) { + if ui.has_input(|key| key == Key::Esc) { ui.send_message(Message::HideSearch { apply: false }); } - if ui.input_global(|key| key == Key::Char('\n')) { + if ui.has_input(|key| key == Key::Char('\n')) { ui.send_message(Message::HideSearch { apply: true }); } } diff --git a/bin/commands/patch/list.rs b/bin/commands/patch/list.rs index 0dc19d3c..3463a684 100644 --- a/bin/commands/patch/list.rs +++ b/bin/commands/patch/list.rs @@ -285,6 +285,27 @@ impl Show for App { } }, ); + if ui.has_input(|key| key == Key::Char('?')) { + ui.send_message(Message::Changed(Change::Page { page: Page::Help })); + } + if ui.has_input(|key| key == Key::Char('\n')) { + ui.send_message(Message::ExitFromMode); + } + if ui.has_input(|key| key == Key::Char('d')) { + ui.send_message(Message::Exit { + operation: Some(PatchOperation::Diff), + }); + } + if ui.has_input(|key| key == Key::Char('r')) { + ui.send_message(Message::Exit { + operation: Some(PatchOperation::Review), + }); + } + if ui.has_input(|key| key == Key::Char('c')) { + ui.send_message(Message::Exit { + operation: Some(PatchOperation::Checkout), + }); + } } Page::Help => { @@ -302,15 +323,15 @@ impl Show for App { ui.shortcuts(frame, &[("?", "close")], '∙'); }); - if ui.input_global(|key| key == Key::Char('?')) { + if ui.has_input(|key| key == Key::Char('?')) { ui.send_message(Message::Changed(Change::Page { page: Page::Main })); } - if ui.input_global(|key| key == Key::Char('q')) { - ui.send_message(Message::Quit); - } } } - if ui.input_global(|key| key == Key::Ctrl('c')) { + if ui.has_input(|key| key == Key::Char('q')) { + ui.send_message(Message::Quit); + } + if ui.has_input(|key| key == Key::Ctrl('c')) { ui.send_message(Message::Quit); } }); @@ -367,7 +388,7 @@ impl App { } // TODO(erikli): Should only work if table has focus - if ui.input_global(|key| key == Key::Char('/')) { + if ui.has_input(|key| key == Key::Char('/')) { ui.send_message(Message::ShowSearch); } }, @@ -379,30 +400,6 @@ impl App { self.show_browser_context(frame, ui); self.show_browser_shortcuts(frame, ui); }); - if ui.input_global(|key| key == Key::Char('q')) { - ui.send_message(Message::Quit); - } - if ui.input_global(|key| key == Key::Char('?')) { - ui.send_message(Message::Changed(Change::Page { page: Page::Help })); - } - if ui.input_global(|key| key == Key::Char('\n')) { - ui.send_message(Message::ExitFromMode); - } - if ui.input_global(|key| key == Key::Char('d')) { - ui.send_message(Message::Exit { - operation: Some(PatchOperation::Diff), - }); - } - if ui.input_global(|key| key == Key::Char('r')) { - ui.send_message(Message::Exit { - operation: Some(PatchOperation::Review), - }); - } - if ui.input_global(|key| key == Key::Char('c')) { - ui.send_message(Message::Exit { - operation: Some(PatchOperation::Checkout), - }); - } } pub fn show_browser_search(&self, frame: &mut Frame, ui: &mut im::Ui) { @@ -428,10 +425,10 @@ impl App { ui.send_message(Message::Changed(Change::Search { search })); } - if ui.input_global(|key| key == Key::Esc) { + if ui.has_input(|key| key == Key::Esc) { ui.send_message(Message::HideSearch { apply: false }); } - if ui.input_global(|key| key == Key::Char('\n')) { + if ui.has_input(|key| key == Key::Char('\n')) { ui.send_message(Message::HideSearch { apply: true }); } } diff --git a/bin/commands/patch/review.rs b/bin/commands/patch/review.rs index 0442b925..39850d4b 100644 --- a/bin/commands/patch/review.rs +++ b/bin/commands/patch/review.rs @@ -489,23 +489,23 @@ impl App<'_> { '∙', ); - if ui.input_global(|key| key == Key::Char('?')) { + if ui.has_input(|key| key == Key::Char('?')) { ui.send_message(Message::ShowHelp); } - if ui.input_global(|key| key == Key::Char('c')) { + if ui.has_input(|key| key == Key::Char('c')) { ui.send_message(Message::Comment); } - if ui.input_global(|key| key == Key::Char('a')) { + if ui.has_input(|key| key == Key::Char('a')) { ui.send_message(Message::Accept); } - if ui.input_global(|key| key == Key::Char('r')) { + if ui.has_input(|key| key == Key::Char('r')) { ui.send_message(Message::Reject); } } ReviewMode::Show => { ui.shortcuts(frame, &[("?", "help"), ("q", "quit")], '∙'); - if ui.input_global(|key| key == Key::Char('?')) { + if ui.has_input(|key| key == Key::Char('?')) { ui.send_message(Message::ShowHelp); } } @@ -571,13 +571,13 @@ impl Show for App<'_> { ui.shortcuts(frame, &[("?", "close"), ("q", "quit")], '∙'); }); - if ui.input_global(|key| key == Key::Char('?')) { + if ui.has_input(|key| key == Key::Char('?')) { ui.send_message(Message::ShowMain); } } } - if ui.input_global(|key| key == Key::Char('q')) { + if ui.has_input(|key| key == Key::Char('q')) { ui.send_message(Message::Quit); } }); diff --git a/bin/ui/im.rs b/bin/ui/im.rs index 25216909..67d42d63 100644 --- a/bin/ui/im.rs +++ b/bin/ui/im.rs @@ -168,15 +168,15 @@ where ); if !*self.show_search { - if ui.input_global(|key| key == Key::Char('/')) { + if ui.has_input(|key| key == Key::Char('/')) { *self.show_search = true; } } else { - if ui.input_global(|key| key == Key::Esc) { + if ui.has_input(|key| key == Key::Esc) { *self.show_search = false; self.search.reset(); } - if ui.input_global(|key| key == Key::Char('\n')) { + if ui.has_input(|key| key == Key::Char('\n')) { *self.show_search = false; self.search.apply(); } diff --git a/examples/hello.rs b/examples/hello.rs index 1f62acc1..79bcd310 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -59,7 +59,7 @@ impl Show for App { Some(Borders::None), ); - if ui.input_global(|key| key == Key::Char('q')) { + if ui.has_input(|key| key == Key::Char('q')) { ui.send_message(Message::Quit); } }); diff --git a/examples/selection.rs b/examples/selection.rs index 93948904..73cc9555 100644 --- a/examples/selection.rs +++ b/examples/selection.rs @@ -120,13 +120,13 @@ impl Show for App { ui.shortcuts(frame, &[("q", "quit")], '|'); - if ui.input_global(|key| key == Key::Char('\n')) { + if ui.has_input(|key| key == Key::Char('\n')) { ui.send_message(Message::Return); } }, ); - if ui.input_global(|key| key == Key::Char('q')) { + if ui.has_input(|key| key == Key::Char('q')) { ui.send_message(Message::Quit); } }); diff --git a/src/ui.rs b/src/ui.rs index de3c1cc5..b7e9a9f1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -95,6 +95,7 @@ impl<'a> Column<'a> { } } +#[derive(Default)] pub struct Spacing(u16); impl From for Spacing { @@ -103,12 +104,6 @@ impl From for Spacing { } } -impl Default for Spacing { - fn default() -> Self { - Self(0) - } -} - impl From for u16 { fn from(spacing: Spacing) -> Self { spacing.0 diff --git a/src/ui/im.rs b/src/ui/im.rs index a45fe460..ebe5c57b 100644 --- a/src/ui/im.rs +++ b/src/ui/im.rs @@ -307,15 +307,11 @@ pub struct Ui { } impl Ui { - pub fn input(&mut self, f: impl Fn(Key) -> bool) -> bool { + pub fn has_input(&mut self, f: impl Fn(Key) -> bool) -> bool { self.has_focus && self.is_area_focused() && self.ctx.inputs.iter().any(|key| f(*key)) } - pub fn input_global(&mut self, f: impl Fn(Key) -> bool) -> bool { - self.has_focus && self.ctx.inputs.iter().any(|key| f(*key)) - } - - pub fn input_with_key(&mut self, f: impl Fn(Key) -> bool) -> Option { + pub fn get_input(&mut self, f: impl Fn(Key) -> bool) -> Option { if self.has_focus && self.is_area_focused() { self.ctx.inputs.iter().find(|key| f(**key)).copied() } else { diff --git a/src/ui/im/widget.rs b/src/ui/im/widget.rs index c88c305a..a69163a4 100644 --- a/src/ui/im/widget.rs +++ b/src/ui/im/widget.rs @@ -131,11 +131,11 @@ impl<'a> Container<'a> { len: self.len, }; - if ui.input_global(|key| key == Key::Char('\t')) { + if ui.has_input(|key| key == Key::Char('\t')) { state.focus_next(); response.changed = true; } - if ui.input_global(|key| key == Key::BackTab) { + if ui.has_input(|key| key == Key::BackTab) { state.focus_prev(); response.changed = true; } @@ -372,7 +372,7 @@ where let area = render_block(frame, area, self.borders, border_style); - if let Some(key) = ui.input_with_key(|_| true) { + if let Some(key) = ui.get_input(|_| true) { let len = self.items.len(); let page_size = area.height as usize; @@ -734,7 +734,7 @@ impl Widget for TextView<'_> { let mut state = TextViewState::new(*self.cursor); - if let Some(key) = ui.input_with_key(|_| true) { + if let Some(key) = ui.get_input(|_| true) { let lines = self.text.lines.clone(); let len = lines.clone().len(); let max_line_len = lines @@ -997,7 +997,7 @@ impl TextEdit<'_> { } } - if let Some(key) = ui.input_with_key(|_| true) { + if let Some(key) = ui.get_input(|_| true) { match key { Key::Char(to_insert) if (key != Key::Alt('\n'))