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, 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,
}
#[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 ThemeData {
pub background: Color,
pub foreground: Color,
pub primary: Color,
pub accent: Color,
pub surface: Color,
pub error: Color,
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 text: TextTheme,
pub button: ButtonStyle,
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::idea_dark()
}
}
impl ThemeData {
pub fn idea_dark() -> Self {
let bg = Color::from_rgb8(43, 45, 48); let fg = Color::from_rgb8(223, 225, 229); let primary = Color::from_rgb8(84, 138, 247); let accent = Color::from_rgb8(242, 197, 92); let surface = Color::from_rgb8(57, 59, 64); let error = Color::from_rgb8(229, 87, 101); let border = Color::from_rgb8(78, 81, 87); let caption = Color::from_rgb8(157, 160, 168); let text = TextTheme {
headline: TextStyleTheme { font_size: 20.0, color: fg },
title: TextStyleTheme { font_size: 15.0, color: fg },
body: TextStyleTheme { font_size: 13.0, color: fg },
caption: TextStyleTheme { font_size: 12.0, color: caption },
};
let button = ButtonStyle {
background: primary,
foreground: Color::WHITE,
radius: 4.0,
focus_ring_color: primary,
focus_ring_width: 2.0,
};
let scrollbar = ScrollbarStyle {
thumb_color: Color::rgba(0.55, 0.58, 0.63, 0.60),
thumb_hover_color: Color::rgba(0.72, 0.75, 0.80, 0.90),
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: Color::from_rgb8(67, 69, 74), border,
focused_border: primary,
text_color: fg,
placeholder_color: Color::from_rgb8(126, 131, 140), radius: 4.0,
};
let tooltip = TooltipStyle { background: surface, foreground: fg, border, radius: 4.0 };
let menu = MenuStyle {
background: surface,
foreground: fg,
hover_background: Color::from_rgb8(74, 77, 82), border,
radius: 6.0,
};
let tab = TabStyle {
background: bg,
active_foreground: fg,
inactive_foreground: caption,
hover_background: Color::from_rgb8(62, 65, 70), indicator_color: primary,
};
let nav = NavStyle {
active_background: Color::rgba(0.33, 0.54, 0.97, 0.18),
active_foreground: primary,
foreground: fg,
hover_background: Color::from_rgb8(74, 77, 82),
indicator_color: primary,
};
let feedback = FeedbackStyle {
progress_track: Color::rgba(1.0, 1.0, 1.0, 0.15),
progress_value: primary,
success: Color::from_rgb8(54, 150, 86), warning: accent,
};
let dropdown = DropdownStyle {
background: surface,
foreground: fg,
placeholder: Color::from_rgb8(126, 131, 140), border,
open_border: primary,
hover: Color::from_rgb8(74, 77, 82), selected_background: Color::rgba(0.33, 0.54, 0.97, 0.22),
disabled: Color::rgba(1.0, 1.0, 1.0, 0.35),
};
let dialog = DialogStyle { barrier: Color::rgba(0.0, 0.0, 0.0, 0.45), background: surface, radius: 8.0 };
let list = ListStyle {
hover_background: Color::rgba(1.0, 1.0, 1.0, 0.06),
selected_background: Color::rgba(0.33, 0.54, 0.97, 0.22),
foreground: fg,
};
Self {
background: bg,
foreground: fg,
primary,
accent,
surface,
error,
border,
divider: Color::from_rgb8(62, 65, 70), hover: Color::rgba(1.0, 1.0, 1.0, 0.08),
disabled: Color::rgba(1.0, 1.0, 1.0, 0.35),
selection: Color::rgba(0.33, 0.54, 0.97, 0.28),
icon: fg,
radius_sm: 2.0,
radius_md: 4.0,
radius_lg: 8.0,
spacing: 8.0,
text,
button,
scrollbar,
input,
tooltip,
menu,
tab,
nav,
feedback,
dropdown,
dialog,
list,
}
}
pub fn idea_light() -> Self {
let bg = Color::from_rgb8(255, 255, 255); let fg = Color::from_rgb8(31, 31, 31); let primary = Color::from_rgb8(53, 116, 240); let accent = Color::from_rgb8(232, 163, 61); let surface = Color::from_rgb8(247, 248, 250); let error = Color::from_rgb8(229, 87, 101); let border = Color::from_rgb8(201, 204, 214); let caption = Color::from_rgb8(140, 140, 140); let text = TextTheme {
headline: TextStyleTheme { font_size: 20.0, color: fg },
title: TextStyleTheme { font_size: 15.0, color: fg },
body: TextStyleTheme { font_size: 13.0, color: fg },
caption: TextStyleTheme { font_size: 12.0, color: caption },
};
let button = ButtonStyle {
background: primary,
foreground: Color::WHITE,
radius: 4.0,
focus_ring_color: primary,
focus_ring_width: 2.0,
};
let scrollbar = ScrollbarStyle {
thumb_color: Color::rgba(0.5, 0.55, 0.65, 0.55),
thumb_hover_color: Color::rgba(0.35, 0.40, 0.50, 0.80),
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: Color::WHITE,
border,
focused_border: primary,
text_color: fg,
placeholder_color: Color::from_rgb8(154, 160, 166), radius: 4.0,
};
let tooltip = TooltipStyle { background: Color::from_rgb8(250, 251, 252), foreground: fg, border, radius: 4.0 };
let menu = MenuStyle {
background: Color::WHITE,
foreground: fg,
hover_background: Color::from_rgb8(235, 239, 245), border,
radius: 6.0,
};
let tab = TabStyle {
background: bg,
active_foreground: fg,
inactive_foreground: caption,
hover_background: Color::from_rgb8(240, 242, 246), indicator_color: primary,
};
let nav = NavStyle {
active_background: Color::rgba(0.21, 0.46, 0.94, 0.12),
active_foreground: primary,
foreground: fg,
hover_background: Color::from_rgb8(240, 242, 246),
indicator_color: primary,
};
let feedback = FeedbackStyle {
progress_track: Color::rgba(0.0, 0.0, 0.0, 0.10),
progress_value: primary,
success: Color::from_rgb8(54, 150, 86),
warning: Color::from_rgb8(237, 162, 0), };
let dropdown = DropdownStyle {
background: Color::WHITE,
foreground: fg,
placeholder: Color::from_rgb8(154, 160, 166), border,
open_border: primary,
hover: Color::from_rgb8(235, 239, 245), selected_background: Color::rgba(0.21, 0.46, 0.94, 0.12),
disabled: Color::rgba(0.0, 0.0, 0.0, 0.35),
};
let dialog = DialogStyle { barrier: Color::rgba(0.0, 0.0, 0.0, 0.45), background: Color::WHITE, radius: 8.0 };
let list = ListStyle {
hover_background: Color::rgba(0.0, 0.0, 0.0, 0.04),
selected_background: Color::rgba(0.21, 0.46, 0.94, 0.12),
foreground: fg,
};
Self {
background: bg,
foreground: fg,
primary,
accent,
surface,
error,
border,
divider: Color::from_rgb8(213, 216, 222), hover: Color::rgba(0.0, 0.0, 0.0, 0.05),
disabled: Color::rgba(0.0, 0.0, 0.0, 0.35),
selection: Color::rgba(0.21, 0.46, 0.94, 0.18),
icon: fg,
radius_sm: 2.0,
radius_md: 4.0,
radius_lg: 8.0,
spacing: 8.0,
text,
button,
scrollbar,
input,
tooltip,
menu,
tab,
nav,
feedback,
dropdown,
dialog,
list,
}
}
pub fn dark() -> Self {
Self::idea_dark()
}
pub fn light() -> Self {
Self::idea_light()
}
}
#[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()
}
}