iced_core/widget/operation/
scrollable.rs1use crate::widget::{Id, Operation};
3use crate::{Rectangle, Vector};
4
5pub trait Scrollable {
7 fn snap_to(&mut self, offset: RelativeOffset);
9
10 fn scroll_to(&mut self, offset: AbsoluteOffset);
12
13 fn scroll_by(
15 &mut self,
16 offset: AbsoluteOffset,
17 bounds: Rectangle,
18 content_bounds: Rectangle,
19 );
20}
21
22pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> {
25 struct SnapTo {
26 target: Id,
27 offset: RelativeOffset,
28 }
29
30 impl<T> Operation<T> for SnapTo {
31 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
32 operate(self);
33 }
34
35 fn scrollable(
36 &mut self,
37 id: Option<&Id>,
38 _bounds: Rectangle,
39 _content_bounds: Rectangle,
40 _translation: Vector,
41 state: &mut dyn Scrollable,
42 ) {
43 if Some(&self.target) == id {
44 state.snap_to(self.offset);
45 }
46 }
47 }
48
49 SnapTo { target, offset }
50}
51
52pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {
55 struct ScrollTo {
56 target: Id,
57 offset: AbsoluteOffset,
58 }
59
60 impl<T> Operation<T> for ScrollTo {
61 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
62 operate(self);
63 }
64
65 fn scrollable(
66 &mut self,
67 id: Option<&Id>,
68 _bounds: Rectangle,
69 _content_bounds: Rectangle,
70 _translation: Vector,
71 state: &mut dyn Scrollable,
72 ) {
73 if Some(&self.target) == id {
74 state.scroll_to(self.offset);
75 }
76 }
77 }
78
79 ScrollTo { target, offset }
80}
81
82pub fn scroll_by<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {
85 struct ScrollBy {
86 target: Id,
87 offset: AbsoluteOffset,
88 }
89
90 impl<T> Operation<T> for ScrollBy {
91 fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
92 operate(self);
93 }
94
95 fn scrollable(
96 &mut self,
97 id: Option<&Id>,
98 bounds: Rectangle,
99 content_bounds: Rectangle,
100 _translation: Vector,
101 state: &mut dyn Scrollable,
102 ) {
103 if Some(&self.target) == id {
104 state.scroll_by(self.offset, bounds, content_bounds);
105 }
106 }
107 }
108
109 ScrollBy { target, offset }
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Default)]
114pub struct AbsoluteOffset {
115 pub x: f32,
117 pub y: f32,
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Default)]
125pub struct RelativeOffset {
126 pub x: f32,
128 pub y: f32,
130}
131
132impl RelativeOffset {
133 pub const START: Self = Self { x: 0.0, y: 0.0 };
135
136 pub const END: Self = Self { x: 1.0, y: 1.0 };
138}