Improve layout of cached example

This commit is contained in:
Héctor Ramón Jiménez 2022-11-03 02:40:51 +01:00
parent 1687d11389
commit adf541d432
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -1,9 +1,8 @@
use iced::theme;
use iced::widget::{ use iced::widget::{
button, column, horizontal_rule, horizontal_space, row, scrollable, text, button, column, horizontal_space, row, scrollable, text, text_input,
text_input,
}; };
use iced::{Element, Sandbox}; use iced::{Element, Length, Sandbox, Settings};
use iced::{Length, Settings};
use iced_lazy::Cached; use iced_lazy::Cached;
use std::collections::HashSet; use std::collections::HashSet;
@ -74,7 +73,8 @@ impl Sandbox for App {
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
let options = let options =
Cached::new((&self.sort_order, self.options.len()), || { Cached::new((&self.sort_order, self.options.len()), || {
let mut options = self.options.iter().collect::<Vec<_>>(); let mut options: Vec<_> = self.options.iter().collect();
options.sort_by(|a, b| match self.sort_order { options.sort_by(|a, b| match self.sort_order {
SortOrder::Ascending => { SortOrder::Ascending => {
a.to_lowercase().cmp(&b.to_lowercase()) a.to_lowercase().cmp(&b.to_lowercase())
@ -84,40 +84,45 @@ impl Sandbox for App {
} }
}); });
options.into_iter().fold( column(
column![horizontal_rule(1)], options
|column, option| { .into_iter()
column .map(|option| {
.push(row![ row![
text(option), text(option),
horizontal_space(Length::Fill), horizontal_space(Length::Fill),
button("Delete").on_press( button("Delete")
Message::DeleteOption(option.to_string(),), .on_press(Message::DeleteOption(
) option.to_string(),
]) ),)
.push(horizontal_rule(1)) .style(theme::Button::Destructive)
}, ]
.into()
})
.collect(),
) )
.spacing(10)
}); });
scrollable( column![
column![ scrollable(options).height(Length::Fill),
button(text(format!( row![
"Toggle Sort Order ({})",
self.sort_order
)))
.on_press(Message::ToggleSortOrder),
options,
text_input( text_input(
"Add a new option", "Add a new option",
&self.input, &self.input,
Message::InputChanged, Message::InputChanged,
) )
.on_submit(Message::AddOption(self.input.clone())), .on_submit(Message::AddOption(self.input.clone())),
button(text(format!(
"Toggle Sort Order ({})",
self.sort_order
)))
.on_press(Message::ToggleSortOrder)
] ]
.spacing(20) .spacing(10)
.padding(20), ]
) .spacing(20)
.padding(20)
.into() .into()
} }
} }