Replace stateful widgets with new iced_pure API

This commit is contained in:
Héctor Ramón Jiménez 2022-07-27 06:49:20 +02:00
parent c44267b85f
commit ff2519b1d4
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
142 changed files with 3631 additions and 14494 deletions

View file

@ -1,9 +1,8 @@
use iced::button;
use iced::scrollable;
use iced::{
Button, Column, Container, Element, Length, ProgressBar, Radio, Row, Rule,
Sandbox, Scrollable, Settings, Space, Text, Theme,
use iced::widget::{
button, column, container, horizontal_rule, progress_bar, radio,
scrollable, text, vertical_space, Row,
};
use iced::{Element, Length, Sandbox, Settings, Theme};
pub fn main() -> iced::Result {
ScrollableDemo::run(Settings::default())
@ -41,14 +40,16 @@ impl Sandbox for ScrollableDemo {
Message::ThemeChanged(theme) => self.theme = theme,
Message::ScrollToTop(i) => {
if let Some(variant) = self.variants.get_mut(i) {
variant.scrollable.snap_to(0.0);
// TODO
// variant.scrollable.snap_to(0.0);
variant.latest_offset = 0.0;
}
}
Message::ScrollToBottom(i) => {
if let Some(variant) = self.variants.get_mut(i) {
variant.scrollable.snap_to(1.0);
// TODO
// variant.scrollable.snap_to(1.0);
variant.latest_offset = 1.0;
}
@ -61,17 +62,17 @@ impl Sandbox for ScrollableDemo {
}
}
fn view(&mut self) -> Element<Message> {
fn view(&self) -> Element<Message> {
let ScrollableDemo {
theme, variants, ..
} = self;
let choose_theme = [Theme::Light, Theme::Dark].iter().fold(
Column::new().spacing(10).push(Text::new("Choose a theme:")),
column!["Choose a theme:"].spacing(10),
|column, option| {
column.push(Radio::new(
*option,
column.push(radio(
format!("{:?}", option),
*option,
Some(*theme),
Message::ThemeChanged,
))
@ -80,88 +81,86 @@ impl Sandbox for ScrollableDemo {
let scrollable_row = Row::with_children(
variants
.iter_mut()
.iter()
.enumerate()
.map(|(i, variant)| {
let mut scrollable =
Scrollable::new(&mut variant.scrollable)
.padding(10)
.spacing(10)
let mut contents = column![
variant.title.as_ref(),
button("Scroll to bottom",)
.width(Length::Fill)
.height(Length::Fill)
.on_scroll(move |offset| {
Message::Scrolled(i, offset)
})
.push(Text::new(variant.title))
.push(
Button::new(
&mut variant.scroll_to_bottom,
Text::new("Scroll to bottom"),
)
.width(Length::Fill)
.padding(10)
.on_press(Message::ScrollToBottom(i)),
);
.padding(10)
.on_press(Message::ScrollToBottom(i)),
]
.padding(10)
.spacing(10)
.width(Length::Fill);
if let Some(scrollbar_width) = variant.scrollbar_width {
scrollable = scrollable
.scrollbar_width(scrollbar_width)
.push(Text::new(format!(
"scrollbar_width: {:?}",
scrollbar_width
)));
contents = contents.push(text(format!(
"scrollbar_width: {:?}",
scrollbar_width
)));
}
if let Some(scrollbar_margin) = variant.scrollbar_margin {
scrollable = scrollable
.scrollbar_margin(scrollbar_margin)
.push(Text::new(format!(
"scrollbar_margin: {:?}",
scrollbar_margin
)));
contents = contents.push(text(format!(
"scrollbar_margin: {:?}",
scrollbar_margin
)));
}
if let Some(scroller_width) = variant.scroller_width {
scrollable = scrollable
.scroller_width(scroller_width)
.push(Text::new(format!(
"scroller_width: {:?}",
scroller_width
)));
contents = contents.push(text(format!(
"scroller_width: {:?}",
scroller_width
)));
}
scrollable = scrollable
.push(Space::with_height(Length::Units(100)))
.push(Text::new(
contents = contents
.push(vertical_space(Length::Units(100)))
.push(
"Some content that should wrap within the \
scrollable. Let's output a lot of short words, so \
that we'll make sure to see how wrapping works \
with these scrollbars.",
))
.push(Space::with_height(Length::Units(1200)))
.push(Text::new("Middle"))
.push(Space::with_height(Length::Units(1200)))
.push(Text::new("The End."))
)
.push(vertical_space(Length::Units(1200)))
.push("Middle")
.push(vertical_space(Length::Units(1200)))
.push("The End.")
.push(
Button::new(
&mut variant.scroll_to_top,
Text::new("Scroll to top"),
)
.width(Length::Fill)
.padding(10)
.on_press(Message::ScrollToTop(i)),
button("Scroll to top")
.width(Length::Fill)
.padding(10)
.on_press(Message::ScrollToTop(i)),
);
Column::new()
.width(Length::Fill)
let mut scrollable = scrollable(contents)
.height(Length::Fill)
.spacing(10)
.push(scrollable)
.push(ProgressBar::new(
0.0..=1.0,
variant.latest_offset,
))
.into()
.on_scroll(move |offset| Message::Scrolled(i, offset));
if let Some(scrollbar_width) = variant.scrollbar_width {
scrollable =
scrollable.scrollbar_width(scrollbar_width);
}
if let Some(scrollbar_margin) = variant.scrollbar_margin {
scrollable =
scrollable.scrollbar_margin(scrollbar_margin);
}
if let Some(scroller_width) = variant.scroller_width {
scrollable = scrollable.scroller_width(scroller_width);
}
column![
scrollable,
progress_bar(0.0..=1.0, variant.latest_offset,)
]
.width(Length::Fill)
.height(Length::Fill)
.spacing(10)
.into()
})
.collect(),
)
@ -169,14 +168,12 @@ impl Sandbox for ScrollableDemo {
.width(Length::Fill)
.height(Length::Fill);
let content = Column::new()
.spacing(20)
.padding(20)
.push(choose_theme)
.push(Rule::horizontal(20))
.push(scrollable_row);
let content =
column![choose_theme, horizontal_rule(20), scrollable_row]
.spacing(20)
.padding(20);
Container::new(content)
container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
@ -192,9 +189,6 @@ impl Sandbox for ScrollableDemo {
/// A version of a scrollable
struct Variant {
title: &'static str,
scrollable: scrollable::State,
scroll_to_top: button::State,
scroll_to_bottom: button::State,
scrollbar_width: Option<u16>,
scrollbar_margin: Option<u16>,
scroller_width: Option<u16>,
@ -206,9 +200,6 @@ impl Variant {
vec![
Self {
title: "Default Scrollbar",
scrollable: scrollable::State::new(),
scroll_to_top: button::State::new(),
scroll_to_bottom: button::State::new(),
scrollbar_width: None,
scrollbar_margin: None,
scroller_width: None,
@ -216,9 +207,6 @@ impl Variant {
},
Self {
title: "Slimmed & Margin",
scrollable: scrollable::State::new(),
scroll_to_top: button::State::new(),
scroll_to_bottom: button::State::new(),
scrollbar_width: Some(4),
scrollbar_margin: Some(3),
scroller_width: Some(4),
@ -226,9 +214,6 @@ impl Variant {
},
Self {
title: "Wide Scroller",
scrollable: scrollable::State::new(),
scroll_to_top: button::State::new(),
scroll_to_bottom: button::State::new(),
scrollbar_width: Some(4),
scrollbar_margin: None,
scroller_width: Some(10),
@ -236,9 +221,6 @@ impl Variant {
},
Self {
title: "Narrow Scroller",
scrollable: scrollable::State::new(),
scroll_to_top: button::State::new(),
scroll_to_bottom: button::State::new(),
scrollbar_width: Some(10),
scrollbar_margin: None,
scroller_width: Some(4),