use crate::inherited::InheritedWidget;
use crate::widget::{Widget, WidgetNode};
use crate::{Color, Point, Size};
use std::any::Any;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq)]
pub struct TextTheme {
pub headline: TextStyleTheme,
pub title: TextStyleTheme,
pub body: TextStyleTheme,
pub caption: TextStyleTheme,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextStyleTheme {
pub font_size: f32,
pub color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonStyle {
pub background: Color,
pub foreground: Color,
pub radius: f32,
pub focus_ring_color: Color,
pub focus_ring_width: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonVariant {
Primary,
Normal,
Tertiary,
Success,
Warning,
Error,
Info,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonVariantStyle {
pub background: Color,
pub foreground: Color,
pub border: Color,
pub hover_background: Color,
pub pressed_background: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ButtonVariants {
pub primary: ButtonVariantStyle,
pub normal: ButtonVariantStyle,
pub tertiary: ButtonVariantStyle,
pub success: ButtonVariantStyle,
pub warning: ButtonVariantStyle,
pub error: ButtonVariantStyle,
pub info: ButtonVariantStyle,
}
impl ButtonVariants {
pub fn get(&self, variant: ButtonVariant) -> &ButtonVariantStyle {
match variant {
ButtonVariant::Primary => &self.primary,
ButtonVariant::Normal => &self.normal,
ButtonVariant::Tertiary => &self.tertiary,
ButtonVariant::Success => &self.success,
ButtonVariant::Warning => &self.warning,
ButtonVariant::Error => &self.error,
ButtonVariant::Info => &self.info,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct AvatarStyle {
pub palette: [Color; 6],
pub foreground: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ControlStyle {
pub background: Color,
pub border: Color,
pub checked_background: Color,
pub checked_foreground: Color,
pub disabled_background: Color,
pub disabled_border: Color,
pub toggle_off: Color,
pub slider_track: Color,
pub slider_range_active: Color,
}
fn mix(a: Color, b: Color, t: f32) -> Color {
Color { r: a.r + (b.r - a.r) * t, g: a.g + (b.g - a.g) * t, b: a.b + (b.b - a.b) * t, a: a.a + (b.a - a.a) * t }
}
#[derive(Debug, Clone, PartialEq)]
pub struct ScrollbarStyle {
pub thumb_color: Color,
pub thumb_hover_color: Color,
pub thumb_radius: f32,
pub thumb_width: f32, pub hit_width: f32, pub thumb_min_size: f32, pub thumb_track_ratio: f32, }
#[derive(Debug, Clone, PartialEq)]
pub struct InputStyle {
pub background: Color,
pub border: Color,
pub focused_border: Color,
pub text_color: Color,
pub placeholder_color: Color,
pub radius: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TooltipStyle {
pub background: Color,
pub foreground: Color,
pub border: Color,
pub radius: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MenuStyle {
pub background: Color,
pub foreground: Color,
pub hover_background: Color,
pub border: Color,
pub radius: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TabStyle {
pub background: Color,
pub active_foreground: Color,
pub inactive_foreground: Color,
pub hover_background: Color,
pub indicator_color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NavStyle {
pub active_background: Color,
pub active_foreground: Color,
pub foreground: Color,
pub hover_background: Color,
pub indicator_color: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct FeedbackStyle {
pub progress_track: Color,
pub progress_value: Color,
pub success: Color,
pub warning: Color,
pub info: Color,
pub error: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DropdownStyle {
pub background: Color,
pub foreground: Color,
pub placeholder: Color,
pub border: Color,
pub open_border: Color,
pub hover: Color,
pub selected_background: Color,
pub disabled: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DialogStyle {
pub barrier: Color,
pub background: Color,
pub radius: f32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ListStyle {
pub hover_background: Color,
pub selected_background: Color,
pub foreground: Color,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SpacingScale {
pub xxs: f32,
pub xs: f32,
pub s: f32,
pub m: f32,
pub l: f32,
pub xl: f32,
pub control_padding: f32,
pub container_padding: f32,
pub container_gap: f32,
}
impl Default for SpacingScale {
fn default() -> Self {
Self {
xxs: 2.0,
xs: 4.0,
s: 8.0,
m: 16.0,
l: 24.0,
xl: 32.0,
control_padding: 8.0,
container_padding: 16.0,
container_gap: 8.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ThemeData {
pub background: Color,
pub foreground: Color,
pub primary: Color,
pub accent: Color,
pub surface: Color,
pub error: Color,
pub high_contrast: bool,
pub border: Color, pub divider: Color, pub hover: Color, pub disabled: Color, pub selection: Color, pub icon: Color,
pub radius_sm: f32, pub radius_md: f32, pub radius_lg: f32,
pub spacing: f32, pub space: SpacingScale,
pub text: TextTheme,
pub button: ButtonStyle,
pub button_variants: ButtonVariants,
pub avatar: AvatarStyle,
pub control: ControlStyle,
pub scrollbar: ScrollbarStyle,
pub input: InputStyle,
pub tooltip: TooltipStyle,
pub menu: MenuStyle,
pub tab: TabStyle,
pub nav: NavStyle,
pub feedback: FeedbackStyle,
pub dropdown: DropdownStyle,
pub dialog: DialogStyle,
pub list: ListStyle,
}
impl Default for ThemeData {
fn default() -> Self {
Self::cloudscape_dark()
}
}
impl ThemeData {
pub fn dark() -> Self {
Self::cloudscape_dark()
}
pub fn light() -> Self {
Self::cloudscape_light()
}
pub fn cloudscape_light() -> Self {
Self::cloudscape(false)
}
pub fn cloudscape_dark() -> Self {
Self::cloudscape(true)
}
pub fn cloudscape(dark: bool) -> Self {
let (bg, surface, fg, caption, primary, accent, error) = if dark {
(
Color::from_rgb8(0x0F, 0x14, 0x1A), Color::from_rgb8(0x16, 0x1D, 0x26), Color::from_rgb8(0xC6, 0xC6, 0xCD), Color::from_rgb8(0xA4, 0xA4, 0xAD), Color::from_rgb8(0x42, 0xB4, 0xFF), Color::from_rgb8(0xFB, 0xD3, 0x32), Color::from_rgb8(0xFF, 0x7A, 0x7A), )
} else {
(
Color::from_rgb8(0xF3, 0xF3, 0xF7), Color::from_rgb8(0xFF, 0xFF, 0xFF), Color::from_rgb8(0x0F, 0x14, 0x1A), Color::from_rgb8(0x42, 0x46, 0x50), Color::from_rgb8(0x00, 0x6C, 0xE0), Color::from_rgb8(0x85, 0x59, 0x00), Color::from_rgb8(0xDB, 0x00, 0x00), )
};
let (border, divider, input_border, disabled_token) = if dark {
(
Color::from_rgb8(0x42, 0x46, 0x50),
Color::from_rgb8(0x42, 0x46, 0x50), Color::from_rgb8(0x65, 0x68, 0x71), Color::from_rgb8(0x65, 0x68, 0x71), )
} else {
(
Color::from_rgb8(0xC6, 0xC6, 0xCD),
Color::from_rgb8(0xC6, 0xC6, 0xCD), Color::from_rgb8(0x8C, 0x8C, 0x94), Color::from_rgb8(0xB4, 0xB4, 0xBB), )
};
let (hover_bg, hover_overlay, selected_bg, scrollbar_thumb, scrollbar_hover, menu_hover, nav_hover) = if dark {
(
Color::from_rgb8(0x1B, 0x23, 0x2D), Color::rgba(1.0, 1.0, 1.0, 0.06),
Color::from_rgb8(0x00, 0x11, 0x29), Color::rgba(0.396, 0.408, 0.443, 0.70), Color::rgba(0.549, 0.549, 0.580, 0.90), Color::from_rgb8(0x1B, 0x23, 0x2D),
Color::from_rgb8(0x1B, 0x23, 0x2D),
)
} else {
(
Color::from_rgb8(0xF3, 0xF3, 0xF7), Color::rgba(0.059, 0.078, 0.102, 0.05),
Color::from_rgb8(0xF0, 0xFB, 0xFF), Color::rgba(0.549, 0.549, 0.565, 0.55), Color::rgba(0.259, 0.275, 0.314, 0.85), Color::from_rgb8(0xF3, 0xF3, 0xF7),
Color::from_rgb8(0xF3, 0xF3, 0xF7),
)
};
let (placeholder, tooltip_bg, tooltip_fg, tooltip_border) = if dark {
(
Color::from_rgb8(0xA4, 0xA4, 0xAD), Color::from_rgb8(0x33, 0x38, 0x43),
Color::from_rgb8(0xDE, 0xDE, 0xE3),
Color::from_rgb8(0x42, 0x46, 0x50),
)
} else {
(
Color::from_rgb8(0x65, 0x68, 0x71), Color::from_rgb8(0x0F, 0x14, 0x1A),
Color::WHITE,
Color::from_rgb8(0x0F, 0x14, 0x1A),
)
};
let (success, warning, track) = if dark {
(
Color::from_rgb8(0x2B, 0xB5, 0x34), Color::from_rgb8(0xFB, 0xD3, 0x32), Color::rgba(1.0, 1.0, 1.0, 0.12),
)
} else {
(
Color::from_rgb8(0x00, 0x80, 0x2F), Color::from_rgb8(0x85, 0x59, 0x00), Color::rgba(0.0, 0.0, 0.0, 0.08),
)
};
let (button_bg, button_fg) = if dark {
(primary, Color::from_rgb8(0x0F, 0x14, 0x1A)) } else {
(primary, Color::WHITE)
};
let text = TextTheme {
headline: TextStyleTheme { font_size: 24.0, color: fg },
title: TextStyleTheme { font_size: 18.0, color: fg },
body: TextStyleTheme { font_size: 14.0, color: fg },
caption: TextStyleTheme { font_size: 12.0, color: caption },
};
let button = ButtonStyle {
background: button_bg,
foreground: button_fg,
radius: 20.0, focus_ring_color: primary,
focus_ring_width: 2.0,
};
let status_variant = |base: Color| ButtonVariantStyle {
background: base,
foreground: button_fg,
border: Color::TRANSPARENT,
hover_background: if dark { mix(base, Color::WHITE, 0.15) } else { mix(base, Color::BLACK, 0.12) },
pressed_background: if dark { mix(base, Color::WHITE, 0.28) } else { mix(base, Color::BLACK, 0.22) },
};
let button_variants = ButtonVariants {
primary: ButtonVariantStyle {
background: primary,
foreground: button_fg,
border: Color::TRANSPARENT,
hover_background: if dark {
mix(primary, Color::WHITE, 0.20)
} else {
Color::from_rgb8(0x00, 0x2B, 0x66)
},
pressed_background: if dark {
mix(primary, Color::WHITE, 0.30)
} else {
Color::from_rgb8(0x00, 0x2B, 0x66)
},
},
normal: ButtonVariantStyle {
background: surface,
foreground: primary,
border: primary,
hover_background: if dark {
Color::from_rgb8(0x1B, 0x23, 0x2D)
} else {
Color::from_rgb8(0xF0, 0xFB, 0xFF)
},
pressed_background: if dark {
Color::from_rgb8(0x33, 0x38, 0x43)
} else {
Color::from_rgb8(0xD1, 0xF1, 0xFF)
},
},
tertiary: ButtonVariantStyle {
background: Color::TRANSPARENT,
foreground: fg,
border: Color::TRANSPARENT,
hover_background: hover_bg,
pressed_background: if dark {
Color::from_rgb8(0x33, 0x38, 0x43)
} else {
Color::from_rgb8(0xDE, 0xDE, 0xE3)
},
},
success: status_variant(success),
warning: status_variant(warning),
error: status_variant(error),
info: status_variant(primary),
};
let avatar = AvatarStyle {
palette: [
Color::from_rgb8(0x00, 0x6C, 0xE0),
Color::from_rgb8(0x00, 0x80, 0x2F),
Color::from_rgb8(0xC2, 0x5A, 0x00),
Color::from_rgb8(0x7B, 0x3F, 0xBF),
Color::from_rgb8(0xDB, 0x00, 0x00),
Color::from_rgb8(0x00, 0x7E, 0x87),
],
foreground: Color::WHITE,
};
let control = if dark {
ControlStyle {
background: Color::from_rgb8(0x16, 0x1D, 0x26),
border: Color::from_rgb8(0x8C, 0x8C, 0x94),
checked_background: Color::from_rgb8(0x00, 0x6C, 0xE0),
checked_foreground: Color::WHITE,
disabled_background: Color::from_rgb8(0x33, 0x38, 0x43),
disabled_border: Color::from_rgb8(0x65, 0x68, 0x71),
toggle_off: Color::from_rgb8(0x8C, 0x8C, 0x94),
slider_track: Color::from_rgb8(0x65, 0x68, 0x71),
slider_range_active: Color::from_rgb8(0x75, 0xCF, 0xFF),
}
} else {
ControlStyle {
background: Color::WHITE,
border: Color::from_rgb8(0x8C, 0x8C, 0x94),
checked_background: primary,
checked_foreground: Color::WHITE,
disabled_background: Color::from_rgb8(0xDE, 0xDE, 0xE3),
disabled_border: Color::from_rgb8(0xB4, 0xB4, 0xBB),
toggle_off: Color::from_rgb8(0x42, 0x46, 0x50),
slider_track: Color::from_rgb8(0x8C, 0x8C, 0x94),
slider_range_active: Color::from_rgb8(0x00, 0x4A, 0x9E),
}
};
let scrollbar = ScrollbarStyle {
thumb_color: scrollbar_thumb,
thumb_hover_color: scrollbar_hover,
thumb_radius: 4.0,
thumb_width: 8.0,
hit_width: 10.0,
thumb_min_size: 24.0,
thumb_track_ratio: 0.32,
};
let input = InputStyle {
background: surface,
border: input_border,
focused_border: primary,
text_color: fg,
placeholder_color: placeholder,
radius: 8.0, };
let tooltip =
TooltipStyle { background: tooltip_bg, foreground: tooltip_fg, border: tooltip_border, radius: 8.0 };
let menu =
MenuStyle { background: surface, foreground: fg, hover_background: menu_hover, border, radius: 16.0 };
let tab = TabStyle {
background: surface,
active_foreground: fg,
inactive_foreground: caption,
hover_background: hover_bg,
indicator_color: primary,
};
let nav = NavStyle {
active_background: selected_bg,
active_foreground: primary,
foreground: fg,
hover_background: nav_hover,
indicator_color: primary,
};
let feedback =
FeedbackStyle { progress_track: track, progress_value: primary, success, warning, info: primary, error };
let dropdown = DropdownStyle {
background: surface,
foreground: fg,
placeholder,
border: input_border,
open_border: primary,
hover: hover_bg,
selected_background: selected_bg,
disabled: disabled_token,
};
let dialog = DialogStyle {
barrier: if dark { Color::rgba(0.0, 0.0, 0.0, 0.60) } else { Color::rgba(0.0, 0.0, 0.0, 0.45) },
background: surface,
radius: 16.0, };
let list = ListStyle { hover_background: hover_bg, selected_background: selected_bg, foreground: fg };
Self {
background: bg,
foreground: fg,
primary,
accent,
surface,
error,
high_contrast: false,
border,
divider,
hover: hover_overlay,
disabled: disabled_token,
selection: selected_bg,
icon: fg,
button_variants,
avatar,
control,
radius_sm: 4.0, radius_md: 8.0, radius_lg: 16.0, spacing: 8.0,
space: SpacingScale::default(),
text,
button,
scrollbar,
input,
tooltip,
menu,
tab,
nav,
feedback,
dropdown,
dialog,
list,
}
}
pub fn high_contrast(dark: bool) -> Self {
let mut theme = Self::cloudscape(dark);
let (bg, fg, dim, primary, on_primary, focus, accent, error, success, warning, hover, selection) = if dark {
(
Color::BLACK,
Color::WHITE,
Color::from_rgb8(0xB0, 0xB0, 0xB0),
Color::from_rgb8(0xFF, 0xD7, 0x00),
Color::BLACK,
Color::WHITE,
Color::from_rgb8(0x00, 0xFF, 0xFF),
Color::from_rgb8(0xFF, 0x80, 0x80),
Color::from_rgb8(0x7C, 0xFC, 0x7C),
Color::from_rgb8(0xFF, 0xD7, 0x00),
Color::rgba(1.0, 1.0, 1.0, 0.20),
Color::rgba(1.0, 0.843, 0.0, 0.35),
)
} else {
(
Color::WHITE,
Color::BLACK,
Color::from_rgb8(0x50, 0x50, 0x50),
Color::from_rgb8(0x00, 0x33, 0xCC),
Color::WHITE,
Color::BLACK,
Color::from_rgb8(0x00, 0x5A, 0x5A),
Color::from_rgb8(0xB0, 0x00, 0x20),
Color::from_rgb8(0x00, 0x64, 0x00),
Color::from_rgb8(0x7A, 0x4F, 0x00),
Color::rgba(0.0, 0.0, 0.0, 0.12),
Color::rgba(0.0, 0.2, 0.8, 0.18),
)
};
theme.high_contrast = true;
theme.background = bg;
theme.surface = bg;
theme.foreground = fg;
theme.primary = primary;
theme.accent = accent;
theme.error = error;
theme.border = fg;
theme.divider = fg;
theme.hover = hover;
theme.disabled = dim;
theme.selection = selection;
theme.icon = fg;
theme.radius_sm = 0.0;
theme.radius_md = 0.0;
theme.radius_lg = 0.0;
for style in [&mut theme.text.headline, &mut theme.text.title, &mut theme.text.body] {
style.color = fg;
}
theme.text.caption.color = dim;
theme.button.background = primary;
theme.button.foreground = on_primary;
theme.button.radius = 0.0;
theme.button.focus_ring_color = focus;
theme.button.focus_ring_width = 3.0;
theme.button_variants.primary = ButtonVariantStyle {
background: primary,
foreground: on_primary,
border: Color::TRANSPARENT,
hover_background: mix(primary, focus, 0.18),
pressed_background: mix(primary, bg, 0.20),
};
theme.button_variants.normal = ButtonVariantStyle {
background: bg,
foreground: primary,
border: primary,
hover_background: selection,
pressed_background: hover,
};
theme.button_variants.tertiary = ButtonVariantStyle {
background: Color::TRANSPARENT,
foreground: fg,
border: Color::TRANSPARENT,
hover_background: selection,
pressed_background: hover,
};
let status_variant = |fill: Color| ButtonVariantStyle {
background: fill,
foreground: on_primary,
border: Color::TRANSPARENT,
hover_background: mix(fill, focus, 0.15),
pressed_background: mix(fill, bg, 0.15),
};
theme.button_variants.success = status_variant(success);
theme.button_variants.warning = status_variant(warning);
theme.button_variants.error = status_variant(error);
theme.button_variants.info = status_variant(accent);
theme.avatar.palette = [
Color::from_rgb8(0x00, 0x50, 0x80),
Color::from_rgb8(0x00, 0x50, 0x00),
Color::from_rgb8(0x6B, 0x2A, 0x00),
Color::from_rgb8(0x3B, 0x00, 0x80),
Color::from_rgb8(0x90, 0x00, 0x00),
Color::from_rgb8(0x00, 0x4A, 0x4A),
];
theme.avatar.foreground = Color::WHITE;
theme.control.background = bg;
theme.control.border = fg;
theme.control.checked_background = primary;
theme.control.checked_foreground = on_primary;
theme.control.disabled_background = bg;
theme.control.disabled_border = dim;
theme.control.toggle_off = dim;
theme.control.slider_track = dim;
theme.control.slider_range_active = primary;
theme.scrollbar.thumb_color = dim;
theme.scrollbar.thumb_hover_color = primary;
theme.input.background = bg;
theme.input.border = fg;
theme.input.focused_border = primary;
theme.input.text_color = fg;
theme.input.placeholder_color = dim;
theme.input.radius = 0.0;
theme.tooltip.background = bg;
theme.tooltip.foreground = fg;
theme.tooltip.border = fg;
theme.tooltip.radius = 0.0;
theme.menu.background = bg;
theme.menu.foreground = fg;
theme.menu.hover_background = selection;
theme.menu.border = fg;
theme.menu.radius = 0.0;
theme.tab.background = bg;
theme.tab.active_foreground = fg;
theme.tab.inactive_foreground = dim;
theme.tab.hover_background = selection;
theme.tab.indicator_color = primary;
theme.nav.active_background = selection;
theme.nav.active_foreground = fg;
theme.nav.foreground = fg;
theme.nav.hover_background = selection;
theme.nav.indicator_color = primary;
theme.feedback.progress_track = dim;
theme.feedback.progress_value = primary;
theme.feedback.success = success;
theme.feedback.warning = warning;
theme.feedback.info = accent;
theme.feedback.error = error;
theme.dropdown.background = bg;
theme.dropdown.foreground = fg;
theme.dropdown.placeholder = dim;
theme.dropdown.border = fg;
theme.dropdown.open_border = primary;
theme.dropdown.hover = selection;
theme.dropdown.selected_background = selection;
theme.dropdown.disabled = dim;
theme.dialog.barrier = if dark { Color::rgba(0.0, 0.0, 0.0, 0.85) } else { Color::rgba(0.0, 0.0, 0.0, 0.60) };
theme.dialog.background = bg;
theme.dialog.radius = 0.0;
theme.list.hover_background = selection;
theme.list.selected_background = selection;
theme.list.foreground = fg;
theme
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Direction {
#[default]
Ltr,
Rtl,
}
pub const fn resolve_color(explicit: Option<Color>, themed: Color) -> Color {
match explicit {
Some(color) => color,
None => themed,
}
}
pub const fn resolve_f32(explicit: Option<f32>, themed: f32) -> f32 {
match explicit {
Some(value) => value,
None => themed,
}
}
#[derive(Debug)]
pub struct ThemeWidget {
pub theme: Rc<ThemeData>,
pub direction: Direction,
pub child: Option<WidgetNode>,
}
impl ThemeWidget {
pub fn new(theme: ThemeData) -> Self {
Self::from_rc(Rc::new(theme))
}
pub fn from_rc(theme: Rc<ThemeData>) -> Self {
Self { theme, direction: Direction::Ltr, child: None }
}
pub fn dark() -> Self {
Self::new(ThemeData::dark())
}
pub fn light() -> Self {
Self::new(ThemeData::light())
}
pub fn with_direction(mut self, d: Direction) -> Self {
self.direction = d;
self
}
pub fn child(mut self, c: impl Widget) -> Self {
self.child = Some(WidgetNode::new(c));
self
}
pub fn child_node(mut self, c: WidgetNode) -> Self {
self.child = Some(c);
self
}
}
impl InheritedWidget for ThemeWidget {
fn update_should_notify(&self, old: &dyn InheritedWidget) -> bool {
if let Some(old_theme) = old.as_any().downcast_ref::<ThemeWidget>() {
if Rc::ptr_eq(&self.theme, &old_theme.theme) {
return self.direction != old_theme.direction;
}
*self.theme != *old_theme.theme || self.direction != old_theme.direction
} else {
true
}
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl ThemeWidget {
pub fn theme(&self) -> &ThemeData {
&self.theme
}
pub fn direction(&self) -> Direction {
self.direction
}
}
impl Widget for ThemeWidget {
fn as_any(&self) -> &dyn Any {
self
}
fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
Some(self)
}
fn create_render_object(&self) -> Box<dyn crate::render_object::RenderObject> {
match &self.child {
Some(c) => Box::new(crate::render_object::ProxyRenderObject::new(c.0.create_render_object())),
None => Box::new(crate::render_object::RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
}
}
fn children(&self) -> Vec<WidgetNode> {
self.child.clone().into_iter().collect()
}
fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
self.child.clone().into_iter().map(|c| (Point::ZERO, c)).collect()
}
}
#[derive(Debug)]
pub struct DirectionWidget {
pub direction: Direction,
pub child: Option<WidgetNode>,
}
impl DirectionWidget {
pub fn new(d: Direction) -> Self {
Self { direction: d, child: None }
}
pub fn child(mut self, c: impl Widget) -> Self {
self.child = Some(WidgetNode::new(c));
self
}
}
impl InheritedWidget for DirectionWidget {
fn update_should_notify(&self, old: &dyn InheritedWidget) -> bool {
if let Some(old_dir) = old.as_any().downcast_ref::<DirectionWidget>() {
self.direction != old_dir.direction
} else {
true
}
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl DirectionWidget {
pub fn direction(&self) -> Direction {
self.direction
}
}
impl Widget for DirectionWidget {
fn as_any(&self) -> &dyn Any {
self
}
fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
Some(self)
}
fn create_render_object(&self) -> Box<dyn crate::render_object::RenderObject> {
match &self.child {
Some(c) => Box::new(crate::render_object::ProxyRenderObject::new(c.0.create_render_object())),
None => Box::new(crate::render_object::RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
}
}
fn children(&self) -> Vec<WidgetNode> {
self.child.clone().into_iter().collect()
}
fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
self.child.clone().into_iter().map(|c| (Point::ZERO, c)).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cloudscape_palette_matches_design_tokens() {
let light = ThemeData::cloudscape_light();
assert_eq!(light.primary, Color::from_rgb8(0x00, 0x6C, 0xE0));
assert_eq!(light.background, Color::from_rgb8(0xF3, 0xF3, 0xF7));
assert_eq!(light.surface, Color::WHITE);
assert_eq!(light.foreground, Color::from_rgb8(0x0F, 0x14, 0x1A));
assert_eq!(light.button.foreground, Color::WHITE);
assert_eq!(light.button.radius, 20.0, "Cloudscape 主按钮为胶囊形");
assert_eq!(light.input.radius, 8.0);
assert_eq!(light.dialog.radius, 16.0);
let dark = ThemeData::cloudscape_dark();
assert_eq!(dark.primary, Color::from_rgb8(0x42, 0xB4, 0xFF));
assert_eq!(dark.background, Color::from_rgb8(0x0F, 0x14, 0x1A));
assert_eq!(dark.surface, Color::from_rgb8(0x16, 0x1D, 0x26));
assert_eq!(dark.foreground, Color::from_rgb8(0xC6, 0xC6, 0xCD));
assert_eq!(dark.button.foreground, dark.background);
}
#[test]
fn cloudscape_selector_and_alias_consistency() {
assert_eq!(ThemeData::cloudscape(false), ThemeData::cloudscape_light());
assert_eq!(ThemeData::cloudscape(true), ThemeData::cloudscape_dark());
assert_ne!(ThemeData::cloudscape_light(), ThemeData::cloudscape_dark());
}
#[test]
fn high_contrast_palette_meets_wcag_and_uses_square_corners() {
fn channel(value: f32) -> f32 {
if value <= 0.04045 {
value / 12.92
} else {
((value + 0.055) / 1.055).powf(2.4)
}
}
fn luminance(color: Color) -> f32 {
0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b)
}
fn contrast(a: Color, b: Color) -> f32 {
let (la, lb) = (luminance(a), luminance(b));
(la.max(lb) + 0.05) / (la.min(lb) + 0.05)
}
for dark in [false, true] {
let theme = ThemeData::high_contrast(dark);
assert!(theme.high_contrast);
assert_eq!(theme.background, if dark { Color::BLACK } else { Color::WHITE });
assert!(contrast(theme.foreground, theme.background) >= 7.0, "正文对比度不足");
assert!(contrast(theme.primary, theme.background) >= 4.5, "主色对比度不足");
assert!(contrast(theme.border, theme.background) >= 3.0, "边框对比度不足");
assert!(contrast(theme.text.caption.color, theme.background) >= 4.5, "次要文字对比度不足");
assert!(contrast(theme.button.foreground, theme.button.background) >= 4.5, "主按钮文字对比度不足");
assert!(
contrast(theme.button_variants.normal.foreground, theme.button_variants.normal.background) >= 4.5,
"常规按钮文字对比度不足"
);
assert!(contrast(theme.input.text_color, theme.input.background) >= 7.0, "输入框文字对比度不足");
assert!(contrast(theme.tooltip.foreground, theme.tooltip.background) >= 7.0, "Tooltip 文字对比度不足");
assert_eq!(theme.radius_md, 0.0);
assert_eq!(theme.button.radius, 0.0);
assert_eq!(theme.dialog.radius, 0.0);
assert!(theme.button.focus_ring_width >= 3.0);
}
}
#[test]
fn cloudscape_focus_and_emphasis_follow_primary() {
for t in [ThemeData::cloudscape_light(), ThemeData::cloudscape_dark()] {
assert_eq!(t.button.focus_ring_color, t.primary);
assert_eq!(t.text.body.color, t.foreground);
assert_eq!(t.tab.indicator_color, t.primary);
assert_eq!(t.nav.indicator_color, t.primary);
assert_eq!(t.feedback.progress_value, t.primary);
assert_eq!(t.dropdown.open_border, t.primary);
assert_eq!(t.input.focused_border, t.primary);
}
}
#[test]
fn cloudscape_light_and_dark_have_opposite_luminance() {
let light = ThemeData::cloudscape_light();
let dark = ThemeData::cloudscape_dark();
let luma = |c: Color| c.r * 0.2126 + c.g * 0.7152 + c.b * 0.0722;
assert!(luma(light.background) > 0.8, "浅色画布应接近白色");
assert!(luma(dark.background) < 0.15, "深色画布应接近黑色");
assert!(luma(light.foreground) < 0.2, "浅色主题正文应为深色");
assert!(luma(dark.foreground) > 0.6, "深色主题正文应为浅色");
}
#[test]
fn cloudscape_button_variants_and_avatar_are_themed() {
let light = ThemeData::cloudscape_light();
assert_eq!(light.button_variants.primary.background, light.primary);
assert_eq!(light.button_variants.primary.foreground, Color::WHITE);
assert_eq!(light.button_variants.normal.border, light.primary);
assert_eq!(light.button_variants.normal.background, light.surface);
assert_eq!(light.button_variants.tertiary.background, Color::TRANSPARENT);
let dark = ThemeData::cloudscape_dark();
assert_eq!(dark.button_variants.primary.foreground, dark.background);
assert_eq!(dark.button_variants.normal.border, dark.primary);
assert_eq!(dark.avatar.foreground, Color::WHITE);
assert_ne!(dark.avatar.palette[0], dark.avatar.palette[1]);
assert_eq!(dark.button_variants.success.background, dark.feedback.success);
assert_eq!(dark.button_variants.warning.background, dark.feedback.warning);
assert_eq!(dark.button_variants.error.background, dark.feedback.error);
assert_eq!(dark.feedback.error, dark.error);
}
#[test]
fn theme_variants_follow_primary_and_status_tokens() {
let t = ThemeData::cloudscape_dark();
assert_eq!(t.button_variants.primary.background, t.primary);
assert_eq!(t.feedback.info, t.primary);
assert_eq!(t.feedback.error, t.error);
assert_eq!(t.avatar.foreground, Color::WHITE);
}
#[test]
fn spacing_scale_defaults() {
let s = ThemeData::cloudscape_dark().space;
assert_eq!((s.xxs, s.xs, s.s, s.m, s.l, s.xl), (2.0, 4.0, 8.0, 16.0, 24.0, 32.0));
assert_eq!(s.control_padding, 8.0, "控件默认内边距");
assert_eq!(s.container_padding, 16.0, "容器默认内边距");
assert_eq!(s.container_gap, 8.0, "容器默认子项间距");
}
}