diff options
| author | 2022-08-05 15:40:55 -0700 | |
|---|---|---|
| committer | 2022-11-03 02:23:19 +0100 | |
| commit | 50eb9e34b8ea939c263c1f548ef3f228400d4bda (patch) | |
| tree | 9cb81212dabed5b4bcf74236a83098544cb936c7 /examples/cached/src | |
| parent | 71381418116afc1f9c40c8faf2cb1e53072a1cfa (diff) | |
| download | iced-50eb9e34b8ea939c263c1f548ef3f228400d4bda.tar.gz iced-50eb9e34b8ea939c263c1f548ef3f228400d4bda.tar.bz2 iced-50eb9e34b8ea939c263c1f548ef3f228400d4bda.zip | |
add example
Diffstat (limited to 'examples/cached/src')
| -rw-r--r-- | examples/cached/src/main.rs | 141 | 
1 files changed, 141 insertions, 0 deletions
| 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<String>, +    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<Message> { +        let options = +            Cached::new((&self.sort_order, self.options.len()), || { +                let mut options = self.options.iter().collect::<Vec<_>>(); +                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() +    } +} | 
