NewTask state to manage adding a new task

This commit is contained in:
Joseph Ferano 2023-06-04 14:51:47 +07:00
parent c9a69c7abe
commit 167f506291
3 changed files with 36 additions and 11 deletions

View File

@ -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 struct AppState {
pub project: Project, pub project: Project,
pub quit: bool, pub quit: bool,
pub columns: Vec<Column>, pub columns: Vec<Column>,
pub popup_text: Option<String>, pub new_task_state: Option<NewTask>,
} }
impl AppState { impl AppState {
pub fn new(project: Project) -> Self { pub fn new(project: Project) -> Self {
AppState { AppState {
quit: false, quit: false,
popup_text: None, new_task_state: None,
project, project,
columns: vec![], columns: vec![],
} }
} }
} }
impl Column { impl Column {

View File

@ -1,12 +1,14 @@
use crossterm::event; use crossterm::event;
use crossterm::event::{Event, KeyCode}; 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> { pub fn handle_input(state: &mut AppState) -> Result<(), std::io::Error> {
let project = &mut state.project; let project = &mut state.project;
let column = project.get_selected_column_mut(); let column = project.get_selected_column_mut();
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.code { match key.code {
// KeyCode::BackTab =>
// KeyCode::Tab =>
KeyCode::Char('q') => state.quit = true, KeyCode::Char('q') => state.quit = true,
KeyCode::Char('h') | KeyCode::Char('h') |
KeyCode::Left => { project.select_previous_column(); }, 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('-') |
KeyCode::Char('K') => project.move_task_up(), KeyCode::Char('K') => project.move_task_up(),
KeyCode::Char('p') => { KeyCode::Char('p') => {
match state.popup_text { match state.new_task_state {
None => state.popup_text = Some("".to_string()), None => state.new_task_state = Some(NewTask::default()),
Some(_) => state.popup_text = None, Some(_) => state.new_task_state = None,
} }
} }
_ => {} _ => {}

View File

@ -62,6 +62,7 @@ fn draw_task_info<B: Backend>(f: &mut Frame<B>, area: &Rect, state: &AppState) {
f.render_widget(p, *area); f.render_widget(p, *area);
} }
} }
fn centered_rect_for_popup(percent_x: u16, percent_y: u16, r: Rect) -> Rect { fn centered_rect_for_popup(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default() let popup_layout = Layout::default()
.direction(Direction::Vertical) .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<B: Backend>(f: &mut Frame<B>, state: &mut AppState) { pub fn draw_new_task_popup<B: Backend>(f: &mut Frame<B>, state: &mut AppState) {
let area = centered_rect_for_popup(45, 60, f.size()); let area = centered_rect_for_popup(45, 60, f.size());
f.render_widget(Clear, area); f.render_widget(Clear, area);
match &state.popup_text { match &state.new_task_state {
None => {} None => {}
Some(s) => { Some(task) => {
let block = Block::default() let block = Block::default()
.title("Add Task") .title("Add Task")
.title_alignment(Alignment::Center) .title_alignment(Alignment::Center)
.borders(Borders::ALL); .borders(Borders::ALL);
let block_inner = block.inner(area); 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() let layout = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(
@ -157,7 +158,7 @@ pub fn draw<B: Backend>(f: &mut Frame<B>, state: &mut AppState) {
.block(block); .block(block);
f.render_widget(foot_txt, main_layout[3]); 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); draw_new_task_popup(f, state);
} }
} }