diff --git a/src/tui/app.rs b/src/tui/app.rs index d071c73..2d971dd 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -241,6 +241,7 @@ impl<'a> Component for App<'a> { }; match action { Action::Quit | Action::Handled => return Ok(action), + Action::Save => self.editor.save()?, _ => {} } Ok(Action::Noop) diff --git a/src/tui/title_bar.rs b/src/tui/title_bar.rs index cb615f5..929bdff 100644 --- a/src/tui/title_bar.rs +++ b/src/tui/title_bar.rs @@ -1,4 +1,8 @@ +use crate::tui::component::Action::Pass; use crate::tui::component::{Action, Component, ComponentState}; +use crate::tui::title_bar::MenuBarItemOption::{ + About, Exit, Reload, Save, ShowHideExplorer, ShowHideLogger, +}; use ratatui::buffer::Buffer; use ratatui::crossterm::event::{KeyCode, KeyEvent}; use ratatui::layout::Rect; @@ -16,6 +20,29 @@ enum MenuBarItem { Help, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr, EnumIter)] +enum MenuBarItemOption { + Save, + Reload, + Exit, + ShowHideExplorer, + ShowHideLogger, + About, +} + +impl MenuBarItemOption { + fn id(&self) -> &str { + match self { + MenuBarItemOption::Save => "Save", + MenuBarItemOption::Reload => "Reload", + MenuBarItemOption::Exit => "Exit", + MenuBarItemOption::ShowHideExplorer => "Show / hide explorer", + MenuBarItemOption::ShowHideLogger => "Show / hide logger", + MenuBarItemOption::About => "About", + } + } +} + impl MenuBarItem { pub fn next(self) -> Self { let cur = self as usize; @@ -37,11 +64,11 @@ impl MenuBarItem { } } - pub fn options(&self) -> &[&str] { + pub fn options(&self) -> &[MenuBarItemOption] { match self { - MenuBarItem::File => &["Save", "Reload"], - MenuBarItem::View => &["Show/hide explorer", "Show/hide logger"], - MenuBarItem::Help => &["About"], + MenuBarItem::File => &[Save, Reload, Exit], + MenuBarItem::View => &[ShowHideExplorer, ShowHideLogger], + MenuBarItem::Help => &[About], } } } @@ -91,7 +118,7 @@ impl MenuBar { ) { let popup_area = Self::rect_under_option(title_bar_anchor, area, 40, 10); Clear::default().render(popup_area, buf); - let options = opened.options().iter().map(|i| ListItem::new(*i)); + let options = opened.options().iter().map(|i| ListItem::new(i.id())); StatefulWidget::render( List::new(options) .block(Block::bordered().title(self.selected.id())) @@ -149,8 +176,18 @@ impl Component for MenuBar { Ok(Action::Handled) } KeyCode::Enter => { - // TODO: Handle action for the item. - Ok(Action::Handled) + if let Some(selected) = self.list_state.selected() { + let seletion = self.selected.options()[selected]; + return match seletion { + Save => Ok(Action::Save), + Exit => Ok(Action::Quit), + Reload => Ok(Action::Noop), // TODO + ShowHideExplorer => Ok(Action::Noop), // TODO + ShowHideLogger => Ok(Action::Noop), // TODO + About => Ok(Action::Noop), // TODO + }; + } + Ok(Action::Noop) } KeyCode::Esc | KeyCode::Char('q') => { self.opened = None;