Implement scrollable::snap_to operation

This commit is contained in:
Héctor Ramón Jiménez 2022-08-04 03:55:41 +02:00
parent 6eb3dd7e5e
commit 13dd1ca0a8
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
13 changed files with 293 additions and 204 deletions

View file

@ -1,8 +1,9 @@
use iced::executor;
use iced::widget::{
button, column, container, horizontal_rule, progress_bar, radio,
scrollable, text, vertical_space, Row,
};
use iced::{Element, Length, Sandbox, Settings, Theme};
use iced::{Application, Command, Element, Length, Settings, Theme};
pub fn main() -> iced::Result {
ScrollableDemo::run(Settings::default())
@ -21,43 +22,57 @@ enum Message {
Scrolled(usize, f32),
}
impl Sandbox for ScrollableDemo {
impl Application for ScrollableDemo {
type Message = Message;
type Theme = Theme;
type Executor = executor::Default;
type Flags = ();
fn new() -> Self {
ScrollableDemo {
theme: Default::default(),
variants: Variant::all(),
}
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
(
ScrollableDemo {
theme: Default::default(),
variants: Variant::all(),
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Scrollable - Iced")
}
fn update(&mut self, message: Message) {
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::ThemeChanged(theme) => self.theme = theme,
Message::ThemeChanged(theme) => {
self.theme = theme;
Command::none()
}
Message::ScrollToTop(i) => {
if let Some(variant) = self.variants.get_mut(i) {
// TODO
// variant.scrollable.snap_to(0.0);
variant.latest_offset = 0.0;
scrollable::snap_to(Variant::id(i), 0.0)
} else {
Command::none()
}
}
Message::ScrollToBottom(i) => {
if let Some(variant) = self.variants.get_mut(i) {
// TODO
// variant.scrollable.snap_to(1.0);
variant.latest_offset = 1.0;
scrollable::snap_to(Variant::id(i), 1.0)
} else {
Command::none()
}
}
Message::Scrolled(i, offset) => {
if let Some(variant) = self.variants.get_mut(i) {
variant.latest_offset = offset;
}
Command::none()
}
}
}
@ -136,6 +151,7 @@ impl Sandbox for ScrollableDemo {
);
let mut scrollable = scrollable(contents)
.id(Variant::id(i))
.height(Length::Fill)
.on_scroll(move |offset| Message::Scrolled(i, offset));
@ -228,4 +244,8 @@ impl Variant {
},
]
}
pub fn id(i: usize) -> scrollable::Id {
scrollable::Id::new(format!("scrollable-{}", i))
}
}