Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
lib/ui: Improve input request functions
Erik Kundt committed 4 months ago
commit b54e67f757ec5c985a9df26f9d3d974aa376a59f
parent d85ffb5
9 files changed +75 -91
modified 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<Message> 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<Message> 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<Message>) {
@@ -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 });
        }
    }
modified bin/commands/patch/list.rs
@@ -285,6 +285,27 @@ impl Show<Message> 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<Message> 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<Message>) {
@@ -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 });
        }
    }
modified 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<Message> 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);
            }
        });
modified 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();
            }
modified examples/hello.rs
@@ -59,7 +59,7 @@ impl Show<Message> 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);
            }
        });
modified examples/selection.rs
@@ -120,13 +120,13 @@ impl Show<Message> 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);
            }
        });
modified src/ui.rs
@@ -95,6 +95,7 @@ impl<'a> Column<'a> {
    }
}

+
#[derive(Default)]
pub struct Spacing(u16);

impl From<u16> for Spacing {
@@ -103,12 +104,6 @@ impl From<u16> for Spacing {
    }
}

-
impl Default for Spacing {
-
    fn default() -> Self {
-
        Self(0)
-
    }
-
}
-

impl From<Spacing> for u16 {
    fn from(spacing: Spacing) -> Self {
        spacing.0
modified src/ui/im.rs
@@ -307,15 +307,11 @@ pub struct Ui<M> {
}

impl<M> Ui<M> {
-
    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<Key> {
+
    pub fn get_input(&mut self, f: impl Fn(Key) -> bool) -> Option<Key> {
        if self.has_focus && self.is_area_focused() {
            self.ctx.inputs.iter().find(|key| f(**key)).copied()
        } else {
modified 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'))