From 5980bc1fcb8f75800fd8f2e4f84e1e7b27f2055d Mon Sep 17 00:00:00 2001 From: veeso Date: Tue, 9 Mar 2021 21:52:11 +0100 Subject: [PATCH] Working on activity refactoring --- src/ui/activities/auth_activity/mod.rs | 26 +++++++- src/ui/activities/auth_activity/update.rs | 72 +++++++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/ui/activities/auth_activity/update.rs diff --git a/src/ui/activities/auth_activity/mod.rs b/src/ui/activities/auth_activity/mod.rs index ffe55d3..cb28c55 100644 --- a/src/ui/activities/auth_activity/mod.rs +++ b/src/ui/activities/auth_activity/mod.rs @@ -25,9 +25,10 @@ // Sub modules mod bookmarks; -mod callbacks; -mod input; -mod layout; +mod callbacks; // REMOVE +mod input; // REMOVE +mod layout; // REMOVE +mod update; // Dependencies extern crate crossterm; @@ -40,6 +41,7 @@ use crate::filetransfer::FileTransferProtocol; use crate::system::bookmarks_client::BookmarksClient; use crate::system::config_client::ConfigClient; use crate::system::environment; +use crate::ui::layout::view::View; use crate::utils::git; // Includes @@ -51,6 +53,22 @@ use tui::style::Color; // Types 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 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 setup: bool, // Becomes true if user has requested setup context: Option, + view: View, bookmarks_client: Option, config_client: Option, selected_field: InputField, // Selected field in AuthCredentials Form @@ -144,6 +163,7 @@ impl AuthActivity { quit: false, setup: false, context: None, + view: View::init(), bookmarks_client: None, config_client: None, selected_field: InputField::Address, diff --git a/src/ui/activities/auth_activity/update.rs b/src/ui/activities/auth_activity/update.rs new file mode 100644 index 0000000..1663db7 --- /dev/null +++ b/src/ui/activities/auth_activity/update.rs @@ -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 . +* +*/ + +// 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 key for all other components + } + (_, _) => None, // Ignore other events + } + } + } + +} \ No newline at end of file