Skip to content

Commit 11382d8

Browse files
Ericky Dos Santosclaude
andcommitted
feat(operation): scroll_to_animated by id
The animated twin of scroll_to: finds the scrollable with the given Id and hands it an offset plus a ScrollAnimation. Lets an application drive a scroll region to an absolute, index-computed target — a carousel parking its focused card — instead of leaning on ensure-visible measurements of geometry that may still be animating. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 59549d5 commit 11382d8

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

core/src/widget/operation/scrollable.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,45 @@ pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset<Option<f32>>) -> impl Ope
129129
ScrollTo { target, offset }
130130
}
131131

132+
/// Produces an [`Operation`] that animates the widget with the given [`Id`]
133+
/// to the provided [`AbsoluteOffset`].
134+
pub fn scroll_to_animated<T>(
135+
target: Id,
136+
offset: AbsoluteOffset<Option<f32>>,
137+
animation: ScrollAnimation,
138+
) -> impl Operation<T> {
139+
struct ScrollToAnimated {
140+
target: Id,
141+
offset: AbsoluteOffset<Option<f32>>,
142+
animation: ScrollAnimation,
143+
}
144+
145+
impl<T> Operation<T> for ScrollToAnimated {
146+
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<T>)) {
147+
operate(self);
148+
}
149+
150+
fn scrollable(
151+
&mut self,
152+
id: Option<&Id>,
153+
_bounds: Rectangle,
154+
_content_bounds: Rectangle,
155+
_translation: Vector,
156+
state: &mut dyn Scrollable,
157+
) {
158+
if Some(&self.target) == id {
159+
state.scroll_to_animated(self.offset, self.animation);
160+
}
161+
}
162+
}
163+
164+
ScrollToAnimated {
165+
target,
166+
offset,
167+
animation,
168+
}
169+
}
170+
132171
/// Produces an [`Operation`] that scrolls the widget with the given [`Id`] by
133172
/// the provided [`AbsoluteOffset`].
134173
pub fn scroll_by<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {

0 commit comments

Comments
 (0)