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

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

View File

@ -5,17 +5,21 @@ use mockall::automock;
use crate::tui::{
event::{Event, EventError, EventReceiver},
ui::IUi,
ui::{IUiBrowse, IUiCore, IUiInfo, Ui},
};
#[cfg_attr(test, automock)]
pub trait IEventHandler<UI> {
fn handle_next_event(&self, ui: &mut UI) -> Result<(), EventError>;
pub trait IEventHandler<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
fn handle_next_event(&self, ui: Ui<BS, IS>) -> Result<Ui<BS, IS>, EventError>;
}
trait IEventHandlerPrivate<UI> {
fn handle_key_event(ui: &mut UI, key_event: KeyEvent) -> Result<(), EventError>;
fn quit(ui: &mut UI) -> Result<(), EventError>;
trait IEventHandlerPrivate<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
fn handle_key_event(ui: Ui<BS, IS>, key_event: KeyEvent) -> Result<Ui<BS, IS>, 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 {
@ -29,48 +33,72 @@ impl EventHandler {
}
}
impl<UI: IUi> IEventHandler<UI> for EventHandler {
fn handle_next_event(&self, ui: &mut UI) -> Result<(), EventError> {
impl<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> IEventHandler<BS, IS> for EventHandler {
fn handle_next_event(&self, mut ui: Ui<BS, IS>) -> Result<Ui<BS, IS>, EventError> {
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::Resize(_, _) => {}
};
Ok(())
Ok(ui)
}
}
impl<UI: IUi> IEventHandlerPrivate<UI> for EventHandler {
fn handle_key_event(ui: &mut UI, key_event: KeyEvent) -> Result<(), EventError> {
impl<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> IEventHandlerPrivate<BS, IS> for EventHandler {
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 {
// Exit application on `ESC` or `q`.
KeyCode::Esc | KeyCode::Char('q') => {
Self::quit(ui)?;
<Self as IEventHandlerPrivate<BS, IS>>::quit(ui)?;
}
// Exit application on `Ctrl-C`.
KeyCode::Char('c') | KeyCode::Char('C') => {
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.
_ => {}
}
@ -78,7 +106,7 @@ impl<UI: IUi> IEventHandlerPrivate<UI> for EventHandler {
Ok(())
}
fn quit(ui: &mut UI) -> Result<(), EventError> {
fn quit<C: IUiCore>(ui: &mut C) -> Result<(), EventError> {
ui.quit();
ui.save()?;
Ok(())

View File

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

View File

@ -35,20 +35,48 @@ impl From<musichoard::Error> for UiError {
}
}
pub trait IUi {
pub enum Ui<BS: IUiBrowse<IS>, IS: IUiInfo<BS>> {
Browse(BS),
Info(IS),
}
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 quit(&mut self);
fn save(&mut self) -> Result<(), UiError>;
}
pub trait IUiBrowse<IS: IUiInfo<Self>>: IUiCore + Sized {
fn increment_category(&mut self);
fn decrement_category(&mut self);
fn increment_selection(&mut self);
fn decrement_selection(&mut self);
fn toggle_overlay(&mut self);
fn render<B: Backend>(&mut self, frame: &mut Frame<'_, B>);
fn display_info_overlay(self) -> Ui<Self, IS>;
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 {
@ -278,10 +306,9 @@ impl Selection {
}
}
pub struct Ui<MH> {
pub struct UiImpl<MH> {
music_hoard: MH,
selection: Selection,
overlay: bool,
running: bool,
}
@ -536,15 +563,20 @@ impl<'a, 'b> TrackState<'a, 'b> {
}
}
impl<MH: IMusicHoard> Ui<MH> {
impl<MH: IMusicHoard> Ui<UiImpl<MH>, UiImpl<MH>> {
pub fn new(mh: MH) -> Result<Self, Error> {
Ok(Ui::Browse(UiImpl::new(mh)?))
}
}
impl<MH: IMusicHoard> UiImpl<MH> {
pub fn new(mut music_hoard: MH) -> Result<Self, Error> {
music_hoard.load_from_database()?;
music_hoard.rescan_library()?;
let selection = Selection::new(Some(music_hoard.get_collection()));
Ok(Ui {
Ok(UiImpl {
music_hoard,
selection,
overlay: false,
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 {
self.running
}
@ -705,7 +737,9 @@ impl<MH: IMusicHoard> IUi for Ui<MH> {
self.music_hoard.save_to_database()?;
Ok(())
}
}
impl<MH: IMusicHoard> IUiBrowse<Self> for UiImpl<MH> {
fn increment_category(&mut self) {
self.selection.increment_category();
}
@ -724,15 +758,23 @@ impl<MH: IMusicHoard> IUi for Ui<MH> {
.decrement_selection(self.music_hoard.get_collection());
}
fn toggle_overlay(&mut self) {
self.overlay = !self.overlay;
fn display_info_overlay(self) -> Ui<Self, Self> {
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);
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);
}
}