use iced::widget::scrollable;
use iced::widget::text_input;
use iced::{Background, Border};
use oxiui_core::{Palette, Theme};
use oxiui_theme::{DesignTokens, RadiusStep, SpacingStep, TypographyScale};
pub fn palette_to_iced_theme(p: &Palette) -> iced::Theme {
let iced_palette = iced::theme::palette::Palette {
background: color_to_iced(&p.background),
text: color_to_iced(&p.text),
primary: color_to_iced(&p.primary),
success: color_to_iced(&p.muted),
warning: color_to_iced(&p.muted),
danger: color_to_iced(&p.surface),
};
iced::Theme::custom("OxiUI COOLJAPAN", iced_palette)
}
pub fn palette_to_iced_theme_ext(theme: &dyn Theme) -> iced::Theme {
palette_to_iced_theme(theme.palette())
}
pub(crate) fn color_to_iced(c: &oxiui_core::Color) -> iced::Color {
let oxiui_core::Color(r, g, b, a) = *c;
iced::Color::from_rgba8(r, g, b, a as f32 / 255.0)
}
pub fn text_input_style_from_palette(p: &Palette) -> text_input::Style {
let background_color = color_to_iced(&p.background);
let text_color = color_to_iced(&p.text);
let primary_color = color_to_iced(&p.primary);
let muted_color = color_to_iced(&p.muted);
let mut selection = primary_color;
selection.a = 0.4;
text_input::Style {
background: Background::Color(background_color),
border: Border {
color: primary_color,
width: 1.0,
radius: 2.0.into(),
},
icon: muted_color,
placeholder: muted_color,
value: text_color,
selection,
}
}
pub fn scrollable_style_from_palette(p: &Palette) -> scrollable::Style {
let primary_color = color_to_iced(&p.primary);
let surface_color = color_to_iced(&p.surface);
let rail = scrollable::Rail {
background: Some(Background::Color(surface_color)),
border: Border::default(),
scroller: scrollable::Scroller {
background: Background::Color(primary_color),
border: Border::default(),
},
};
scrollable::Style {
container: iced::widget::container::Style::default(),
vertical_rail: rail,
horizontal_rail: rail,
gap: None,
auto_scroll: scrollable::AutoScroll {
background: Background::Color(surface_color),
border: Border::default(),
shadow: iced::Shadow::default(),
icon: primary_color,
},
}
}
pub fn text_input_style_from_theme(theme: &dyn Theme) -> text_input::Style {
text_input_style_from_palette(theme.palette())
}
pub fn scrollable_style_from_theme(theme: &dyn Theme) -> scrollable::Style {
scrollable_style_from_palette(theme.palette())
}
pub fn palette_and_tokens_to_iced_theme(
palette: &Palette,
_tokens: Option<&DesignTokens>,
_typography: Option<&TypographyScale>,
) -> iced::Theme {
palette_to_iced_theme(palette)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DesignTokensAdapter {
pub border_radius: f32,
pub body_font_size: f32,
pub headline_font_size: f32,
pub base_spacing: f32,
}
impl DesignTokensAdapter {
pub fn from_tokens(tokens: &DesignTokens, typography: &TypographyScale) -> Self {
Self {
border_radius: tokens.radius(RadiusStep::Md),
body_font_size: typography.body.size,
headline_font_size: typography.headline.size,
base_spacing: tokens.spacing(SpacingStep::Sm),
}
}
pub fn body_text_size(&self) -> iced::Pixels {
iced::Pixels(self.body_font_size)
}
pub fn headline_text_size(&self) -> iced::Pixels {
iced::Pixels(self.headline_font_size)
}
pub fn standard_padding(&self) -> iced::Padding {
iced::Padding::from(self.base_spacing)
}
}