Require a Direction when computing State::offset in scrollable

This commit is contained in:
Héctor Ramón Jiménez 2023-06-27 22:41:16 +02:00
parent 493571695a
commit 412e15b170
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -358,10 +358,11 @@ where
let bounds = layout.bounds(); let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap(); let content_layout = layout.children().next().unwrap();
let content_bounds = content_layout.bounds(); let content_bounds = content_layout.bounds();
let offset = tree let offset = tree.state.downcast_ref::<State>().offset(
.state &self.direction,
.downcast_ref::<State>() bounds,
.offset(bounds, content_bounds); content_bounds,
);
overlay.translate(Vector::new(-offset.x, -offset.y)) overlay.translate(Vector::new(-offset.x, -offset.y))
}) })
@ -493,7 +494,8 @@ pub fn update<Message>(
if !(mouse_over_x_scrollbar || mouse_over_y_scrollbar) => if !(mouse_over_x_scrollbar || mouse_over_y_scrollbar) =>
{ {
mouse::Cursor::Available( mouse::Cursor::Available(
cursor_position + state.offset(bounds, content_bounds), cursor_position
+ state.offset(direction, bounds, content_bounds),
) )
} }
_ => mouse::Cursor::Unavailable, _ => mouse::Cursor::Unavailable,
@ -564,12 +566,6 @@ pub fn update<Message>(
cursor_position.y - scroll_box_touched_at.y, cursor_position.y - scroll_box_touched_at.y,
); );
let delta = Vector::new(
delta.x
* direction.horizontal().map_or(0.0, |_| 1.0),
delta.y * direction.vertical().map_or(0.0, |_| 1.0),
);
state.scroll(delta, bounds, content_bounds); state.scroll(delta, bounds, content_bounds);
state.scroll_area_touched_at = Some(cursor_position); state.scroll_area_touched_at = Some(cursor_position);
@ -775,7 +771,7 @@ pub fn mouse_interaction(
{ {
mouse::Interaction::Idle mouse::Interaction::Idle
} else { } else {
let offset = state.offset(bounds, content_bounds); let offset = state.offset(direction, bounds, content_bounds);
let cursor = match cursor_over_scrollable { let cursor = match cursor_over_scrollable {
Some(cursor_position) Some(cursor_position)
@ -822,7 +818,7 @@ pub fn draw<Renderer>(
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
scrollbars.is_mouse_over(cursor); scrollbars.is_mouse_over(cursor);
let offset = state.offset(bounds, content_bounds); let offset = state.offset(direction, bounds, content_bounds);
let cursor = match cursor_over_scrollable { let cursor = match cursor_over_scrollable {
Some(cursor_position) Some(cursor_position)
@ -1161,16 +1157,25 @@ impl State {
); );
} }
/// Returns the scrolling offset of the [`State`], given the bounds of the /// Returns the scrolling offset of the [`State`], given a [`Direction`],
/// [`Scrollable`] and its contents. /// the bounds of the [`Scrollable`] and its contents.
pub fn offset( pub fn offset(
&self, &self,
direction: &Direction,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
) -> Vector { ) -> Vector {
Vector::new( Vector::new(
self.offset_x.absolute(bounds.width, content_bounds.width), if direction.horizontal().is_some() {
self.offset_y.absolute(bounds.height, content_bounds.height), self.offset_x.absolute(bounds.width, content_bounds.width)
} else {
0.0
},
if direction.vertical().is_some() {
self.offset_y.absolute(bounds.height, content_bounds.height)
} else {
0.0
},
) )
} }
@ -1196,7 +1201,7 @@ impl Scrollbars {
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
) -> Self { ) -> Self {
let offset = state.offset(bounds, content_bounds); let offset = state.offset(direction, bounds, content_bounds);
let show_scrollbar_x = direction.horizontal().and_then(|h| { let show_scrollbar_x = direction.horizontal().and_then(|h| {
if content_bounds.width > bounds.width { if content_bounds.width > bounds.width {