InputMode as Option<Popup> in AuthActivity

This commit is contained in:
ChristianVisintin
2020-12-27 11:03:44 +01:00
parent d756bf7786
commit 6dd4cfaa3c
4 changed files with 61 additions and 72 deletions

View File

@@ -27,7 +27,7 @@
extern crate dirs; extern crate dirs;
// Locals // Locals
use super::{AuthActivity, Color, DialogYesNoOption, InputMode, PopupType}; use super::{AuthActivity, Color, DialogYesNoOption, Popup};
use crate::system::bookmarks_client::BookmarksClient; use crate::system::bookmarks_client::BookmarksClient;
use crate::system::environment; use crate::system::environment;
@@ -89,7 +89,7 @@ impl AuthActivity {
let port: u16 = match self.port.parse::<usize>() { let port: u16 = match self.port.parse::<usize>() {
Ok(val) => { Ok(val) => {
if val > 65535 { if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port must be in range 0-65535"), String::from("Specified port must be in range 0-65535"),
)); ));
@@ -98,7 +98,7 @@ impl AuthActivity {
val as u16 val as u16
} }
Err(_) => { Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port is not a number"), String::from("Specified port is not a number"),
)); ));
@@ -174,7 +174,7 @@ impl AuthActivity {
let port: u16 = match self.port.parse::<usize>() { let port: u16 = match self.port.parse::<usize>() {
Ok(val) => { Ok(val) => {
if val > 65535 { if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port must be in range 0-65535"), String::from("Specified port must be in range 0-65535"),
)); ));
@@ -183,7 +183,7 @@ impl AuthActivity {
val as u16 val as u16
} }
Err(_) => { Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port is not a number"), String::from("Specified port is not a number"),
)); ));
@@ -208,7 +208,7 @@ impl AuthActivity {
fn write_bookmarks(&mut self) { fn write_bookmarks(&mut self) {
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() { if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
if let Err(err) = bookmarks_cli.write_bookmarks() { if let Err(err) = bookmarks_cli.write_bookmarks() {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
format!("Could not write bookmarks: {}", err), format!("Could not write bookmarks: {}", err),
)); ));
@@ -225,12 +225,13 @@ impl AuthActivity {
Ok(path) => { Ok(path) => {
// If some configure client, otherwise do nothing; don't bother users telling them that bookmarks are not supported on their system. // If some configure client, otherwise do nothing; don't bother users telling them that bookmarks are not supported on their system.
if let Some(path) = path { if let Some(path) = path {
let (bookmarks_file, key_file): (PathBuf, PathBuf) = environment::get_bookmarks_paths(path.as_path()); let (bookmarks_file, key_file): (PathBuf, PathBuf) =
environment::get_bookmarks_paths(path.as_path());
// Initialize client // Initialize client
match BookmarksClient::new(bookmarks_file.as_path(), key_file.as_path(), 16) { match BookmarksClient::new(bookmarks_file.as_path(), key_file.as_path(), 16) {
Ok(cli) => self.bookmarks_client = Some(cli), Ok(cli) => self.bookmarks_client = Some(cli),
Err(err) => { Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
format!( format!(
"Could not initialize bookmarks (at \"{}\", \"{}\"): {}", "Could not initialize bookmarks (at \"{}\", \"{}\"): {}",
@@ -244,7 +245,7 @@ impl AuthActivity {
} }
} }
Err(err) => { Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
format!("Could not initialize configuration directory: {}", err), format!("Could not initialize configuration directory: {}", err),
)) ))

View File

@@ -25,7 +25,7 @@
use super::{ use super::{
AuthActivity, DialogCallback, DialogYesNoOption, FileTransferProtocol, InputEvent, InputField, AuthActivity, DialogCallback, DialogYesNoOption, FileTransferProtocol, InputEvent, InputField,
InputForm, InputMode, PopupType, InputForm, Popup,
}; };
use crossterm::event::{KeyCode, KeyModifiers}; use crossterm::event::{KeyCode, KeyModifiers};
@@ -36,13 +36,13 @@ impl AuthActivity {
/// ///
/// Handle input event, based on current input mode /// Handle input event, based on current input mode
pub(super) fn handle_input_event(&mut self, ev: &InputEvent) { pub(super) fn handle_input_event(&mut self, ev: &InputEvent) {
let popup: Option<PopupType> = match &self.input_mode { let popup: Option<Popup> = match &self.popup {
InputMode::Popup(ptype) => Some(ptype.clone()), Some(ptype) => Some(ptype.clone()),
_ => None, _ => None,
}; };
match self.input_mode { match &self.popup {
InputMode::Form => self.handle_input_event_mode_form(ev), None => self.handle_input_event_mode_form(ev),
InputMode::Popup(_) => { Some(_) => {
if let Some(ptype) = popup { if let Some(ptype) = popup {
self.handle_input_event_mode_popup(ev, ptype) self.handle_input_event_mode_popup(ev, ptype)
} }
@@ -69,7 +69,7 @@ impl AuthActivity {
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
// Show quit dialog // Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to quit termscp?"), String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit, AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -81,10 +81,8 @@ impl AuthActivity {
// Check form // Check form
// Check address // Check address
if self.address.is_empty() { if self.address.is_empty() {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup =
Color::Red, Some(Popup::Alert(Color::Red, String::from("Invalid address")));
String::from("Invalid address"),
));
return; return;
} }
// Check port // Check port
@@ -92,7 +90,7 @@ impl AuthActivity {
match self.port.parse::<usize>() { match self.port.parse::<usize>() {
Ok(val) => { Ok(val) => {
if val > 65535 { if val > 65535 {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port must be in range 0-65535"), String::from("Specified port must be in range 0-65535"),
)); ));
@@ -100,7 +98,7 @@ impl AuthActivity {
} }
} }
Err(_) => { Err(_) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
String::from("Specified port is not a number"), String::from("Specified port is not a number"),
)); ));
@@ -157,7 +155,7 @@ impl AuthActivity {
match ch { match ch {
'H' | 'h' => { 'H' | 'h' => {
// Show help // Show help
self.input_mode = InputMode::Popup(PopupType::Help); self.popup = Some(Popup::Help);
} }
'C' | 'c' => { 'C' | 'c' => {
// Show setup // Show setup
@@ -167,7 +165,7 @@ impl AuthActivity {
// Default choice option to no // Default choice option to no
self.choice_opt = DialogYesNoOption::No; self.choice_opt = DialogYesNoOption::No;
// Save bookmark as... // Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark); self.popup = Some(Popup::SaveBookmark);
} }
_ => { /* Nothing to do */ } _ => { /* Nothing to do */ }
} }
@@ -225,7 +223,7 @@ impl AuthActivity {
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
// Show quit dialog // Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to quit termscp?"), String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit, AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -257,7 +255,7 @@ impl AuthActivity {
} }
KeyCode::Delete => { KeyCode::Delete => {
// Ask if user wants to delete bookmark // Ask if user wants to delete bookmark
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to delete the selected bookmark?"), String::from("Are you sure you want to delete the selected bookmark?"),
AuthActivity::callback_del_bookmark, AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -278,7 +276,7 @@ impl AuthActivity {
} }
'E' | 'e' => { 'E' | 'e' => {
// Ask if user wants to delete bookmark; NOTE: same as <DEL> // Ask if user wants to delete bookmark; NOTE: same as <DEL>
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to delete the selected bookmark?"), String::from("Are you sure you want to delete the selected bookmark?"),
AuthActivity::callback_del_bookmark, AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -286,13 +284,13 @@ impl AuthActivity {
} }
'H' | 'h' => { 'H' | 'h' => {
// Show help // Show help
self.input_mode = InputMode::Popup(PopupType::Help); self.popup = Some(Popup::Help);
} }
'S' | 's' => { 'S' | 's' => {
// Default choice option to no // Default choice option to no
self.choice_opt = DialogYesNoOption::No; self.choice_opt = DialogYesNoOption::No;
// Save bookmark as... // Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark); self.popup = Some(Popup::SaveBookmark);
} }
_ => { /* Nothing to do */ } _ => { /* Nothing to do */ }
}, },
@@ -309,7 +307,7 @@ impl AuthActivity {
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
// Show quit dialog // Show quit dialog
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to quit termscp?"), String::from("Are you sure you want to quit termscp?"),
AuthActivity::callback_quit, AuthActivity::callback_quit,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -341,7 +339,7 @@ impl AuthActivity {
} }
KeyCode::Delete => { KeyCode::Delete => {
// Ask if user wants to delete bookmark // Ask if user wants to delete bookmark
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to delete the selected host?"), String::from("Are you sure you want to delete the selected host?"),
AuthActivity::callback_del_bookmark, AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -362,7 +360,7 @@ impl AuthActivity {
} }
'E' | 'e' => { 'E' | 'e' => {
// Ask if user wants to delete bookmark; NOTE: same as <DEL> // Ask if user wants to delete bookmark; NOTE: same as <DEL>
self.input_mode = InputMode::Popup(PopupType::YesNo( self.popup = Some(Popup::YesNo(
String::from("Are you sure you want to delete the selected host?"), String::from("Are you sure you want to delete the selected host?"),
AuthActivity::callback_del_bookmark, AuthActivity::callback_del_bookmark,
AuthActivity::callback_nothing_to_do, AuthActivity::callback_nothing_to_do,
@@ -370,13 +368,13 @@ impl AuthActivity {
} }
'H' | 'h' => { 'H' | 'h' => {
// Show help // Show help
self.input_mode = InputMode::Popup(PopupType::Help); self.popup = Some(Popup::Help);
} }
'S' | 's' => { 'S' | 's' => {
// Default choice option to no // Default choice option to no
self.choice_opt = DialogYesNoOption::No; self.choice_opt = DialogYesNoOption::No;
// Save bookmark as... // Save bookmark as...
self.input_mode = InputMode::Popup(PopupType::SaveBookmark); self.popup = Some(Popup::SaveBookmark);
} }
_ => { /* Nothing to do */ } _ => { /* Nothing to do */ }
}, },
@@ -388,12 +386,12 @@ impl AuthActivity {
/// ### handle_input_event_mode_text /// ### handle_input_event_mode_text
/// ///
/// Handler for input event when in popup mode /// Handler for input event when in popup mode
fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: PopupType) { fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: Popup) {
match ptype { match ptype {
PopupType::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev), Popup::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev),
PopupType::Help => self.handle_input_event_mode_popup_help(ev), Popup::Help => self.handle_input_event_mode_popup_help(ev),
PopupType::SaveBookmark => self.handle_input_event_mode_popup_save_bookmark(ev), Popup::SaveBookmark => self.handle_input_event_mode_popup_save_bookmark(ev),
PopupType::YesNo(_, yes_cb, no_cb) => { Popup::YesNo(_, yes_cb, no_cb) => {
self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb) self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb)
} }
} }
@@ -406,7 +404,7 @@ impl AuthActivity {
// Only enter should be allowed here // Only enter should be allowed here
if let InputEvent::Key(key) = ev { if let InputEvent::Key(key) = ev {
if matches!(key.code, KeyCode::Esc | KeyCode::Enter) { if matches!(key.code, KeyCode::Esc | KeyCode::Enter) {
self.input_mode = InputMode::Form; // Hide popup self.popup = None; // Hide popup
} }
} }
} }
@@ -419,7 +417,7 @@ impl AuthActivity {
if let InputEvent::Key(key) = ev { if let InputEvent::Key(key) = ev {
if matches!(key.code, KeyCode::Esc | KeyCode::Enter) { if matches!(key.code, KeyCode::Esc | KeyCode::Enter) {
// Set input mode back to form // Set input mode back to form
self.input_mode = InputMode::Form; self.popup = None;
} }
} }
} }
@@ -436,7 +434,7 @@ impl AuthActivity {
// Clear current input text // Clear current input text
self.input_txt.clear(); self.input_txt.clear();
// Set mode back to form // Set mode back to form
self.input_mode = InputMode::Form; self.popup = None;
// Reset choice option to yes // Reset choice option to yes
self.choice_opt = DialogYesNoOption::Yes; self.choice_opt = DialogYesNoOption::Yes;
} }
@@ -446,7 +444,7 @@ impl AuthActivity {
// Clear current input text // Clear current input text
self.input_txt.clear(); self.input_txt.clear();
// Set mode back to form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh? // Set mode back to form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
self.input_mode = InputMode::Form; self.popup = None;
// Call cb // Call cb
self.callback_save_bookmark(input_text); self.callback_save_bookmark(input_text);
// Reset choice option to yes // Reset choice option to yes
@@ -477,7 +475,7 @@ impl AuthActivity {
match key.code { match key.code {
KeyCode::Enter => { KeyCode::Enter => {
// @! Set input mode to Form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh? // @! Set input mode to Form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
self.input_mode = InputMode::Form; self.popup = None;
// Check if user selected yes or not // Check if user selected yes or not
match self.choice_opt { match self.choice_opt {
DialogYesNoOption::No => no_cb(self), DialogYesNoOption::No => no_cb(self),

View File

@@ -25,8 +25,7 @@
// Locals // Locals
use super::{ use super::{
AuthActivity, Context, DialogYesNoOption, FileTransferProtocol, InputField, InputForm, AuthActivity, Context, DialogYesNoOption, FileTransferProtocol, InputField, InputForm, Popup,
InputMode, PopupType,
}; };
use crate::utils::fmt::align_text_center; use crate::utils::fmt::align_text_center;
// Ext // Ext
@@ -123,23 +122,23 @@ impl AuthActivity {
f.render_stateful_widget(tab, bookmark_chunks[1], &mut recents_state); f.render_stateful_widget(tab, bookmark_chunks[1], &mut recents_state);
} }
// Draw popup // Draw popup
if let InputMode::Popup(popup) = &self.input_mode { if let Some(popup) = &self.popup {
// Calculate popup size // Calculate popup size
let (width, height): (u16, u16) = match popup { let (width, height): (u16, u16) = match popup {
PopupType::Alert(_, _) => (50, 10), Popup::Alert(_, _) => (50, 10),
PopupType::Help => (50, 70), Popup::Help => (50, 70),
PopupType::SaveBookmark => (20, 20), Popup::SaveBookmark => (20, 20),
PopupType::YesNo(_, _, _) => (30, 10), Popup::YesNo(_, _, _) => (30, 10),
}; };
let popup_area: Rect = self.draw_popup_area(f.size(), width, height); let popup_area: Rect = self.draw_popup_area(f.size(), width, height);
f.render_widget(Clear, popup_area); //this clears out the background f.render_widget(Clear, popup_area); //this clears out the background
match popup { match popup {
PopupType::Alert(color, txt) => f.render_widget( Popup::Alert(color, txt) => f.render_widget(
self.draw_popup_alert(*color, txt.clone(), popup_area.width), self.draw_popup_alert(*color, txt.clone(), popup_area.width),
popup_area, popup_area,
), ),
PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area), Popup::Help => f.render_widget(self.draw_popup_help(), popup_area),
PopupType::SaveBookmark => { Popup::SaveBookmark => {
let popup_chunks = Layout::default() let popup_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(
@@ -160,7 +159,7 @@ impl AuthActivity {
popup_chunks[0].y + 1, popup_chunks[0].y + 1,
) )
} }
PopupType::YesNo(txt, _, _) => { Popup::YesNo(txt, _, _) => {
f.render_widget(self.draw_popup_yesno(txt.clone()), popup_area) f.render_widget(self.draw_popup_yesno(txt.clone()), popup_area)
} }
} }

View File

@@ -71,26 +71,17 @@ enum DialogYesNoOption {
No, No,
} }
/// ### PopupType /// ### Popup
/// ///
/// PopupType describes the type of the popup displayed /// Popup describes the type of the popup displayed
#[derive(Clone)] #[derive(Clone)]
enum PopupType { enum Popup {
Alert(Color, String), // Show a message displaying text with the provided color Alert(Color, String), // Show a message displaying text with the provided color
Help, // Help page Help, // Help page
SaveBookmark, SaveBookmark,
YesNo(String, DialogCallback, DialogCallback), // Yes, no callback 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
enum InputMode {
Form,
Popup(PopupType),
}
#[derive(std::cmp::PartialEq)] #[derive(std::cmp::PartialEq)]
/// ### InputForm /// ### InputForm
/// ///
@@ -117,7 +108,7 @@ pub struct AuthActivity {
bookmarks_client: Option<BookmarksClient>, bookmarks_client: Option<BookmarksClient>,
config_client: Option<ConfigClient>, config_client: Option<ConfigClient>,
selected_field: InputField, // Selected field in AuthCredentials Form selected_field: InputField, // Selected field in AuthCredentials Form
input_mode: InputMode, popup: Option<Popup>,
input_form: InputForm, input_form: InputForm,
password_placeholder: String, password_placeholder: String,
redraw: bool, // Should ui actually be redrawned? redraw: bool, // Should ui actually be redrawned?
@@ -151,7 +142,7 @@ impl AuthActivity {
bookmarks_client: None, bookmarks_client: None,
config_client: None, config_client: None,
selected_field: InputField::Address, selected_field: InputField::Address,
input_mode: InputMode::Form, popup: None,
input_form: InputForm::AuthCredentials, input_form: InputForm::AuthCredentials,
password_placeholder: String::new(), password_placeholder: String::new(),
redraw: true, // True at startup redraw: true, // True at startup
@@ -181,7 +172,7 @@ impl AuthActivity {
self.config_client = Some(cli); self.config_client = Some(cli);
} }
Err(err) => { Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
format!("Could not initialize user configuration: {}", err), format!("Could not initialize user configuration: {}", err),
)) ))
@@ -190,7 +181,7 @@ impl AuthActivity {
} }
} }
Err(err) => { Err(err) => {
self.input_mode = InputMode::Popup(PopupType::Alert( self.popup = Some(Popup::Alert(
Color::Red, Color::Red,
format!("Could not initialize configuration directory: {}", err), format!("Could not initialize configuration directory: {}", err),
)) ))
@@ -212,7 +203,7 @@ impl Activity for AuthActivity {
self.context.as_mut().unwrap().clear_screen(); self.context.as_mut().unwrap().clear_screen();
// Put raw mode on enabled // Put raw mode on enabled
let _ = enable_raw_mode(); let _ = enable_raw_mode();
self.input_mode = InputMode::Form; self.popup = None;
// Init bookmarks client // Init bookmarks client
if self.bookmarks_client.is_none() { if self.bookmarks_client.is_none() {
self.init_bookmarks_client(); self.init_bookmarks_client();