Add textarea support
This commit is contained in:
110
src/app.rs
110
src/app.rs
@@ -1,19 +1,22 @@
|
||||
use crate::event::{Event, EventHandler};
|
||||
use std::any::Any;
|
||||
use std::collections::HashMap;
|
||||
use crate::event::{AppEvent, EventHandler};
|
||||
use ratatui::widgets::{Block, Borders, Paragraph};
|
||||
use ratatui::{DefaultTerminal};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::time::Duration;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind};
|
||||
use crossterm::event::KeyCode::Char;
|
||||
use crossterm::event::Event as CrosstermEvent;
|
||||
use diesel::{Connection, SqliteConnection};
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::prelude::{Widget};
|
||||
use ratatui::prelude::{StatefulWidget, 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;
|
||||
|
||||
enum AppState {
|
||||
Running,
|
||||
@@ -32,15 +35,16 @@ pub(crate) struct App {
|
||||
events: EventHandler,
|
||||
db_connection: SqliteConnection,
|
||||
app_config: ApplicationConfig,
|
||||
current_event: Option<Event>
|
||||
stateful_widgets: HashMap<String, Box<dyn Any>>,
|
||||
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
pub async fn create() -> Self {
|
||||
let app_conf =
|
||||
if APP_CONIFG_FILE_PATH.exists() { ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH).unwrap() }
|
||||
else { ApplicationConfig::new() };
|
||||
Self::initialize();
|
||||
Self::initialize_folders();
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
Self {
|
||||
state: AppState::Running,
|
||||
@@ -48,11 +52,11 @@ impl App {
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf,
|
||||
current_event: None
|
||||
stateful_widgets: HashMap::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn initialize() {
|
||||
fn initialize_folders() {
|
||||
if !APP_CONFIG_DIR.exists() {
|
||||
std::fs::create_dir_all(APP_CONFIG_DIR.as_path()).unwrap();
|
||||
}
|
||||
@@ -67,9 +71,9 @@ impl App {
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
pub async fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||
pub async fn run(mut self, terminal: &mut DefaultTerminal) -> Result<()> {
|
||||
loop {
|
||||
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
let event = self.events.next().await?;
|
||||
self.update(event)?;
|
||||
if matches!(self.state, AppState::Exiting) {
|
||||
@@ -78,20 +82,27 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, event: Event) -> Result<()> {
|
||||
self.current_event = Some(event.clone());
|
||||
if let Event::Key(key) = event && matches!(self.state, AppState::Input) {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.state = AppState::Running;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
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 let AppEvent::Raw(cross_event) = event &&
|
||||
let CrosstermEvent::Key(key) = cross_event{
|
||||
self.handle_key_event(&key)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
if let Event::Key(key) = event &&
|
||||
event::KeyEventKind::is_press(&key.kind) &&
|
||||
!matches!(self.state, AppState::Input) {
|
||||
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!(key.kind, KeyEventKind::Press) && !matches!(self.state, AppState::Input) {
|
||||
match key.code {
|
||||
Char('q') => self.quit()?,
|
||||
Char('a') => self.folder_popup(),
|
||||
@@ -100,6 +111,10 @@ impl App {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
frame.render_widget(self, frame.area())
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for &mut App {
|
||||
@@ -119,15 +134,8 @@ impl Widget for &mut App {
|
||||
self.render_game_list(chunks[1], buf);
|
||||
self.render_footer(chunks[2], buf);
|
||||
|
||||
let popup_area = Rect {
|
||||
x: area.width / 4,
|
||||
y: area.height / 3,
|
||||
width: area.width / 2,
|
||||
height: area.height / 3,
|
||||
};
|
||||
|
||||
match self.popup {
|
||||
AppPopUp::AddFolder => self.render_folder_popup(popup_area, buf),
|
||||
AppPopUp::AddFolder => self.render_folder_popup(area, buf),
|
||||
AppPopUp::None => {},
|
||||
};
|
||||
}
|
||||
@@ -140,8 +148,7 @@ impl App {
|
||||
.title(Line::raw("Games"))
|
||||
.borders(Borders::ALL)
|
||||
.style(Style::default());
|
||||
|
||||
game_list.render(area, buf);
|
||||
self.render_widget(game_list, area, buf);
|
||||
}
|
||||
|
||||
fn render_header(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
@@ -151,7 +158,7 @@ impl App {
|
||||
Style::default().fg(Color::Green),
|
||||
)
|
||||
);
|
||||
title.render(area, buf);
|
||||
self.render_widget(title, area, buf);
|
||||
}
|
||||
|
||||
fn render_footer(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
@@ -163,11 +170,39 @@ impl App {
|
||||
}
|
||||
let line = Line::from(navigation_text);
|
||||
let footer = Paragraph::new(line);
|
||||
footer.render(area, buf);
|
||||
self.render_widget(footer, area, buf);
|
||||
}
|
||||
|
||||
fn render_folder_popup(&mut self, area: Rect, buf: &mut Buffer) {
|
||||
todo!()
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,6 +215,7 @@ impl App {
|
||||
self.app_config
|
||||
.clone()
|
||||
.write_to_file(&APP_CONIFG_FILE_PATH.to_path_buf())?;
|
||||
self.events.task.abort();
|
||||
}
|
||||
else {
|
||||
self.popup = AppPopUp::None;
|
||||
|
||||
Reference in New Issue
Block a user