iced_native/widget/operation/
scrollable.rs1use crate::widget::{Id, Operation};
3
4pub trait Scrollable {
6 fn snap_to(&mut self, offset: RelativeOffset);
8}
9
10pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> {
13 struct SnapTo {
14 target: Id,
15 offset: RelativeOffset,
16 }
17
18 impl<T> Operation<T> for SnapTo {
19 fn container(
20 &mut self,
21 _id: Option<&Id>,
22 operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
23 ) {
24 operate_on_children(self)
25 }
26
27 fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
28 if Some(&self.target) == id {
29 state.snap_to(self.offset);
30 }
31 }
32 }
33
34 SnapTo { target, offset }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Default)]
41pub struct RelativeOffset {
42 pub x: f32,
44 pub y: f32,
46}
47
48impl RelativeOffset {
49 pub const START: Self = Self { x: 0.0, y: 0.0 };
51
52 pub const END: Self = Self { x: 1.0, y: 1.0 };
54}