Implement theme styling for Rule

This commit is contained in:
Héctor Ramón Jiménez 2022-06-01 01:56:46 +02:00
parent 6f69df3d41
commit c275fde67a
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
8 changed files with 86 additions and 123 deletions

View file

@ -1,6 +1,27 @@
//! Display a horizontal or vertical rule for dividing content.
use iced_core::Color;
/// The appearance of a rule.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The color of the rule.
pub color: Color,
/// The width (thickness) of the rule line.
pub width: u16,
/// The radius of the line corners.
pub radius: f32,
/// The [`FillMode`] of the rule.
pub fill_mode: FillMode,
}
/// A set of rules that dictate the style of a rule.
pub trait StyleSheet {
type Style: Default + Copy;
/// Produces the style of a rule.
fn style(&self, style: Self::Style) -> Appearance;
}
/// The fill mode of a rule.
#[derive(Debug, Clone, Copy)]
pub enum FillMode {
@ -64,56 +85,3 @@ impl FillMode {
}
}
}
/// The appearance of a rule.
#[derive(Debug, Clone, Copy)]
pub struct Style {
/// The color of the rule.
pub color: Color,
/// The width (thickness) of the rule line.
pub width: u16,
/// The radius of the line corners.
pub radius: f32,
/// The [`FillMode`] of the rule.
pub fill_mode: FillMode,
}
impl std::default::Default for Style {
fn default() -> Self {
Style {
color: [0.6, 0.6, 0.6, 0.6].into(),
width: 1,
radius: 0.0,
fill_mode: FillMode::Full,
}
}
}
/// A set of rules that dictate the style of a rule.
pub trait StyleSheet {
/// Produces the style of a rule.
fn style(&self) -> Style;
}
struct Default;
impl StyleSheet for Default {
fn style(&self) -> Style {
Style::default()
}
}
impl<'a> std::default::Default for Box<dyn StyleSheet + 'a> {
fn default() -> Self {
Box::new(Default)
}
}
impl<'a, T> From<T> for Box<dyn StyleSheet + 'a>
where
T: 'a + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
}
}

View file

@ -6,6 +6,7 @@ use crate::application;
use crate::button;
use crate::pane_grid;
use crate::radio;
use crate::rule;
use crate::slider;
use crate::toggler;
@ -279,3 +280,21 @@ impl pane_grid::StyleSheet for Theme {
})
}
}
/*
* Rule
*/
impl rule::StyleSheet for Theme {
type Style = ();
fn style(&self, _style: Self::Style) -> rule::Appearance {
let palette = self.extended_palette();
rule::Appearance {
color: palette.background.strong.color,
width: 1,
radius: 0.0,
fill_mode: rule::FillMode::Full,
}
}
}