Redraw interface only if needed

This commit is contained in:
ChristianVisintin
2020-11-30 14:38:14 +01:00
parent 015a17d9f7
commit b5c47b9470
2 changed files with 76 additions and 58 deletions

View File

@@ -82,6 +82,7 @@ pub struct AuthActivity {
input_mode: InputMode, input_mode: InputMode,
popup_message: Option<String>, popup_message: Option<String>,
password_placeholder: String, password_placeholder: String,
redraw: bool, // Should ui actually be redrawned?
} }
impl AuthActivity { impl AuthActivity {
@@ -102,6 +103,7 @@ impl AuthActivity {
input_mode: InputMode::Text, input_mode: InputMode::Text,
popup_message: None, popup_message: None,
password_placeholder: String::new(), password_placeholder: String::new(),
redraw: true, // True at startup
} }
} }
@@ -432,69 +434,77 @@ impl Activity for AuthActivity {
} }
// Start catching Input Events // Start catching Input Events
if let Ok(input_events) = self.context.as_ref().unwrap().input_hnd.fetch_events() { if let Ok(input_events) = self.context.as_ref().unwrap().input_hnd.fetch_events() {
if input_events.len() > 0 {
self.redraw = true; // Set redraw to true if there is at least one event
}
// Iterate over input events // Iterate over input events
for event in input_events.iter() { for event in input_events.iter() {
self.handle_input_event(event); self.handle_input_event(event);
} }
} }
// Determine input mode // Redraw if necessary
self.input_mode = self.select_input_mode(); if self.redraw {
// draw interface // Determine input mode
let mut ctx: Context = self.context.take().unwrap(); self.input_mode = self.select_input_mode();
let _ = ctx.terminal.draw(|f| { // draw interface
let chunks = Layout::default() let mut ctx: Context = self.context.take().unwrap();
.direction(Direction::Vertical) let _ = ctx.terminal.draw(|f| {
.margin(2) let chunks = Layout::default()
.constraints( .direction(Direction::Vertical)
[ .margin(2)
Constraint::Length(5), .constraints(
Constraint::Length(3), [
Constraint::Length(3), Constraint::Length(5),
Constraint::Length(3), Constraint::Length(3),
Constraint::Length(3), Constraint::Length(3),
Constraint::Length(3), Constraint::Length(3),
Constraint::Length(3), Constraint::Length(3),
] Constraint::Length(3),
.as_ref(), Constraint::Length(3),
) ]
.split(f.size()); .as_ref(),
// Draw header )
f.render_widget(self.draw_header(), chunks[0]); .split(f.size());
// Draw input fields // Draw header
f.render_widget(self.draw_remote_address(), chunks[1]); f.render_widget(self.draw_header(), chunks[0]);
f.render_widget(self.draw_remote_port(), chunks[2]); // Draw input fields
f.render_widget(self.draw_protocol_select(), chunks[3]); f.render_widget(self.draw_remote_address(), chunks[1]);
f.render_widget(self.draw_protocol_username(), chunks[4]); f.render_widget(self.draw_remote_port(), chunks[2]);
f.render_widget(self.draw_protocol_password(), chunks[5]); f.render_widget(self.draw_protocol_select(), chunks[3]);
// Draw footer f.render_widget(self.draw_protocol_username(), chunks[4]);
f.render_widget(self.draw_footer(), chunks[6]); f.render_widget(self.draw_protocol_password(), chunks[5]);
if self.popup_message.is_some() { // Draw footer
let (popup, popup_area): (Paragraph, Rect) = self.draw_popup(f.size()); f.render_widget(self.draw_footer(), chunks[6]);
f.render_widget(Clear, popup_area); //this clears out the background if self.popup_message.is_some() {
f.render_widget(popup, popup_area); let (popup, popup_area): (Paragraph, Rect) = self.draw_popup(f.size());
} f.render_widget(Clear, popup_area); //this clears out the background
// Set cursor f.render_widget(popup, popup_area);
match self.selected_field {
InputField::Address => f.set_cursor(
chunks[1].x + self.address.width() as u16 + 1,
chunks[1].y + 1,
),
InputField::Port => {
f.set_cursor(chunks[2].x + self.port.width() as u16 + 1, chunks[2].y + 1)
} }
InputField::Username => f.set_cursor( // Set cursor
chunks[4].x + self.username.width() as u16 + 1, match self.selected_field {
chunks[4].y + 1, InputField::Address => f.set_cursor(
), chunks[1].x + self.address.width() as u16 + 1,
InputField::Password => f.set_cursor( chunks[1].y + 1,
chunks[5].x + self.password_placeholder.width() as u16 + 1, ),
chunks[5].y + 1, InputField::Port => {
), f.set_cursor(chunks[2].x + self.port.width() as u16 + 1, chunks[2].y + 1)
_ => {} }
} InputField::Username => f.set_cursor(
}); chunks[4].x + self.username.width() as u16 + 1,
// Reset ctx chunks[4].y + 1,
self.context = Some(ctx); ),
InputField::Password => f.set_cursor(
chunks[5].x + self.password_placeholder.width() as u16 + 1,
chunks[5].y + 1,
),
_ => {}
}
});
// Reset ctx
self.context = Some(ctx);
// Set redraw to false
self.redraw = false;
}
} }
/// ### on_destroy /// ### on_destroy

View File

@@ -2235,6 +2235,7 @@ impl Activity for FileTransferActivity {
/// `on_draw` is the function which draws the graphical interface. /// `on_draw` is the function which draws the graphical interface.
/// 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) {
let mut redraw: bool = false; // Should ui actually be redrawned?
// Context must be something // Context must be something
if self.context.is_none() { if self.context.is_none() {
return; return;
@@ -2254,16 +2255,23 @@ impl Activity for FileTransferActivity {
self.draw(); self.draw();
// Connect to remote // Connect to remote
self.connect(); self.connect();
// Redraw
redraw = true;
} }
// Handle input events // Handle input events
if let Ok(event) = self.context.as_ref().unwrap().input_hnd.read_event() { if let Ok(event) = self.context.as_ref().unwrap().input_hnd.read_event() {
// Iterate over input events // Iterate over input events
if let Some(event) = event { if let Some(event) = event {
// Handle event
self.handle_input_event(&event); self.handle_input_event(&event);
// Set redraw to true
redraw = true;
} }
} }
// @! draw interface // @! draw interface
self.draw(); if redraw {
self.draw();
}
} }
/// ### on_destroy /// ### on_destroy