Skip to content

Commit

Permalink
Migrate scrollbar disabled style status to iced 0.13.
Browse files Browse the repository at this point in the history
Feel free to change the 'name' of it. I originally used 'disabled' to signal that it's not usable, but still visible. 'Overflowing' may be a better term.
  • Loading branch information
dtzxporter committed Sep 19, 2024
1 parent bf3b6f1 commit db2a4dc
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,21 @@ where
is_vertical_scrollbar_dragged: state
.y_scroller_grabbed_at
.is_some(),
is_horizontal_scrollbar_disabled: scrollbars.x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.y_disabled(),
}
} else if cursor_over_scrollable.is_some() {
Status::Hovered {
is_horizontal_scrollbar_hovered: mouse_over_x_scrollbar,
is_vertical_scrollbar_hovered: mouse_over_y_scrollbar,
is_horizontal_scrollbar_disabled: scrollbars.x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.y_disabled(),
}
} else {
Status::Active
Status::Active {
is_horizontal_scrollbar_disabled: scrollbars.x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.y_disabled(),
}
};

let style = theme.style(&self.class, status);
Expand Down Expand Up @@ -1559,15 +1566,27 @@ impl Scrollbars {
) -> Self {
let translation = state.translation(direction, bounds, content_bounds);

let show_scrollbar_x = direction.horizontal().filter(|scrollbar| {
scrollbar.spacing.is_some() || content_bounds.width > bounds.width
});
let show_scrollbar_x = direction
.horizontal()
.filter(|scrollbar| {
scrollbar.spacing.is_some()
|| content_bounds.width > bounds.width
})
.map(|properties| {
(properties, content_bounds.width <= bounds.width)
});

let show_scrollbar_y = direction.vertical().filter(|scrollbar| {
scrollbar.spacing.is_some() || content_bounds.height > bounds.height
});
let show_scrollbar_y = direction
.vertical()
.filter(|scrollbar| {
scrollbar.spacing.is_some()
|| content_bounds.height > bounds.height
})
.map(|properties| {
(properties, content_bounds.height <= bounds.height)
});

let y_scrollbar = if let Some(vertical) = show_scrollbar_y {
let y_scrollbar = if let Some((vertical, disabled)) = show_scrollbar_y {
let Scrollbar {
width,
margin,
Expand All @@ -1578,7 +1597,7 @@ impl Scrollbars {
// Adjust the height of the vertical scrollbar if the horizontal scrollbar
// is present
let x_scrollbar_height = show_scrollbar_x
.map_or(0.0, |h| h.width.max(h.scroller_width) + h.margin);
.map_or(0.0, |(h, _)| h.width.max(h.scroller_width) + h.margin);

let total_scrollbar_width =
width.max(scroller_width) + 2.0 * margin;
Expand Down Expand Up @@ -1632,12 +1651,14 @@ impl Scrollbars {
bounds: scrollbar_bounds,
scroller,
alignment: vertical.alignment,
disabled,
})
} else {
None
};

let x_scrollbar = if let Some(horizontal) = show_scrollbar_x {
let x_scrollbar = if let Some((horizontal, disabled)) = show_scrollbar_x
{
let Scrollbar {
width,
margin,
Expand Down Expand Up @@ -1701,6 +1722,7 @@ impl Scrollbars {
bounds: scrollbar_bounds,
scroller,
alignment: horizontal.alignment,
disabled,
})
} else {
None
Expand Down Expand Up @@ -1729,6 +1751,14 @@ impl Scrollbars {
}
}

fn y_disabled(&self) -> bool {
self.y.map(|y| y.disabled).unwrap_or(false)
}

fn x_disabled(&self) -> bool {
self.x.map(|x| x.disabled).unwrap_or(false)
}

fn grab_y_scroller(&self, cursor_position: Point) -> Option<f32> {
let scrollbar = self.y?;
let scroller = scrollbar.scroller?;
Expand Down Expand Up @@ -1775,6 +1805,7 @@ pub(super) mod internals {
pub bounds: Rectangle,
pub scroller: Option<Scroller>,
pub alignment: Anchor,
pub disabled: bool,
}

impl Scrollbar {
Expand Down Expand Up @@ -1838,20 +1869,33 @@ pub(super) mod internals {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
/// The [`Scrollable`] can be interacted with.
Active,
Active {
/// Whether or not the horizontal scrollbar is disabled meaning the content isn't overflowing.
is_horizontal_scrollbar_disabled: bool,
/// Whether or not the vertical scrollbar is disabled meaning the content isn't overflowing.
is_vertical_scrollbar_disabled: bool,
},
/// The [`Scrollable`] is being hovered.
Hovered {
/// Indicates if the horizontal scrollbar is being hovered.
is_horizontal_scrollbar_hovered: bool,
/// Indicates if the vertical scrollbar is being hovered.
is_vertical_scrollbar_hovered: bool,
/// Whether or not the horizontal scrollbar is disabled meaning the content isn't overflowing.
is_horizontal_scrollbar_disabled: bool,
/// Whether or not the vertical scrollbar is disabled meaning the content isn't overflowing.
is_vertical_scrollbar_disabled: bool,
},
/// The [`Scrollable`] is being dragged.
Dragged {
/// Indicates if the horizontal scrollbar is being dragged.
is_horizontal_scrollbar_dragged: bool,
/// Indicates if the vertical scrollbar is being dragged.
is_vertical_scrollbar_dragged: bool,
/// Whether or not the horizontal scrollbar is disabled meaning the content isn't overflowing.
is_horizontal_scrollbar_disabled: bool,
/// Whether or not the vertical scrollbar is disabled meaning the content isn't overflowing.
is_vertical_scrollbar_disabled: bool,
},
}

Expand Down Expand Up @@ -1929,7 +1973,7 @@ pub fn default(theme: &Theme, status: Status) -> Style {
};

match status {
Status::Active => Style {
Status::Active { .. } => Style {
container: container::Style::default(),
vertical_rail: scrollbar,
horizontal_rail: scrollbar,
Expand All @@ -1938,6 +1982,7 @@ pub fn default(theme: &Theme, status: Status) -> Style {
Status::Hovered {
is_horizontal_scrollbar_hovered,
is_vertical_scrollbar_hovered,
..
} => {
let hovered_scrollbar = Rail {
scroller: Scroller {
Expand Down Expand Up @@ -1965,6 +2010,7 @@ pub fn default(theme: &Theme, status: Status) -> Style {
Status::Dragged {
is_horizontal_scrollbar_dragged,
is_vertical_scrollbar_dragged,
..
} => {
let dragged_scrollbar = Rail {
scroller: Scroller {
Expand Down

0 comments on commit db2a4dc

Please sign in to comment.