Skip to content

Commit

Permalink
Merge pull request #2585 from dtzxporter/scrollbar-show-always-disabled
Browse files Browse the repository at this point in the history
Scrollbar disabled style status to allow custom styles for always visible, but not overflowing scrollbars.
  • Loading branch information
hecrj authored Jan 6, 2025
2 parents e543329 + eee3588 commit 2b4f428
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions widget/src/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,14 +906,21 @@ where
is_vertical_scrollbar_dragged: state
.y_scroller_grabbed_at
.is_some(),
is_horizontal_scrollbar_disabled: scrollbars.is_x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.is_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.is_x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.is_y_disabled(),
}
} else {
Status::Active
Status::Active {
is_horizontal_scrollbar_disabled: scrollbars.is_x_disabled(),
is_vertical_scrollbar_disabled: scrollbars.is_y_disabled(),
}
};

if let Event::Window(window::Event::RedrawRequested(_now)) = event {
Expand Down Expand Up @@ -968,8 +975,13 @@ where
_ => mouse::Cursor::Unavailable,
};

let style = theme
.style(&self.class, self.last_status.unwrap_or(Status::Active));
let style = theme.style(
&self.class,
self.last_status.unwrap_or(Status::Active {
is_horizontal_scrollbar_disabled: false,
is_vertical_scrollbar_disabled: false,
}),
);

container::draw_background(renderer, &style.container, layout.bounds());

Expand Down Expand Up @@ -1661,6 +1673,7 @@ impl Scrollbars {
bounds: scrollbar_bounds,
scroller,
alignment: vertical.alignment,
disabled: content_bounds.height <= bounds.height,
})
} else {
None
Expand Down Expand Up @@ -1730,6 +1743,7 @@ impl Scrollbars {
bounds: scrollbar_bounds,
scroller,
alignment: horizontal.alignment,
disabled: content_bounds.width <= bounds.width,
})
} else {
None
Expand Down Expand Up @@ -1758,6 +1772,14 @@ impl Scrollbars {
}
}

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

fn is_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 @@ -1804,6 +1826,7 @@ pub(super) mod internals {
pub bounds: Rectangle,
pub scroller: Option<Scroller>,
pub alignment: Anchor,
pub disabled: bool,
}

impl Scrollbar {
Expand Down Expand Up @@ -1867,20 +1890,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 @@ -1958,7 +1994,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 @@ -1967,6 +2003,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 @@ -1994,6 +2031,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 2b4f428

Please sign in to comment.