summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-10-30 05:00:59 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-10-30 05:00:59 +0100
commit1505d8f9413c3b0371b9735c3e2a465c1ec0ae92 (patch)
tree1d8ebd63099bc1edaed9dead75e99a3eb45024ea /examples
parentfedcab6f4f5ffd3a6dfffe7dd41c58df2e314099 (diff)
downloadiced-1505d8f9413c3b0371b9735c3e2a465c1ec0ae92.tar.gz
iced-1505d8f9413c3b0371b9735c3e2a465c1ec0ae92.tar.bz2
iced-1505d8f9413c3b0371b9735c3e2a465c1ec0ae92.zip
Implement task addition in `todos` example
Diffstat (limited to 'examples')
-rw-r--r--examples/todos.rs63
1 files changed, 55 insertions, 8 deletions
diff --git a/examples/todos.rs b/examples/todos.rs
index 6a5d5e85..6189c8db 100644
--- a/examples/todos.rs
+++ b/examples/todos.rs
@@ -1,22 +1,45 @@
use iced::{
- text::HorizontalAlignment, text_input, Align, Application, Color, Column,
- Element, Justify, Length, Text, TextInput,
+ scrollable, text::HorizontalAlignment, text_input, Align, Application,
+ Checkbox, Color, Column, Element, Length, Scrollable, Text, TextInput,
};
pub fn main() {
Todos::default().run()
}
-#[derive(Default)]
+#[derive(Debug, Default)]
struct Todos {
+ scroll: scrollable::State,
input: text_input::State,
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)]
pub enum Message {
InputChanged(String),
CreateTask,
+ TaskChanged(usize, bool),
}
impl Application for Todos {
@@ -27,8 +50,20 @@ impl Application for Todos {
Message::InputChanged(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> {
@@ -47,15 +82,27 @@ impl Application for Todos {
.size(30)
.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))
- .height(Length::Fill)
.align_self(Align::Center)
- .justify_content(Justify::Center)
.spacing(20)
- .padding(20)
.push(title)
.push(input)
+ .push(tasks);
+
+ Scrollable::new(&mut self.scroll)
+ .padding(40)
+ .push(content)
.into()
}
}