Skip to content

Commit 60973e4

Browse files
committed
feat(scrollable): add padding field for Scrollbar
This allows e.g. for the Scrollbar to be shorter than the scrollable
1 parent bb22add commit 60973e4

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

widget/src/scrollable.rs

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ pub struct Scrollbar {
298298
scroller_width: f32,
299299
alignment: Anchor,
300300
spacing: Option<f32>,
301+
padding: Padding,
301302
}
302303

303304
impl Default for Scrollbar {
@@ -308,6 +309,7 @@ impl Default for Scrollbar {
308309
scroller_width: 10.0,
309310
alignment: Anchor::Start,
310311
spacing: None,
312+
padding: Padding::ZERO,
311313
}
312314
}
313315
}
@@ -357,6 +359,12 @@ impl Scrollbar {
357359
self.spacing = Some(spacing.into().0);
358360
self
359361
}
362+
363+
/// Sets the [`Padding`] of the [`Scrollbar`] .
364+
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
365+
self.padding = padding.into();
366+
self
367+
}
360368
}
361369

362370
/// The anchor of the scroller of the [`Scrollable`] relative to its [`Viewport`]
@@ -1767,6 +1775,7 @@ impl Scrollbars {
17671775
width,
17681776
margin,
17691777
scroller_width,
1778+
padding,
17701779
..
17711780
} = *vertical;
17721781

@@ -1775,22 +1784,28 @@ impl Scrollbars {
17751784
let x_scrollbar_height =
17761785
show_scrollbar_x.map_or(0.0, |h| h.width.max(h.scroller_width) + h.margin);
17771786

1787+
let padded_height =
1788+
(bounds.height - x_scrollbar_height - padding.top - padding.bottom).max(0.0);
1789+
17781790
let total_scrollbar_width = width.max(scroller_width) + 2.0 * margin;
17791791

17801792
// Total bounds of the scrollbar + margin + scroller width
17811793
let total_scrollbar_bounds = Rectangle {
1782-
x: bounds.x + bounds.width - total_scrollbar_width,
1783-
y: bounds.y,
1794+
x: bounds.x + bounds.width - total_scrollbar_width - padding.right,
1795+
y: bounds.y + padding.top,
17841796
width: total_scrollbar_width,
1785-
height: (bounds.height - x_scrollbar_height).max(0.0),
1797+
height: padded_height,
17861798
};
17871799

17881800
// Bounds of just the scrollbar
17891801
let scrollbar_bounds = Rectangle {
1790-
x: bounds.x + bounds.width - total_scrollbar_width / 2.0 - width / 2.0,
1791-
y: bounds.y,
1802+
x: bounds.x + bounds.width
1803+
- total_scrollbar_width / 2.0
1804+
- width / 2.0
1805+
- padding.right,
1806+
y: bounds.y + padding.top,
17921807
width,
1793-
height: (bounds.height - x_scrollbar_height).max(0.0),
1808+
height: padded_height,
17941809
};
17951810

17961811
let ratio = bounds.height / content_bounds.height;
@@ -1804,7 +1819,10 @@ impl Scrollbars {
18041819
translation.y * ratio * scrollbar_bounds.height / bounds.height;
18051820

18061821
let scroller_bounds = Rectangle {
1807-
x: bounds.x + bounds.width - total_scrollbar_width / 2.0 - scroller_width / 2.0,
1822+
x: bounds.x + bounds.width
1823+
- total_scrollbar_width / 2.0
1824+
- scroller_width / 2.0
1825+
- padding.right,
18081826
y: (scrollbar_bounds.y + scroller_offset).max(0.0),
18091827
width: scroller_width,
18101828
height: scroller_height,
@@ -1831,6 +1849,7 @@ impl Scrollbars {
18311849
width,
18321850
margin,
18331851
scroller_width,
1852+
padding,
18341853
..
18351854
} = *horizontal;
18361855

@@ -1839,21 +1858,27 @@ impl Scrollbars {
18391858
let scrollbar_y_width =
18401859
y_scrollbar.map_or(0.0, |scrollbar| scrollbar.total_bounds.width);
18411860

1861+
let padded_width =
1862+
(bounds.width - scrollbar_y_width - padding.left - padding.right).max(0.0);
1863+
18421864
let total_scrollbar_height = width.max(scroller_width) + 2.0 * margin;
18431865

18441866
// Total bounds of the scrollbar + margin + scroller width
18451867
let total_scrollbar_bounds = Rectangle {
1846-
x: bounds.x,
1847-
y: bounds.y + bounds.height - total_scrollbar_height,
1848-
width: (bounds.width - scrollbar_y_width).max(0.0),
1868+
x: bounds.x + padding.left,
1869+
y: bounds.y + bounds.height - total_scrollbar_height - padding.bottom,
1870+
width: padded_width,
18491871
height: total_scrollbar_height,
18501872
};
18511873

18521874
// Bounds of just the scrollbar
18531875
let scrollbar_bounds = Rectangle {
1854-
x: bounds.x,
1855-
y: bounds.y + bounds.height - total_scrollbar_height / 2.0 - width / 2.0,
1856-
width: (bounds.width - scrollbar_y_width).max(0.0),
1876+
x: bounds.x + padding.left,
1877+
y: bounds.y + bounds.height
1878+
- total_scrollbar_height / 2.0
1879+
- width / 2.0
1880+
- padding.bottom,
1881+
width: padded_width,
18571882
height: width,
18581883
};
18591884

@@ -1870,7 +1895,8 @@ impl Scrollbars {
18701895
x: (scrollbar_bounds.x + scroller_offset).max(0.0),
18711896
y: bounds.y + bounds.height
18721897
- total_scrollbar_height / 2.0
1873-
- scroller_width / 2.0,
1898+
- scroller_width / 2.0
1899+
- padding.bottom,
18741900
width: scroller_length,
18751901
height: scroller_width,
18761902
};

0 commit comments

Comments
 (0)