will_umount method in Activity

This commit is contained in:
veeso
2021-03-21 17:16:52 +01:00
parent 30c2aa144b
commit f0d87ff8c4
9 changed files with 95 additions and 50 deletions

View File

@@ -32,7 +32,7 @@ use crate::system::config_client::ConfigClient;
use crate::system::environment; use crate::system::environment;
use crate::ui::activities::{ use crate::ui::activities::{
auth_activity::AuthActivity, filetransfer_activity::FileTransferActivity, auth_activity::AuthActivity, filetransfer_activity::FileTransferActivity,
setup_activity::SetupActivity, Activity, setup_activity::SetupActivity, Activity, ExitReason,
}; };
use crate::ui::context::{Context, FileTransferParams}; use crate::ui::context::{Context, FileTransferParams};
@@ -147,20 +147,24 @@ impl ActivityManager {
// Draw activity // Draw activity
activity.on_draw(); activity.on_draw();
// Check if has to be terminated // Check if has to be terminated
if activity.quit { if let Some(exit_reason) = activity.will_umount() {
// Quit activities match exit_reason {
result = None; ExitReason::Quit => {
break; result = None;
} break;
if activity.setup { }
// User requested activity ExitReason::EnterSetup => {
result = Some(NextActivity::SetupActivity); // User requested activity
break; result = Some(NextActivity::SetupActivity);
} break;
if activity.submit { }
// User submitted, set next activity ExitReason::Connect => {
result = Some(NextActivity::FileTransfer); // User submitted, set next activity
break; result = Some(NextActivity::FileTransfer);
break;
}
_ => { /* Nothing to do */ }
}
} }
// Sleep for ticks // Sleep for ticks
sleep(self.interval); sleep(self.interval);
@@ -197,15 +201,19 @@ impl ActivityManager {
// Draw activity // Draw activity
activity.on_draw(); activity.on_draw();
// Check if has to be terminated // Check if has to be terminated
if activity.quit { if let Some(exit_reason) = activity.will_umount() {
// Quit activities match exit_reason {
result = None; ExitReason::Quit => {
break; result = None;
} break;
if activity.disconnected { }
// User disconnected, set next activity to authentication ExitReason::Disconnect => {
result = Some(NextActivity::Authentication); // User disconnected, set next activity to authentication
break; result = Some(NextActivity::Authentication);
break;
}
_ => { /* Nothing to do */ }
}
} }
// Sleep for ticks // Sleep for ticks
sleep(self.interval); sleep(self.interval);
@@ -234,7 +242,7 @@ impl ActivityManager {
// Draw activity // Draw activity
activity.on_draw(); activity.on_draw();
// Check if activity has terminated // Check if activity has terminated
if activity.quit { if let Some(ExitReason::Quit) = activity.will_umount() {
break; break;
} }
// Sleep for ticks // Sleep for ticks

View File

@@ -33,7 +33,7 @@ extern crate crossterm;
extern crate tui; extern crate tui;
// locals // locals
use super::{Activity, Context}; use super::{Activity, Context, ExitReason};
use crate::filetransfer::FileTransferProtocol; use crate::filetransfer::FileTransferProtocol;
use crate::system::bookmarks_client::BookmarksClient; use crate::system::bookmarks_client::BookmarksClient;
use crate::ui::context::FileTransferParams; use crate::ui::context::FileTransferParams;
@@ -69,9 +69,7 @@ const STORE_KEY_LATEST_VERSION: &str = "AUTH_LATEST_VERSION";
/// ///
/// AuthActivity is the data holder for the authentication activity /// AuthActivity is the data holder for the authentication activity
pub struct AuthActivity { pub struct AuthActivity {
pub submit: bool, // becomes true after user has submitted fields exit_reason: Option<ExitReason>,
pub quit: bool, // Becomes true if user has pressed esc
pub setup: bool, // Becomes true if user has requested setup
context: Option<Context>, context: Option<Context>,
view: View, view: View,
bookmarks_client: Option<BookmarksClient>, bookmarks_client: Option<BookmarksClient>,
@@ -92,9 +90,7 @@ impl AuthActivity {
/// Instantiates a new AuthActivity /// Instantiates a new AuthActivity
pub fn new() -> AuthActivity { pub fn new() -> AuthActivity {
AuthActivity { AuthActivity {
submit: false, exit_reason: None,
quit: false,
setup: false,
context: None, context: None,
view: View::init(), view: View::init(),
bookmarks_client: None, bookmarks_client: None,
@@ -197,6 +193,15 @@ impl Activity for AuthActivity {
} }
} }
/// ### will_umount
///
/// `will_umount` is the method which must be able to report to the activity manager, whether
/// the activity should be terminated or not.
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
fn will_umount(&self) -> Option<&ExitReason> {
self.exit_reason.as_ref()
}
/// ### on_destroy /// ### on_destroy
/// ///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity. /// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.

View File

@@ -244,7 +244,7 @@ impl AuthActivity {
} }
// Enter setup // Enter setup
(_, &MSG_KEY_CTRL_C) => { (_, &MSG_KEY_CTRL_C) => {
self.setup = true; self.exit_reason = Some(super::ExitReason::EnterSetup);
None None
} }
// Save bookmark; show popup // Save bookmark; show popup
@@ -306,7 +306,7 @@ impl AuthActivity {
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(choice))) => { (COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(choice))) => {
// If choice is 0, quit termscp // If choice is 0, quit termscp
if *choice == 0 { if *choice == 0 {
self.quit = true; self.exit_reason = Some(super::ExitReason::Quit);
} }
self.umount_quit(); self.umount_quit();
None None
@@ -334,8 +334,8 @@ impl AuthActivity {
true => None, true => None,
false => Some(password), false => Some(password),
}; };
// Submit true // Set exit reason
self.submit = true; self.exit_reason = Some(super::ExitReason::Connect);
// Return None // Return None
None None
} }

View File

@@ -37,7 +37,7 @@ extern crate textwrap;
extern crate tui; extern crate tui;
// locals // locals
use super::{Activity, Context}; use super::{Activity, Context, ExitReason};
use crate::filetransfer::ftp_transfer::FtpFileTransfer; use crate::filetransfer::ftp_transfer::FtpFileTransfer;
use crate::filetransfer::scp_transfer::ScpFileTransfer; use crate::filetransfer::scp_transfer::ScpFileTransfer;
use crate::filetransfer::sftp_transfer::SftpFileTransfer; use crate::filetransfer::sftp_transfer::SftpFileTransfer;
@@ -199,8 +199,7 @@ impl Default for TransferStates {
/// ///
/// FileTransferActivity is the data holder for the file transfer activity /// FileTransferActivity is the data holder for the file transfer activity
pub struct FileTransferActivity { pub struct FileTransferActivity {
pub disconnected: bool, // Has disconnected from remote? exit_reason: Option<ExitReason>, // Exit reason
pub quit: bool, // Has quit term scp?
context: Option<Context>, // Context holder context: Option<Context>, // Context holder
view: View, // View view: View, // View
client: Box<dyn FileTransfer>, // File transfer client client: Box<dyn FileTransfer>, // File transfer client
@@ -221,8 +220,7 @@ impl FileTransferActivity {
// Get config client // Get config client
let config_client: Option<ConfigClient> = Self::init_config_client(); let config_client: Option<ConfigClient> = Self::init_config_client();
FileTransferActivity { FileTransferActivity {
disconnected: false, exit_reason: None,
quit: false,
context: None, context: None,
view: View::init(), view: View::init(),
client: match protocol { client: match protocol {
@@ -310,6 +308,15 @@ impl Activity for FileTransferActivity {
} }
} }
/// ### will_umount
///
/// `will_umount` is the method which must be able to report to the activity manager, whether
/// the activity should be terminated or not.
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
fn will_umount(&self) -> Option<&ExitReason> {
self.exit_reason.as_ref()
}
/// ### on_destroy /// ### on_destroy
/// ///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity. /// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.

View File

@@ -98,7 +98,7 @@ impl FileTransferActivity {
// Disconnect // Disconnect
let _ = self.client.disconnect(); let _ = self.client.disconnect();
// Quit // Quit
self.disconnected = true; self.exit_reason = Some(super::ExitReason::Disconnect);
} }
/// ### disconnect_and_quit /// ### disconnect_and_quit
@@ -106,7 +106,7 @@ impl FileTransferActivity {
/// disconnect from remote and then quit /// disconnect from remote and then quit
pub(super) fn disconnect_and_quit(&mut self) { pub(super) fn disconnect_and_quit(&mut self) {
self.disconnect(); self.disconnect();
self.quit = true; self.exit_reason = Some(super::ExitReason::Quit);
} }
/// ### reload_remote_dir /// ### reload_remote_dir

View File

@@ -553,7 +553,7 @@ impl FileTransferActivity {
} }
// -- fatal // -- fatal
(COMPONENT_TEXT_FATAL, &MSG_KEY_ESC) | (COMPONENT_TEXT_FATAL, &MSG_KEY_ENTER) => { (COMPONENT_TEXT_FATAL, &MSG_KEY_ESC) | (COMPONENT_TEXT_FATAL, &MSG_KEY_ENTER) => {
self.disconnected = true; self.exit_reason = Some(super::ExitReason::Disconnect);
None None
} }
// -- help // -- help

View File

@@ -33,7 +33,16 @@ pub mod auth_activity;
pub mod filetransfer_activity; pub mod filetransfer_activity;
pub mod setup_activity; pub mod setup_activity;
// Activity trait // -- Exit reason
pub enum ExitReason {
Quit,
Connect,
Disconnect,
EnterSetup,
}
// -- Activity trait
pub trait Activity { pub trait Activity {
/// ### on_create /// ### on_create
@@ -49,6 +58,13 @@ pub trait Activity {
/// This function must be called at each tick to refresh the interface /// This function must be called at each tick to refresh the interface
fn on_draw(&mut self); fn on_draw(&mut self);
/// ### will_umount
///
/// `will_umount` is the method which must be able to report to the activity manager, whether
/// the activity should be terminated or not.
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
fn will_umount(&self) -> Option<&ExitReason>;
/// ### on_destroy /// ### on_destroy
/// ///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity. /// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.

View File

@@ -35,7 +35,7 @@ extern crate crossterm;
extern crate tui; extern crate tui;
// Locals // Locals
use super::{Activity, Context}; use super::{Activity, Context, ExitReason};
use crate::ui::layout::view::View; use crate::ui::layout::view::View;
// Ext // Ext
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
@@ -71,7 +71,7 @@ enum ViewLayout {
/// ///
/// Setup activity states holder /// Setup activity states holder
pub struct SetupActivity { pub struct SetupActivity {
pub quit: bool, // Becomes true when user requests the activity to terminate exit_reason: Option<ExitReason>,
context: Option<Context>, // Context holder context: Option<Context>, // Context holder
view: View, // View view: View, // View
layout: ViewLayout, // View layout layout: ViewLayout, // View layout
@@ -86,7 +86,7 @@ impl Default for SetupActivity {
user_input_buffer.push(String::new()); user_input_buffer.push(String::new());
} }
SetupActivity { SetupActivity {
quit: false, exit_reason: None,
context: None, context: None,
view: View::init(), view: View::init(),
layout: ViewLayout::SetupForm, layout: ViewLayout::SetupForm,
@@ -142,6 +142,15 @@ impl Activity for SetupActivity {
} }
} }
/// ### will_umount
///
/// `will_umount` is the method which must be able to report to the activity manager, whether
/// the activity should be terminated or not.
/// If not, the call will return `None`, otherwise return`Some(ExitReason)`
fn will_umount(&self) -> Option<&ExitReason> {
self.exit_reason.as_ref()
}
/// ### on_destroy /// ### on_destroy
/// ///
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity. /// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.

View File

@@ -112,12 +112,12 @@ impl SetupActivity {
self.mount_error(err.as_str()); self.mount_error(err.as_str());
} }
// Exit // Exit
self.quit = true; self.exit_reason = Some(super::ExitReason::Quit);
None None
} }
(COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(1))) => { (COMPONENT_RADIO_QUIT, Msg::OnSubmit(Payload::Unsigned(1))) => {
// Quit // Quit
self.quit = true; self.exit_reason = Some(super::ExitReason::Quit);
self.umount_quit(); self.umount_quit();
None None
} }