Implement task addition in todos example
This commit is contained in:
parent
fedcab6f4f
commit
1505d8f941
1 changed files with 55 additions and 8 deletions
|
|
@ -1,22 +1,45 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
text::HorizontalAlignment, text_input, Align, Application, Color, Column,
|
scrollable, text::HorizontalAlignment, text_input, Align, Application,
|
||||||
Element, Justify, Length, Text, TextInput,
|
Checkbox, Color, Column, Element, Length, Scrollable, Text, TextInput,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
Todos::default().run()
|
Todos::default().run()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Todos {
|
struct Todos {
|
||||||
|
scroll: scrollable::State,
|
||||||
input: text_input::State,
|
input: text_input::State,
|
||||||
input_value: String,
|
input_value: String,
|
||||||
|
tasks: Vec<Task>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Task {
|
||||||
|
description: String,
|
||||||
|
completed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Task {
|
||||||
|
fn new(description: String) -> Self {
|
||||||
|
Task {
|
||||||
|
description,
|
||||||
|
completed: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&mut self) -> Element<bool> {
|
||||||
|
Checkbox::new(self.completed, &self.description, |checked| checked)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
InputChanged(String),
|
InputChanged(String),
|
||||||
CreateTask,
|
CreateTask,
|
||||||
|
TaskChanged(usize, bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Todos {
|
impl Application for Todos {
|
||||||
|
|
@ -27,8 +50,20 @@ impl Application for Todos {
|
||||||
Message::InputChanged(value) => {
|
Message::InputChanged(value) => {
|
||||||
self.input_value = value;
|
self.input_value = value;
|
||||||
}
|
}
|
||||||
Message::CreateTask => {}
|
Message::CreateTask => {
|
||||||
|
if !self.input_value.is_empty() {
|
||||||
|
self.tasks.push(Task::new(self.input_value.clone()));
|
||||||
|
self.input_value = String::new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::TaskChanged(i, completed) => {
|
||||||
|
if let Some(task) = self.tasks.get_mut(i) {
|
||||||
|
task.completed = completed;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbg!(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<Message> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
|
|
@ -47,15 +82,27 @@ impl Application for Todos {
|
||||||
.size(30)
|
.size(30)
|
||||||
.on_submit(Message::CreateTask);
|
.on_submit(Message::CreateTask);
|
||||||
|
|
||||||
Column::new()
|
let tasks = self.tasks.iter_mut().enumerate().fold(
|
||||||
|
Column::new().spacing(20),
|
||||||
|
|column, (i, task)| {
|
||||||
|
column.push(
|
||||||
|
task.view()
|
||||||
|
.map(move |state| Message::TaskChanged(i, state)),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let content = Column::new()
|
||||||
.max_width(Length::Units(800))
|
.max_width(Length::Units(800))
|
||||||
.height(Length::Fill)
|
|
||||||
.align_self(Align::Center)
|
.align_self(Align::Center)
|
||||||
.justify_content(Justify::Center)
|
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.padding(20)
|
|
||||||
.push(title)
|
.push(title)
|
||||||
.push(input)
|
.push(input)
|
||||||
|
.push(tasks);
|
||||||
|
|
||||||
|
Scrollable::new(&mut self.scroll)
|
||||||
|
.padding(40)
|
||||||
|
.push(content)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue