From 50eb9e34b8ea939c263c1f548ef3f228400d4bda Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 5 Aug 2022 15:40:55 -0700 Subject: add example --- examples/cached/Cargo.toml | 11 ++++ examples/cached/src/main.rs | 141 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 examples/cached/Cargo.toml create mode 100644 examples/cached/src/main.rs (limited to 'examples/cached') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml new file mode 100644 index 00000000..21f59886 --- /dev/null +++ b/examples/cached/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "pure_cached" +version = "0.1.0" +authors = ["Nick Senger "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } +iced_native = { path = "../../native" } +iced_lazy = { path = "../../lazy" } diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs new file mode 100644 index 00000000..d7787979 --- /dev/null +++ b/examples/cached/src/main.rs @@ -0,0 +1,141 @@ +use iced::widget::{ + button, column, horizontal_rule, horizontal_space, row, scrollable, text, + text_input, +}; +use iced::{Element, Sandbox}; +use iced::{Length, Settings}; +use iced_lazy::Cached; + +use std::collections::HashSet; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Hash)] +enum SortOrder { + Ascending, + Descending, +} + +impl std::fmt::Display for SortOrder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Ascending => "Ascending", + Self::Descending => "Descending", + } + ) + } +} + +struct App { + options: HashSet, + input: String, + sort_order: SortOrder, +} + +impl Default for App { + fn default() -> Self { + Self { + options: ["Foo", "Bar", "Baz", "Qux", "Corge", "Waldo", "Fred"] + .into_iter() + .map(ToString::to_string) + .collect(), + input: Default::default(), + sort_order: SortOrder::Ascending, + } + } +} + +#[derive(Debug, Clone)] +enum Message { + InputChanged(String), + ToggleSortOrder, + DeleteOption(String), + AddOption(String), +} + +impl Sandbox for App { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Cached - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::InputChanged(input) => { + self.input = input; + } + Message::ToggleSortOrder => { + self.sort_order = match self.sort_order { + SortOrder::Ascending => SortOrder::Descending, + SortOrder::Descending => SortOrder::Ascending, + } + } + Message::AddOption(option) => { + self.options.insert(option); + self.input.clear(); + } + Message::DeleteOption(option) => { + self.options.remove(&option); + } + } + } + + fn view(&self) -> Element { + let options = + Cached::new((&self.sort_order, self.options.len()), || { + let mut options = self.options.iter().collect::>(); + options.sort_by(|a, b| match self.sort_order { + SortOrder::Ascending => { + a.to_lowercase().cmp(&b.to_lowercase()) + } + SortOrder::Descending => { + b.to_lowercase().cmp(&a.to_lowercase()) + } + }); + + options.into_iter().fold( + column![horizontal_rule(1)], + |column, option| { + column + .push(row![ + text(option), + horizontal_space(Length::Fill), + button("Delete").on_press( + Message::DeleteOption(option.to_string(),), + ) + ]) + .push(horizontal_rule(1)) + }, + ) + }); + + scrollable( + column![ + button(text(format!( + "Toggle Sort Order ({})", + self.sort_order + ))) + .on_press(Message::ToggleSortOrder), + options, + text_input( + "Add a new option", + &self.input, + Message::InputChanged, + ) + .on_submit(Message::AddOption(self.input.clone())), + ] + .spacing(20), + ) + .into() + } +} -- cgit From 4f83500bb8f522504a7ec0875a6f134eac733175 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:31:04 +0100 Subject: Rename `pure_cached` example to `cached` --- examples/cached/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/cached') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml index 21f59886..4d4013e6 100644 --- a/examples/cached/Cargo.toml +++ b/examples/cached/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "pure_cached" +name = "cached" version = "0.1.0" authors = ["Nick Senger "] edition = "2021" -- cgit From 1fb84ae5d3732ed51b35fb5419a5ad014e22ca5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:32:23 +0100 Subject: Remove `iced_native` dependency from `cached` example --- examples/cached/Cargo.toml | 1 - 1 file changed, 1 deletion(-) (limited to 'examples/cached') diff --git a/examples/cached/Cargo.toml b/examples/cached/Cargo.toml index 4d4013e6..2c7edde2 100644 --- a/examples/cached/Cargo.toml +++ b/examples/cached/Cargo.toml @@ -7,5 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["debug"] } -iced_native = { path = "../../native" } iced_lazy = { path = "../../lazy" } -- cgit From 0e295be8917a543e3641f8c4657db87fed0ce91b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:32:38 +0100 Subject: Move declaration of `SortOrder` in `cached` example --- examples/cached/src/main.rs | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'examples/cached') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index d7787979..b900ff36 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -12,25 +12,6 @@ pub fn main() -> iced::Result { App::run(Settings::default()) } -#[derive(Hash)] -enum SortOrder { - Ascending, - Descending, -} - -impl std::fmt::Display for SortOrder { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Self::Ascending => "Ascending", - Self::Descending => "Descending", - } - ) - } -} - struct App { options: HashSet, input: String, @@ -139,3 +120,22 @@ impl Sandbox for App { .into() } } + +#[derive(Debug, Hash)] +enum SortOrder { + Ascending, + Descending, +} + +impl std::fmt::Display for SortOrder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Ascending => "Ascending", + Self::Descending => "Descending", + } + ) + } +} -- cgit From 0478df9fd61c67255b0ea213aa83f56f5698ae7d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:35:05 +0100 Subject: Add `padding` to main `column` in `cached` example --- examples/cached/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'examples/cached') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index b900ff36..85a4a4f0 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -115,7 +115,8 @@ impl Sandbox for App { ) .on_submit(Message::AddOption(self.input.clone())), ] - .spacing(20), + .spacing(20) + .padding(20), ) .into() } -- cgit From adf541d4325df22d577342f870f0f95fa357797a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:40:51 +0100 Subject: Improve layout of `cached` example --- examples/cached/src/main.rs | 59 ++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'examples/cached') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 85a4a4f0..39364dc9 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -1,9 +1,8 @@ +use iced::theme; use iced::widget::{ - button, column, horizontal_rule, horizontal_space, row, scrollable, text, - text_input, + button, column, horizontal_space, row, scrollable, text, text_input, }; -use iced::{Element, Sandbox}; -use iced::{Length, Settings}; +use iced::{Element, Length, Sandbox, Settings}; use iced_lazy::Cached; use std::collections::HashSet; @@ -74,7 +73,8 @@ impl Sandbox for App { fn view(&self) -> Element { let options = Cached::new((&self.sort_order, self.options.len()), || { - let mut options = self.options.iter().collect::>(); + let mut options: Vec<_> = self.options.iter().collect(); + options.sort_by(|a, b| match self.sort_order { SortOrder::Ascending => { a.to_lowercase().cmp(&b.to_lowercase()) @@ -84,40 +84,45 @@ impl Sandbox for App { } }); - options.into_iter().fold( - column![horizontal_rule(1)], - |column, option| { - column - .push(row![ + column( + options + .into_iter() + .map(|option| { + row![ text(option), horizontal_space(Length::Fill), - button("Delete").on_press( - Message::DeleteOption(option.to_string(),), - ) - ]) - .push(horizontal_rule(1)) - }, + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), ) + .spacing(10) }); - scrollable( - column![ - button(text(format!( - "Toggle Sort Order ({})", - self.sort_order - ))) - .on_press(Message::ToggleSortOrder), - options, + column![ + scrollable(options).height(Length::Fill), + row![ text_input( "Add a new option", &self.input, Message::InputChanged, ) .on_submit(Message::AddOption(self.input.clone())), + button(text(format!( + "Toggle Sort Order ({})", + self.sort_order + ))) + .on_press(Message::ToggleSortOrder) ] - .spacing(20) - .padding(20), - ) + .spacing(10) + ] + .spacing(20) + .padding(20) .into() } } -- cgit From 1cdc1fcd0669bfea096237c07b32742c1a3f2158 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:46:31 +0100 Subject: Rename `iced_lazy::Cached` to `Lazy` :tada: --- examples/cached/src/main.rs | 59 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'examples/cached') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 39364dc9..7c8b06f0 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{ button, column, horizontal_space, row, scrollable, text, text_input, }; use iced::{Element, Length, Sandbox, Settings}; -use iced_lazy::Cached; +use iced_lazy::lazy; use std::collections::HashSet; @@ -71,39 +71,36 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = - Cached::new((&self.sort_order, self.options.len()), || { - let mut options: Vec<_> = self.options.iter().collect(); + let options = lazy((&self.sort_order, self.options.len()), || { + let mut options: Vec<_> = self.options.iter().collect(); - options.sort_by(|a, b| match self.sort_order { - SortOrder::Ascending => { - a.to_lowercase().cmp(&b.to_lowercase()) - } - SortOrder::Descending => { - b.to_lowercase().cmp(&a.to_lowercase()) - } - }); - - column( - options - .into_iter() - .map(|option| { - row![ - text(option), - horizontal_space(Length::Fill), - button("Delete") - .on_press(Message::DeleteOption( - option.to_string(), - ),) - .style(theme::Button::Destructive) - ] - .into() - }) - .collect(), - ) - .spacing(10) + options.sort_by(|a, b| match self.sort_order { + SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + SortOrder::Descending => { + b.to_lowercase().cmp(&a.to_lowercase()) + } }); + column( + options + .into_iter() + .map(|option| { + row![ + text(option), + horizontal_space(Length::Fill), + button("Delete") + .on_press(Message::DeleteOption( + option.to_string(), + ),) + .style(theme::Button::Destructive) + ] + .into() + }) + .collect(), + ) + .spacing(10) + }); + column![ scrollable(options).height(Length::Fill), row![ -- cgit From 6efda2457e7b80e9d3d145ceb9910bfbb5af9994 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 02:47:43 +0100 Subject: Rename `SortOrder` to `Order` in `cached` example --- examples/cached/src/main.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'examples/cached') diff --git a/examples/cached/src/main.rs b/examples/cached/src/main.rs index 7c8b06f0..8845b874 100644 --- a/examples/cached/src/main.rs +++ b/examples/cached/src/main.rs @@ -14,7 +14,7 @@ pub fn main() -> iced::Result { struct App { options: HashSet, input: String, - sort_order: SortOrder, + order: Order, } impl Default for App { @@ -25,7 +25,7 @@ impl Default for App { .map(ToString::to_string) .collect(), input: Default::default(), - sort_order: SortOrder::Ascending, + order: Order::Ascending, } } } @@ -33,7 +33,7 @@ impl Default for App { #[derive(Debug, Clone)] enum Message { InputChanged(String), - ToggleSortOrder, + ToggleOrder, DeleteOption(String), AddOption(String), } @@ -54,10 +54,10 @@ impl Sandbox for App { Message::InputChanged(input) => { self.input = input; } - Message::ToggleSortOrder => { - self.sort_order = match self.sort_order { - SortOrder::Ascending => SortOrder::Descending, - SortOrder::Descending => SortOrder::Ascending, + Message::ToggleOrder => { + self.order = match self.order { + Order::Ascending => Order::Descending, + Order::Descending => Order::Ascending, } } Message::AddOption(option) => { @@ -71,14 +71,12 @@ impl Sandbox for App { } fn view(&self) -> Element { - let options = lazy((&self.sort_order, self.options.len()), || { + let options = lazy((&self.order, self.options.len()), || { let mut options: Vec<_> = self.options.iter().collect(); - options.sort_by(|a, b| match self.sort_order { - SortOrder::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), - SortOrder::Descending => { - b.to_lowercase().cmp(&a.to_lowercase()) - } + options.sort_by(|a, b| match self.order { + Order::Ascending => a.to_lowercase().cmp(&b.to_lowercase()), + Order::Descending => b.to_lowercase().cmp(&a.to_lowercase()), }); column( @@ -110,11 +108,8 @@ impl Sandbox for App { Message::InputChanged, ) .on_submit(Message::AddOption(self.input.clone())), - button(text(format!( - "Toggle Sort Order ({})", - self.sort_order - ))) - .on_press(Message::ToggleSortOrder) + button(text(format!("Toggle Order ({})", self.order))) + .on_press(Message::ToggleOrder) ] .spacing(10) ] @@ -125,12 +120,12 @@ impl Sandbox for App { } #[derive(Debug, Hash)] -enum SortOrder { +enum Order { Ascending, Descending, } -impl std::fmt::Display for SortOrder { +impl std::fmt::Display for Order { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, -- cgit