Fix Scrollable::spacing not embedding the Scrollbar

This commit is contained in:
Héctor Ramón Jiménez 2024-07-16 19:05:46 +02:00
parent 24f7476823
commit b518e30610
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 28 additions and 45 deletions

View file

@ -216,14 +216,13 @@ impl ScrollableDemo {
.padding([40, 0]) .padding([40, 0])
.spacing(40), .spacing(40),
) )
.direction(scrollable::Direction::Vertical { .direction(scrollable::Direction::Vertical(
scrollbar: scrollable::Scrollbar::new() scrollable::Scrollbar::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width) .scroller_width(self.scroller_width)
.anchor(self.anchor), .anchor(self.anchor),
spacing: None, ))
})
.width(Fill) .width(Fill)
.height(Fill) .height(Fill)
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())
@ -243,14 +242,13 @@ impl ScrollableDemo {
.padding([0, 40]) .padding([0, 40])
.spacing(40), .spacing(40),
) )
.direction(scrollable::Direction::Horizontal { .direction(scrollable::Direction::Horizontal(
scrollbar: scrollable::Scrollbar::new() scrollable::Scrollbar::new()
.width(self.scrollbar_width) .width(self.scrollbar_width)
.margin(self.scrollbar_margin) .margin(self.scrollbar_margin)
.scroller_width(self.scroller_width) .scroller_width(self.scroller_width)
.anchor(self.anchor), .anchor(self.anchor),
spacing: None, ))
})
.width(Fill) .width(Fill)
.height(Fill) .height(Fill)
.id(SCROLLABLE_ID.clone()) .id(SCROLLABLE_ID.clone())

View file

@ -133,10 +133,7 @@ where
/// Sets the [`Anchor`] of the horizontal direction of the [`Scrollable`], if applicable. /// Sets the [`Anchor`] of the horizontal direction of the [`Scrollable`], if applicable.
pub fn anchor_x(mut self, alignment: Anchor) -> Self { pub fn anchor_x(mut self, alignment: Anchor) -> Self {
match &mut self.direction { match &mut self.direction {
Direction::Horizontal { Direction::Horizontal(horizontal)
scrollbar: horizontal,
..
}
| Direction::Both { horizontal, .. } => { | Direction::Both { horizontal, .. } => {
horizontal.alignment = alignment; horizontal.alignment = alignment;
} }
@ -149,10 +146,7 @@ where
/// Sets the [`Anchor`] of the vertical direction of the [`Scrollable`], if applicable. /// Sets the [`Anchor`] of the vertical direction of the [`Scrollable`], if applicable.
pub fn anchor_y(mut self, alignment: Anchor) -> Self { pub fn anchor_y(mut self, alignment: Anchor) -> Self {
match &mut self.direction { match &mut self.direction {
Direction::Vertical { Direction::Vertical(vertical)
scrollbar: vertical,
..
}
| Direction::Both { vertical, .. } => { | Direction::Both { vertical, .. } => {
vertical.alignment = alignment; vertical.alignment = alignment;
} }
@ -169,9 +163,9 @@ where
/// of the [`Scrollable`]. /// of the [`Scrollable`].
pub fn spacing(mut self, new_spacing: impl Into<Pixels>) -> Self { pub fn spacing(mut self, new_spacing: impl Into<Pixels>) -> Self {
match &mut self.direction { match &mut self.direction {
Direction::Horizontal { spacing, .. } Direction::Horizontal(scrollbar)
| Direction::Vertical { spacing, .. } => { | Direction::Vertical(scrollbar) => {
*spacing = Some(new_spacing.into().0); scrollbar.spacing = Some(new_spacing.into().0);
} }
Direction::Both { .. } => {} Direction::Both { .. } => {}
} }
@ -202,19 +196,9 @@ where
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction { pub enum Direction {
/// Vertical scrolling /// Vertical scrolling
Vertical { Vertical(Scrollbar),
/// The vertical [`Scrollbar`].
scrollbar: Scrollbar,
/// The amount of spacing between the [`Scrollbar`] and the contents, if embedded.
spacing: Option<f32>,
},
/// Horizontal scrolling /// Horizontal scrolling
Horizontal { Horizontal(Scrollbar),
/// The horizontal [`Scrollbar`].
scrollbar: Scrollbar,
/// The amount of spacing between the [`Scrollbar`] and the contents, if embedded.
spacing: Option<f32>,
},
/// Both vertical and horizontal scrolling /// Both vertical and horizontal scrolling
Both { Both {
/// The properties of the vertical scrollbar. /// The properties of the vertical scrollbar.
@ -228,28 +212,25 @@ impl Direction {
/// Returns the horizontal [`Scrollbar`], if any. /// Returns the horizontal [`Scrollbar`], if any.
pub fn horizontal(&self) -> Option<&Scrollbar> { pub fn horizontal(&self) -> Option<&Scrollbar> {
match self { match self {
Self::Horizontal { scrollbar, .. } => Some(scrollbar), Self::Horizontal(scrollbar) => Some(scrollbar),
Self::Both { horizontal, .. } => Some(horizontal), Self::Both { horizontal, .. } => Some(horizontal),
Self::Vertical { .. } => None, Self::Vertical(_) => None,
} }
} }
/// Returns the vertical [`Scrollbar`], if any. /// Returns the vertical [`Scrollbar`], if any.
pub fn vertical(&self) -> Option<&Scrollbar> { pub fn vertical(&self) -> Option<&Scrollbar> {
match self { match self {
Self::Vertical { scrollbar, .. } => Some(scrollbar), Self::Vertical(scrollbar) => Some(scrollbar),
Self::Both { vertical, .. } => Some(vertical), Self::Both { vertical, .. } => Some(vertical),
Self::Horizontal { .. } => None, Self::Horizontal(_) => None,
} }
} }
} }
impl Default for Direction { impl Default for Direction {
fn default() -> Self { fn default() -> Self {
Self::Vertical { Self::Vertical(Scrollbar::default())
scrollbar: Scrollbar::default(),
spacing: None,
}
} }
} }
@ -363,14 +344,18 @@ where
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let (right_padding, bottom_padding) = match self.direction { let (right_padding, bottom_padding) = match self.direction {
Direction::Vertical { Direction::Vertical(Scrollbar {
scrollbar, width,
margin,
spacing: Some(spacing), spacing: Some(spacing),
} => (scrollbar.width + scrollbar.margin * 2.0 + spacing, 0.0), ..
Direction::Horizontal { }) => (width + margin * 2.0 + spacing, 0.0),
scrollbar, Direction::Horizontal(Scrollbar {
width,
margin,
spacing: Some(spacing), spacing: Some(spacing),
} => (0.0, scrollbar.width + scrollbar.margin * 2.0 + spacing), ..
}) => (0.0, width + margin * 2.0 + spacing),
_ => (0.0, 0.0), _ => (0.0, 0.0),
}; };