diff options
Diffstat (limited to 'examples/todos')
-rw-r--r-- | examples/todos/Cargo.toml | 5 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 25 |
2 files changed, 21 insertions, 9 deletions
diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index 3334d84f..3c62bfbc 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -9,18 +9,21 @@ publish = false iced.workspace = true iced.features = ["async-std", "debug"] +once_cell.workspace = true serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -once_cell.workspace = true +uuid = { version = "1.0", features = ["v4", "fast-rng", "serde"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] async-std.workspace = true directories-next = "2.0" +tracing-subscriber = "0.3" [target.'cfg(target_arch = "wasm32")'.dependencies] iced.workspace = true iced.features = ["debug", "webgl"] +uuid = { version = "1.0", features = ["js"] } web-sys = { workspace = true, features = ["Window", "Storage"] } wasm-timer.workspace = true diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 62c17926..501bf67e 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -3,8 +3,8 @@ use iced::font::{self, Font}; use iced::keyboard; use iced::theme::{self, Theme}; use iced::widget::{ - self, button, checkbox, column, container, row, scrollable, text, - text_input, Text, + self, button, checkbox, column, container, keyed_column, row, scrollable, + text, text_input, Text, }; use iced::window; use iced::{Application, Element}; @@ -12,10 +12,14 @@ use iced::{Color, Command, Length, Settings, Subscription}; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; +use uuid::Uuid; static INPUT_ID: Lazy<text_input::Id> = Lazy::new(text_input::Id::unique); pub fn main() -> iced::Result { + #[cfg(not(target_arch = "wasm32"))] + tracing_subscriber::fmt::init(); + Todos::run(Settings { window: window::Settings { size: (500, 800), @@ -220,17 +224,19 @@ impl Application for Todos { tasks.iter().filter(|task| filter.matches(task)); let tasks: Element<_> = if filtered_tasks.count() > 0 { - column( + keyed_column( tasks .iter() .enumerate() .filter(|(_, task)| filter.matches(task)) .map(|(i, task)| { - task.view(i).map(move |message| { - Message::TaskMessage(i, message) - }) - }) - .collect(), + ( + task.id, + task.view(i).map(move |message| { + Message::TaskMessage(i, message) + }), + ) + }), ) .spacing(10) .into() @@ -279,6 +285,8 @@ impl Application for Todos { #[derive(Debug, Clone, Serialize, Deserialize)] struct Task { + #[serde(default = "Uuid::new_v4")] + id: Uuid, description: String, completed: bool, @@ -314,6 +322,7 @@ impl Task { fn new(description: String) -> Self { Task { + id: Uuid::new_v4(), description, completed: false, state: TaskState::Idle, |