Added theme management, custom theme colors
Moorly CI / windows-build (push) Successful in 3m4s
Moorly CI / linux-build (push) Successful in 3m36s

This commit is contained in:
Squid committed 2026-11-06 21:58:06 +01:00
1 parent da752bcfd7
commit 188e4f64d4
77 files changed
+40005 -104

No files matched your search

+475
View File
@@ -0,0 +1,475 @@
//! Use a badge for color highlighting important information.
//!
//! *This API requires the following crate features to be activated: badge*
use super::{Status, StyleFn, colors};
use iced_core::{Background, Color, Theme, theme::palette};
/// The style of a [`Badge`](crate::widget::badge::Badge).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`Badge`](crate::widget::badge::Badge).
pub background: Background,
/// The border radius of the [`Badge`](crate::widget::badge::Badge).
/// If no radius is specified the default one will be used.
pub border_radius: Option<f32>,
/// The border with of the [`Badge`](crate::widget::badge::Badge).
pub border_width: f32,
/// The border color of the [`Badge`](crate::widget::badge::Badge).
pub border_color: Option<Color>,
/// The default text color of the [`Badge`](crate::widget::badge::Badge).
pub text_color: Color,
}
/// The Catalog of a [`Badge`](crate::widget::badge::Badge).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl std::default::Default for Style {
fn default() -> Self {
Self {
background: Background::Color([0.87, 0.87, 0.87].into()),
border_radius: None,
border_width: 1.0,
border_color: Some([0.8, 0.8, 0.8].into()),
text_color: Color::BLACK,
}
}
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = styled(palette.primary.strong);
match status {
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}
/// The secondary theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn secondary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = styled(palette.secondary.strong);
match status {
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}
/// The success theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn success(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = styled(palette.success.strong);
match status {
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}
/// The danger theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn danger(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = styled(palette.danger.strong);
match status {
Status::Hovered => Style {
background: Background::Color(palette.primary.base.color),
..base
},
Status::Disabled => disabled(base),
_ => base,
}
}
/// The warning theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn warning(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::WARNING, colors::BLACK);
match status {
Status::Disabled => disabled(base),
_ => base,
}
}
/// The info theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn info(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::INFO, colors::BLACK);
match status {
Status::Disabled => disabled(base),
_ => base,
}
}
/// The light theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn light(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::LIGHT, colors::BLACK);
match status {
Status::Disabled => disabled(base),
_ => base,
}
}
/// The dark theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn dark(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::DARK, colors::WHITE);
match status {
Status::Disabled => disabled(base),
_ => base,
}
}
/// The white theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn white(_theme: &Theme, status: Status) -> Style {
let base = from_color(colors::WHITE, colors::BLACK);
match status {
Status::Disabled => disabled(base),
_ => base,
}
}
fn from_color(color: Color, text_color: Color) -> Style {
Style {
background: Background::Color(color),
border_color: Some(color),
text_color,
..Style::default()
}
}
fn styled(pair: palette::Pair) -> Style {
Style {
background: Background::Color(pair.color),
border_color: Some(pair.color),
text_color: pair.text,
..Style::default()
}
}
fn disabled(style: Style) -> Style {
Style {
background: style.background.scale_alpha(0.5),
text_color: style.text_color.scale_alpha(0.5),
..style
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Color, Theme};
#[test]
fn style_default() {
let style = Style::default();
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_radius.is_none());
assert_eq!(style.border_width, 1.0);
assert!(style.border_color.is_some());
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn from_color_creates_style() {
let color = Color::from_rgb(1.0, 0.0, 0.0);
let text = Color::WHITE;
let style = from_color(color, text);
assert_eq!(style.background, Background::Color(color));
assert_eq!(style.border_color, Some(color));
assert_eq!(style.text_color, text);
assert_eq!(style.border_width, 1.0);
assert!(style.border_radius.is_none());
}
#[test]
fn disabled_scales_alpha() {
let base = Style {
background: Background::Color(Color::from_rgb(1.0, 0.0, 0.0)),
border_radius: Some(5.0),
border_width: 2.0,
border_color: Some(Color::from_rgb(0.5, 0.5, 0.5)),
text_color: Color::WHITE,
};
let disabled_style = disabled(base);
// Background and text color should have scaled alpha
assert!(matches!(disabled_style.background, Background::Color(_)));
assert_eq!(disabled_style.border_radius, Some(5.0));
assert_eq!(disabled_style.border_width, 2.0);
assert_eq!(
disabled_style.border_color,
Some(Color::from_rgb(0.5, 0.5, 0.5))
);
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn secondary_theme_active() {
let theme = Theme::TokyoNight;
let style = secondary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn secondary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = secondary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn secondary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = secondary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn success_theme_active() {
let theme = Theme::TokyoNight;
let style = success(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn success_theme_hovered() {
let theme = Theme::TokyoNight;
let style = success(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn success_theme_disabled() {
let theme = Theme::TokyoNight;
let style = success(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn danger_theme_active() {
let theme = Theme::TokyoNight;
let style = danger(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn danger_theme_hovered() {
let theme = Theme::TokyoNight;
let style = danger(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn danger_theme_disabled() {
let theme = Theme::TokyoNight;
let style = danger(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn warning_theme_active() {
let theme = Theme::TokyoNight;
let style = warning(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
assert_eq!(style.text_color, colors::BLACK);
}
#[test]
fn warning_theme_disabled() {
let theme = Theme::TokyoNight;
let style = warning(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn info_theme_active() {
let theme = Theme::TokyoNight;
let style = info(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
assert_eq!(style.text_color, colors::BLACK);
}
#[test]
fn info_theme_disabled() {
let theme = Theme::TokyoNight;
let style = info(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn light_theme_active() {
let theme = Theme::TokyoNight;
let style = light(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
assert_eq!(style.text_color, colors::BLACK);
}
#[test]
fn light_theme_disabled() {
let theme = Theme::TokyoNight;
let style = light(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn dark_theme_active() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
assert_eq!(style.text_color, colors::WHITE);
}
#[test]
fn dark_theme_disabled() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn white_theme_active() {
let theme = Theme::TokyoNight;
let style = white(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
assert_eq!(style.text_color, colors::BLACK);
}
#[test]
fn white_theme_disabled() {
let theme = Theme::TokyoNight;
let style = white(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(style.border_color.is_some());
}
}
+348
View File
@@ -0,0 +1,348 @@
//! Displays a [`Card`](crate::widget::Card).
//!
//! *This API requires the following crate features to be activated: card*
use super::{Status, StyleFn, colors};
use iced_core::{Background, Color, Theme};
/// The appearance of a [`Card`](crate::widget::card::Card).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`Card`](crate::widget::card::Card).
pub background: Background,
/// The border radius of the [`Card`](crate::widget::card::Card).
pub border_radius: f32,
/// The border width of the [`Card`](crate::widget::card::Card).
pub border_width: f32,
/// The border color of the [`Card`](crate::widget::card::Card).
pub border_color: Color,
/// The background of the head of the [`Card`](crate::widget::card::Card).
pub head_background: Background,
/// The text color of the head of the [`Card`](crate::widget::card::Card).
pub head_text_color: Color,
/// The background of the body of the [`Card`](crate::widget::card::Card).
pub body_background: Background,
/// The text color of the body of the [`Card`](crate::widget::card::Card).
pub body_text_color: Color,
/// The background of the foot of the [`Card`](crate::widget::card::Card).
pub foot_background: Background,
/// The text color of the foot of the [`Card`](crate::widget::card::Card).
pub foot_text_color: Color,
/// The color of the close icon of the [`Card`](crate::widget::card::Card).
pub close_color: Color,
}
/// The appearance of a [`Card`](crate::widget::card::Card).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Default for Style {
fn default() -> Self {
Self {
background: Color::WHITE.into(),
border_radius: 10.0,
border_width: 1.0,
border_color: [0.87, 0.87, 0.87].into(),
head_background: Background::Color([0.87, 0.87, 0.87].into()),
head_text_color: Color::BLACK,
body_background: Color::TRANSPARENT.into(),
body_text_color: Color::BLACK,
foot_background: Color::TRANSPARENT.into(),
foot_text_color: Color::BLACK,
close_color: Color::BLACK,
}
}
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn primary(theme: &Theme, _status: Status) -> Style {
backing_with_text(theme, colors::PRIMARY, colors::WHITE)
}
/// The secondary theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn secondary(theme: &Theme, _status: Status) -> Style {
backing_with_text(theme, colors::SECONDARY, colors::WHITE)
}
/// The success theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn success(theme: &Theme, _status: Status) -> Style {
backing_with_text(theme, colors::SUCCESS, colors::WHITE)
}
/// The danger theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn danger(theme: &Theme, _status: Status) -> Style {
backing_with_text(theme, colors::DANGER, colors::WHITE)
}
/// The warning theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn warning(theme: &Theme, _status: Status) -> Style {
backing_only(theme, colors::WARNING)
}
/// The info theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn info(theme: &Theme, _status: Status) -> Style {
backing_only(theme, colors::INFO)
}
/// The light theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn light(theme: &Theme, _status: Status) -> Style {
backing_only(theme, colors::LIGHT)
}
/// The dark theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn dark(theme: &Theme, _status: Status) -> Style {
backing_with_text(theme, colors::DARK, colors::WHITE)
}
/// The white theme of a [`Card`](crate::widget::card::Card).
#[must_use]
pub fn white(theme: &Theme, _status: Status) -> Style {
backing_only(theme, colors::WHITE)
}
fn backing_with_text(theme: &Theme, color: Color, text_color: Color) -> Style {
let palette = theme.extended_palette();
let foreground = theme.palette();
Style {
border_color: color,
head_background: color.into(),
head_text_color: text_color,
close_color: text_color,
background: palette.background.base.color.into(),
body_text_color: foreground.text,
foot_text_color: foreground.text,
..Style::default()
}
}
fn backing_only(theme: &Theme, color: Color) -> Style {
let palette = theme.extended_palette();
let foreground = theme.palette();
Style {
border_color: color,
head_background: color.into(),
background: palette.background.base.color.into(),
body_text_color: foreground.text,
foot_text_color: foreground.text,
..Style::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Color, Theme};
#[test]
fn style_default() {
let style = Style::default();
assert_eq!(style.background, Background::Color(Color::WHITE));
assert_eq!(style.border_radius, 10.0);
assert_eq!(style.border_width, 1.0);
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, Color::BLACK);
assert_eq!(style.body_background, Background::Color(Color::TRANSPARENT));
assert_eq!(style.body_text_color, Color::BLACK);
assert_eq!(style.foot_background, Background::Color(Color::TRANSPARENT));
assert_eq!(style.foot_text_color, Color::BLACK);
assert_eq!(style.close_color, Color::BLACK);
}
#[test]
fn backing_with_text_creates_style() {
let theme = Theme::TokyoNight;
let color = Color::from_rgb(1.0, 0.0, 0.0);
let text = Color::WHITE;
let style = backing_with_text(&theme, color, text);
assert_eq!(style.border_color, color);
assert_eq!(style.head_background, Background::Color(color));
assert_eq!(style.head_text_color, text);
assert_eq!(style.close_color, text);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.body_background, Background::Color(_)));
assert!(matches!(style.foot_background, Background::Color(_)));
}
#[test]
fn backing_only_creates_style() {
let theme = Theme::TokyoNight;
let color = Color::from_rgb(0.0, 1.0, 0.0);
let style = backing_only(&theme, color);
assert_eq!(style.border_color, color);
assert_eq!(style.head_background, Background::Color(color));
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.body_background, Background::Color(_)));
assert!(matches!(style.foot_background, Background::Color(_)));
}
#[test]
fn primary_theme() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, colors::WHITE);
assert_eq!(style.close_color, colors::WHITE);
}
#[test]
fn secondary_theme() {
let theme = Theme::TokyoNight;
let style = secondary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, colors::WHITE);
assert_eq!(style.close_color, colors::WHITE);
}
#[test]
fn success_theme() {
let theme = Theme::TokyoNight;
let style = success(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, colors::WHITE);
assert_eq!(style.close_color, colors::WHITE);
}
#[test]
fn danger_theme() {
let theme = Theme::TokyoNight;
let style = danger(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, colors::WHITE);
assert_eq!(style.close_color, colors::WHITE);
}
#[test]
fn warning_theme() {
let theme = Theme::TokyoNight;
let style = warning(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.border_color, colors::WARNING);
}
#[test]
fn info_theme() {
let theme = Theme::TokyoNight;
let style = info(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.border_color, colors::INFO);
}
#[test]
fn light_theme() {
let theme = Theme::TokyoNight;
let style = light(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.border_color, colors::LIGHT);
}
#[test]
fn dark_theme() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.head_text_color, colors::WHITE);
assert_eq!(style.close_color, colors::WHITE);
assert_eq!(style.border_color, colors::DARK);
}
#[test]
fn white_theme() {
let theme = Theme::TokyoNight;
let style = white(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
assert_eq!(style.border_color, colors::WHITE);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
}
#[test]
fn primary_theme_with_hovered_status() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
}
#[test]
fn primary_theme_with_disabled_status() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.head_background, Background::Color(_)));
}
}
+192
View File
@@ -0,0 +1,192 @@
//! Use a color picker as an input element for picking colors.
//!
//! *This API requires the following crate features to be activated: `color_picker`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
/// The appearance of a [`ColorPicker`](crate::widget::ColorPicker).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`ColorPicker`](crate::widget::ColorPicker).
pub background: Background,
/// The border radius of the [`ColorPicker`](crate::widget::ColorPicker).
pub border_radius: f32,
/// The border with of the [`ColorPicker`](crate::widget::ColorPicker).
pub border_width: f32,
/// The border color of the [`ColorPicker`](crate::widget::ColorPicker).
pub border_color: Color,
/// The border radius of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
pub bar_border_radius: f32,
/// The border width of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
pub bar_border_width: f32,
/// The border color of the bars of the [`ColorPicker`](crate::widget::ColorPicker).
pub bar_border_color: Color,
}
/// The Catalog of a [`ColorPicker`](crate::widget::ColorPicker).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let foreground = theme.palette();
let base = Style {
background: palette.background.base.color.into(),
border_radius: 15.0,
border_width: 1.0,
border_color: foreground.text,
bar_border_radius: 5.0,
bar_border_width: 1.0,
bar_border_color: foreground.text,
};
match status {
Status::Focused => Style {
border_color: palette.background.strong.color,
bar_border_color: palette.background.strong.color,
..base
},
_ => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
assert_eq!(style.bar_border_radius, 5.0);
assert_eq!(style.bar_border_width, 1.0);
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
assert_eq!(style.bar_border_radius, 5.0);
assert_eq!(style.bar_border_width, 1.0);
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn focused_changes_border_colors() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let focused_style = primary(&theme, Status::Focused);
// Border colors should be different when focused
assert_ne!(base_style.border_color, focused_style.border_color);
assert_ne!(base_style.bar_border_color, focused_style.bar_border_color);
// Other properties should remain the same
assert_eq!(base_style.background, focused_style.background);
assert_eq!(base_style.border_radius, focused_style.border_radius);
assert_eq!(base_style.border_width, focused_style.border_width);
assert_eq!(
base_style.bar_border_radius,
focused_style.bar_border_radius
);
assert_eq!(base_style.bar_border_width, focused_style.bar_border_width);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
assert_eq!(style.bar_border_radius, 5.0);
assert_eq!(style.bar_border_width, 1.0);
}
#[test]
fn catalog_style_focused() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
}
#[test]
fn style_fields_are_valid() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Verify all fields have valid values
assert!(style.border_radius > 0.0);
assert!(style.border_width > 0.0);
assert!(style.bar_border_radius > 0.0);
assert!(style.bar_border_width > 0.0);
}
}
+662
View File
@@ -0,0 +1,662 @@
//! Predefined color palette based on the CSS color palette
//!
//!
//! Thanks to:
//! * [W3 Schools](https://www.w3schools.com/cssref/css_colors.asp)
//! * [Corecoding](https://corecoding.com/utilities/rgb-or-hex-to-float.php)
use iced_core::Color;
/// Primary <span style="color:dodgerblue">Color</span>.
pub const PRIMARY: Color = DODGER_BLUE;
/// Secondary <span style="color:dimgray">Color</span>.
pub const SECONDARY: Color = DIM_GRAY;
/// Success <span style="color:limeGreen">Color</span>.
pub const SUCCESS: Color = LIME_GREEN;
/// Danger <span style="color:red">Color</span>.
pub const DANGER: Color = RED;
/// Warning <span style="color:gold">Color</span>.
pub const WARNING: Color = GOLD;
/// Info <span style="color:skyBlue">Color</span>.
pub const INFO: Color = SKY_BLUE;
/// Light <span style="color:ghostWhite">Color</span>.
pub const LIGHT: Color = GHOST_WHITE;
/// Dark <span style="color:rgb(0.204, 0.227, 0.251)">Color</span>.
pub const DARK: Color = Color::from_rgb(0.204, 0.227, 0.251);
/// Alice Blue <span style="color:aliceBlue">Color</span>.
pub const ALICE_BLUE: Color = Color::from_rgb(0.941, 0.973, 1.0);
/// Antique White <span style="color:antiqueWhite">Color</span>.
pub const ANTIQUE_WHITE: Color = Color::from_rgb(0.98, 0.922, 0.843);
/// Aqua <span style="color:aqua">Color</span>.
pub const AQUA: Color = Color::from_rgb(0.0, 1.0, 1.0);
/// Aquamarine <span style="color:aquamarine">Color</span>.
pub const AQUAMARINE: Color = Color::from_rgb(0.498, 1.0, 0.831);
/// Azure <span style="color:azure">Color</span>.
pub const AZURE: Color = Color::from_rgb(0.941, 1.0, 1.0);
/// Beige <span style="color:beige">Color</span>.
pub const BEIGE: Color = Color::from_rgb(0.961, 0.961, 0.863);
/// Bisque <span style="color:bisque">Color</span>.
pub const BISQUE: Color = Color::from_rgb(1.0, 0.894, 0.769);
/// Black <span style="color:black">Color</span>.
pub const BLACK: Color = Color::BLACK;
/// Blanched Almond <span style="color:blanchedAlmond">Color</span>.
pub const BLANCHED_ALMOND: Color = Color::from_rgb(1.0, 0.922, 0.804);
/// Blue <span style="color:blue">Color</span>.
pub const BLUE: Color = Color::from_rgb(0.0, 0.0, 1.0);
/// Blue Violet <span style="color:blueViolet">Color</span>.
pub const BLUE_VIOLET: Color = Color::from_rgb(0.541, 0.169, 0.886);
/// Brown <span style="color:brown">Color</span>.
pub const BROWN: Color = Color::from_rgb(0.647, 0.165, 0.165);
/// Burly Wood <span style="color:burlyWood">Color</span>.
pub const BURLY_WOOD: Color = Color::from_rgb(0.871, 0.722, 0.529);
/// Cadet Blue <span style="color:cadetBlue">Color</span>.
pub const CADET_BLUE: Color = Color::from_rgb(0.373, 0.62, 0.627);
/// Chartreuse <span style="color:chartreuse">Color</span>.
pub const CHARTREUSE: Color = Color::from_rgb(0.498, 1.0, 0.0);
/// Chocolate <span style="color:chocolate">Color</span>.
pub const CHOCOLATE: Color = Color::from_rgb(0.824, 0.412, 0.118);
/// Coral <span style="color:coral">Color</span>.
pub const CORAL: Color = Color::from_rgb(1.0, 0.498, 0.314);
/// Cornflower Blue <span style="color:cornflowerBlue">Color</span>.
pub const CORNFLOWER_BLUE: Color = Color::from_rgb(0.392, 0.584, 0.929);
/// Cornsilk <span style="color:cornsilk">Color</span>.
pub const CORNSILK: Color = Color::from_rgb(1.0, 0.973, 0.863);
/// Crimson <span style="color:crimson">Color</span>.
pub const CRIMSON: Color = Color::from_rgb(0.863, 0.078, 0.235);
/// Cyan <span style="color:cyan">Color</span>.
pub const CYAN: Color = Color::from_rgb(0.0, 1.0, 1.0);
/// Dark Blue <span style="color:darkBlue">Color</span>.
pub const DARK_BLUE: Color = Color::from_rgb(0.0, 0.0, 0.545);
/// Dark Cyan <span style="color:darkCyan">Color</span>.
pub const DARK_CYAN: Color = Color::from_rgb(0.0, 0.545, 0.545);
/// Dark Golden Rod <span style="color:darkGoldenRod">Color</span>.
pub const DARK_GOLDEN_ROD: Color = Color::from_rgb(0.722, 0.525, 0.043);
/// Dark Gray <span style="color:darkGray">Color</span>.
pub const DARK_GRAY: Color = Color::from_rgb(0.663, 0.663, 0.663);
/// Dark Grey <span style="color:darkGrey">Color</span>.
pub const DARK_GREY: Color = DARK_GRAY;
/// Dark Green <span style="color:darkGreen">Color</span>.
pub const DARK_GREEN: Color = Color::from_rgb(0.0, 0.392, 0.0);
/// Dark Khaki <span style="color:darkKhaki">Color</span>.
pub const DARK_KHAKI: Color = Color::from_rgb(0.741, 0.718, 0.42);
/// Dark Magenta <span style="color:darkMagenta">Color</span>.
pub const DARK_MAGENTA: Color = Color::from_rgb(0.545, 0.0, 0.545);
/// Dark Olive Green <span style="color:darkOliveGreen">Color</span>.
pub const DARK_OLIVE_GREEN: Color = Color::from_rgb(0.333, 0.42, 0.184);
/// Dark Orange <span style="color:darkOrange">Color</span>.
pub const DARK_ORANGE: Color = Color::from_rgb(1.0, 0.549, 0.0);
/// Dark Orchid <span style="color:darkOrchid">Color</span>.
pub const DARK_ORCHID: Color = Color::from_rgb(0.6, 0.196, 0.8);
/// Dark Red <span style="color:darkRed">Color</span>.
pub const DARK_RED: Color = Color::from_rgb(0.545, 0.0, 0.0);
/// Dark Salmon <span style="color:darkSalmon">Color</span>.
pub const DARK_SALMON: Color = Color::from_rgb(0.914, 0.588, 0.478);
/// Dark Sea Green <span style="color:darkSeaGreen">Color</span>.
pub const DARK_SEA_GREEN: Color = Color::from_rgb(0.561, 0.737, 0.561);
/// Dark Slate Blue <span style="color:darkSlateBlue">Color</span>.
pub const DARK_SLATE_BLUE: Color = Color::from_rgb(0.282, 0.239, 0.545);
/// Dark Slate Gray <span style="color:darkSlateGray">Color</span>.
pub const DARK_SLATE_GRAY: Color = Color::from_rgb(0.184, 0.31, 0.31);
/// Dark Slate Grey <span style="color:darkSlateGrey">Color</span>.
pub const DARK_SLATE_GREY: Color = DARK_SLATE_GRAY;
/// Dark Turquoise <span style="color:darkTurquoise">Color</span>.
pub const DARK_TURQUOISE: Color = Color::from_rgb(0.0, 0.808, 0.82);
/// Dark Violet <span style="color:darkViolet">Color</span>.
pub const DARK_VIOLET: Color = Color::from_rgb(0.58, 0.0, 0.827);
/// Deep Pink <span style="color:deepPink">Color</span>.
pub const DEEP_PINK: Color = Color::from_rgb(1.0, 0.078, 0.576);
/// Deep Sky Blue <span style="color:deepSkyBlue">Color</span>.
pub const DEEP_SKY_BLUE: Color = Color::from_rgb(0.0, 0.749, 1.0);
/// Dim Gray <span style="color:dimgray">Color</span>.
pub const DIM_GRAY: Color = Color::from_rgb(0.412, 0.412, 0.412);
/// Dim Grey <span style="color:dimgrey">Color</span>.
pub const DIM_GREY: Color = DIM_GRAY;
/// Dodger Blue <span style="color:dodgerBlue">Color</span>.
pub const DODGER_BLUE: Color = Color::from_rgb(0.118, 0.565, 1.0);
/// Fire Brick <span style="color:fireBrick">Color</span>.
pub const FIRE_BRICK: Color = Color::from_rgb(0.698, 0.133, 0.133);
/// Floral White <span style="color:floralWhite">Color</span>.
pub const FLORAL_WHITE: Color = Color::from_rgb(1.0, 0.98, 0.941);
/// Forest Green <span style="color:forestGreen">Color</span>.
pub const FOREST_GREEN: Color = Color::from_rgb(0.133, 0.545, 0.133);
/// Fuchsia <span style="color:fuchsia">Color</span>.
pub const FUCHSIA: Color = Color::from_rgb(1.0, 0.0, 1.0);
/// Gainsboro <span style="color:gainsboro">Color</span>.
pub const GAINSBORO: Color = Color::from_rgb(0.863, 0.863, 0.863);
/// Ghost White <span style="color:ghostWhite">Color</span>.
pub const GHOST_WHITE: Color = Color::from_rgb(0.973, 0.973, 1.0);
/// Gold <span style="color:gold">Color</span>.
pub const GOLD: Color = Color::from_rgb(1.0, 0.843, 0.0);
/// Golden Rod <span style="color:goldenRod">Color</span>.
pub const GOLDEN_ROD: Color = Color::from_rgb(0.855, 0.647, 0.125);
/// Gray <span style="color:gray">Color</span>.
pub const GRAY: Color = Color::from_rgb(0.502, 0.502, 0.502);
/// Grey <span style="color:grey">Color</span>.
pub const GREY: Color = GRAY;
/// Green <span style="color:green">Color</span>.
pub const GREEN: Color = Color::from_rgb(0.0, 0.502, 0.0);
/// Green Yellow <span style="color:greenYellow">Color</span>.
pub const GREEN_YELLOW: Color = Color::from_rgb(0.678, 1.0, 0.184);
/// Honey Dew <span style="color:honeyDew">Color</span>.
pub const HONEY_DEW: Color = Color::from_rgb(0.941, 1.0, 0.941);
/// Hot Pink <span style="color:hotPink">Color</span>.
pub const HOT_PINK: Color = Color::from_rgb(1.0, 0.412, 0.706);
/// Indian Red <span style="color:indianRed">Color</span>.
pub const INDIAN_RED: Color = Color::from_rgb(0.804, 0.361, 0.361);
/// Indigo <span style="color:indigo">Color</span>.
pub const INDIGO: Color = Color::from_rgb(0.294, 0.0, 0.51);
/// Ivory <span style="color:ivory">Color</span>.
pub const IVORY: Color = Color::from_rgb(1.0, 1.0, 0.941);
/// Khaki <span style="color:khaki">Color</span>.
pub const KHAKI: Color = Color::from_rgb(0.941, 0.902, 0.549);
/// Lavender <span style="color:lavender">Color</span>.
pub const LAVENDER: Color = Color::from_rgb(0.902, 0.902, 0.98);
/// Lavender Blush <span style="color:lavenderBlush">Color</span>.
pub const LAVENDER_BLUSH: Color = Color::from_rgb(1.0, 0.941, 0.961);
/// Lawn Green <span style="color:lawnGreen">Color</span>.
pub const LAWN_GREEN: Color = Color::from_rgb(0.486, 0.988, 0.0);
/// Lemon Chiffon <span style="color:lemonChiffon">Color</span>.
pub const LEMON_CHIFFON: Color = Color::from_rgb(1.0, 0.98, 0.804);
/// Light Blue <span style="color:lightBlue">Color</span>.
pub const LIGHT_BLUE: Color = Color::from_rgb(0.678, 0.847, 0.902);
/// Light Coral <span style="color:lightCoral">Color</span>.
pub const LIGHT_CORAL: Color = Color::from_rgb(0.941, 0.502, 0.502);
/// Light Cyan <span style="color:lightCyan">Color</span>.
pub const LIGHT_CYAN: Color = Color::from_rgb(0.878, 1.0, 1.0);
/// Light Golden Rod Yellow <span style="color:lightGoldenRodYellow">Color</span>.
pub const LIGHT_GOLDEN_ROD_YELLOW: Color = Color::from_rgb(0.98, 0.98, 0.824);
/// Light Gray <span style="color:lightGray">Color</span>.
pub const LIGHT_GRAY: Color = Color::from_rgb(0.827, 0.827, 0.827);
/// Light Grey <span style="color:lightGrey">Color</span>.
pub const LIGHT_GREY: Color = LIGHT_GRAY;
/// Light Green <span style="color:lightGreen">Color</span>.
pub const LIGHT_GREEN: Color = Color::from_rgb(0.565, 0.933, 0.565);
/// Light Pink <span style="color:lightPink">Color</span>.
pub const LIGHT_PINK: Color = Color::from_rgb(1.0, 0.714, 0.757);
/// Light Salmon <span style="color:lightSalmon">Color</span>.
pub const LIGHT_SALMON: Color = Color::from_rgb(1.0, 0.627, 0.478);
/// Light Sea Green <span style="color:lightSeaGreen">Color</span>.
pub const LIGHT_SEA_GREEN: Color = Color::from_rgb(0.125, 0.698, 0.667);
/// Light Sky Blue <span style="color:lightSkyBlue">Color</span>.
pub const LIGHT_SKY_BLUE: Color = Color::from_rgb(0.529, 0.808, 0.98);
/// Light Slate Gray <span style="color:lightSlateGray">Color</span>.
pub const LIGHT_SLATE_GRAY: Color = Color::from_rgb(0.467, 0.533, 0.6);
/// Light Slate Grey <span style="color:lightSlateGrey">Color</span>.
pub const LIGHT_SLATE_GREY: Color = LIGHT_SLATE_GRAY;
/// Light Steel Blue <span style="color:lightSteelBlue">Color</span>.
pub const LIGHT_STEEL_BLUE: Color = Color::from_rgb(0.69, 0.769, 0.871);
/// Light Yellow <span style="color:lightYellow">Color</span>.
pub const LIGHT_YELLOW: Color = Color::from_rgb(1.0, 1.0, 0.878);
/// Lime <span style="color:lime">Color</span>.
pub const LIME: Color = Color::from_rgb(0.0, 1.0, 0.0);
/// Lime Green <span style="color:limeGreen">Color</span>.
pub const LIME_GREEN: Color = Color::from_rgb(0.196, 0.804, 0.196);
/// Linen <span style="color:linen">Color</span>.
pub const LINEN: Color = Color::from_rgb(0.98, 0.941, 0.902);
/// Magenta <span style="color:magenta">Color</span>.
pub const MAGENTA: Color = Color::from_rgb(1.0, 0.0, 1.0);
/// Maroon <span style="color:maroon">Color</span>.
pub const MAROON: Color = Color::from_rgb(0.502, 0.0, 0.0);
/// Medium Aqua Marine <span style="color:mediumAquaMarine">Color</span>.
pub const MEDIUM_AQUA_MARINE: Color = Color::from_rgb(0.4, 0.804, 0.667);
/// Medium Blue <span style="color:mediumBlue">Color</span>.
pub const MEDIUM_BLUE: Color = Color::from_rgb(0.0, 0.0, 0.804);
/// Medium Orchid <span style="color:mediumOrchid">Color</span>.
pub const MEDIUM_ORCHID: Color = Color::from_rgb(0.729, 0.333, 0.827);
/// Medium Purple <span style="color:mediumPurple">Color</span>.
pub const MEDIUM_PURPLE: Color = Color::from_rgb(0.576, 0.439, 0.859);
/// Medium Sea Green <span style="color:mediumSeaGreen">Color</span>.
pub const MEDIUM_SEA_GREEN: Color = Color::from_rgb(0.235, 0.702, 0.443);
/// Medium Slate Blue <span style="color:mediumSlateBlue">Color</span>.
pub const MEDIUM_SLATE_BLUE: Color = Color::from_rgb(0.482, 0.408, 0.933);
/// Medium Spring Green <span style="color:mediumSpringGreen">Color</span>.
pub const MEDIUM_SPRING_GREEN: Color = Color::from_rgb(0.0, 0.98, 0.604);
/// Medium Turquoise <span style="color:mediumTurquoise">Color</span>.
pub const MEDIUM_TURQUOISE: Color = Color::from_rgb(0.282, 0.82, 0.8);
/// Medium Violet Red <span style="color:mediumVioletRed">Color</span>.
pub const MEDIUM_VIOLET_RED: Color = Color::from_rgb(0.78, 0.082, 0.522);
/// Midnight Blue <span style="color:midnightBlue">Color</span>.
pub const MIDNIGHT_BLUE: Color = Color::from_rgb(0.098, 0.098, 0.439);
/// Mint Cream <span style="color:mintCream">Color</span>.
pub const MINT_CREAM: Color = Color::from_rgb(0.961, 1.0, 0.98);
/// Misty Rose <span style="color:mistyRose">Color</span>.
pub const MISTY_ROSE: Color = Color::from_rgb(1.0, 0.894, 0.882);
/// Moccasin <span style="color:moccasin">Color</span>.
pub const MOCCASIN: Color = Color::from_rgb(1.0, 0.894, 0.71);
/// Navajo White <span style="color:navajo_white">Color</span>.
pub const NAVAJO_WHITE: Color = Color::from_rgb(1.0, 0.871, 0.678);
/// Navy <span style="color:navy">Color</span>.
pub const NAVY: Color = Color::from_rgb(0.0, 0.0, 0.502);
/// Old Lace <span style="color:oldLace">Color</span>.
pub const OLD_LACE: Color = Color::from_rgb(0.992, 0.961, 0.902);
/// Olive <span style="color:olive">Color</span>.
pub const OLIVE: Color = Color::from_rgb(0.502, 0.502, 0.0);
/// Olive Drab <span style="color:oliveDrab">Color</span>.
pub const OLIVE_DRAB: Color = Color::from_rgb(0.42, 0.557, 0.137);
/// Orange <span style="color:orange">Color</span>.
pub const ORANGE: Color = Color::from_rgb(1.0, 0.647, 0.0);
/// Orange Red <span style="color:orangeRed">Color</span>.
pub const ORANGE_RED: Color = Color::from_rgb(1.0, 0.271, 0.0);
/// Orchid <span style="color:orchid">Color</span>.
pub const ORCHID: Color = Color::from_rgb(0.855, 0.439, 0.839);
/// Pale Golden Rod <span style="color:paleGoldenRod">Color</span>.
pub const PALE_GOLDEN_ROD: Color = Color::from_rgb(0.933, 0.91, 0.667);
/// Pale Green <span style="color:paleGreen">Color</span>.
pub const PALE_GREEN: Color = Color::from_rgb(0.596, 0.984, 0.596);
/// Pale Turquoise <span style="color:paleTurquoise">Color</span>.
pub const PALE_TURQUOISE: Color = Color::from_rgb(0.686, 0.933, 0.933);
/// Pale Violet Red <span style="color:paleVioletRed">Color</span>.
pub const PALE_VIOLET_RED: Color = Color::from_rgb(0.859, 0.439, 0.576);
/// Papaya Whip <span style="color:papayaWhip">Color</span>.
pub const PAPAYA_WHIP: Color = Color::from_rgb(1.0, 0.937, 0.835);
/// Peach Puff <span style="color:peachPuff">Color</span>.
pub const PEACH_PUFF: Color = Color::from_rgb(1.0, 0.855, 0.725);
/// Peru <span style="color:peru">Color</span>.
pub const PERU: Color = Color::from_rgb(0.804, 0.522, 0.247);
/// Pink <span style="color:pink">Color</span>.
pub const PINK: Color = Color::from_rgb(1.0, 0.753, 0.796);
/// Plum <span style="color:plum">Color</span>.
pub const PLUM: Color = Color::from_rgb(0.867, 0.627, 0.867);
/// Powder Blue <span style="color:powderBlue">Color</span>.
pub const POWDER_BLUE: Color = Color::from_rgb(0.69, 0.878, 0.902);
/// Purple <span style="color:purple">Color</span>.
pub const PURPLE: Color = Color::from_rgb(0.502, 0.0, 0.502);
/// Rebecca Purple <span style="color:rebeccaPurple">Color</span>.
pub const REBECCA_PURPLE: Color = Color::from_rgb(0.4, 0.2, 0.6);
/// Red <span style="color:red">Color</span>.
pub const RED: Color = Color::from_rgb(1.0, 0.0, 0.0);
/// Rosy Brown <span style="color:rosyBrown">Color</span>.
pub const ROSY_BROWN: Color = Color::from_rgb(0.737, 0.561, 0.561);
/// Royal Blue <span style="color:royalBlue">Color</span>.
pub const ROYAL_BLUE: Color = Color::from_rgb(0.255, 0.412, 0.882);
/// Saddle Brown <span style="color:saddleBrown">Color</span>.
pub const SADDLE_BROWN: Color = Color::from_rgb(0.545, 0.271, 0.075);
/// Salmon <span style="color:salmon">Color</span>.
pub const SALMON: Color = Color::from_rgb(0.98, 0.502, 0.447);
/// Sandy Brown <span style="color:sandyBrown">Color</span>.
pub const SANDY_BROWN: Color = Color::from_rgb(0.957, 0.643, 0.376);
/// Sea Green <span style="color:seaGreen">Color</span>.
pub const SEA_GREEN: Color = Color::from_rgb(0.18, 0.545, 0.341);
/// Sea Shell <span style="color:seaShell">Color</span>.
pub const SEA_SHELL: Color = Color::from_rgb(1.0, 0.961, 0.933);
/// Sienna <span style="color:sienna">Color</span>.
pub const SIENNA: Color = Color::from_rgb(0.627, 0.322, 0.176);
/// Silver <span style="color:silver">Color</span>.
pub const SILVER: Color = Color::from_rgb(0.753, 0.753, 0.753);
/// Sky Blue <span style="color:skyBlue">Color</span>.
pub const SKY_BLUE: Color = Color::from_rgb(0.529, 0.808, 0.922);
/// Slate Blue <span style="color:slateBlue">Color</span>.
pub const SLATE_BLUE: Color = Color::from_rgb(0.416, 0.353, 0.804);
/// Slate Gray <span style="color:slateGray">Color</span>.
pub const SLATE_GRAY: Color = Color::from_rgb(0.439, 0.502, 0.565);
/// Slate Grey <span style="color:slateGrey">Color</span>.
pub const SLATE_GREY: Color = SLATE_GRAY;
/// Snow <span style="color:snow">Color</span>.
pub const SNOW: Color = Color::from_rgb(1.0, 0.98, 0.98);
/// Spring Green <span style="color:springGreen">Color</span>.
pub const SPRING_GREEN: Color = Color::from_rgb(0.0, 1.0, 0.498);
/// Steel Blue <span style="color:steelBlue">Color</span>.
pub const STEEL_BLUE: Color = Color::from_rgb(0.275, 0.51, 0.706);
/// Tan <span style="color:tan">Color</span>.
pub const TAN: Color = Color::from_rgb(0.824, 0.706, 0.549);
/// Teal <span style="color:teal">Color</span>.
pub const TEAL: Color = Color::from_rgb(0.0, 0.502, 0.502);
/// Thistle <span style="color:thistle">Color</span>.
pub const THISTLE: Color = Color::from_rgb(0.847, 0.749, 0.847);
/// Tomato <span style="color:tomato">Color</span>.
pub const TOMATO: Color = Color::from_rgb(1.0, 0.388, 0.278);
/// Turquoise <span style="color:turquoise">Color</span>.
pub const TURQUOISE: Color = Color::from_rgb(0.251, 0.878, 0.816);
/// Violet <span style="color:violet">Color</span>.
pub const VIOLET: Color = Color::from_rgb(0.933, 0.51, 0.933);
/// Wheat <span style="color:wheat">Color</span>.
pub const WHEAT: Color = Color::from_rgb(0.961, 0.871, 0.702);
/// White <span style="color:white">Color</span>.
pub const WHITE: Color = Color::WHITE;
/// White Smoke <span style="color:whiteSmoke">Color</span>.
pub const WHITE_SMOKE: Color = Color::WHITE;
/// Yellow <span style="color:yellow">Color</span>.
pub const YELLOW: Color = Color::from_rgb(1.0, 1.0, 0.0);
/// Yellow Green <span style="color:yellowGreen">Color</span>.
pub const YELLOW_GREEN: Color = Color::from_rgb(0.604, 0.804, 0.196);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn semantic_colors_are_defined() {
// Verify semantic colors point to the correct values
assert_eq!(PRIMARY, DODGER_BLUE);
assert_eq!(SECONDARY, DIM_GRAY);
assert_eq!(SUCCESS, LIME_GREEN);
assert_eq!(DANGER, RED);
assert_eq!(WARNING, GOLD);
assert_eq!(INFO, SKY_BLUE);
assert_eq!(LIGHT, GHOST_WHITE);
}
#[test]
fn dark_color_is_custom() {
// DARK is a custom color, not an alias
assert_eq!(DARK, Color::from_rgb(0.204, 0.227, 0.251));
}
#[test]
fn black_and_white_constants() {
assert_eq!(BLACK, Color::BLACK);
assert_eq!(WHITE, Color::WHITE);
assert_eq!(WHITE_SMOKE, Color::WHITE);
}
#[test]
fn gray_grey_aliases_match() {
// American/British spelling aliases should be equal
assert_eq!(GRAY, GREY);
assert_eq!(DARK_GRAY, DARK_GREY);
assert_eq!(DIM_GRAY, DIM_GREY);
assert_eq!(LIGHT_GRAY, LIGHT_GREY);
assert_eq!(SLATE_GRAY, SLATE_GREY);
assert_eq!(DARK_SLATE_GRAY, DARK_SLATE_GREY);
assert_eq!(LIGHT_SLATE_GRAY, LIGHT_SLATE_GREY);
}
#[test]
fn primary_colors_are_valid() {
// Test basic RGB colors
assert_eq!(RED, Color::from_rgb(1.0, 0.0, 0.0));
assert_eq!(GREEN, Color::from_rgb(0.0, 0.502, 0.0));
assert_eq!(BLUE, Color::from_rgb(0.0, 0.0, 1.0));
}
#[test]
fn cyan_magenta_yellow_are_valid() {
assert_eq!(CYAN, Color::from_rgb(0.0, 1.0, 1.0));
assert_eq!(MAGENTA, Color::from_rgb(1.0, 0.0, 1.0));
assert_eq!(YELLOW, Color::from_rgb(1.0, 1.0, 0.0));
}
#[test]
fn aqua_equals_cyan() {
assert_eq!(AQUA, CYAN);
}
#[test]
fn fuchsia_equals_magenta() {
assert_eq!(FUCHSIA, MAGENTA);
}
#[test]
fn color_values_are_in_valid_range() {
// Sample various colors to ensure RGB values are between 0.0 and 1.0
let sample_colors = [
ALICE_BLUE,
ANTIQUE_WHITE,
CORAL,
CRIMSON,
DARK_GOLDEN_ROD,
DEEP_PINK,
DODGER_BLUE,
FOREST_GREEN,
GOLD,
INDIAN_RED,
LAVENDER,
LIME_GREEN,
MEDIUM_PURPLE,
NAVY,
ORANGE,
PALE_GREEN,
PURPLE,
ROSY_BROWN,
SALMON,
SLATE_BLUE,
TOMATO,
TURQUOISE,
VIOLET,
];
for color in &sample_colors {
assert!(
color.r >= 0.0 && color.r <= 1.0,
"Red component out of range"
);
assert!(
color.g >= 0.0 && color.g <= 1.0,
"Green component out of range"
);
assert!(
color.b >= 0.0 && color.b <= 1.0,
"Blue component out of range"
);
}
}
#[test]
fn specific_colors_have_correct_values() {
// Test a few specific colors to ensure accuracy
assert_eq!(GOLD, Color::from_rgb(1.0, 0.843, 0.0));
assert_eq!(LIME_GREEN, Color::from_rgb(0.196, 0.804, 0.196));
assert_eq!(SKY_BLUE, Color::from_rgb(0.529, 0.808, 0.922));
assert_eq!(DODGER_BLUE, Color::from_rgb(0.118, 0.565, 1.0));
assert_eq!(GHOST_WHITE, Color::from_rgb(0.973, 0.973, 1.0));
assert_eq!(DIM_GRAY, Color::from_rgb(0.412, 0.412, 0.412));
}
#[test]
fn dark_colors_are_darker() {
// Verify dark variants have lower RGB values than their base colors
const {
assert!(DARK_BLUE.r <= BLUE.r);
assert!(DARK_BLUE.g <= BLUE.g);
assert!(DARK_BLUE.b <= BLUE.b);
assert!(DARK_GREEN.r <= GREEN.r);
assert!(DARK_GREEN.g <= GREEN.g);
assert!(DARK_GREEN.b <= GREEN.b);
assert!(DARK_RED.r <= RED.r);
assert!(DARK_RED.g <= RED.g);
assert!(DARK_RED.b <= RED.b);
}
}
#[test]
fn light_colors_are_lighter() {
// Verify some light variants are generally lighter (higher average RGB brightness)
// Note: Not all CSS "light" colors are consistently lighter than their base
let light_blue_avg = (LIGHT_BLUE.r + LIGHT_BLUE.g + LIGHT_BLUE.b) / 3.0;
let blue_avg = (BLUE.r + BLUE.g + BLUE.b) / 3.0;
assert!(light_blue_avg > blue_avg);
let light_green_avg = (LIGHT_GREEN.r + LIGHT_GREEN.g + LIGHT_GREEN.b) / 3.0;
let green_avg = (GREEN.r + GREEN.g + GREEN.b) / 3.0;
assert!(light_green_avg > green_avg);
const {
// Light Gray should be lighter than Gray in all components (grayscale)
assert!(LIGHT_GRAY.r >= GRAY.r);
assert!(LIGHT_GRAY.g >= GRAY.g);
assert!(LIGHT_GRAY.b >= GRAY.b);
}
}
#[test]
fn brown_shades_exist() {
// Test various brown colors
assert_eq!(BROWN, Color::from_rgb(0.647, 0.165, 0.165));
assert_eq!(SADDLE_BROWN, Color::from_rgb(0.545, 0.271, 0.075));
assert_eq!(SANDY_BROWN, Color::from_rgb(0.957, 0.643, 0.376));
assert_eq!(ROSY_BROWN, Color::from_rgb(0.737, 0.561, 0.561));
}
#[test]
fn purple_shades_exist() {
// Test various purple colors
assert_eq!(PURPLE, Color::from_rgb(0.502, 0.0, 0.502));
assert_eq!(REBECCA_PURPLE, Color::from_rgb(0.4, 0.2, 0.6));
assert_eq!(MEDIUM_PURPLE, Color::from_rgb(0.576, 0.439, 0.859));
}
#[test]
fn orange_shades_exist() {
// Test various orange colors
assert_eq!(ORANGE, Color::from_rgb(1.0, 0.647, 0.0));
assert_eq!(DARK_ORANGE, Color::from_rgb(1.0, 0.549, 0.0));
assert_eq!(ORANGE_RED, Color::from_rgb(1.0, 0.271, 0.0));
}
}
+176
View File
@@ -0,0 +1,176 @@
//! Use a badge for color highlighting important information.
//!
//! *This API requires the following crate features to be activated: badge*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
/// The style of a [`ContextMenu`](crate::widget::ContextMenu).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`ContextMenu`](crate::widget::ContextMenu).
///
/// This is used to color the backdrop of the modal.
pub background: Background,
}
impl Default for Style {
fn default() -> Self {
Self {
background: Background::Color([0.87, 0.87, 0.87, 0.30].into()),
}
}
}
/// The Catalog of a [`ContextMenu`](crate::widget::ContextMenu).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`ContextMenu`](crate::widget::ContextMenu).
#[must_use]
pub fn primary(theme: &Theme, _status: Status) -> Style {
let palette = theme.extended_palette();
Style {
background: Background::Color(Color {
a: 0f32,
..palette.background.base.color
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn style_default() {
let style = Style::default();
assert!(matches!(style.background, Background::Color(_)));
// Verify the default has a semi-transparent background
if let Background::Color(color) = style.background {
assert_eq!(color.a, 0.30);
}
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
}
#[test]
fn primary_theme_has_transparent_background() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// The primary theme should create a transparent background
#[allow(clippy::panic)]
if let Background::Color(color) = style.background {
assert_eq!(
color.a, 0.0,
"Primary theme should have fully transparent background"
);
} else {
panic!("Expected Background::Color");
}
}
#[test]
fn status_does_not_affect_style() {
let theme = Theme::TokyoNight;
let active_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
let disabled_style = primary(&theme, Status::Disabled);
let focused_style = primary(&theme, Status::Focused);
// All statuses should produce the same style
assert_eq!(
format!("{:?}", active_style.background),
format!("{:?}", hovered_style.background)
);
assert_eq!(
format!("{:?}", active_style.background),
format!("{:?}", disabled_style.background)
);
assert_eq!(
format!("{:?}", active_style.background),
format!("{:?}", focused_style.background)
);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = theme.style(&class, Status::Active);
let hovered_style = theme.style(&class, Status::Hovered);
let disabled_style = theme.style(&class, Status::Disabled);
assert!(matches!(active_style.background, Background::Color(_)));
assert!(matches!(hovered_style.background, Background::Color(_)));
assert!(matches!(disabled_style.background, Background::Color(_)));
}
}
+271
View File
@@ -0,0 +1,271 @@
//! Use a date picker as an input element for picking dates.
//!
//! *This API requires the following crate features to be activated: `date_picker`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
/// The appearance of a [`DatePicker`](crate::widget::DatePicker).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`DatePicker`](crate::widget::DatePicker).
pub background: Background,
/// The border radius of the [`DatePicker`](crate::widget::DatePicker).
pub border_radius: f32,
/// The border with of the [`DatePicker`](crate::widget::DatePicker).
pub border_width: f32,
/// The border color of the [`DatePicker`](crate::widget::DatePicker).
pub border_color: Color,
/// The text color of the [`DatePicker`](crate::widget::DatePicker).
pub text_color: Color,
/// The attenuated color of the days which are not in the selected month
/// of the [`DatePicker`](crate::widget::DatePicker).
pub text_attenuated_color: Color,
/// The background of the days in the calender of the
/// [`DatePicker`](crate::widget::DatePicker).
pub day_background: Background,
}
/// The Catalog of a [`DatePicker`](crate::widget::DatePicker).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let foreground = theme.palette();
let base = Style {
background: palette.background.base.color.into(),
border_radius: 15.0,
border_width: 1.0,
border_color: foreground.text,
text_color: foreground.text,
text_attenuated_color: Color {
a: foreground.text.a * 0.5,
..foreground.text
},
day_background: palette.background.base.color.into(),
};
match status {
Status::Selected => Style {
day_background: palette.primary.strong.color.into(),
text_color: palette.primary.strong.text,
..base
},
Status::Hovered => Style {
day_background: palette.primary.weak.color.into(),
text_color: palette.primary.weak.text,
..base
},
Status::Focused => Style {
border_color: Color::from_rgb(0.5, 0.5, 0.5),
..base
},
_ => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
assert!(matches!(style.day_background, Background::Color(_)));
}
#[test]
fn primary_theme_selected() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Selected);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.day_background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert!(matches!(style.day_background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_color, Color::from_rgb(0.5, 0.5, 0.5));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn selected_changes_day_background_and_text() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let selected_style = primary(&theme, Status::Selected);
// Day background and text color should be different when selected
assert_ne!(
format!("{:?}", base_style.day_background),
format!("{:?}", selected_style.day_background)
);
assert_ne!(base_style.text_color, selected_style.text_color);
// Other properties should remain the same
assert_eq!(base_style.background, selected_style.background);
assert_eq!(base_style.border_radius, selected_style.border_radius);
assert_eq!(base_style.border_width, selected_style.border_width);
assert_eq!(base_style.border_color, selected_style.border_color);
}
#[test]
fn hovered_changes_day_background_and_text() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
// Day background and text color should be different when hovered
assert_ne!(
format!("{:?}", base_style.day_background),
format!("{:?}", hovered_style.day_background)
);
assert_ne!(base_style.text_color, hovered_style.text_color);
// Other properties should remain the same
assert_eq!(base_style.background, hovered_style.background);
assert_eq!(base_style.border_radius, hovered_style.border_radius);
assert_eq!(base_style.border_width, hovered_style.border_width);
assert_eq!(base_style.border_color, hovered_style.border_color);
}
#[test]
fn focused_changes_border_color() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let focused_style = primary(&theme, Status::Focused);
// Border color should be different when focused
assert_ne!(base_style.border_color, focused_style.border_color);
assert_eq!(focused_style.border_color, Color::from_rgb(0.5, 0.5, 0.5));
// Other properties should remain the same
assert_eq!(base_style.background, focused_style.background);
assert_eq!(base_style.border_radius, focused_style.border_radius);
assert_eq!(base_style.border_width, focused_style.border_width);
assert_eq!(base_style.text_color, focused_style.text_color);
assert_eq!(
format!("{:?}", base_style.day_background),
format!("{:?}", focused_style.day_background)
);
}
#[test]
fn text_attenuated_color_has_reduced_alpha() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Attenuated color should have half the alpha
assert!(
style.text_attenuated_color.a <= style.text_color.a,
"Attenuated color should have reduced alpha"
);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = theme.style(&class, Status::Active);
let selected_style = theme.style(&class, Status::Selected);
let hovered_style = theme.style(&class, Status::Hovered);
let focused_style = theme.style(&class, Status::Focused);
assert!(matches!(active_style.background, Background::Color(_)));
assert!(matches!(selected_style.background, Background::Color(_)));
assert!(matches!(hovered_style.background, Background::Color(_)));
assert!(matches!(focused_style.background, Background::Color(_)));
}
#[test]
fn style_fields_are_valid() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Verify all fields have valid values
assert!(style.border_radius > 0.0);
assert!(style.border_width > 0.0);
assert!(style.text_attenuated_color.a >= 0.0);
assert!(style.text_attenuated_color.a <= 1.0);
}
}
+301
View File
@@ -0,0 +1,301 @@
//! Change the appearance of menu bars and their menus.
use super::{Status, StyleFn};
use iced_core::{Background, Border, Color, Shadow, Theme, Vector};
/// The appearance of a menu bar and its menus.
#[derive(Debug, Clone, Copy)]
pub struct Style {
/// The background of the menu bar.
pub bar_background: Background,
/// The border of the menu bar.
pub bar_border: Border,
/// The shadow of the menu bar.
pub bar_shadow: Shadow,
/// The background of the menus.
pub menu_background: Background,
/// The border of the menus.
pub menu_border: Border,
/// The shadow of the menus
pub menu_shadow: Shadow,
/// The backgraound of the path
pub path: Background,
/// The border of the path
pub path_border: Border,
}
impl std::default::Default for Style {
fn default() -> Self {
Self {
bar_background: Color::from([0.85; 3]).into(),
bar_border: Border {
radius: 8.0.into(),
..Default::default()
},
bar_shadow: Shadow::default(),
menu_background: Color::from([0.85; 3]).into(),
menu_border: Border {
radius: 8.0.into(),
..Default::default()
},
menu_shadow: Shadow {
color: Color::from([0.0, 0.0, 0.0, 0.5]),
offset: Vector::ZERO,
blur_radius: 10.0,
},
path: Color::from([0.3; 3]).into(),
path_border: Border {
radius: 6.0.into(),
..Default::default()
},
}
}
}
/// The Catalog of a [`Menu`](crate::widget::menu::Menu).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Menu`](crate::widget::menu::Menu).
#[must_use]
pub fn primary(theme: &Theme, _status: Status) -> Style {
let palette = theme.extended_palette();
Style {
bar_background: palette.background.base.color.into(),
menu_background: palette.background.base.color.into(),
path: palette.primary.weak.color.into(),
..Default::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn style_default() {
let style = Style::default();
// Verify bar properties
assert!(matches!(style.bar_background, Background::Color(_)));
assert_eq!(style.bar_border.radius.top_left, 8.0);
assert_eq!(style.bar_border.radius.top_right, 8.0);
assert_eq!(style.bar_border.radius.bottom_left, 8.0);
assert_eq!(style.bar_border.radius.bottom_right, 8.0);
// Verify menu properties
assert!(matches!(style.menu_background, Background::Color(_)));
assert_eq!(style.menu_border.radius.top_left, 8.0);
assert_eq!(style.menu_shadow.blur_radius, 10.0);
// Verify path properties
assert!(matches!(style.path, Background::Color(_)));
assert_eq!(style.path_border.radius.top_left, 6.0);
}
#[test]
fn default_shadow_has_blur() {
let style = Style::default();
// Menu shadow should have blur
assert_eq!(style.menu_shadow.blur_radius, 10.0);
assert_eq!(style.menu_shadow.offset, Vector::ZERO);
// Verify shadow color has transparency
if let Background::Color(color) = style.bar_background {
// Just verify it exists
let _ = color;
}
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
#[test]
fn primary_uses_default_borders_and_shadows() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
let default_style = Style::default();
// Primary theme should use default borders
assert_eq!(style.bar_border.radius, default_style.bar_border.radius);
assert_eq!(style.menu_border.radius, default_style.menu_border.radius);
assert_eq!(style.path_border.radius, default_style.path_border.radius);
// Primary theme should use default shadows
assert_eq!(
style.bar_shadow.blur_radius,
default_style.bar_shadow.blur_radius
);
assert_eq!(
style.menu_shadow.blur_radius,
default_style.menu_shadow.blur_radius
);
}
#[test]
fn status_does_not_affect_style() {
let theme = Theme::TokyoNight;
let active_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
let disabled_style = primary(&theme, Status::Disabled);
// All statuses should produce the same style
assert_eq!(
format!("{:?}", active_style.bar_background),
format!("{:?}", hovered_style.bar_background)
);
assert_eq!(
format!("{:?}", active_style.bar_background),
format!("{:?}", disabled_style.bar_background)
);
assert_eq!(
format!("{:?}", active_style.menu_background),
format!("{:?}", hovered_style.menu_background)
);
assert_eq!(
format!("{:?}", active_style.path),
format!("{:?}", hovered_style.path)
);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = theme.style(&class, Status::Active);
let hovered_style = theme.style(&class, Status::Hovered);
let disabled_style = theme.style(&class, Status::Disabled);
assert!(matches!(active_style.bar_background, Background::Color(_)));
assert!(matches!(hovered_style.bar_background, Background::Color(_)));
assert!(matches!(
disabled_style.bar_background,
Background::Color(_)
));
}
#[test]
fn default_border_radii_are_consistent() {
let style = Style::default();
// Bar border should have uniform radius
assert_eq!(
style.bar_border.radius.top_left,
style.bar_border.radius.top_right
);
assert_eq!(
style.bar_border.radius.bottom_left,
style.bar_border.radius.bottom_right
);
// Menu border should have uniform radius
assert_eq!(
style.menu_border.radius.top_left,
style.menu_border.radius.top_right
);
assert_eq!(
style.menu_border.radius.bottom_left,
style.menu_border.radius.bottom_right
);
// Path border should have uniform radius
assert_eq!(
style.path_border.radius.top_left,
style.path_border.radius.top_right
);
assert_eq!(
style.path_border.radius.bottom_left,
style.path_border.radius.bottom_right
);
}
#[test]
fn all_backgrounds_are_colors() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// All backgrounds should be Color variants
assert!(matches!(style.bar_background, Background::Color(_)));
assert!(matches!(style.menu_background, Background::Color(_)));
assert!(matches!(style.path, Background::Color(_)));
}
}
+275
View File
@@ -0,0 +1,275 @@
//! Display fields that can only be filled with numeric type.
//!
//! *This API requires the following crate features to be activated: `number_input`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
use iced_widget::{container, text, text_input};
/// The appearance of a [`NumberInput`](crate::widget::number_input::NumberInput).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`NumberInput`](crate::widget::number_input::NumberInput).
pub button_background: Option<Background>,
/// The Color of the arrows of [`NumberInput`](crate::widget::number_input::NumberInput).
pub icon_color: Color,
}
impl Default for Style {
fn default() -> Self {
Self {
button_background: None,
icon_color: Color::BLACK,
}
}
}
/// The Catalog of a [`NumberInput`](crate::widget::number_input::NumberInput).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The Extended Catalog of a [`NumberInput`](crate::widget::number_input::NumberInput).
pub trait ExtendedCatalog:
text_input::Catalog + container::Catalog + text::Catalog + self::Catalog
{
/// The default class produced by the [`Catalog`].
#[must_use]
fn default_input<'a>() -> <Self as text_input::Catalog>::Class<'a> {
<Self as text_input::Catalog>::default()
}
/// The [`Style`] of a class with the given status.
fn style(&self, class: &<Self as self::Catalog>::Class<'_>, status: Status) -> Style;
}
impl ExtendedCatalog for Theme {
fn style(&self, class: &<Self as self::Catalog>::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Badge`](crate::widget::badge::Badge).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = Style {
button_background: Some(palette.primary.strong.color.into()),
icon_color: palette.primary.strong.text,
};
match status {
Status::Disabled => Style {
button_background: base.button_background.map(|bg| match bg {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
Background::Gradient(grad) => Background::Gradient(grad),
}),
icon_color: Color {
a: base.icon_color.a * 0.5,
..base.icon_color
},
},
_ => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::Theme;
#[test]
fn style_default() {
let style = Style::default();
assert!(style.button_background.is_none());
assert_eq!(style.icon_color, Color::BLACK);
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(style.button_background.is_some());
#[allow(clippy::panic)]
if let Some(Background::Color(_)) = style.button_background {
// Background is a color
} else {
panic!("Expected button_background to be Some(Background::Color)");
}
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(style.button_background.is_some());
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(style.button_background.is_some());
}
#[test]
fn primary_theme_selected() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Selected);
assert!(style.button_background.is_some());
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(style.button_background.is_some());
}
#[test]
fn disabled_reduces_alpha() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let disabled_style = primary(&theme, Status::Disabled);
// Icon color alpha should be reduced
assert!(
disabled_style.icon_color.a <= base_style.icon_color.a,
"Disabled icon color should have reduced alpha"
);
// Button background alpha should be reduced for Color backgrounds
if let (Some(Background::Color(base_color)), Some(Background::Color(disabled_color))) = (
base_style.button_background,
disabled_style.button_background,
) {
assert!(
disabled_color.a <= base_color.a,
"Disabled button background should have reduced alpha"
);
}
}
#[test]
fn disabled_has_half_alpha() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let disabled_style = primary(&theme, Status::Disabled);
// Icon color should have approximately half the alpha
let expected_icon_alpha = base_style.icon_color.a * 0.5;
assert!(
(disabled_style.icon_color.a - expected_icon_alpha).abs() < 0.01,
"Disabled icon alpha should be approximately half"
);
}
#[test]
fn non_disabled_statuses_use_base_style() {
let theme = Theme::TokyoNight;
let active_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
let focused_style = primary(&theme, Status::Focused);
// All non-disabled statuses should have the same style
assert_eq!(
format!("{:?}", active_style.button_background),
format!("{:?}", hovered_style.button_background)
);
assert_eq!(
format!("{:?}", active_style.button_background),
format!("{:?}", focused_style.button_background)
);
assert_eq!(active_style.icon_color, hovered_style.icon_color);
assert_eq!(active_style.icon_color, focused_style.icon_color);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = <Theme as Catalog>::style(&theme, &class, Status::Active);
assert!(style.button_background.is_some());
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = <Theme as Catalog>::style(&theme, &class, Status::Active);
let hovered_style = <Theme as Catalog>::style(&theme, &class, Status::Hovered);
let disabled_style = <Theme as Catalog>::style(&theme, &class, Status::Disabled);
assert!(active_style.button_background.is_some());
assert!(hovered_style.button_background.is_some());
assert!(disabled_style.button_background.is_some());
}
#[test]
fn extended_catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = <Theme as ExtendedCatalog>::style(&theme, &class, Status::Active);
assert!(style.button_background.is_some());
}
#[test]
fn extended_catalog_default_input() {
let _class = <Theme as ExtendedCatalog>::default_input();
}
#[test]
fn disabled_preserves_gradient_backgrounds() {
// Create a style with a gradient background
let gradient = iced_core::Gradient::Linear(iced_core::gradient::Linear {
angle: 0.0.into(),
stops: [None; 8],
});
let style_with_gradient = Style {
button_background: Some(Background::Gradient(gradient)),
icon_color: Color::WHITE,
};
// If we had a disabled version, it should preserve the gradient
// This tests that gradients are handled in the match statement
#[allow(clippy::panic)]
if let Some(Background::Color(_)) = style_with_gradient.button_background {
panic!("Expected gradient to be preserved");
}
}
}
+256
View File
@@ -0,0 +1,256 @@
//! Selection List
//!
//! *This API requires the following crate features to be activated: `selection_list`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
/// The appearance of a menu.
#[derive(Debug, Clone, Copy)]
pub struct Style {
/// The List Label Text Color
pub text_color: Color,
/// The background
pub background: Background,
/// The container Border width
pub border_width: f32,
/// The container Border color
pub border_color: Color,
}
/// The Catalog of a [`Badge`](crate::widget::selection_list::SelectionList).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Badge`](crate::widget::selection_list::SelectionList).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let base = Style {
text_color: palette.background.base.text,
background: palette.background.base.color.into(),
border_width: 1.0,
border_color: palette.background.weak.color,
};
match status {
Status::Hovered => Style {
text_color: palette.primary.base.text,
background: palette.primary.base.color.into(),
..base
},
Status::Selected => Style {
text_color: palette.primary.strong.text,
background: palette.primary.strong.color.into(),
..base
},
_ => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_selected() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Selected);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn hovered_changes_text_and_background() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
// Text color and background should be different when hovered
assert_ne!(base_style.text_color, hovered_style.text_color);
assert_ne!(
format!("{:?}", base_style.background),
format!("{:?}", hovered_style.background)
);
// Border properties should remain the same
assert_eq!(base_style.border_width, hovered_style.border_width);
assert_eq!(base_style.border_color, hovered_style.border_color);
}
#[test]
fn selected_changes_text_and_background() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let selected_style = primary(&theme, Status::Selected);
// Text color and background should be different when selected
assert_ne!(base_style.text_color, selected_style.text_color);
assert_ne!(
format!("{:?}", base_style.background),
format!("{:?}", selected_style.background)
);
// Border properties should remain the same
assert_eq!(base_style.border_width, selected_style.border_width);
assert_eq!(base_style.border_color, selected_style.border_color);
}
#[test]
fn selected_and_hovered_have_different_styles() {
let theme = Theme::TokyoNight;
let hovered_style = primary(&theme, Status::Hovered);
let selected_style = primary(&theme, Status::Selected);
// Selected and hovered should use different colors
assert_ne!(hovered_style.text_color, selected_style.text_color);
assert_ne!(
format!("{:?}", hovered_style.background),
format!("{:?}", selected_style.background)
);
}
#[test]
fn non_interactive_statuses_use_base_style() {
let theme = Theme::TokyoNight;
let active_style = primary(&theme, Status::Active);
let disabled_style = primary(&theme, Status::Disabled);
let focused_style = primary(&theme, Status::Focused);
// Non-interactive statuses should have the same style
assert_eq!(active_style.text_color, disabled_style.text_color);
assert_eq!(active_style.text_color, focused_style.text_color);
assert_eq!(
format!("{:?}", active_style.background),
format!("{:?}", disabled_style.background)
);
assert_eq!(
format!("{:?}", active_style.background),
format!("{:?}", focused_style.background)
);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_width, 1.0);
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = theme.style(&class, Status::Active);
let hovered_style = theme.style(&class, Status::Hovered);
let selected_style = theme.style(&class, Status::Selected);
assert!(matches!(active_style.background, Background::Color(_)));
assert!(matches!(hovered_style.background, Background::Color(_)));
assert!(matches!(selected_style.background, Background::Color(_)));
}
#[test]
fn border_width_is_consistent() {
let theme = Theme::TokyoNight;
// All statuses should have the same border width
let active = primary(&theme, Status::Active);
let hovered = primary(&theme, Status::Hovered);
let selected = primary(&theme, Status::Selected);
assert_eq!(active.border_width, 1.0);
assert_eq!(hovered.border_width, 1.0);
assert_eq!(selected.border_width, 1.0);
}
#[test]
fn border_color_is_consistent() {
let theme = Theme::TokyoNight;
// All statuses should have the same border color
let active = primary(&theme, Status::Active);
let hovered = primary(&theme, Status::Hovered);
let selected = primary(&theme, Status::Selected);
assert_eq!(active.border_color, hovered.border_color);
assert_eq!(active.border_color, selected.border_color);
}
#[test]
fn style_fields_are_valid() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Verify all fields have valid values
assert!(style.border_width > 0.0);
}
}
+458
View File
@@ -0,0 +1,458 @@
//! This is the style for [`Sidebar`](crate::widget::sidebar::Sidebar) and
//! [`SidebarWithContent`](crate::widget::sidebar::SidebarWithContent).
//!
//! *This API requires the following crate features to be activated: `sidebar`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme, border::Radius};
/// The appearance of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the sidebar.
pub background: Option<Background>,
/// The border color of the sidebar.
pub border_color: Option<Color>,
/// The border width of the sidebar.
pub border_width: f32,
/// The background of the tab labels.
pub tab_label_background: Background,
/// The border color of the tab labels.
pub tab_label_border_color: Color,
/// The border with of the tab labels.
pub tab_label_border_width: f32,
/// The icon color of the tab labels.
pub icon_color: Color,
/// The color of the closing icon border
pub icon_background: Option<Background>,
/// How soft/hard the corners of the icon border are
pub icon_border_radius: Radius,
/// The text color of the tab labels.
pub text_color: Color,
}
impl Default for Style {
fn default() -> Self {
Self {
background: None,
border_color: None,
border_width: 0.0,
tab_label_background: Background::Color([0.87, 0.87, 0.87].into()),
tab_label_border_color: [0.7, 0.7, 0.7].into(),
tab_label_border_width: 1.0,
icon_color: Color::BLACK,
icon_background: Some(Background::Color(Color::TRANSPARENT)),
icon_border_radius: 4.0.into(),
text_color: Color::BLACK,
}
}
}
/// The Catalog of a [`Sidebar`](crate::widget::sidebar::Sidebar).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let mut base = Style::default();
let palette = theme.extended_palette();
base.text_color = palette.background.base.text;
match status {
Status::Disabled => {
base.tab_label_background = Background::Color(palette.background.strong.color);
}
Status::Hovered => {
base.tab_label_background = Background::Color(palette.primary.strong.color);
}
_ => {
base.tab_label_background = Background::Color(palette.primary.base.color);
}
}
base
}
/// The dark theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn dark(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([0.1, 0.1, 0.1].into()),
tab_label_border_color: [0.3, 0.3, 0.3].into(),
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
}
base
}
/// The red theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn red(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([1.0, 0.0, 0.0].into()),
tab_label_border_color: Color::TRANSPARENT,
tab_label_border_width: 0.0,
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
base.icon_color = Color::BLACK;
base.text_color = Color::BLACK;
}
base
}
/// The blue theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn blue(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([0.0, 0.0, 1.0].into()),
tab_label_border_color: [0.0, 0.0, 1.0].into(),
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.5, 0.5, 1.0].into());
base.tab_label_border_color = [0.5, 0.5, 1.0].into();
}
base
}
/// The blue theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn green(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Color::WHITE.into(),
icon_color: [0.0, 0.5, 0.0].into(),
text_color: [0.0, 0.5, 0.0].into(),
..Default::default()
};
match status {
Status::Disabled => {
base.icon_color = [0.7, 0.7, 0.7].into();
base.text_color = [0.7, 0.7, 0.7].into();
base.tab_label_border_color = [0.7, 0.7, 0.7].into();
}
_ => {
base.tab_label_border_color = [0.0, 0.5, 0.0].into();
}
}
base
}
/// The purple theme of a [`Sidebar`](crate::widget::sidebar::Sidebar).
#[must_use]
pub fn purple(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Color::WHITE.into(),
tab_label_border_color: Color::TRANSPARENT,
icon_color: [0.7, 0.0, 1.0].into(),
text_color: [0.7, 0.0, 1.0].into(),
..Default::default()
};
if status == Status::Disabled {
base.icon_color = Color::BLACK;
base.text_color = Color::BLACK;
}
base
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn style_default() {
let style = Style::default();
assert!(style.background.is_none());
assert!(style.border_color.is_none());
assert_eq!(style.border_width, 0.0);
assert!(matches!(style.tab_label_background, Background::Color(_)));
assert_eq!(style.tab_label_border_width, 1.0);
assert_eq!(style.icon_color, Color::BLACK);
assert!(style.icon_background.is_some());
assert_eq!(style.text_color, Color::BLACK);
assert_eq!(style.icon_border_radius.top_left, 4.0);
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_changes_background_by_status() {
let theme = Theme::TokyoNight;
let active = primary(&theme, Status::Active);
let hovered = primary(&theme, Status::Hovered);
let disabled = primary(&theme, Status::Disabled);
// Different statuses should have different backgrounds
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", hovered.tab_label_background)
);
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", disabled.tab_label_background)
);
}
#[test]
fn dark_theme_active() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Active);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn dark_theme_disabled() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn dark_theme_changes_background_when_disabled() {
let theme = Theme::TokyoNight;
let active = dark(&theme, Status::Active);
let disabled = dark(&theme, Status::Disabled);
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", disabled.tab_label_background)
);
}
#[test]
fn red_theme_active() {
let theme = Theme::TokyoNight;
let style = red(&theme, Status::Active);
assert_eq!(
style.tab_label_background,
Background::Color([1.0, 0.0, 0.0].into())
);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
assert_eq!(style.tab_label_border_width, 0.0);
}
#[test]
fn red_theme_disabled() {
let theme = Theme::TokyoNight;
let style = red(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::BLACK);
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn blue_theme_active() {
let theme = Theme::TokyoNight;
let style = blue(&theme, Status::Active);
assert_eq!(
style.tab_label_background,
Background::Color([0.0, 0.0, 1.0].into())
);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn blue_theme_disabled() {
let theme = Theme::TokyoNight;
let style = blue(&theme, Status::Disabled);
assert_eq!(
style.tab_label_background,
Background::Color([0.5, 0.5, 1.0].into())
);
assert_eq!(style.tab_label_border_color, [0.5, 0.5, 1.0].into());
}
#[test]
fn green_theme_active() {
let theme = Theme::TokyoNight;
let style = green(&theme, Status::Active);
assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
assert_eq!(style.icon_color, [0.0, 0.5, 0.0].into());
assert_eq!(style.text_color, [0.0, 0.5, 0.0].into());
assert_eq!(style.tab_label_border_color, [0.0, 0.5, 0.0].into());
}
#[test]
fn green_theme_disabled() {
let theme = Theme::TokyoNight;
let style = green(&theme, Status::Disabled);
assert_eq!(style.icon_color, [0.7, 0.7, 0.7].into());
assert_eq!(style.text_color, [0.7, 0.7, 0.7].into());
assert_eq!(style.tab_label_border_color, [0.7, 0.7, 0.7].into());
}
#[test]
fn purple_theme_active() {
let theme = Theme::TokyoNight;
let style = purple(&theme, Status::Active);
assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
assert_eq!(style.icon_color, [0.7, 0.0, 1.0].into());
assert_eq!(style.text_color, [0.7, 0.0, 1.0].into());
assert_eq!(style.tab_label_border_color, Color::TRANSPARENT);
}
#[test]
fn purple_theme_disabled() {
let theme = Theme::TokyoNight;
let style = purple(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::BLACK);
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active = theme.style(&class, Status::Active);
let hovered = theme.style(&class, Status::Hovered);
let disabled = theme.style(&class, Status::Disabled);
assert!(matches!(active.tab_label_background, Background::Color(_)));
assert!(matches!(hovered.tab_label_background, Background::Color(_)));
assert!(matches!(
disabled.tab_label_background,
Background::Color(_)
));
}
#[test]
fn icon_border_radius_is_set() {
let style = Style::default();
// Icon border radius should be set to 4.0 (all corners)
assert_eq!(style.icon_border_radius.top_left, 4.0);
assert_eq!(style.icon_border_radius.top_right, 4.0);
assert_eq!(style.icon_border_radius.bottom_left, 4.0);
assert_eq!(style.icon_border_radius.bottom_right, 4.0);
}
#[test]
fn different_themes_produce_different_styles() {
let theme = Theme::TokyoNight;
let status = Status::Active;
let _primary_style = primary(&theme, status);
let dark_style = dark(&theme, status);
let red_style = red(&theme, status);
let blue_style = blue(&theme, status);
let green_style = green(&theme, status);
let purple_style = purple(&theme, status);
// All themes should produce different background styles
assert_ne!(
format!("{:?}", dark_style.tab_label_background),
format!("{:?}", red_style.tab_label_background)
);
assert_ne!(
format!("{:?}", red_style.tab_label_background),
format!("{:?}", blue_style.tab_label_background)
);
assert_ne!(
format!("{:?}", blue_style.tab_label_background),
format!("{:?}", green_style.tab_label_background)
);
// Text colors also differ between some themes
assert_ne!(green_style.text_color, purple_style.text_color);
}
}
+156
View File
@@ -0,0 +1,156 @@
//! Status Enum of an mouse Event.
//!
/// The Status of a widget event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
/// can be pressed.
Active,
/// can be pressed and it is being hovered.
Hovered,
/// is being pressed.
Pressed,
/// cannot be pressed.
Disabled,
/// is focused.
Focused,
/// is Selected.
Selected,
}
/// The style function of widget.
pub type StyleFn<'a, Theme, Style> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn status_active_exists() {
let status = Status::Active;
assert_eq!(status, Status::Active);
}
#[test]
fn status_hovered_exists() {
let status = Status::Hovered;
assert_eq!(status, Status::Hovered);
}
#[test]
fn status_pressed_exists() {
let status = Status::Pressed;
assert_eq!(status, Status::Pressed);
}
#[test]
fn status_disabled_exists() {
let status = Status::Disabled;
assert_eq!(status, Status::Disabled);
}
#[test]
fn status_focused_exists() {
let status = Status::Focused;
assert_eq!(status, Status::Focused);
}
#[test]
fn status_selected_exists() {
let status = Status::Selected;
assert_eq!(status, Status::Selected);
}
#[test]
fn status_equality() {
assert_eq!(Status::Active, Status::Active);
assert_eq!(Status::Hovered, Status::Hovered);
assert_eq!(Status::Pressed, Status::Pressed);
assert_eq!(Status::Disabled, Status::Disabled);
assert_eq!(Status::Focused, Status::Focused);
assert_eq!(Status::Selected, Status::Selected);
}
#[test]
fn status_inequality() {
assert_ne!(Status::Active, Status::Hovered);
assert_ne!(Status::Active, Status::Pressed);
assert_ne!(Status::Active, Status::Disabled);
assert_ne!(Status::Active, Status::Focused);
assert_ne!(Status::Active, Status::Selected);
assert_ne!(Status::Hovered, Status::Pressed);
assert_ne!(Status::Hovered, Status::Disabled);
assert_ne!(Status::Disabled, Status::Focused);
}
#[test]
fn status_clone() {
let status = Status::Active;
let cloned = status;
assert_eq!(status, cloned);
}
#[test]
fn status_copy() {
let status = Status::Active;
let copied = status;
assert_eq!(status, copied);
}
#[test]
fn status_debug() {
let status = Status::Active;
let debug_str = format!("{status:?}");
assert_eq!(debug_str, "Active");
let status = Status::Hovered;
let debug_str = format!("{status:?}");
assert_eq!(debug_str, "Hovered");
}
#[test]
fn all_statuses_are_different() {
let statuses = [
Status::Active,
Status::Hovered,
Status::Pressed,
Status::Disabled,
Status::Focused,
Status::Selected,
];
// Each status should be unique
for (i, status1) in statuses.iter().enumerate() {
for (j, status2) in statuses.iter().enumerate() {
if i == j {
assert_eq!(status1, status2);
} else {
assert_ne!(status1, status2);
}
}
}
}
#[test]
fn style_fn_type_compiles() {
// This test verifies that the StyleFn type alias is correctly defined
// and can be used with a simple function
fn example_style_fn(_theme: &(), status: Status) -> u32 {
match status {
Status::Active => 1,
Status::Hovered => 2,
Status::Pressed => 3,
Status::Disabled => 4,
Status::Focused => 5,
Status::Selected => 6,
}
}
let style_fn: StyleFn<(), u32> = Box::new(example_style_fn);
assert_eq!(style_fn(&(), Status::Active), 1);
assert_eq!(style_fn(&(), Status::Hovered), 2);
assert_eq!(style_fn(&(), Status::Pressed), 3);
assert_eq!(style_fn(&(), Status::Disabled), 4);
assert_eq!(style_fn(&(), Status::Focused), 5);
assert_eq!(style_fn(&(), Status::Selected), 6);
}
}
+172
View File
@@ -0,0 +1,172 @@
//! Helper enum for the state of the style
/// The state of the style
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum StyleState {
/// Use the active style
Active,
/// Use the selected style
Selected,
/// Use the hovered style
Hovered,
/// Use the focused style
Focused,
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[test]
fn style_state_active_exists() {
let state = StyleState::Active;
assert_eq!(state, StyleState::Active);
}
#[test]
fn style_state_selected_exists() {
let state = StyleState::Selected;
assert_eq!(state, StyleState::Selected);
}
#[test]
fn style_state_hovered_exists() {
let state = StyleState::Hovered;
assert_eq!(state, StyleState::Hovered);
}
#[test]
fn style_state_focused_exists() {
let state = StyleState::Focused;
assert_eq!(state, StyleState::Focused);
}
#[test]
fn style_state_equality() {
assert_eq!(StyleState::Active, StyleState::Active);
assert_eq!(StyleState::Selected, StyleState::Selected);
assert_eq!(StyleState::Hovered, StyleState::Hovered);
assert_eq!(StyleState::Focused, StyleState::Focused);
}
#[test]
fn style_state_inequality() {
assert_ne!(StyleState::Active, StyleState::Selected);
assert_ne!(StyleState::Active, StyleState::Hovered);
assert_ne!(StyleState::Active, StyleState::Focused);
assert_ne!(StyleState::Selected, StyleState::Hovered);
assert_ne!(StyleState::Selected, StyleState::Focused);
assert_ne!(StyleState::Hovered, StyleState::Focused);
}
#[test]
fn style_state_debug() {
let state = StyleState::Active;
let debug_str = format!("{state:?}");
assert_eq!(debug_str, "Active");
let state = StyleState::Selected;
let debug_str = format!("{state:?}");
assert_eq!(debug_str, "Selected");
let state = StyleState::Hovered;
let debug_str = format!("{state:?}");
assert_eq!(debug_str, "Hovered");
let state = StyleState::Focused;
let debug_str = format!("{state:?}");
assert_eq!(debug_str, "Focused");
}
#[test]
fn style_state_ordering() {
// Verify the order based on enum definition
assert!(StyleState::Active < StyleState::Selected);
assert!(StyleState::Selected < StyleState::Hovered);
assert!(StyleState::Hovered < StyleState::Focused);
assert!(StyleState::Active <= StyleState::Active);
assert!(StyleState::Active <= StyleState::Selected);
assert!(StyleState::Focused >= StyleState::Active);
assert!(StyleState::Focused >= StyleState::Focused);
}
#[test]
fn style_state_hash() {
// Verify that StyleState can be used as a HashMap key
let mut map = HashMap::new();
let _ = map.insert(StyleState::Active, "active");
let _ = map.insert(StyleState::Selected, "selected");
let _ = map.insert(StyleState::Hovered, "hovered");
let _ = map.insert(StyleState::Focused, "focused");
assert_eq!(map.get(&StyleState::Active), Some(&"active"));
assert_eq!(map.get(&StyleState::Selected), Some(&"selected"));
assert_eq!(map.get(&StyleState::Hovered), Some(&"hovered"));
assert_eq!(map.get(&StyleState::Focused), Some(&"focused"));
}
#[test]
fn all_style_states_are_different() {
let states = [
StyleState::Active,
StyleState::Selected,
StyleState::Hovered,
StyleState::Focused,
];
// Each state should be unique
for (i, state1) in states.iter().enumerate() {
for (j, state2) in states.iter().enumerate() {
if i == j {
assert_eq!(state1, state2);
} else {
assert_ne!(state1, state2);
}
}
}
}
#[test]
fn style_state_can_be_matched() {
let state = StyleState::Active;
let value = match state {
StyleState::Active => 1,
StyleState::Selected => 2,
StyleState::Hovered => 3,
StyleState::Focused => 4,
};
assert_eq!(value, 1);
let state = StyleState::Focused;
let value = match state {
StyleState::Active => 1,
StyleState::Selected => 2,
StyleState::Hovered => 3,
StyleState::Focused => 4,
};
assert_eq!(value, 4);
}
#[test]
fn style_state_ord_comparison() {
let mut states = vec![
StyleState::Focused,
StyleState::Active,
StyleState::Hovered,
StyleState::Selected,
];
states.sort();
assert_eq!(
states,
vec![
StyleState::Active,
StyleState::Selected,
StyleState::Hovered,
StyleState::Focused,
]
);
}
}
+472
View File
@@ -0,0 +1,472 @@
//! Displays a [`TabBar`](crate::widget::tab_bar::TabBar) to select the content
//! to be displayed.
//!
//! You have to manage the logic to show the contend by yourself or you may want
//! to use the [`Tabs`](crate::widget::tabs::Tabs) widget instead.
//!
//! *This API requires the following crate features to be activated: `tab_bar`*
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme, border::Radius};
/// The appearance of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the tab bar.
pub background: Option<Background>,
/// The border color of the tab bar.
pub border_color: Option<Color>,
/// The border width of the tab bar.
pub border_width: f32,
/// The border radius of a tab.
pub tab_border_radius: Radius,
/// The background of the tab labels.
pub tab_label_background: Background,
/// The border color of the tab labels.
pub tab_label_border_color: Color,
/// The border with of the tab labels.
pub tab_label_border_width: f32,
/// The icon color of the tab labels.
pub icon_color: Color,
/// The color of the closing icon border
pub icon_background: Option<Background>,
/// How soft/hard the corners of the icon border are
pub icon_border_radius: Radius,
/// The text color of the tab labels.
pub text_color: Color,
}
impl Default for Style {
fn default() -> Self {
Self {
background: None,
border_color: None,
border_width: 0.0,
tab_border_radius: Radius::default(),
tab_label_background: Background::Color([0.87, 0.87, 0.87].into()),
tab_label_border_color: [0.7, 0.7, 0.7].into(),
tab_label_border_width: 1.0,
icon_color: Color::BLACK,
icon_background: Some(Background::Color(Color::TRANSPARENT)),
icon_border_radius: 4.0.into(),
text_color: Color::BLACK,
}
}
}
/// The Catalog of a [`TabBar`](crate::widget::tab_bar::TabBar).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let mut base = Style::default();
let palette = theme.extended_palette();
base.text_color = palette.background.base.text;
match status {
Status::Disabled => {
base.tab_label_background = Background::Color(palette.background.strong.color);
}
Status::Hovered => {
base.tab_label_background = Background::Color(palette.primary.strong.color);
}
_ => {
base.tab_label_background = Background::Color(palette.primary.base.color);
}
}
base
}
/// The dark theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn dark(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([0.1, 0.1, 0.1].into()),
tab_label_border_color: [0.3, 0.3, 0.3].into(),
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
}
base
}
/// The red theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn red(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([1.0, 0.0, 0.0].into()),
tab_label_border_color: Color::TRANSPARENT,
tab_label_border_width: 0.0,
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
base.icon_color = Color::BLACK;
base.text_color = Color::BLACK;
}
base
}
/// The blue theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn blue(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Background::Color([0.0, 0.0, 1.0].into()),
tab_label_border_color: [0.0, 0.0, 1.0].into(),
icon_color: Color::WHITE,
text_color: Color::WHITE,
..Default::default()
};
if status == Status::Disabled {
base.tab_label_background = Background::Color([0.5, 0.5, 1.0].into());
base.tab_label_border_color = [0.5, 0.5, 1.0].into();
}
base
}
/// The blue theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn green(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Color::WHITE.into(),
icon_color: [0.0, 0.5, 0.0].into(),
text_color: [0.0, 0.5, 0.0].into(),
..Default::default()
};
match status {
Status::Disabled => {
base.icon_color = [0.7, 0.7, 0.7].into();
base.text_color = [0.7, 0.7, 0.7].into();
base.tab_label_border_color = [0.7, 0.7, 0.7].into();
}
_ => {
base.tab_label_border_color = [0.0, 0.5, 0.0].into();
}
}
base
}
/// The purple theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn purple(_theme: &Theme, status: Status) -> Style {
let mut base = Style {
tab_label_background: Color::WHITE.into(),
tab_label_border_color: Color::TRANSPARENT,
icon_color: [0.7, 0.0, 1.0].into(),
text_color: [0.7, 0.0, 1.0].into(),
..Default::default()
};
if status == Status::Disabled {
base.icon_color = Color::BLACK;
base.text_color = Color::BLACK;
}
base
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn style_default() {
let style = Style::default();
assert!(style.background.is_none());
assert!(style.border_color.is_none());
assert_eq!(style.border_width, 0.0);
assert!(matches!(style.tab_label_background, Background::Color(_)));
assert_eq!(style.tab_label_border_width, 1.0);
assert_eq!(style.icon_color, Color::BLACK);
assert!(style.icon_background.is_some());
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn primary_changes_background_by_status() {
let theme = Theme::TokyoNight;
let active = primary(&theme, Status::Active);
let hovered = primary(&theme, Status::Hovered);
let disabled = primary(&theme, Status::Disabled);
// Different statuses should have different backgrounds
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", hovered.tab_label_background)
);
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", disabled.tab_label_background)
);
}
#[test]
fn dark_theme_active() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Active);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn dark_theme_disabled() {
let theme = Theme::TokyoNight;
let style = dark(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn dark_theme_changes_background_when_disabled() {
let theme = Theme::TokyoNight;
let active = dark(&theme, Status::Active);
let disabled = dark(&theme, Status::Disabled);
assert_ne!(
format!("{:?}", active.tab_label_background),
format!("{:?}", disabled.tab_label_background)
);
}
#[test]
fn red_theme_active() {
let theme = Theme::TokyoNight;
let style = red(&theme, Status::Active);
assert_eq!(
style.tab_label_background,
Background::Color([1.0, 0.0, 0.0].into())
);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
assert_eq!(style.tab_label_border_width, 0.0);
}
#[test]
fn red_theme_disabled() {
let theme = Theme::TokyoNight;
let style = red(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::BLACK);
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn blue_theme_active() {
let theme = Theme::TokyoNight;
let style = blue(&theme, Status::Active);
assert_eq!(
style.tab_label_background,
Background::Color([0.0, 0.0, 1.0].into())
);
assert_eq!(style.icon_color, Color::WHITE);
assert_eq!(style.text_color, Color::WHITE);
}
#[test]
fn blue_theme_disabled() {
let theme = Theme::TokyoNight;
let style = blue(&theme, Status::Disabled);
assert_eq!(
style.tab_label_background,
Background::Color([0.5, 0.5, 1.0].into())
);
assert_eq!(style.tab_label_border_color, [0.5, 0.5, 1.0].into());
}
#[test]
fn green_theme_active() {
let theme = Theme::TokyoNight;
let style = green(&theme, Status::Active);
assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
assert_eq!(style.icon_color, [0.0, 0.5, 0.0].into());
assert_eq!(style.text_color, [0.0, 0.5, 0.0].into());
assert_eq!(style.tab_label_border_color, [0.0, 0.5, 0.0].into());
}
#[test]
fn green_theme_disabled() {
let theme = Theme::TokyoNight;
let style = green(&theme, Status::Disabled);
assert_eq!(style.icon_color, [0.7, 0.7, 0.7].into());
assert_eq!(style.text_color, [0.7, 0.7, 0.7].into());
assert_eq!(style.tab_label_border_color, [0.7, 0.7, 0.7].into());
}
#[test]
fn purple_theme_active() {
let theme = Theme::TokyoNight;
let style = purple(&theme, Status::Active);
assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
assert_eq!(style.icon_color, [0.7, 0.0, 1.0].into());
assert_eq!(style.text_color, [0.7, 0.0, 1.0].into());
assert_eq!(style.tab_label_border_color, Color::TRANSPARENT);
}
#[test]
fn purple_theme_disabled() {
let theme = Theme::TokyoNight;
let style = purple(&theme, Status::Disabled);
assert_eq!(style.icon_color, Color::BLACK);
assert_eq!(style.text_color, Color::BLACK);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.tab_label_background, Background::Color(_)));
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active = theme.style(&class, Status::Active);
let hovered = theme.style(&class, Status::Hovered);
let disabled = theme.style(&class, Status::Disabled);
assert!(matches!(active.tab_label_background, Background::Color(_)));
assert!(matches!(hovered.tab_label_background, Background::Color(_)));
assert!(matches!(
disabled.tab_label_background,
Background::Color(_)
));
}
#[test]
fn icon_border_radius_is_set() {
let style = Style::default();
// Icon border radius should be set to 4.0 (all corners)
assert_eq!(style.icon_border_radius.top_left, 4.0);
assert_eq!(style.icon_border_radius.top_right, 4.0);
assert_eq!(style.icon_border_radius.bottom_left, 4.0);
assert_eq!(style.icon_border_radius.bottom_right, 4.0);
}
#[test]
fn different_themes_produce_different_styles() {
let theme = Theme::TokyoNight;
let status = Status::Active;
let _primary_style = primary(&theme, status);
let dark_style = dark(&theme, status);
let red_style = red(&theme, status);
let blue_style = blue(&theme, status);
let green_style = green(&theme, status);
let purple_style = purple(&theme, status);
// All themes should produce different background styles
assert_ne!(
format!("{:?}", dark_style.tab_label_background),
format!("{:?}", red_style.tab_label_background)
);
assert_ne!(
format!("{:?}", red_style.tab_label_background),
format!("{:?}", blue_style.tab_label_background)
);
assert_ne!(
format!("{:?}", blue_style.tab_label_background),
format!("{:?}", green_style.tab_label_background)
);
// Text colors also differ between some themes
assert_ne!(green_style.text_color, purple_style.text_color);
}
}
+317
View File
@@ -0,0 +1,317 @@
//! Use a time picker as an input element for picking times.
//!
//! *This API requires the following crate features to be activated: `time_picker`*
#![allow(clippy::doc_markdown)]
use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme};
/// The style of a [`TimePicker`](crate::widget::TimePicker).
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// The background of the [`TimePicker`](crate::widget::TimePicker).
pub background: Background,
/// The border radius of the [`TimePicker`](crate::widget::TimePicker).
pub border_radius: f32,
/// The border width of the [`TimePicker`](crate::widget::TimePicker).
pub border_width: f32,
/// The border color of the [`TimePicker`](crate::widget::TimePicker).
pub border_color: Color,
/// The text color of the [`TimePicker`](crate::widget::TimePicker).
pub text_color: Color,
/// The color of the clock numbers of the
/// [`TimePicker`](crate::widget::TimePicker).
pub clock_number_color: Color,
/// The background of the clock numbers of the
/// [`TimePicker`](crate::widget::TimePicker).
pub clock_number_background: Color,
/// The color of the dots on the clock of the
/// [`TimePicker`](crate::widget::TimePicker).
pub clock_dots_color: Color,
/// The color of the hands of the clock of the
/// [`TimePicker`](crate::widget::TimePicker).
pub clock_hand_color: Color,
/// The with of the hands of the clock of the
/// [`TimePicker](crate::widget::TimePicker).
pub clock_hand_width: f32,
}
/// The Catalog of a [`TimePicker`](crate::widget::TimePicker).
pub trait Catalog {
///Style for the trait to use.
type Class<'a>;
/// The default class produced by the [`Catalog`].
fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self, Style>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
/// The primary theme of a [`TimePicker`](crate::widget::TimePicker).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette();
let foreground = theme.palette();
let base = Style {
background: palette.background.base.color.into(),
border_radius: 15.0,
border_width: 1.0,
border_color: foreground.text,
text_color: foreground.text,
clock_number_color: foreground.text,
clock_number_background: palette.background.base.color,
clock_dots_color: [0.87, 0.87, 0.87].into(),
clock_hand_color: [0.87, 0.87, 0.87].into(),
clock_hand_width: 3.0,
};
match status {
Status::Focused => Style {
border_color: Color::from_rgb(0.5, 0.5, 0.5),
..base
},
Status::Hovered => Style {
clock_number_color: palette.primary.weak.text,
clock_number_background: palette.primary.weak.color,
..base
},
Status::Selected => Style {
clock_number_color: palette.primary.strong.text,
clock_number_background: palette.primary.strong.color,
..base
},
_ => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_core::{Background, Theme};
#[test]
fn primary_theme_active() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
assert_eq!(style.clock_hand_width, 3.0);
}
#[test]
fn primary_theme_focused() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Focused);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_color, Color::from_rgb(0.5, 0.5, 0.5));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_hovered() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Hovered);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_selected() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Selected);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn primary_theme_disabled() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Disabled);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn focused_changes_border_color() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let focused_style = primary(&theme, Status::Focused);
// Border color should change when focused
assert_ne!(base_style.border_color, focused_style.border_color);
assert_eq!(focused_style.border_color, Color::from_rgb(0.5, 0.5, 0.5));
// Other properties should remain the same
assert_eq!(base_style.background, focused_style.background);
assert_eq!(base_style.border_radius, focused_style.border_radius);
assert_eq!(base_style.border_width, focused_style.border_width);
assert_eq!(base_style.text_color, focused_style.text_color);
}
#[test]
fn hovered_changes_clock_number_styling() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let hovered_style = primary(&theme, Status::Hovered);
// Clock number color and background should change when hovered
assert_ne!(
base_style.clock_number_color,
hovered_style.clock_number_color
);
assert_ne!(
base_style.clock_number_background,
hovered_style.clock_number_background
);
// Other properties should remain the same
assert_eq!(base_style.background, hovered_style.background);
assert_eq!(base_style.border_color, hovered_style.border_color);
assert_eq!(base_style.text_color, hovered_style.text_color);
}
#[test]
fn selected_changes_clock_number_styling() {
let theme = Theme::TokyoNight;
let base_style = primary(&theme, Status::Active);
let selected_style = primary(&theme, Status::Selected);
// Clock number color and background should change when selected
assert_ne!(
base_style.clock_number_color,
selected_style.clock_number_color
);
assert_ne!(
base_style.clock_number_background,
selected_style.clock_number_background
);
// Other properties should remain the same
assert_eq!(base_style.background, selected_style.background);
assert_eq!(base_style.border_color, selected_style.border_color);
assert_eq!(base_style.text_color, selected_style.text_color);
}
#[test]
fn selected_and_hovered_have_different_styles() {
let theme = Theme::TokyoNight;
let hovered_style = primary(&theme, Status::Hovered);
let selected_style = primary(&theme, Status::Selected);
// Hovered and selected should use different colors
assert_ne!(
hovered_style.clock_number_color,
selected_style.clock_number_color
);
assert_ne!(
hovered_style.clock_number_background,
selected_style.clock_number_background
);
}
#[test]
fn clock_components_have_valid_styling() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Verify clock components have valid colors
assert_eq!(style.clock_dots_color, [0.87, 0.87, 0.87].into());
assert_eq!(style.clock_hand_color, [0.87, 0.87, 0.87].into());
assert_eq!(style.clock_hand_width, 3.0);
}
#[test]
fn non_interactive_statuses_use_base_style() {
let theme = Theme::TokyoNight;
let active_style = primary(&theme, Status::Active);
let disabled_style = primary(&theme, Status::Disabled);
// Non-interactive statuses should have the same style
assert_eq!(active_style.background, disabled_style.background);
assert_eq!(active_style.border_color, disabled_style.border_color);
assert_eq!(active_style.text_color, disabled_style.text_color);
assert_eq!(
active_style.clock_number_color,
disabled_style.clock_number_color
);
assert_eq!(
active_style.clock_number_background,
disabled_style.clock_number_background
);
}
#[test]
fn catalog_default_class() {
let _class = <Theme as Catalog>::default();
}
#[test]
fn catalog_style() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let style = theme.style(&class, Status::Active);
assert!(matches!(style.background, Background::Color(_)));
assert_eq!(style.border_radius, 15.0);
assert_eq!(style.border_width, 1.0);
}
#[test]
fn catalog_style_with_different_statuses() {
let theme = Theme::TokyoNight;
let class = <Theme as Catalog>::default();
let active_style = theme.style(&class, Status::Active);
let focused_style = theme.style(&class, Status::Focused);
let hovered_style = theme.style(&class, Status::Hovered);
let selected_style = theme.style(&class, Status::Selected);
assert!(matches!(active_style.background, Background::Color(_)));
assert!(matches!(focused_style.background, Background::Color(_)));
assert!(matches!(hovered_style.background, Background::Color(_)));
assert!(matches!(selected_style.background, Background::Color(_)));
}
#[test]
fn style_fields_are_valid() {
let theme = Theme::TokyoNight;
let style = primary(&theme, Status::Active);
// Verify all fields have valid values
assert!(style.border_radius > 0.0);
assert!(style.border_width > 0.0);
assert!(style.clock_hand_width > 0.0);
}
}