diff --git a/src/ui/activities/auth_activity/callbacks.rs b/src/ui/activities/auth_activity/callbacks.rs index ca859e1..5fe802d 100644 --- a/src/ui/activities/auth_activity/callbacks.rs +++ b/src/ui/activities/auth_activity/callbacks.rs @@ -53,6 +53,8 @@ impl AuthActivity { /// /// Callback used to save bookmark with name pub(super) fn callback_save_bookmark(&mut self, input: String) { - self.save_bookmark(input); + if !input.is_empty() { + self.save_bookmark(input); + } } } diff --git a/src/ui/activities/auth_activity/input.rs b/src/ui/activities/auth_activity/input.rs index a1dba71..4ced128 100644 --- a/src/ui/activities/auth_activity/input.rs +++ b/src/ui/activities/auth_activity/input.rs @@ -160,8 +160,10 @@ impl AuthActivity { self.input_mode = InputMode::Popup(PopupType::Help); } 'S' | 's' => { + // Default choice option to no + self.choice_opt = DialogYesNoOption::No; // Save bookmark as... - self.input_mode = InputMode::Popup(PopupType::Input( + self.input_mode = InputMode::Popup(PopupType::SaveBookmark( String::from("Save bookmark as..."), AuthActivity::callback_save_bookmark, )); @@ -282,8 +284,10 @@ impl AuthActivity { self.input_mode = InputMode::Popup(PopupType::Help); } 'S' | 's' => { + // Default choice option to no + self.choice_opt = DialogYesNoOption::No; // Save bookmark as... - self.input_mode = InputMode::Popup(PopupType::Input( + self.input_mode = InputMode::Popup(PopupType::SaveBookmark( String::from("Save bookmark as..."), AuthActivity::callback_save_bookmark, )); @@ -363,8 +367,10 @@ impl AuthActivity { self.input_mode = InputMode::Popup(PopupType::Help); } 'S' | 's' => { + // Default choice option to no + self.choice_opt = DialogYesNoOption::No; // Save bookmark as... - self.input_mode = InputMode::Popup(PopupType::Input( + self.input_mode = InputMode::Popup(PopupType::SaveBookmark( String::from("Save bookmark as..."), AuthActivity::callback_save_bookmark, )); @@ -383,7 +389,7 @@ impl AuthActivity { match ptype { PopupType::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev), PopupType::Help => self.handle_input_event_mode_popup_help(ev), - PopupType::Input(_, cb) => self.handle_input_event_mode_popup_input(ev, cb), + PopupType::SaveBookmark(_, cb) => self.handle_input_event_mode_popup_save_bookmark(ev, cb), PopupType::YesNo(_, yes_cb, no_cb) => { self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb) } @@ -418,10 +424,10 @@ impl AuthActivity { } } - /// ### handle_input_event_mode_popup_input + /// ### handle_input_event_mode_popup_save_bookmark /// - /// Input event handler for input popup - pub(super) fn handle_input_event_mode_popup_input( + /// Input event handler for SaveBookmark popup + pub(super) fn handle_input_event_mode_popup_save_bookmark( &mut self, ev: &InputEvent, cb: OnInputSubmitCallback, @@ -435,6 +441,8 @@ impl AuthActivity { self.input_txt.clear(); // Set mode back to form self.input_mode = InputMode::Form; + // Reset choice option to yes + self.choice_opt = DialogYesNoOption::Yes; } KeyCode::Enter => { // Submit @@ -445,7 +453,11 @@ impl AuthActivity { self.input_mode = InputMode::Form; // Call cb cb(self, input_text); + // Reset choice option to yes + self.choice_opt = DialogYesNoOption::Yes; } + KeyCode::Left => self.choice_opt = DialogYesNoOption::Yes, // Move yes/no with arrows + KeyCode::Right => self.choice_opt = DialogYesNoOption::No, // Move yes/no with arrows KeyCode::Char(ch) => self.input_txt.push(ch), KeyCode::Backspace => { let _ = self.input_txt.pop(); diff --git a/src/ui/activities/auth_activity/layout.rs b/src/ui/activities/auth_activity/layout.rs index e13c703..ed32e53 100644 --- a/src/ui/activities/auth_activity/layout.rs +++ b/src/ui/activities/auth_activity/layout.rs @@ -126,7 +126,7 @@ impl AuthActivity { let (width, height): (u16, u16) = match popup { PopupType::Alert(_, _) => (50, 10), PopupType::Help => (50, 70), - PopupType::Input(_, _) => (40, 10), + PopupType::SaveBookmark(_, _) => (40, 20), PopupType::YesNo(_, _, _) => (30, 10), }; let popup_area: Rect = self.draw_popup_area(f.size(), width, height); @@ -137,12 +137,27 @@ impl AuthActivity { popup_area, ), PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area), - PopupType::Input(txt, _) => { - f.render_widget(self.draw_popup_input(txt.clone()), popup_area); + PopupType::SaveBookmark(txt, _) => { + let popup_chunks = Layout::default() + .direction(Direction::Vertical) + .margin(1) + .constraints( + [ + Constraint::Length(3), // Input form + Constraint::Length(2), // Yes/No + ] + .as_ref(), + ) + .split(popup_area); + let (input, yes_no): (Paragraph, Tabs) = + self.draw_popup_save_bookmark(txt.clone()); + // Render parts + f.render_widget(input, popup_chunks[0]); + f.render_widget(yes_no, popup_chunks[1]); // Set cursor f.set_cursor( - popup_area.x + self.input_txt.width() as u16 + 1, - popup_area.y + 1, + popup_chunks[0].x + self.input_txt.width() as u16 + 1, + popup_chunks[0].y + 1, ) } PopupType::YesNo(txt, _, _) => { @@ -414,10 +429,29 @@ impl AuthActivity { /// ### draw_popup_input /// /// Draw input popup - pub(super) fn draw_popup_input(&self, text: String) -> Paragraph { - Paragraph::new(self.input_txt.as_ref()) - .style(Style::default().fg(Color::White)) - .block(Block::default().borders(Borders::ALL).title(text)) + pub(super) fn draw_popup_save_bookmark(&self, text: String) -> (Paragraph, Tabs) { + let input: Paragraph = Paragraph::new(self.input_txt.as_ref()) + .style(Style::default().fg(Color::LightYellow)) + .block(Block::default().borders(Borders::ALL).title(text)); + let choices: Vec = vec![Spans::from("Yes"), Spans::from("No")]; + let index: usize = match self.choice_opt { + DialogYesNoOption::Yes => 0, + DialogYesNoOption::No => 1, + }; + let tabs: Tabs = Tabs::new(choices) + .block( + Block::default() + .borders(Borders::ALL) + .title("Save password?"), + ) + .select(index) + .style(Style::default()) + .highlight_style( + Style::default() + .add_modifier(Modifier::BOLD) + .fg(Color::LightRed), + ); + (input, tabs) } /// ### draw_popup_yesno diff --git a/src/ui/activities/auth_activity/mod.rs b/src/ui/activities/auth_activity/mod.rs index ef08e1a..08ed4bc 100644 --- a/src/ui/activities/auth_activity/mod.rs +++ b/src/ui/activities/auth_activity/mod.rs @@ -76,7 +76,7 @@ enum DialogYesNoOption { enum PopupType { Alert(Color, String), // Show a message displaying text with the provided color Help, // Help page - Input(String, OnInputSubmitCallback), // Input description; Callback for submit + SaveBookmark(String, OnInputSubmitCallback), // Input description; Callback for submit YesNo(String, DialogCallback, DialogCallback), // Yes, no callback }