improv(scrollable): match padding impl to upstream

This commit is contained in:
Vukašin Vojinović 2026-09-18 20:29:00 +02:00 • committed by Michael Murphy
parent ffc3ac317e
commit 021cb93fa3

View file

@ -248,64 +248,19 @@ where
self self
} }
/// Sets whether the user should be allowed to auto-scroll the [`Scrollable`] /// Adds padding at the ends of the [`Scrollbar`]s of the [`Scrollable`].
/// with the middle mouse button.
/// ///
/// By default, it is disabled. /// The `padding` provided will be used as space at the top and bottom of a
pub fn auto_scroll(mut self, auto_scroll: bool) -> Self { /// vertical [`Scrollbar`], and at the left and right ends of a horizontal
self.auto_scroll = auto_scroll; /// [`Scrollbar`], when they are visible.
self ///
} /// Unlike [`Self::spacing`], the padding does not affect the layout of the
/// [`Scrollable`].
/// Sets the scrollbar width of the [`Scrollbar`]. pub fn padding(mut self, new_padding: impl Into<Pixels>) -> Self {
pub fn scrollbar_width(mut self, width: impl Into<Pixels>) -> Self { let padding = new_padding.into().0;
let width = width.into().0.max(0.0);
match &mut self.direction { match &mut self.direction {
Direction::Horizontal(scrollbar) Direction::Horizontal(scrollbar) | Direction::Vertical(scrollbar) => {
| Direction::Vertical(scrollbar) => {
scrollbar.width = width;
}
Direction::Both {
horizontal,
vertical,
} => {
horizontal.width = width;
vertical.width = width;
}
}
self
}
/// Sets the scroller width of the [`Scrollbar`].
pub fn scroller_width(mut self, width: impl Into<Pixels>) -> Self {
let width = width.into().0.max(0.0);
match &mut self.direction {
Direction::Horizontal(scrollbar)
| Direction::Vertical(scrollbar) => {
scrollbar.scroller_width = width;
}
Direction::Both {
horizontal,
vertical,
} => {
horizontal.scroller_width = width;
vertical.scroller_width = width;
}
}
self
}
/// Sets the padding at the start and end of the [`Scrollbar`].
pub fn scrollbar_padding(mut self, padding: impl Into<Pixels>) -> Self {
let padding = padding.into().0.max(0.0);
match &mut self.direction {
Direction::Horizontal(scrollbar)
| Direction::Vertical(scrollbar) => {
scrollbar.padding = padding; scrollbar.padding = padding;
} }
Direction::Both { Direction::Both {
@ -320,6 +275,15 @@ where
self self
} }
/// Sets whether the user should be allowed to auto-scroll the [`Scrollable`]
/// with the middle mouse button.
///
/// By default, it is disabled.
pub fn auto_scroll(mut self, auto_scroll: bool) -> Self {
self.auto_scroll = auto_scroll;
self
}
/// Sets the style of this [`Scrollable`]. /// Sets the style of this [`Scrollable`].
#[must_use] #[must_use]
pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
@ -504,9 +468,15 @@ impl Scrollbar {
self self
} }
/// Sets the padding at the start and end of the [`Scrollbar`]. /// Sets the padding of the [`Scrollbar`].
///
/// The padding is added at the top and bottom of the scrollbar (or at the
/// left and right ends for a horizontal [`Scrollbar`]) when it is visible.
///
/// Unlike [`Self::margin`] and [`Self::spacing`], the padding does not
/// affect the layout of the [`Scrollable`].
pub fn padding(mut self, padding: impl Into<Pixels>) -> Self { pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
self.padding = padding.into().0.max(0.0); self.padding = padding.into().0;
self self
} }
} }
@ -2414,13 +2384,16 @@ impl Scrollbars {
let total_scrollbar_width = let total_scrollbar_width =
width.max(scroller_width) + 2.0 * margin; width.max(scroller_width) + 2.0 * margin;
// The padding is purely visual: it shrinks the top and bottom of the
// scrollbar without affecting the layout
let scrollbar_height = (bounds.height - x_scrollbar_height - 2.0 * padding).max(0.0);
// Total bounds of the scrollbar + margin + scroller width // Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle { let total_scrollbar_bounds = Rectangle {
x: bounds.x + bounds.width - total_scrollbar_width, x: bounds.x + bounds.width - total_scrollbar_width,
y: bounds.y + padding, y: bounds.y + padding,
width: total_scrollbar_width, width: total_scrollbar_width,
height: (bounds.height - x_scrollbar_height - 2.0 * padding) height: scrollbar_height,
.max(0.0),
}; };
// Bounds of just the scrollbar // Bounds of just the scrollbar
@ -2430,8 +2403,7 @@ impl Scrollbars {
- width / 2.0, - width / 2.0,
y: bounds.y + padding, y: bounds.y + padding,
width, width,
height: (bounds.height - x_scrollbar_height - 2.0 * padding) height: scrollbar_height,
.max(0.0),
}; };
let ratio = bounds.height / content_bounds.height; let ratio = bounds.height / content_bounds.height;
@ -2488,12 +2460,15 @@ impl Scrollbars {
let total_scrollbar_height = let total_scrollbar_height =
width.max(scroller_width) + 2.0 * margin; width.max(scroller_width) + 2.0 * margin;
// The padding is purely visual: it shrinks the left and right ends of
// the scrollbar without affecting the layout
let scrollbar_width = (bounds.width - scrollbar_y_width - 2.0 * padding).max(0.0);
// Total bounds of the scrollbar + margin + scroller width // Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle { let total_scrollbar_bounds = Rectangle {
x: bounds.x + padding, x: bounds.x + padding,
y: bounds.y + bounds.height - total_scrollbar_height, y: bounds.y + bounds.height - total_scrollbar_height,
width: (bounds.width - scrollbar_y_width - 2.0 * padding) width: scrollbar_width,
.max(0.0),
height: total_scrollbar_height, height: total_scrollbar_height,
}; };
@ -2503,8 +2478,7 @@ impl Scrollbars {
y: bounds.y + bounds.height y: bounds.y + bounds.height
- total_scrollbar_height / 2.0 - total_scrollbar_height / 2.0
- width / 2.0, - width / 2.0,
width: (bounds.width - scrollbar_y_width - 2.0 * padding) width: scrollbar_width,
.max(0.0),
height: width, height: width,
}; };