Working on activity refactoring

This commit is contained in:
veeso
2021-03-09 21:52:11 +01:00
parent b2aaf5c57f
commit 5980bc1fcb
2 changed files with 95 additions and 3 deletions

View File

@@ -25,9 +25,10 @@
// Sub modules // Sub modules
mod bookmarks; mod bookmarks;
mod callbacks; mod callbacks; // REMOVE
mod input; mod input; // REMOVE
mod layout; mod layout; // REMOVE
mod update;
// Dependencies // Dependencies
extern crate crossterm; extern crate crossterm;
@@ -40,6 +41,7 @@ use crate::filetransfer::FileTransferProtocol;
use crate::system::bookmarks_client::BookmarksClient; use crate::system::bookmarks_client::BookmarksClient;
use crate::system::config_client::ConfigClient; use crate::system::config_client::ConfigClient;
use crate::system::environment; use crate::system::environment;
use crate::ui::layout::view::View;
use crate::utils::git; use crate::utils::git;
// Includes // Includes
@@ -51,6 +53,22 @@ use tui::style::Color;
// Types // Types
type DialogCallback = fn(&mut AuthActivity); type DialogCallback = fn(&mut AuthActivity);
// -- components
const COMPONENT_TEXT_HEADER: &str = "TEXT_HEADER";
const COMPONENT_TEXT_FOOTER: &str = "TEXT_FOOTER";
const COMPONENT_TEXT_HELP: &str = "TEXT_HELP";
const COMPONENT_TEXT_ERROR: &str = "TEXT_ERROR";
const COMPONENT_INPUT_ADDR: &str = "INPUT_ADDRESS";
const COMPONENT_INPUT_PORT: &str = "INPUT_PORT";
const COMPONENT_INPUT_USERNAME: &str = "INPUT_USERNAME";
const COMPONENT_INPUT_PASSWORD: &str = "INPUT_PASSWORD";
const COMPONENT_INPUT_BOOKMARK_NAME: &str = "INPUT_BOOKMARK_NAME";
const COMPONENT_RADIO_PROTOCOL: &str = "RADIO_PROTOCOL";
const COMPONENT_RADIO_BOOKMARK_DEL: &str = "RADIO_DELETE_BOOKMARK";
const COMPONENT_RADIO_BOOKMARK_SAVE_PWD: &str = "RADIO_SAVE_PASSWORD";
const COMPONENT_BOOKMARKS_LIST: &str = "BOOKMARKS_LIST";
const COMPONENT_RECENTS_LIST: &str = "RECENTS_LIST";
/// ### InputField /// ### InputField
/// ///
/// InputField describes the current input field to edit /// InputField describes the current input field to edit
@@ -106,6 +124,7 @@ pub struct AuthActivity {
pub quit: bool, // Becomes true if user has pressed esc pub quit: bool, // Becomes true if user has pressed esc
pub setup: bool, // Becomes true if user has requested setup pub setup: bool, // Becomes true if user has requested setup
context: Option<Context>, context: Option<Context>,
view: View,
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
@@ -144,6 +163,7 @@ impl AuthActivity {
quit: false, quit: false,
setup: false, setup: false,
context: None, context: None,
view: View::init(),
bookmarks_client: None, bookmarks_client: None,
config_client: None, config_client: None,
selected_field: InputField::Address, selected_field: InputField::Address,

View File

@@ -0,0 +1,72 @@
//! ## AuthActivity
//!
//! `auth_activity` is the module which implements the authentication activity
/*
*
* Copyright (C) 2020-2021 Christian Visintin - christian.visintin1997@gmail.com
*
* This file is part of "TermSCP"
*
* TermSCP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TermSCP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
*
*/
// locals
use super::{
AuthActivity, FileTransferProtocol, InputEvent,
COMPONENT_TEXT_HELP
};
use crate::ui::layout::{Msg, Payload};
// ext
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
// -- update
impl AuthActivity {
/// ### handle_input_event
///
/// Handle input event, based on current input mode
pub(super) fn handle_input_event(&mut self, ev: InputEvent) {
// Call update passing the return value from on
self.update(self.view.on(ev));
}
/// ### update
///
/// Update auth activity model based on msg
/// The function exits when returns None
pub(super) fn update(&mut self, msg: Option<(&str, Msg)>) -> Option<(&str, Msg)> {
let key_enter = KeyEvent::from(KeyCode::Enter);
// Match msg
match msg {
None => None, // Exit after None
Some(msg) => match msg {
(COMPONENT_TEXT_HELP, Msg::OnKey(key_enter) | (COMPONENT_TEXT_HELP, Msg::OnKey(KeyEvent::from(KeyCode::Esc))) => {
// Hide text help
match self.view.get_props(COMPONENT_TEXT_HELP) {
None => None,
Some(props) => self.update(self.view.update(COMPONENT_TEXT_HELP, props.hidden().build())),
}
}
(_, Msg::OnSubmit(_)) | (_, Msg::OnKey(KeyEvent::from(KeyCode::Enter))) => {
// Match <ENTER> key for all other components
}
(_, _) => None, // Ignore other events
}
}
}
}