Simplify scrollable styling API

This commit is contained in:
Héctor Ramón Jiménez 2024-02-12 19:24:09 +01:00
parent 0f920e0435
commit 0eaaeaa517
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 82 additions and 179 deletions

View file

@ -1,6 +1,18 @@
//! Change the appearance of a scrollable.
use crate::container;
use crate::core::{Background, Border, Color};
/// The appearance of a scrolable.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The [`container::Appearance`] of a scrollable.
pub container: container::Appearance,
/// The [`Scrollbar`] appearance.
pub scrollbar: Scrollbar,
/// The [`Background`] of the gap between a horizontal and vertical scrollbar.
pub gap: Option<Background>,
}
/// The appearance of the scrollbar of a scrollable.
#[derive(Debug, Clone, Copy)]
pub struct Scrollbar {
@ -21,54 +33,23 @@ pub struct Scroller {
pub border: Border,
}
/// The appearance of a scrolable.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The [`Background`] of a scrollable.
pub background: Option<Background>,
/// The [`Background`] of the gap between a horizontal and vertical scrollbar.
pub gap: Option<Background>,
}
/// A set of rules that dictate the style of a scrollable.
pub trait StyleSheet {
/// The supported style of the [`StyleSheet`].
type Style: Default;
/// Produces the style of the scrollable container.
fn appearance(&self, style: &Self::Style) -> Appearance;
/// Produces the [`Appearance`] of an active scrollable.
fn active(&self, style: &Self::Style) -> Appearance;
/// Produces the style of an active scrollbar.
fn active(&self, style: &Self::Style) -> Scrollbar;
/// Produces the style of a scrollbar when the scrollable is being hovered.
/// Produces the [`Appearance`] of a scrollable when it is being hovered.
fn hovered(
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> Scrollbar;
) -> Appearance;
/// Produces the style of a scrollbar that is being dragged.
fn dragging(&self, style: &Self::Style) -> Scrollbar {
/// Produces the [`Appearance`] of a scrollable when it is being dragged.
fn dragging(&self, style: &Self::Style) -> Appearance {
self.hovered(style, true)
}
/// Produces the style of an active horizontal scrollbar.
fn active_horizontal(&self, style: &Self::Style) -> Scrollbar {
self.active(style)
}
/// Produces the style of a horizontal scrollbar when the scrollable is being hovered.
fn hovered_horizontal(
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> Scrollbar {
self.hovered(style, is_mouse_over_scrollbar)
}
/// Produces the style of a horizontal scrollbar that is being dragged.
fn dragging_horizontal(&self, style: &Self::Style) -> Scrollbar {
self.hovered_horizontal(style, true)
}
}

View file

@ -1188,32 +1188,22 @@ impl Scrollable {
impl scrollable::StyleSheet for Theme {
type Style = Scrollable;
fn appearance(&self, style: &Self::Style) -> scrollable::Appearance {
fn active(&self, style: &Self::Style) -> scrollable::Appearance {
match style {
Scrollable::Default => {
let palette = self.extended_palette();
scrollable::Appearance {
background: None,
gap: Some(palette.background.weak.color.into()),
}
}
Scrollable::Custom(custom) => custom.appearance(self),
}
}
fn active(&self, style: &Self::Style) -> scrollable::Scrollbar {
match style {
Scrollable::Default => {
let palette = self.extended_palette();
scrollable::Scrollbar {
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
scroller: scrollable::Scroller {
color: palette.background.strong.color,
container: container::Appearance::default(),
scrollbar: scrollable::Scrollbar {
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
scroller: scrollable::Scroller {
color: palette.background.strong.color,
border: Border::with_radius(2),
},
},
gap: None,
}
}
Scrollable::Custom(custom) => custom.active(self),
@ -1224,19 +1214,24 @@ impl scrollable::StyleSheet for Theme {
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> scrollable::Scrollbar {
) -> scrollable::Appearance {
match style {
Scrollable::Default => {
if is_mouse_over_scrollbar {
let palette = self.extended_palette();
scrollable::Scrollbar {
background: Some(palette.background.weak.color.into()),
border: Border::with_radius(2),
scroller: scrollable::Scroller {
color: palette.primary.strong.color,
scrollable::Appearance {
scrollbar: scrollable::Scrollbar {
background: Some(
palette.background.weak.color.into(),
),
border: Border::with_radius(2),
scroller: scrollable::Scroller {
color: palette.primary.strong.color,
border: Border::with_radius(2),
},
},
..self.active(style)
}
} else {
self.active(style)
@ -1248,42 +1243,12 @@ impl scrollable::StyleSheet for Theme {
}
}
fn dragging(&self, style: &Self::Style) -> scrollable::Scrollbar {
fn dragging(&self, style: &Self::Style) -> scrollable::Appearance {
match style {
Scrollable::Default => self.hovered(style, true),
Scrollable::Custom(custom) => custom.dragging(self),
}
}
fn active_horizontal(&self, style: &Self::Style) -> scrollable::Scrollbar {
match style {
Scrollable::Default => self.active(style),
Scrollable::Custom(custom) => custom.active_horizontal(self),
}
}
fn hovered_horizontal(
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> scrollable::Scrollbar {
match style {
Scrollable::Default => self.hovered(style, is_mouse_over_scrollbar),
Scrollable::Custom(custom) => {
custom.hovered_horizontal(self, is_mouse_over_scrollbar)
}
}
}
fn dragging_horizontal(
&self,
style: &Self::Style,
) -> scrollable::Scrollbar {
match style {
Scrollable::Default => self.hovered_horizontal(style, true),
Scrollable::Custom(custom) => custom.dragging_horizontal(self),
}
}
}
/// The style of text.