feat(scrollable): add padding to ends of scrollbar

This adds a padding field to the scrollbar, which adds padding at the start and end of scrollbars, without affecting the scrollable. This enables the scrollable to use the full height/width of a container where it would otherwise be visually restricted by scrollbar placement (e.g. due to rounded corners).
Also adds methods for changing scrollbar/scroller width, allowing to match defaults to upstream, while making required changes in libcosmic.
This commit is contained in:
Vukašin Vojinović 2025-04-04 01:21:04 +02:00 committed by Ashley Wulber
parent f04e237946
commit b775402f65
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
2 changed files with 89 additions and 10 deletions

View file

@ -79,6 +79,7 @@ pub struct Image<'a, Handle = image::Handle> {
opacity: f32,
scale: f32,
expand: bool,
_phantom_data: std::marker::PhantomData<&'a ()>,
}
impl<'a, Handle> Image<'a, Handle> {
@ -103,6 +104,7 @@ impl<'a, Handle> Image<'a, Handle> {
scale: 1.0,
expand: false,
border_radius: [0.0; 4].into(),
_phantom_data: std::marker::PhantomData,
}
}

View file

@ -257,6 +257,69 @@ where
self
}
/// Sets the scrollbar width of the [`Scrollbar`].
pub fn scrollbar_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.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;
}
Direction::Both {
horizontal,
vertical,
} => {
horizontal.padding = padding;
vertical.padding = padding;
}
}
self
}
/// Sets the style of this [`Scrollable`].
#[must_use]
pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
@ -379,16 +442,18 @@ pub struct Scrollbar {
scroller_width: f32,
alignment: Anchor,
spacing: Option<f32>,
padding: f32,
}
impl Default for Scrollbar {
fn default() -> Self {
Self {
width: 8.0,
width: 10.0,
margin: 0.0,
scroller_width: 8.0,
scroller_width: 10.0,
alignment: Anchor::Start,
spacing: None,
padding: 0.0,
}
}
}
@ -438,6 +503,12 @@ impl Scrollbar {
self.spacing = Some(spacing.into().0);
self
}
/// Sets the padding at the start and end of the [`Scrollbar`].
pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
self.padding = padding.into().0.max(0.0);
self
}
}
/// The anchor of the scroller of the [`Scrollable`] relative to its [`Viewport`]
@ -2268,6 +2339,7 @@ impl Scrollbars {
width,
margin,
scroller_width,
padding,
..
} = *vertical;
@ -2282,9 +2354,10 @@ impl Scrollbars {
// Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle {
x: bounds.x + bounds.width - total_scrollbar_width,
y: bounds.y,
y: bounds.y + padding,
width: total_scrollbar_width,
height: (bounds.height - x_scrollbar_height).max(0.0),
height: (bounds.height - x_scrollbar_height - 2.0 * padding)
.max(0.0),
};
// Bounds of just the scrollbar
@ -2292,9 +2365,10 @@ impl Scrollbars {
x: bounds.x + bounds.width
- total_scrollbar_width / 2.0
- width / 2.0,
y: bounds.y,
y: bounds.y + padding,
width,
height: (bounds.height - x_scrollbar_height).max(0.0),
height: (bounds.height - x_scrollbar_height - 2.0 * padding)
.max(0.0),
};
let ratio = bounds.height / content_bounds.height;
@ -2339,6 +2413,7 @@ impl Scrollbars {
width,
margin,
scroller_width,
padding,
..
} = *horizontal;
@ -2352,19 +2427,21 @@ impl Scrollbars {
// Total bounds of the scrollbar + margin + scroller width
let total_scrollbar_bounds = Rectangle {
x: bounds.x,
x: bounds.x + padding,
y: bounds.y + bounds.height - total_scrollbar_height,
width: (bounds.width - scrollbar_y_width).max(0.0),
width: (bounds.width - scrollbar_y_width - 2.0 * padding)
.max(0.0),
height: total_scrollbar_height,
};
// Bounds of just the scrollbar
let scrollbar_bounds = Rectangle {
x: bounds.x,
x: bounds.x + padding,
y: bounds.y + bounds.height
- total_scrollbar_height / 2.0
- width / 2.0,
width: (bounds.width - scrollbar_y_width).max(0.0),
width: (bounds.width - scrollbar_y_width - 2.0 * padding)
.max(0.0),
height: width,
};