From 57510c43c853c7332890f8e7f36c6ba1f2a7f252 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Fri, 4 Jun 2021 20:15:06 +0700 Subject: Add buttons to control scrolling in `scrollable` example --- examples/scrollable/src/main.rs | 52 +++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index a570f0f6..32c44df8 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -17,6 +17,8 @@ struct ScrollableDemo { #[derive(Debug, Clone)] enum Message { ThemeChanged(style::Theme), + ScrollToTop(usize), + ScrollToBottom(usize), } impl Sandbox for ScrollableDemo { @@ -36,6 +38,16 @@ impl Sandbox for ScrollableDemo { fn update(&mut self, message: Message) { match message { Message::ThemeChanged(theme) => self.theme = theme, + Message::ScrollToTop(i) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.scrollable.snap_to(0.0); + } + } + Message::ScrollToBottom(i) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.scrollable.snap_to(1.0); + } + } } } @@ -62,7 +74,8 @@ impl Sandbox for ScrollableDemo { let scrollable_row = Row::with_children( variants .iter_mut() - .map(|variant| { + .enumerate() + .map(|(i, variant)| { let mut scrollable = Scrollable::new(&mut variant.scrollable) .padding(10) @@ -70,7 +83,16 @@ impl Sandbox for ScrollableDemo { .width(Length::Fill) .height(Length::Fill) .style(*theme) - .push(Text::new(variant.title)); + .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)), + ); if let Some(scrollbar_width) = variant.scrollbar_width { scrollable = scrollable @@ -110,15 +132,16 @@ impl Sandbox for ScrollableDemo { .push(Space::with_height(Length::Units(1200))) .push(Text::new("Middle")) .push(Space::with_height(Length::Units(1200))) + .push(Text::new("The End.")) .push( Button::new( - &mut variant.button, - Text::new("I am a button"), + &mut variant.scroll_to_top, + Text::new("Scroll to top"), ) .width(Length::Fill) - .padding(10), - ) - .push(Text::new("The End.")); + .padding(10) + .on_press(Message::ScrollToTop(i)), + ); Container::new(scrollable) .width(Length::Fill) @@ -153,7 +176,8 @@ impl Sandbox for ScrollableDemo { struct Variant { title: &'static str, scrollable: scrollable::State, - button: button::State, + scroll_to_top: button::State, + scroll_to_bottom: button::State, scrollbar_width: Option, scrollbar_margin: Option, scroller_width: Option, @@ -165,7 +189,8 @@ impl Variant { Self { title: "Default Scrollbar", scrollable: scrollable::State::new(), - button: button::State::new(), + scroll_to_top: button::State::new(), + scroll_to_bottom: button::State::new(), scrollbar_width: None, scrollbar_margin: None, scroller_width: None, @@ -173,7 +198,8 @@ impl Variant { Self { title: "Slimmed & Margin", scrollable: scrollable::State::new(), - button: button::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), @@ -181,7 +207,8 @@ impl Variant { Self { title: "Wide Scroller", scrollable: scrollable::State::new(), - button: button::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), @@ -189,7 +216,8 @@ impl Variant { Self { title: "Narrow Scroller", scrollable: scrollable::State::new(), - button: button::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), -- cgit From ce3a5f19b92889d03f564133a90d328d430137af Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Fri, 4 Jun 2021 20:46:47 +0700 Subject: Add scrolling progress indicators to `scrollable` example --- examples/scrollable/src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 32c44df8..3416b83d 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,8 +1,8 @@ mod style; use iced::{ - button, scrollable, Button, Column, Container, Element, Length, Radio, Row, - Rule, Sandbox, Scrollable, Settings, Space, Text, + button, scrollable, Button, Column, Container, Element, Length, + ProgressBar, Radio, Row, Rule, Sandbox, Scrollable, Settings, Space, Text, }; pub fn main() -> iced::Result { @@ -19,6 +19,7 @@ enum Message { ThemeChanged(style::Theme), ScrollToTop(usize), ScrollToBottom(usize), + Scrolled(usize, f32), } impl Sandbox for ScrollableDemo { @@ -41,11 +42,20 @@ impl Sandbox for ScrollableDemo { Message::ScrollToTop(i) => { if let Some(variant) = self.variants.get_mut(i) { 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); + + variant.latest_offset = 1.0; + } + } + Message::Scrolled(i, offset) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.latest_offset = offset; } } } @@ -82,6 +92,9 @@ impl Sandbox for ScrollableDemo { .spacing(10) .width(Length::Fill) .height(Length::Fill) + .on_scroll(move |offset| { + Message::Scrolled(i, offset) + }) .style(*theme) .push(Text::new(variant.title)) .push( @@ -143,10 +156,20 @@ impl Sandbox for ScrollableDemo { .on_press(Message::ScrollToTop(i)), ); - Container::new(scrollable) + Column::new() .width(Length::Fill) .height(Length::Fill) - .style(*theme) + .spacing(10) + .push( + Container::new(scrollable) + .width(Length::Fill) + .height(Length::Fill) + .style(*theme), + ) + .push(ProgressBar::new( + 0.0..=1.0, + variant.latest_offset, + )) .into() }) .collect(), @@ -181,6 +204,7 @@ struct Variant { scrollbar_width: Option, scrollbar_margin: Option, scroller_width: Option, + latest_offset: f32, } impl Variant { @@ -194,6 +218,7 @@ impl Variant { scrollbar_width: None, scrollbar_margin: None, scroller_width: None, + latest_offset: 0.0, }, Self { title: "Slimmed & Margin", @@ -203,6 +228,7 @@ impl Variant { scrollbar_width: Some(4), scrollbar_margin: Some(3), scroller_width: Some(4), + latest_offset: 0.0, }, Self { title: "Wide Scroller", @@ -212,6 +238,7 @@ impl Variant { scrollbar_width: Some(4), scrollbar_margin: None, scroller_width: Some(10), + latest_offset: 0.0, }, Self { title: "Narrow Scroller", @@ -221,6 +248,7 @@ impl Variant { scrollbar_width: Some(10), scrollbar_margin: None, scroller_width: Some(4), + latest_offset: 0.0, }, ] } -- cgit