diff --git a/src/interface/app.rs b/src/interface/app.rs index e755ed63..bb7933dd 100644 --- a/src/interface/app.rs +++ b/src/interface/app.rs @@ -21,6 +21,8 @@ const LARGE_APPLICATION_WIDTH: u16 = 75; /// Application result type. pub type AppResult = Result>; +const DEFAULT_QRCODE_LABEL: &'static str = "Press enter to copy the OTP URI code"; + /// Application. pub struct App<'a> { /// Is the application running? @@ -36,6 +38,9 @@ pub struct App<'a> { pub(crate) search_query: String, pub(crate) focus: Focus, pub(crate) popup: Popup, + + /// Info text in the QRCode page + pub(crate) qr_code_page_label: &'static str, } pub struct Popup { @@ -60,7 +65,7 @@ impl<'a> App<'a> { progress: percentage(), label_text: String::from(""), print_percentage: true, - current_page: Main, + current_page: Page::default(), search_query: String::from(""), focus: Focus::MainPage, popup: Popup { @@ -69,9 +74,16 @@ impl<'a> App<'a> { percent_x: 60, percent_y: 20, }, + qr_code_page_label: DEFAULT_QRCODE_LABEL, } } + pub(crate) fn reset(&mut self) { + self.current_page = Page::default(); + self.print_percentage = true; + self.qr_code_page_label = DEFAULT_QRCODE_LABEL; + } + /// Handles the tick event of the terminal. pub fn tick(&mut self, force_update: bool) { // Update progress bar @@ -105,11 +117,15 @@ impl<'a> App<'a> { } else { format!("{} - {}", &element.issuer, &element.label) }; - Paragraph::new(element.get_qrcode()) - .block(Block::default().title(title).borders(Borders::ALL)) - .style(Style::default().fg(Color::White).bg(Color::Reset)) - .alignment(Alignment::Center) - .wrap(Wrap { trim: true }) + Paragraph::new(format!( + "{}\n{}", + element.get_qrcode(), + self.qr_code_page_label + )) + .block(Block::default().title(title).borders(Borders::ALL)) + .style(Style::default().fg(Color::White).bg(Color::Reset)) + .alignment(Alignment::Center) + .wrap(Wrap { trim: true }) }) .unwrap_or_else(|| { Paragraph::new("No element is selected") diff --git a/src/interface/handlers/main_window.rs b/src/interface/handlers/main_window.rs index 6a222830..262c4467 100644 --- a/src/interface/handlers/main_window.rs +++ b/src/interface/handlers/main_window.rs @@ -5,6 +5,7 @@ use super::{ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use crate::{ + clipboard::copy_string_to_clipboard, interface::{ app::{App, Popup}, enums::{Focus, Page, PopupAction}, @@ -102,10 +103,25 @@ pub(super) fn main_handler(key_event: KeyEvent, app: &mut App) { KeyCode::Char('/') => app.focus = Focus::SearchBar, - KeyCode::Enter => { - app.label_text = copy_selected_code_to_clipboard(app); - app.print_percentage = false; - } + KeyCode::Enter => match app.current_page { + Main => { + app.label_text = copy_selected_code_to_clipboard(app); + app.print_percentage = false; + } + Qrcode => { + let selected_element = app + .table + .state + .selected() + .and_then(|index| app.database.elements_ref().get(index)); + + if let Some(element) = selected_element { + let otp_uri = element.get_otpauth_uri(); + let _ = copy_string_to_clipboard(&otp_uri); + app.qr_code_page_label = "OTP URI Copied to clipboard"; + } + } + }, _ => {} } } @@ -128,9 +144,8 @@ fn handle_counter_switch(app: &mut App, increment: bool) { } fn handle_switch_page(app: &mut App, page: Page) { - let default_page = Main; if app.current_page == page { - app.current_page = default_page; + app.reset(); } else { app.current_page = page; }