Isolate input lifecycle in one place
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 2m1s
Cargo CI / Lint (pull_request) Successful in 1m5s

This commit is contained in:
Wojciech Kozlowski 2024-09-15 10:44:12 +02:00
parent e4c77c982d
commit 4b92254a4d
2 changed files with 25 additions and 21 deletions

View File

@ -1,8 +1,6 @@
use tui_input::backend::crossterm::EventHandler;
use crate::tui::app::{machine::App, AppState, IAppInput, InputEvent, InputPublic};
use super::AppInputMode;
use crate::tui::app::{machine::App, AppMode, AppState, IAppInput, InputEvent, InputPublic};
#[derive(Default)]
pub struct Input(tui_input::Input);
@ -13,6 +11,27 @@ impl<'app> From<&'app Input> for InputPublic<'app> {
}
}
impl From<App> for AppMode<App, AppInputMode> {
fn from(mut app: App) -> Self {
if let Some(input) = app.input_mut().take() {
AppMode::Input(AppInputMode::new(input, app))
} else {
AppMode::State(app)
}
}
}
pub struct AppInputMode {
input: Input,
app: App,
}
impl AppInputMode {
pub fn new(input: Input, app: App) -> Self {
AppInputMode { input, app }
}
}
impl IAppInput for AppInputMode {
type APP = App;

View File

@ -21,7 +21,7 @@ use critical_state::CriticalState;
use error_state::ErrorState;
use fetch_state::FetchState;
use info_state::InfoState;
use input::Input;
use input::{AppInputMode, Input};
use match_state::MatchState;
use reload_state::ReloadState;
use search_state::SearchState;
@ -53,17 +53,6 @@ pub struct AppInner {
events: EventSender,
}
pub struct AppInputMode {
input: Input,
app: App,
}
impl AppInputMode {
fn new(input: Input, app: App) -> Self {
AppInputMode { input, app }
}
}
impl App {
pub fn new<MH: IMusicHoard + 'static, MB: IMusicBrainz + Send + 'static>(
mut music_hoard: MH,
@ -147,12 +136,8 @@ impl IApp for App {
self
}
fn mode(mut self) -> super::AppMode<IAppState!(), Self::InputMode> {
if let Some(input) = self.input_mut().take() {
AppMode::Input(AppInputMode::new(input, self.state()))
} else {
AppMode::State(self.state())
}
fn mode(self) -> AppMode<IAppState!(), Self::InputMode> {
self.into()
}
}