Skip to content

Commit

Permalink
Introduce absolute_offset_reversed to scrollable::Viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 13, 2023
1 parent 11287c8 commit d29de5e
Showing 1 changed file with 15 additions and 58 deletions.
73 changes: 15 additions & 58 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,14 +554,7 @@ pub fn update<Message>(

state.scroll(delta, direction, bounds, content_bounds);

notify_on_scroll(
state,
on_scroll,
bounds,
content_bounds,
direction,
shell,
);
notify_on_scroll(state, on_scroll, bounds, content_bounds, shell);

return event::Status::Captured;
}
Expand Down Expand Up @@ -599,7 +592,6 @@ pub fn update<Message>(
on_scroll,
bounds,
content_bounds,
direction,
shell,
);
}
Expand Down Expand Up @@ -645,7 +637,6 @@ pub fn update<Message>(
on_scroll,
bounds,
content_bounds,
direction,
shell,
);

Expand Down Expand Up @@ -681,7 +672,6 @@ pub fn update<Message>(
on_scroll,
bounds,
content_bounds,
direction,
shell,
);
}
Expand Down Expand Up @@ -722,7 +712,6 @@ pub fn update<Message>(
on_scroll,
bounds,
content_bounds,
direction,
shell,
);
}
Expand Down Expand Up @@ -758,7 +747,6 @@ pub fn update<Message>(
on_scroll,
bounds,
content_bounds,
direction,
shell,
);

Expand Down Expand Up @@ -974,7 +962,6 @@ fn notify_on_scroll<Message>(
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
bounds: Rectangle,
content_bounds: Rectangle,
direction: Direction,
shell: &mut Shell<'_, Message>,
) {
if let Some(on_scroll) = on_scroll {
Expand All @@ -984,23 +971,11 @@ fn notify_on_scroll<Message>(
return;
}

let horizontal_alignment = direction
.horizontal()
.map(|p| p.alignment)
.unwrap_or_default();

let vertical_alignment = direction
.vertical()
.map(|p| p.alignment)
.unwrap_or_default();

let viewport = Viewport {
offset_x: state.offset_x,
offset_y: state.offset_y,
bounds,
content_bounds,
vertical_alignment,
horizontal_alignment,
};

// Don't publish redundant viewports to shell
Expand Down Expand Up @@ -1105,8 +1080,6 @@ pub struct Viewport {
offset_y: Offset,
bounds: Rectangle,
content_bounds: Rectangle,
vertical_alignment: Alignment,
horizontal_alignment: Alignment,
}

impl Viewport {
Expand All @@ -1122,6 +1095,20 @@ impl Viewport {
AbsoluteOffset { x, y }
}

/// Returns the [`AbsoluteOffset`] of the current [`Viewport`], but with its
/// alignment reversed.
///
/// This method can be useful to switch the alignment of a [`Scrollable`]
/// while maintaining its scrolling position.
pub fn absolute_offset_reversed(&self) -> AbsoluteOffset {
let AbsoluteOffset { x, y } = self.absolute_offset();

AbsoluteOffset {
x: (x - self.content_bounds.width - self.bounds.width).max(0.0),
y: (y - self.content_bounds.height - self.bounds.height).max(0.0),
}
}

/// Returns the [`RelativeOffset`] of the current [`Viewport`].
pub fn relative_offset(&self) -> RelativeOffset {
let AbsoluteOffset { x, y } = self.absolute_offset();
Expand All @@ -1131,36 +1118,6 @@ impl Viewport {

RelativeOffset { x, y }
}

/// Returns a new [`Viewport`] with the supplied vertical [`Alignment`].
pub fn with_vertical_alignment(self, alignment: Alignment) -> Self {
if self.vertical_alignment != alignment {
let relative = 1.0 - self.relative_offset().y;

Self {
offset_y: Offset::Relative(relative),
vertical_alignment: alignment,
..self
}
} else {
self
}
}

/// Returns a new [`Viewport`] with the supplied horizontal [`Alignment`].
pub fn with_horizontal_alignment(self, alignment: Alignment) -> Self {
if self.horizontal_alignment != alignment {
let relative = 1.0 - self.relative_offset().x;

Self {
offset_x: Offset::Relative(relative),
horizontal_alignment: alignment,
..self
}
} else {
self
}
}
}

impl State {
Expand Down

0 comments on commit d29de5e

Please sign in to comment.