From 167f5062918b937afce5810ba7933f1a79bac51b Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 4 Jun 2023 14:51:47 +0700 Subject: [PATCH] NewTask state to manage adding a new task --- src/app.rs | 28 +++++++++++++++++++++++++--- src/input.rs | 10 ++++++---- src/ui.rs | 9 +++++---- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/app.rs b/src/app.rs index 93bec71..ba6c704 100644 --- a/src/app.rs +++ b/src/app.rs @@ -55,23 +55,45 @@ impl Default for Project { } } +#[derive(Debug)] +pub enum NewTaskFocus { + Title, + Description, + Buttons +} + +pub struct NewTask { + pub title: String, + pub description: String, + pub focus: NewTaskFocus +} + +impl Default for NewTask { + fn default() -> Self { + NewTask { + title: String::new(), + description: String::new(), + focus: NewTaskFocus::Title + } + } +} + pub struct AppState { pub project: Project, pub quit: bool, pub columns: Vec, - pub popup_text: Option, + pub new_task_state: Option, } impl AppState { pub fn new(project: Project) -> Self { AppState { quit: false, - popup_text: None, + new_task_state: None, project, columns: vec![], } } - } impl Column { diff --git a/src/input.rs b/src/input.rs index 1bccc52..737fae1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,12 +1,14 @@ use crossterm::event; use crossterm::event::{Event, KeyCode}; -use crate::app::{AppState}; +use crate::app::{NewTask, AppState}; pub fn handle_input(state: &mut AppState) -> Result<(), std::io::Error> { let project = &mut state.project; let column = project.get_selected_column_mut(); if let Event::Key(key) = event::read()? { match key.code { + // KeyCode::BackTab => + // KeyCode::Tab => KeyCode::Char('q') => state.quit = true, KeyCode::Char('h') | KeyCode::Left => { project.select_previous_column(); }, @@ -25,9 +27,9 @@ pub fn handle_input(state: &mut AppState) -> Result<(), std::io::Error> { KeyCode::Char('-') | KeyCode::Char('K') => project.move_task_up(), KeyCode::Char('p') => { - match state.popup_text { - None => state.popup_text = Some("".to_string()), - Some(_) => state.popup_text = None, + match state.new_task_state { + None => state.new_task_state = Some(NewTask::default()), + Some(_) => state.new_task_state = None, } } _ => {} diff --git a/src/ui.rs b/src/ui.rs index 86da307..867bf57 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -62,6 +62,7 @@ fn draw_task_info(f: &mut Frame, area: &Rect, state: &AppState) { f.render_widget(p, *area); } } + fn centered_rect_for_popup(percent_x: u16, percent_y: u16, r: Rect) -> Rect { let popup_layout = Layout::default() .direction(Direction::Vertical) @@ -91,15 +92,15 @@ fn centered_rect_for_popup(percent_x: u16, percent_y: u16, r: Rect) -> Rect { pub fn draw_new_task_popup(f: &mut Frame, state: &mut AppState) { let area = centered_rect_for_popup(45, 60, f.size()); f.render_widget(Clear, area); - match &state.popup_text { + match &state.new_task_state { None => {} - Some(s) => { + Some(task) => { let block = Block::default() .title("Add Task") .title_alignment(Alignment::Center) .borders(Borders::ALL); let block_inner = block.inner(area); - let main = Paragraph::new(s.as_ref()).block(block); + let main = Paragraph::new(task.description.as_ref()).block(block); let layout = Layout::default() .direction(Direction::Vertical) .constraints( @@ -157,7 +158,7 @@ pub fn draw(f: &mut Frame, state: &mut AppState) { .block(block); f.render_widget(foot_txt, main_layout[3]); - if state.popup_text.is_some() { + if state.new_task_state.is_some() { draw_new_task_popup(f, state); } }