Add type-based statefulness for ui
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 58s
Cargo CI / Lint (pull_request) Failing after 42s

This commit is contained in:
Wojciech Kozlowski 2024-01-28 11:49:41 +01:00
parent ba85505c9a
commit 454f9ac374
3 changed files with 134 additions and 60 deletions

View File

@ -5,17 +5,21 @@ use mockall::automock;
use crate::tui::{ use crate::tui::{
event::{Event, EventError, EventReceiver}, event::{Event, EventError, EventReceiver},
ui::IUi, ui::{IUiBrowse, IUiCore, IUiInfo, Ui},
}; };
#[cfg_attr(test, automock)] #[cfg_attr(test, automock)]
pub trait IEventHandler<UI> { pub trait IEventHandler<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
fn handle_next_event(&self, ui: &mut UI) -> Result<(), EventError>; fn handle_next_event(&self, ui: Ui<BS, IS>) -> Result<Ui<BS, IS>, EventError>;
} }
trait IEventHandlerPrivate<UI> { trait IEventHandlerPrivate<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
fn handle_key_event(ui: &mut UI, key_event: KeyEvent) -> Result<(), EventError>; fn handle_key_event(ui: Ui<BS, IS>, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError>;
fn quit(ui: &mut UI) -> Result<(), EventError>; fn handle_browse_key_event(ui: BS, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError>;
fn handle_info_key_event(ui: IS, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError>;
fn handle_core_key_event<C: IUiCore>(ui: &mut C, key_event: KeyEvent)
-> Result<(), EventError>;
fn quit<C: IUiCore>(ui: &mut C) -> Result<(), EventError>;
} }
pub struct EventHandler { pub struct EventHandler {
@ -29,48 +33,72 @@ impl EventHandler {
} }
} }
impl<UI: IUi> IEventHandler<UI> for EventHandler { impl<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> IEventHandler<BS, IS> for EventHandler {
fn handle_next_event(&self, ui: &mut UI) -> Result<(), EventError> { fn handle_next_event(&self, mut ui: Ui<BS, IS>) -> Result<Ui<BS, IS>, EventError> {
match self.events.recv()? { match self.events.recv()? {
Event::Key(key_event) => Self::handle_key_event(ui, key_event)?, Event::Key(key_event) => ui = Self::handle_key_event(ui, key_event)?,
Event::Mouse(_) => {} Event::Mouse(_) => {}
Event::Resize(_, _) => {} Event::Resize(_, _) => {}
}; };
Ok(()) Ok(ui)
} }
} }
impl<UI: IUi> IEventHandlerPrivate<UI> for EventHandler { impl<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> IEventHandlerPrivate<BS, IS> for EventHandler {
fn handle_key_event(ui: &mut UI, key_event: KeyEvent) -> Result<(), EventError> { fn handle_key_event(ui: Ui<BS, IS>, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError> {
match ui {
Ui::Browse(browse) => Self::handle_browse_key_event(browse, key_event),
Ui::Info(info) => Self::handle_info_key_event(info, key_event),
}
}
fn handle_browse_key_event(mut ui: BS, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError> {
match key_event.code {
// Category change.
KeyCode::Left => ui.decrement_category(),
KeyCode::Right => ui.increment_category(),
// Selection change.
KeyCode::Up => ui.decrement_selection(),
KeyCode::Down => ui.increment_selection(),
// Toggle overlay.
KeyCode::Char('m') | KeyCode::Char('M') => {
return Ok(ui.display_info_overlay());
}
// Other keys.
_ => <Self as IEventHandlerPrivate<BS, IS>>::handle_core_key_event(&mut ui, key_event)?,
}
Ok(Ui::Browse(ui))
}
fn handle_info_key_event(mut ui: IS, key_event: KeyEvent) -> Result<Ui<BS, IS>, EventError> {
match key_event.code {
// Toggle overlay.
KeyCode::Char('m') | KeyCode::Char('M') => {
return Ok(ui.hide_info_overlay());
}
// Other keys.
_ => <Self as IEventHandlerPrivate<BS, IS>>::handle_core_key_event(&mut ui, key_event)?,
}
Ok(Ui::Info(ui))
}
fn handle_core_key_event<C: IUiCore>(
ui: &mut C,
key_event: KeyEvent,
) -> Result<(), EventError> {
match key_event.code { match key_event.code {
// Exit application on `ESC` or `q`. // Exit application on `ESC` or `q`.
KeyCode::Esc | KeyCode::Char('q') => { KeyCode::Esc | KeyCode::Char('q') => {
Self::quit(ui)?; <Self as IEventHandlerPrivate<BS, IS>>::quit(ui)?;
} }
// Exit application on `Ctrl-C`. // Exit application on `Ctrl-C`.
KeyCode::Char('c') | KeyCode::Char('C') => { KeyCode::Char('c') | KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL { if key_event.modifiers == KeyModifiers::CONTROL {
Self::quit(ui)?; <Self as IEventHandlerPrivate<BS, IS>>::quit(ui)?;
} }
} }
// Category change.
KeyCode::Left => {
ui.decrement_category();
}
KeyCode::Right => {
ui.increment_category();
}
// Selection change.
KeyCode::Up => {
ui.decrement_selection();
}
KeyCode::Down => {
ui.increment_selection();
}
// Toggle overlay.
KeyCode::Char('m') | KeyCode::Char('M') => {
ui.toggle_overlay();
}
// Other keys. // Other keys.
_ => {} _ => {}
} }
@ -78,7 +106,7 @@ impl<UI: IUi> IEventHandlerPrivate<UI> for EventHandler {
Ok(()) Ok(())
} }
fn quit(ui: &mut UI) -> Result<(), EventError> { fn quit<C: IUiCore>(ui: &mut C) -> Result<(), EventError> {
ui.quit(); ui.quit();
ui.save()?; ui.save()?;
Ok(()) Ok(())

View File

@ -15,7 +15,7 @@ use std::marker::PhantomData;
use self::event::EventError; use self::event::EventError;
use self::handler::IEventHandler; use self::handler::IEventHandler;
use self::listener::IEventListener; use self::listener::IEventListener;
use self::ui::IUi; use self::ui::{IUiBrowse, IUiInfo, Ui};
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Error { pub enum Error {
@ -43,12 +43,12 @@ impl From<EventError> for Error {
} }
} }
pub struct Tui<B: Backend, UI> { pub struct Tui<B: Backend, BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
terminal: Terminal<B>, terminal: Terminal<B>,
_phantom: PhantomData<UI>, _phantom: PhantomData<Ui<BS, IS>>,
} }
impl<B: Backend, UI: IUi> Tui<B, UI> { impl<B: Backend, BS: IUiBrowse<IS>, IS: IUiInfo<BS>> Tui<B, BS, IS> {
fn init(&mut self) -> Result<(), Error> { fn init(&mut self) -> Result<(), Error> {
self.terminal.hide_cursor()?; self.terminal.hide_cursor()?;
self.terminal.clear()?; self.terminal.clear()?;
@ -65,10 +65,14 @@ impl<B: Backend, UI: IUi> Tui<B, UI> {
self.exit(); self.exit();
} }
fn main_loop(&mut self, mut ui: UI, handler: impl IEventHandler<UI>) -> Result<(), Error> { fn main_loop(
&mut self,
mut ui: Ui<BS, IS>,
handler: impl IEventHandler<BS, IS>,
) -> Result<(), Error> {
while ui.is_running() { while ui.is_running() {
self.terminal.draw(|frame| ui.render(frame))?; self.terminal.draw(|frame| ui.render(frame))?;
handler.handle_next_event(&mut ui)?; ui = handler.handle_next_event(ui)?;
} }
Ok(()) Ok(())
@ -76,8 +80,8 @@ impl<B: Backend, UI: IUi> Tui<B, UI> {
fn main( fn main(
term: Terminal<B>, term: Terminal<B>,
ui: UI, ui: Ui<BS, IS>,
handler: impl IEventHandler<UI>, handler: impl IEventHandler<BS, IS>,
listener: impl IEventListener, listener: impl IEventListener,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut tui = Tui { let mut tui = Tui {
@ -135,8 +139,8 @@ impl<B: Backend, UI: IUi> Tui<B, UI> {
pub fn run( pub fn run(
term: Terminal<B>, term: Terminal<B>,
ui: UI, ui: Ui<BS, IS>,
handler: impl IEventHandler<UI>, handler: impl IEventHandler<BS, IS>,
listener: impl IEventListener, listener: impl IEventListener,
) -> Result<(), Error> { ) -> Result<(), Error> {
Self::enable()?; Self::enable()?;

View File

@ -35,20 +35,54 @@ impl From<musichoard::Error> for UiError {
} }
} }
pub trait IUi { pub enum Ui<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
Browse(BS),
Info(IS),
}
impl<MH: IMusicHoard> Ui<UiImpl<MH>, UiImpl<MH>> {
pub fn new(mh: MH) -> Result<Self, Error> {
Ok(Ui::Browse(UiImpl::new(mh)?))
}
}
impl<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> Ui<BS, IS> {
pub fn is_running(&self) -> bool {
match self {
Ui::Browse(ref browse) => browse.is_running(),
Ui::Info(ref info) => info.is_running(),
}
}
pub fn render<B: Backend>(&mut self, frame: &mut Frame<'_, B>) {
match self {
Ui::Browse(ref mut browse) => browse.render_browse_frame(frame),
Ui::Info(ref mut info) => info.render_info_frame(frame),
}
}
}
pub trait IUiCore {
fn is_running(&self) -> bool; fn is_running(&self) -> bool;
fn quit(&mut self); fn quit(&mut self);
fn save(&mut self) -> Result<(), UiError>; fn save(&mut self) -> Result<(), UiError>;
}
pub trait IUiBrowse<IS: IUiInfo<Self>>: IUiCore + Sized {
fn increment_category(&mut self); fn increment_category(&mut self);
fn decrement_category(&mut self); fn decrement_category(&mut self);
fn increment_selection(&mut self); fn increment_selection(&mut self);
fn decrement_selection(&mut self); fn decrement_selection(&mut self);
fn toggle_overlay(&mut self); fn display_info_overlay(self) -> Ui<Self, IS>;
fn render<B: Backend>(&mut self, frame: &mut Frame<'_, B>);
fn render_browse_frame<B: Backend>(&mut self, frame: &mut Frame<'_, B>);
}
pub trait IUiInfo<BS: IUiBrowse<Self>>: IUiCore + Sized {
fn hide_info_overlay(self) -> Ui<BS, Self>;
fn render_info_frame<B: Backend>(&mut self, frame: &mut Frame<'_, B>);
} }
struct TrackSelection { struct TrackSelection {
@ -278,10 +312,9 @@ impl Selection {
} }
} }
pub struct Ui<MH> { pub struct UiImpl<MH> {
music_hoard: MH, music_hoard: MH,
selection: Selection, selection: Selection,
overlay: bool,
running: bool, running: bool,
} }
@ -536,15 +569,14 @@ impl<'a, 'b> TrackState<'a, 'b> {
} }
} }
impl<MH: IMusicHoard> Ui<MH> { impl<MH: IMusicHoard> UiImpl<MH> {
pub fn new(mut music_hoard: MH) -> Result<Self, Error> { pub fn new(mut music_hoard: MH) -> Result<Self, Error> {
music_hoard.load_from_database()?; music_hoard.load_from_database()?;
music_hoard.rescan_library()?; music_hoard.rescan_library()?;
let selection = Selection::new(Some(music_hoard.get_collection())); let selection = Selection::new(Some(music_hoard.get_collection()));
Ok(Ui { Ok(UiImpl {
music_hoard, music_hoard,
selection, selection,
overlay: false,
running: true, running: true,
}) })
} }
@ -692,7 +724,7 @@ impl<MH: IMusicHoard> Ui<MH> {
} }
} }
impl<MH: IMusicHoard> IUi for Ui<MH> { impl<MH: IMusicHoard> IUiCore for UiImpl<MH> {
fn is_running(&self) -> bool { fn is_running(&self) -> bool {
self.running self.running
} }
@ -705,7 +737,9 @@ impl<MH: IMusicHoard> IUi for Ui<MH> {
self.music_hoard.save_to_database()?; self.music_hoard.save_to_database()?;
Ok(()) Ok(())
} }
}
impl<MH: IMusicHoard> IUiBrowse<Self> for UiImpl<MH> {
fn increment_category(&mut self) { fn increment_category(&mut self) {
self.selection.increment_category(); self.selection.increment_category();
} }
@ -724,15 +758,23 @@ impl<MH: IMusicHoard> IUi for Ui<MH> {
.decrement_selection(self.music_hoard.get_collection()); .decrement_selection(self.music_hoard.get_collection());
} }
fn toggle_overlay(&mut self) { fn display_info_overlay(self) -> Ui<Self, Self> {
self.overlay = !self.overlay; Ui::Info(self)
} }
fn render<B: Backend>(&mut self, frame: &mut Frame<'_, B>) { fn render_browse_frame<B: Backend>(&mut self, frame: &mut Frame<'_, B>) {
self.render_collection(frame); self.render_collection(frame);
if self.overlay {
self.render_overlay(frame);
} }
}
impl<MH: IMusicHoard> IUiInfo<Self> for UiImpl<MH> {
fn hide_info_overlay(self) -> Ui<Self, Self> {
Ui::Browse(self)
}
fn render_info_frame<B: Backend>(&mut self, frame: &mut Frame<'_, B>) {
self.render_collection(frame);
self.render_overlay(frame);
} }
} }