From 0bab9a77a2822fe9b7a42f24dc4cf0d4db45dd01 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Tue, 15 Dec 2020 12:28:06 +0100 Subject: [PATCH] Working on bookmarks --- src/ui/activities/auth_activity/input.rs | 73 ++++++++++++++++++- src/ui/activities/auth_activity/mod.rs | 26 ++++++- .../activities/filetransfer_activity/input.rs | 6 +- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/src/ui/activities/auth_activity/input.rs b/src/ui/activities/auth_activity/input.rs index 5e9b008..d878ce1 100644 --- a/src/ui/activities/auth_activity/input.rs +++ b/src/ui/activities/auth_activity/input.rs @@ -24,7 +24,7 @@ */ use super::{ - AuthActivity, FileTransferProtocol, InputEvent, InputField, InputForm, InputMode, PopupType, + AuthActivity, DialogCallback, DialogYesNoOption, FileTransferProtocol, InputEvent, InputField, InputForm, InputMode, OnInputSubmitCallback, PopupType, }; use crossterm::event::KeyCode; @@ -192,6 +192,10 @@ impl AuthActivity { pub(super) fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: PopupType) { match ptype { PopupType::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev), + PopupType::Input(_, cb) => self.handle_input_event_mode_popup_input(ev, cb), + PopupType::YesNo(_, yes_cb, no_cb) => { + self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb) + } } } @@ -206,4 +210,71 @@ impl AuthActivity { } } } + + /// ### handle_input_event_mode_popup_input + /// + /// Input event handler for input popup + pub(super) fn handle_input_event_mode_popup_input( + &mut self, + ev: &InputEvent, + cb: OnInputSubmitCallback, + ) { + // If enter, close popup, otherwise push chars to input + if let InputEvent::Key(key) = ev { + match key.code { + KeyCode::Esc => { + // Abort input + // Clear current input text + self.input_txt.clear(); + // Set mode back to form + self.input_mode = InputMode::Form; + } + KeyCode::Enter => { + // Submit + let input_text: String = self.input_txt.clone(); + // Clear current input text + self.input_txt.clear(); + // Set mode back to form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh? + self.input_mode = InputMode::Form; + // Call cb + cb(self, input_text); + } + KeyCode::Char(ch) => self.input_txt.push(ch), + KeyCode::Backspace => { + let _ = self.input_txt.pop(); + } + _ => { /* Nothing to do */ } + } + } + } + + /// ### handle_input_event_mode_popup_yesno + /// + /// Input event handler for popup alert + pub(super) fn handle_input_event_mode_popup_yesno( + &mut self, + ev: &InputEvent, + yes_cb: DialogCallback, + no_cb: DialogCallback, + ) { + // If enter, close popup, otherwise move dialog option + if let InputEvent::Key(key) = ev { + match key.code { + KeyCode::Enter => { + // @! Set input mode to Form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh? + self.input_mode = InputMode::Form; + // Check if user selected yes or not + match self.choice_opt { + DialogYesNoOption::No => no_cb(self), + DialogYesNoOption::Yes => yes_cb(self), + } + // Reset choice option to yes + self.choice_opt = DialogYesNoOption::Yes; + } + KeyCode::Right => self.choice_opt = DialogYesNoOption::No, // Set to NO + KeyCode::Left => self.choice_opt = DialogYesNoOption::Yes, // Set to YES + _ => { /* Nothing to do */ } + } + } + } } diff --git a/src/ui/activities/auth_activity/mod.rs b/src/ui/activities/auth_activity/mod.rs index 7584ec2..61cb46e 100644 --- a/src/ui/activities/auth_activity/mod.rs +++ b/src/ui/activities/auth_activity/mod.rs @@ -43,6 +43,10 @@ use crossterm::event::Event as InputEvent; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use tui::style::Color; +// Types +type DialogCallback = fn(&mut AuthActivity); +type OnInputSubmitCallback = fn(&mut AuthActivity, String); + /// ### InputField /// /// InputField describes the current input field to edit @@ -55,19 +59,29 @@ enum InputField { Password, } +/// ### DialogYesNoOption +/// +/// Current yes/no dialog option +#[derive(std::cmp::PartialEq, Clone)] +enum DialogYesNoOption { + Yes, + No, +} + /// ### PopupType /// /// PopupType describes the type of the popup displayed -#[derive(std::cmp::PartialEq, Clone)] +#[derive(Clone)] enum PopupType { Alert(Color, String), // Show a message displaying text with the provided color + Input(String, OnInputSubmitCallback), // Input description; Callback for submit + YesNo(String, DialogCallback, DialogCallback), // Yes, no callback } /// ### InputMode /// /// InputMode describes the current input mode /// Each input mode handle the input events in a different way -#[derive(std::cmp::PartialEq)] enum InputMode { Form, Popup(PopupType), @@ -79,6 +93,8 @@ enum InputMode { /// InputForm describes the selected input form enum InputForm { AuthCredentials, + Bookmarks, + Recents, } /// ### AuthActivity @@ -98,7 +114,9 @@ pub struct AuthActivity { input_mode: InputMode, input_form: InputForm, password_placeholder: String, - redraw: bool, // Should ui actually be redrawned? + redraw: bool, // Should ui actually be redrawned? + input_txt: String, // Input text + choice_opt: DialogYesNoOption, // Dialog popup selected option } impl Default for AuthActivity { @@ -127,6 +145,8 @@ impl AuthActivity { input_form: InputForm::AuthCredentials, password_placeholder: String::new(), redraw: true, // True at startup + input_txt: String::new(), + choice_opt: DialogYesNoOption::Yes, } } } diff --git a/src/ui/activities/filetransfer_activity/input.rs b/src/ui/activities/filetransfer_activity/input.rs index bcec781..1d594d5 100644 --- a/src/ui/activities/filetransfer_activity/input.rs +++ b/src/ui/activities/filetransfer_activity/input.rs @@ -645,7 +645,7 @@ impl FileTransferActivity { } } - /// ### handle_input_event_mode_explorer_alert + /// ### handle_input_event_mode_popup_progress /// /// Input event handler for popup alert pub(super) fn handle_input_event_mode_popup_progress(&mut self, ev: &InputEvent) { @@ -660,14 +660,14 @@ impl FileTransferActivity { } } - /// ### handle_input_event_mode_explorer_alert + /// ### handle_input_event_mode_popup_wait /// /// Input event handler for popup alert pub(super) fn handle_input_event_mode_popup_wait(&mut self, _ev: &InputEvent) { // There's nothing you can do here I guess... maybe ctrl+c in the future idk } - /// ### handle_input_event_mode_explorer_alert + /// ### handle_input_event_mode_popup_yesno /// /// Input event handler for popup alert pub(super) fn handle_input_event_mode_popup_yesno(