Simplify theming for Scrollable widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-05 04:42:25 +01:00
parent 29326215cc
commit d681aaa57e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 342 additions and 422 deletions

View file

@ -1,51 +0,0 @@
//! Change the appearance of a container.
use crate::core::{Background, Border, Color, Pixels, Shadow};
/// The appearance of a container.
#[derive(Debug, Clone, Copy, Default)]
pub struct Appearance {
/// The text [`Color`] of the container.
pub text_color: Option<Color>,
/// The [`Background`] of the container.
pub background: Option<Background>,
/// The [`Border`] of the container.
pub border: Border,
/// The [`Shadow`] of the container.
pub shadow: Shadow,
}
impl Appearance {
/// Derives a new [`Appearance`] with a border of the given [`Color`] and
/// `width`.
pub fn with_border(
self,
color: impl Into<Color>,
width: impl Into<Pixels>,
) -> Self {
Self {
border: Border {
color: color.into(),
width: width.into().0,
..Border::default()
},
..self
}
}
/// Derives a new [`Appearance`] with the given [`Background`].
pub fn with_background(self, background: impl Into<Background>) -> Self {
Self {
background: Some(background.into()),
..self
}
}
}
/// A set of rules that dictate the [`Appearance`] of a container.
pub trait StyleSheet {
/// The supported style of the [`StyleSheet`].
type Style: Default;
/// Produces the [`Appearance`] of a container.
fn appearance(&self, style: &Self::Style) -> Appearance;
}

View file

@ -17,7 +17,6 @@
pub use iced_core as core;
pub mod application;
pub mod container;
pub mod menu;
pub mod pane_grid;
pub mod pick_list;
@ -25,7 +24,6 @@ pub mod progress_bar;
pub mod qr_code;
pub mod radio;
pub mod rule;
pub mod scrollable;
pub mod slider;
pub mod svg;
pub mod text_editor;

View file

@ -1,55 +0,0 @@
//! 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 {
/// The [`Background`] of a scrollbar.
pub background: Option<Background>,
/// The [`Border`] of a scrollbar.
pub border: Border,
/// The appearance of the [`Scroller`] of a scrollbar.
pub scroller: Scroller,
}
/// The appearance of the scroller of a scrollable.
#[derive(Debug, Clone, Copy)]
pub struct Scroller {
/// The [`Color`] of the scroller.
pub color: Color,
/// The [`Border`] of the scroller.
pub border: Border,
}
/// 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 [`Appearance`] of an active scrollable.
fn active(&self, style: &Self::Style) -> Appearance;
/// Produces the [`Appearance`] of a scrollable when it is being hovered.
fn hovered(
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> Appearance;
/// Produces the [`Appearance`] of a scrollable when it is being dragged.
fn dragging(&self, style: &Self::Style) -> Appearance {
self.hovered(style, true)
}
}

View file

@ -4,7 +4,6 @@ pub mod palette;
pub use palette::Palette;
use crate::application;
use crate::container;
use crate::core::widget::text;
use crate::menu;
use crate::pane_grid;
@ -13,7 +12,6 @@ use crate::progress_bar;
use crate::qr_code;
use crate::radio;
use crate::rule;
use crate::scrollable;
use crate::slider;
use crate::svg;
use crate::text_editor;
@ -793,91 +791,6 @@ impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
}
}
/// The style of a scrollable.
#[derive(Default)]
pub enum Scrollable {
/// The default style.
#[default]
Default,
/// A custom style.
Custom(Box<dyn scrollable::StyleSheet<Style = Theme>>),
}
impl Scrollable {
/// Creates a custom [`Scrollable`] theme.
pub fn custom<T: scrollable::StyleSheet<Style = Theme> + 'static>(
style: T,
) -> Self {
Self::Custom(Box::new(style))
}
}
impl scrollable::StyleSheet for Theme {
type Style = Scrollable;
fn active(&self, style: &Self::Style) -> scrollable::Appearance {
match style {
Scrollable::Default => {
let palette = self.extended_palette();
scrollable::Appearance {
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),
}
}
fn hovered(
&self,
style: &Self::Style,
is_mouse_over_scrollbar: bool,
) -> scrollable::Appearance {
match style {
Scrollable::Default => {
if is_mouse_over_scrollbar {
let palette = self.extended_palette();
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)
}
}
Scrollable::Custom(custom) => {
custom.hovered(self, is_mouse_over_scrollbar)
}
}
}
fn dragging(&self, style: &Self::Style) -> scrollable::Appearance {
match style {
Scrollable::Default => self.hovered(style, true),
Scrollable::Custom(custom) => custom.dragging(self),
}
}
}
impl text::StyleSheet for Theme {}
/// The style of a text input.