Refactor popup
This commit is contained in:
103
src/app.rs
103
src/app.rs
@@ -1,7 +1,6 @@
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use crate::event::{AppEvent, EventHandler};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph, StatefulWidget};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::time::Duration;
|
||||
use color_eyre::Result;
|
||||
@@ -11,32 +10,29 @@ use crossterm::event::Event as CrosstermEvent;
|
||||
use diesel::{Connection, SqliteConnection};
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::prelude::{StatefulWidget, Widget};
|
||||
use ratatui::prelude::{Widget};
|
||||
use ratatui::style::{Color, Style};
|
||||
use ratatui::text::{Line, Span, Text};
|
||||
use crate::config::types::ApplicationConfig;
|
||||
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
use crate::widgets::textarea::TextArea;
|
||||
use crate::widgets::popups::folder::AddFolderPopup;
|
||||
|
||||
enum AppState {
|
||||
enum AppStatus {
|
||||
Running,
|
||||
Exiting,
|
||||
Input
|
||||
}
|
||||
|
||||
enum AppPopUp {
|
||||
AddFolder,
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) struct App {
|
||||
state: AppState,
|
||||
popup: AppPopUp,
|
||||
status: AppStatus,
|
||||
events: EventHandler,
|
||||
db_connection: SqliteConnection,
|
||||
app_config: ApplicationConfig,
|
||||
stateful_widgets: HashMap<String, Box<dyn Any>>,
|
||||
state: AppState,
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
popup: Option<Box<dyn Any>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -46,13 +42,13 @@ impl App {
|
||||
else { ApplicationConfig::new() };
|
||||
Self::initialize_folders();
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
let state = AppState { popup: None };
|
||||
Self {
|
||||
state: AppState::Running,
|
||||
popup: AppPopUp::None,
|
||||
status: AppStatus::Running,
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf,
|
||||
stateful_widgets: HashMap::new()
|
||||
state
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,20 +72,16 @@ impl App {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
let event = self.events.next().await?;
|
||||
self.update(event)?;
|
||||
if matches!(self.state, AppState::Exiting) {
|
||||
if matches!(self.status, AppStatus::Exiting) {
|
||||
break Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, event: AppEvent) -> Result<()> {
|
||||
if matches!(self.state, AppState::Input) && let AppEvent::Raw(raw) = &event {
|
||||
self.stateful_widgets.iter_mut()
|
||||
.map(|(_, widget)| widget.downcast_mut::<TextArea>())
|
||||
.filter(|widget| widget.is_some())
|
||||
.map(|widget| widget.unwrap())
|
||||
.filter(|widget| widget.active)
|
||||
.for_each(|textarea| textarea.handle_input(raw));
|
||||
if matches!(self.status, AppStatus::Input) && let AppEvent::Raw(raw) = &event &&
|
||||
let Some(boxed) = &mut self.state.popup && let Some(popup) = boxed.downcast_mut::<AddFolderPopup>() {
|
||||
popup.textarea.handle_input(raw)?;
|
||||
}
|
||||
if let AppEvent::Raw(cross_event) = event &&
|
||||
let CrosstermEvent::Key(key) = cross_event{
|
||||
@@ -99,10 +91,10 @@ impl App {
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key: &KeyEvent) -> Result<()> {
|
||||
if matches!(self.state, AppState::Input) && matches!(key.code, KeyCode::Esc) {
|
||||
self.state = AppState::Running;
|
||||
if matches!(self.status, AppStatus::Input) && matches!(key.code, KeyCode::Esc) {
|
||||
self.status = AppStatus::Running;
|
||||
}
|
||||
if matches!(key.kind, KeyEventKind::Press) && !matches!(self.state, AppState::Input) {
|
||||
if matches!(key.kind, KeyEventKind::Press) && !matches!(self.status, AppStatus::Input) {
|
||||
match key.code {
|
||||
Char('q') => self.quit()?,
|
||||
Char('a') => self.folder_popup(),
|
||||
@@ -134,10 +126,9 @@ impl Widget for &mut App {
|
||||
self.render_game_list(chunks[1], buf);
|
||||
self.render_footer(chunks[2], buf);
|
||||
|
||||
match self.popup {
|
||||
AppPopUp::AddFolder => self.render_folder_popup(area, buf),
|
||||
AppPopUp::None => {},
|
||||
};
|
||||
if let Some(boxed) = &mut self.state.popup && let Some(popup) = boxed.downcast_mut::<AddFolderPopup>() {
|
||||
popup.clone().render(area, buf, popup);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +139,7 @@ impl App {
|
||||
.title(Line::raw("Games"))
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
self.render_widget(game_list, area, buf);
|
||||
game_list.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_header(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
@@ -158,51 +149,19 @@ impl App {
|
||||
Style::default().fg(Color::Green),
|
||||
)
|
||||
);
|
||||
self.render_widget(title, area, buf);
|
||||
title.render(area, buf);
|
||||
}
|
||||
|
||||
fn render_footer(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let mut navigation_text = vec![
|
||||
Span::styled("(q) quit / (a) add folders", Style::default().fg(Color::Green)),
|
||||
];
|
||||
if matches!(self.state, AppState::Input) {
|
||||
if matches!(self.status, AppStatus::Input) {
|
||||
navigation_text[0] = Span::styled("Input Mode", Style::default().fg(Color::Green));
|
||||
}
|
||||
let line = Line::from(navigation_text);
|
||||
let footer = Paragraph::new(line);
|
||||
self.render_widget(footer, area, buf);
|
||||
}
|
||||
|
||||
fn render_folder_popup(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
let popup_area = Rect {
|
||||
x: area.width / 4,
|
||||
y: area.height / 3,
|
||||
width: area.width / 2,
|
||||
height: area.height / 3,
|
||||
};
|
||||
let mut textarea = TextArea::new("New Folder", "test");
|
||||
textarea.active = true;
|
||||
self.render_stateful_widget("folder_textarea", textarea, popup_area, buf);
|
||||
}
|
||||
|
||||
fn render_widget<T>(&mut self, widget: T, area: Rect, buf: &mut Buffer)
|
||||
where T: Widget + Clone + 'static
|
||||
{
|
||||
widget.clone().render(area, buf);
|
||||
}
|
||||
|
||||
fn render_stateful_widget<T>(&mut self, id: &str, widget: T, area: Rect, buf: &mut Buffer)
|
||||
where T: StatefulWidget + Clone + 'static
|
||||
{
|
||||
if !self.stateful_widgets.contains_key(id) {
|
||||
self.stateful_widgets.insert(id.to_string(), Box::new(widget.clone()));
|
||||
}
|
||||
let cached_widget = self.stateful_widgets
|
||||
.get_mut(id)
|
||||
.unwrap()
|
||||
.downcast_mut::<T::State>()
|
||||
.unwrap();
|
||||
widget.render(area, buf, cached_widget);
|
||||
footer.render(area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,21 +169,21 @@ impl App {
|
||||
// event handlers
|
||||
impl App {
|
||||
fn quit(&mut self) -> Result<()> {
|
||||
if matches!(self.popup, AppPopUp::None) {
|
||||
self.state = AppState::Exiting;
|
||||
if self.state.popup.is_none() {
|
||||
self.status = AppStatus::Exiting;
|
||||
self.app_config
|
||||
.clone()
|
||||
.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
self.events.task.abort();
|
||||
}
|
||||
else {
|
||||
self.popup = AppPopUp::None;
|
||||
self.state.popup = None;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn folder_popup(&mut self) {
|
||||
self.popup = AppPopUp::AddFolder;
|
||||
self.state = AppState::Input;
|
||||
self.state.popup = Some(Box::new(AddFolderPopup::new()));
|
||||
self.status = AppStatus::Input;
|
||||
}
|
||||
}
|
||||
1
src/widgets/components/mod.rs
Normal file
1
src/widgets/components/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod textarea;
|
||||
@@ -6,6 +6,8 @@ use ratatui::text::Text;
|
||||
use ratatui::widgets::{Block, Borders, Paragraph, Widget};
|
||||
use tui_input::backend::crossterm::EventHandler;
|
||||
use tui_input::Input;
|
||||
use color_eyre::Result;
|
||||
use ratatui::crossterm::cursor::SetCursorStyle;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TextArea {
|
||||
@@ -23,7 +25,7 @@ impl StatefulWidget for TextArea {
|
||||
let input_value = state.input.value().to_string();
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(self.title.clone());
|
||||
.title(state.title.clone());
|
||||
let paragraph = Paragraph::new(Text::from(input_value))
|
||||
.block(block);
|
||||
paragraph.render(area, buf);
|
||||
@@ -39,11 +41,8 @@ impl TextArea {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self, event: &Event) {
|
||||
pub fn handle_input(&mut self, event: &Event) -> Result<()> {
|
||||
let state_result = self.input.handle_event(event);
|
||||
if state_result.is_none() {
|
||||
return;
|
||||
}
|
||||
let state = state_result.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod textarea;
|
||||
pub mod components;
|
||||
pub mod popups;
|
||||
46
src/widgets/popups/folder.rs
Normal file
46
src/widgets/popups/folder.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Margin, Rect};
|
||||
use ratatui::prelude::{StatefulWidget, Widget};
|
||||
use ratatui::widgets::{Block, Borders};
|
||||
use crate::widgets::components::textarea::TextArea;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AddFolderPopup {
|
||||
pub textarea: TextArea
|
||||
}
|
||||
|
||||
impl AddFolderPopup {
|
||||
pub fn new() -> Self {
|
||||
let mut textarea = TextArea::new("Folder Path", "");
|
||||
textarea.active = true;
|
||||
Self {
|
||||
textarea
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatefulWidget for AddFolderPopup {
|
||||
type State = AddFolderPopup;
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State)
|
||||
where
|
||||
Self: Sized
|
||||
{
|
||||
let popup_area = Rect {
|
||||
x: area.width / 4,
|
||||
y: area.height / 3,
|
||||
width: area.width / 2,
|
||||
height: area.height / 3,
|
||||
};
|
||||
let block = Block::default()
|
||||
.title("Add New Folder")
|
||||
.borders(Borders::ALL);
|
||||
block.render(popup_area, buf);
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![
|
||||
Constraint::Length(1)
|
||||
])
|
||||
.split(popup_area.inner(Margin::new(1, 1)));
|
||||
self.textarea.render(chunks[0], buf, &mut state.textarea);
|
||||
}
|
||||
}
|
||||
1
src/widgets/popups/mod.rs
Normal file
1
src/widgets/popups/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod folder;
|
||||
Reference in New Issue
Block a user