Skip to content

Commit

Permalink
wip: pass bounds and content_bounds parameters in `Operations::Sc…
Browse files Browse the repository at this point in the history
…rollable::scroll_to` not to depend on `scrollable::State::last_notified`
  • Loading branch information
lazytanuki committed Jul 9, 2024
1 parent 76bbb8d commit 9499a6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
19 changes: 17 additions & 2 deletions core/src/widget/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub trait Operation<T>: Send {
_state: &mut dyn Scrollable,
_id: Option<&Id>,
_bounds: Rectangle,
_content_bounds: Rectangle,
_translation: Vector,
) {
}
Expand Down Expand Up @@ -127,9 +128,16 @@ where
state: &mut dyn Scrollable,
id: Option<&Id>,
bounds: Rectangle,
content_bounds: Rectangle,
translation: Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
self.operation.scrollable(
state,
id,
bounds,
content_bounds,
translation,
);
}

fn focusable(
Expand Down Expand Up @@ -170,9 +178,16 @@ where
state: &mut dyn Scrollable,
id: Option<&Id>,
bounds: Rectangle,
content_bounds: Rectangle,
translation: Vector,
) {
self.operation.scrollable(state, id, bounds, translation);
self.operation.scrollable(
state,
id,
bounds,
content_bounds,
translation,
);
}

fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
Expand Down
13 changes: 10 additions & 3 deletions core/src/widget/operation/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ pub trait Scrollable {
fn snap_to(&mut self, offset: RelativeOffset);

/// Scroll the widget to the given [`AbsoluteOffset`] along the horizontal & vertical axis.
fn scroll_to(&mut self, offset: AbsoluteOffset);
fn scroll_to(
&mut self,
offset: AbsoluteOffset,
bounds: Rectangle,
content_bounds: Rectangle,
);
}

/// Produces an [`Operation`] that snaps the widget with the given [`Id`] to
Expand All @@ -34,6 +39,7 @@ pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> {
state: &mut dyn Scrollable,
id: Option<&Id>,
_bounds: Rectangle,
_content_bounds: Rectangle,
_translation: Vector,
) {
if Some(&self.target) == id {
Expand Down Expand Up @@ -67,11 +73,12 @@ pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {
&mut self,
state: &mut dyn Scrollable,
id: Option<&Id>,
_bounds: Rectangle,
bounds: Rectangle,
content_bounds: Rectangle,
_translation: Vector,
) {
if Some(&self.target) == id {
state.scroll_to(self.offset);
state.scroll_to(self.offset, bounds, content_bounds);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions widget/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ pub fn visible_bounds(id: Id) -> Task<Option<Rectangle>> {
_state: &mut dyn widget::operation::Scrollable,
_id: Option<&widget::Id>,
bounds: Rectangle,
_content_bounds: Rectangle,
translation: Vector,
) {
match self.scrollables.last() {
Expand Down
31 changes: 18 additions & 13 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ where
state,
self.id.as_ref().map(|id| &id.0),
bounds,
content_bounds,
translation,
);

Expand Down Expand Up @@ -1031,8 +1032,13 @@ impl operation::Scrollable for State {
State::snap_to(self, offset);
}

fn scroll_to(&mut self, offset: AbsoluteOffset) {
State::scroll_to(self, offset);
fn scroll_to(
&mut self,
offset: AbsoluteOffset,
bounds: Rectangle,
content_bounds: Rectangle,
) {
State::scroll_to(self, offset, bounds, content_bounds);
}
}

Expand Down Expand Up @@ -1210,17 +1216,16 @@ impl State {
}

/// Scroll to the provided [`AbsoluteOffset`].
pub fn scroll_to(&mut self, offset: AbsoluteOffset) {
// TODO: do we have cases where `self.last_notified` is `None`?
if let Some(viewport) = self.last_notified {
self.offset_x_relative = Offset::Absolute(offset.x.max(0.0))
.relative(viewport.bounds.width, viewport.content_bounds.width);
self.offset_y_relative = Offset::Absolute(offset.y.max(0.0))
.relative(
viewport.bounds.height,
viewport.content_bounds.height,
);
}
pub fn scroll_to(
&mut self,
offset: AbsoluteOffset,
bounds: Rectangle,
content_bounds: Rectangle,
) {
self.offset_x_relative = Offset::Absolute(offset.x.max(0.0))
.relative(bounds.width, content_bounds.width);
self.offset_y_relative = Offset::Absolute(offset.y.max(0.0))
.relative(bounds.height, content_bounds.height);
}

/// Returns the scrolling translation of the [`State`], given a [`Direction`],
Expand Down

0 comments on commit 9499a6b

Please sign in to comment.