use abi_stable::{ StableAbi, std_types::{RBox, ROption, RString, RVec}, }; #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct Color { pub r: f32, pub g: f32, pub b: f32, pub a: f32, } impl Color { pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0); pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0); pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0); pub const fn rgb(r: f32, g: f32, b: f32) -> Self { Self { r, g, b, a: 1.0 } } pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self { Self { r, g, b, a } } } pub fn parse_hex(hex: &str) -> Option { let hex = hex.strip_prefix('#').unwrap_or(hex); let channels = match hex.len() { 3 | 6 => 3, 4 | 8 => 4, _ => return None, }; let step = if hex.len() <= 4 { 1 } else { 2 }; let mut nibbles = [0u8; 8]; for (i, byte) in hex.bytes().enumerate() { nibbles[i] = match byte { b'0'..=b'9' => byte - b'0', b'a'..=b'f' => byte - b'a' + 10, b'A'..=b'F' => byte - b'A' + 10, _ => return None, }; } let mut out = [0u8; 4]; out[3] = 255; for i in 0..channels { if step == 1 { out[i] = nibbles[i] * 16 + nibbles[i]; } else { out[i] = nibbles[2 * i] * 16 + nibbles[2 * i + 1]; } } Some(Color { r: out[0] as f32 / 255.0, g: out[1] as f32 / 255.0, b: out[2] as f32 / 255.0, a: out[3] as f32 / 255.0, }) } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct Border { pub width: f32, pub radius: f32, pub color: Color, } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct Style { pub background: ROption, pub border: ROption, pub text_color: ROption, } impl Style { pub fn bg(color: Color) -> Self { Self { background: ROption::RSome(color), border: ROption::RNone, text_color: ROption::RNone, } } pub fn with_border(mut self, width: f32, radius: f32, color: Color) -> Self { self.border = ROption::RSome(Border { width, radius, color, }); self } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct Padding { pub top: f32, pub right: f32, pub bottom: f32, pub left: f32, } impl Padding { pub const ZERO: Self = Self { top: 0.0, right: 0.0, bottom: 0.0, left: 0.0, }; pub const fn all(value: f32) -> Self { Self { top: value, right: value, bottom: value, left: value, } } pub const fn xy(x: f32, y: f32) -> Self { Self { top: y, right: x, bottom: y, left: x, } } pub fn to_array(&self) -> [f32; 4] { [self.top, self.right, self.bottom, self.left] } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Align { #[default] Start, Center, End, Fill, } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub enum Length { #[default] Shrink, Fill, Fixed(f32), } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub struct Theme { pub base: Color, pub crust: Color, pub mantle: Color, pub primary: Color, pub secondary: Color, pub green: Color, pub red: Color, pub blue: Color, pub yellow: Color, pub orange: Color, pub text: Color, pub subtext: Color, pub overlay: Color, } impl Default for Theme { fn default() -> Self { let rgb = |r: u8, g: u8, b: u8| Color::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0); Self { base: rgb(14, 28, 36), crust: rgb(6, 16, 22), mantle: rgb(10, 21, 28), primary: rgb(130, 182, 204), secondary: rgb(162, 211, 226), green: rgb(143, 214, 207), red: rgb(111, 168, 192), blue: rgb(143, 214, 234), yellow: rgb(159, 208, 224), orange: rgb(143, 198, 220), text: rgb(219, 234, 241), subtext: rgb(176, 205, 217), overlay: rgb(143, 179, 194), } } } impl Theme { pub fn color(&self, name: &str) -> Option { let color = match name { "base" => self.base, "crust" => self.crust, "mantle" => self.mantle, "primary" => self.primary, "secondary" => self.secondary, "green" => self.green, "red" => self.red, "blue" => self.blue, "yellow" => self.yellow, "orange" => self.orange, "text" => self.text, "subtext" => self.subtext, "overlay" => self.overlay, _ if let Some((name, value)) = name.split_once('/') => { let mut color = self.color(name)?; color.a = value.parse::().ok()?; return Some(color); } _ => return None, }; Some(color) } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct AnimationOffset { pub x: f32, pub y: f32, } impl AnimationOffset { pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } } impl From<(f32, f32)> for AnimationOffset { fn from((x, y): (f32, f32)) -> Self { Self { x, y } } } impl From for (f32, f32) { fn from(offset: AnimationOffset) -> Self { (offset.x, offset.y) } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug)] pub struct CustomAnimation { pub func: extern "C" fn(f32) -> AnimationOffset, } impl PartialEq for CustomAnimation { fn eq(&self, other: &Self) -> bool { std::ptr::fn_addr_eq(self.func, other.func) } } impl CustomAnimation { pub const fn new(func: extern "C" fn(f32) -> AnimationOffset) -> Self { Self { func } } pub fn offset(&self, progress: f32) -> AnimationOffset { (self.func)(progress) } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq, Eq)] pub enum AnimationEdge { Top, Bottom, Left, Right, } impl From for AnimationEdge { fn from(edge: icss::widgets::animation::Edge) -> Self { match edge { icss::widgets::animation::Edge::Top => AnimationEdge::Top, icss::widgets::animation::Edge::Bottom => AnimationEdge::Bottom, icss::widgets::animation::Edge::Left => AnimationEdge::Left, icss::widgets::animation::Edge::Right => AnimationEdge::Right, } } } impl From for icss::widgets::animation::Edge { fn from(edge: AnimationEdge) -> Self { match edge { AnimationEdge::Top => icss::widgets::animation::Edge::Top, AnimationEdge::Bottom => icss::widgets::animation::Edge::Bottom, AnimationEdge::Left => icss::widgets::animation::Edge::Left, AnimationEdge::Right => icss::widgets::animation::Edge::Right, } } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub enum AnimationKind { None, Slide, FadeIn, FadeOut, SlideIn(AnimationEdge), SlideOut(AnimationEdge), Custom(CustomAnimation), } impl From for AnimationKind { fn from(kind: icss::widgets::animation::AnimKind) -> Self { match kind { icss::widgets::animation::AnimKind::FadeIn => AnimationKind::FadeIn, icss::widgets::animation::AnimKind::FadeOut => AnimationKind::FadeOut, icss::widgets::animation::AnimKind::SlideIn(edge) => AnimationKind::SlideIn(edge.into()), icss::widgets::animation::AnimKind::SlideOut(edge) => AnimationKind::SlideOut(edge.into()), } } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Easing { Linear, #[default] Ease, EaseIn, EaseOut, EaseInOut, } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default, PartialEq)] pub struct CubicBezier { pub x1: f32, pub y1: f32, pub x2: f32, pub y2: f32, } impl CubicBezier { pub const fn new(x1: f32, y1: f32, x2: f32, y2: f32) -> Self { Self { x1, y1, x2, y2 } } } impl From for iced_anim::transition::bezier::Bezier { fn from(b: CubicBezier) -> Self { iced_anim::transition::bezier::Bezier::new(b.x1, b.y1, b.x2, b.y2) } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, Default)] pub enum AnimationCurve { Linear, #[default] Ease, EaseIn, EaseOut, EaseInOut, Bezier(CubicBezier), Custom(extern "C" fn(f32) -> f32), } impl PartialEq for AnimationCurve { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::Linear, Self::Linear) => true, (Self::Ease, Self::Ease) => true, (Self::EaseIn, Self::EaseIn) => true, (Self::EaseOut, Self::EaseOut) => true, (Self::EaseInOut, Self::EaseInOut) => true, (Self::Bezier(a), Self::Bezier(b)) => a == b, (Self::Custom(a), Self::Custom(b)) => std::ptr::fn_addr_eq(*a, *b), _ => false, } } } impl From for AnimationCurve { fn from(easing: Easing) -> Self { match easing { Easing::Linear => AnimationCurve::Linear, Easing::Ease => AnimationCurve::Ease, Easing::EaseIn => AnimationCurve::EaseIn, Easing::EaseOut => AnimationCurve::EaseOut, Easing::EaseInOut => AnimationCurve::EaseInOut, } } } impl From for AnimationCurve { fn from(easing: icss::widgets::animation::Easing) -> Self { match easing { icss::widgets::animation::Easing::Linear => AnimationCurve::Linear, icss::widgets::animation::Easing::EaseIn => AnimationCurve::EaseIn, icss::widgets::animation::Easing::EaseOut => AnimationCurve::EaseOut, icss::widgets::animation::Easing::EaseInOut => AnimationCurve::EaseInOut, } } } impl From for Easing { fn from(easing: icss::widgets::animation::Easing) -> Self { match easing { icss::widgets::animation::Easing::Linear => Easing::Linear, icss::widgets::animation::Easing::EaseIn => Easing::EaseIn, icss::widgets::animation::Easing::EaseOut => Easing::EaseOut, icss::widgets::animation::Easing::EaseInOut => Easing::EaseInOut, } } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub struct SpringMotion { pub damping: f32, pub response_ms: u32, } impl SpringMotion { pub const SMOOTH: Self = Self { damping: 1.0, response_ms: 500, }; pub const SNAPPY: Self = Self { damping: 0.85, response_ms: 500, }; pub const BOUNCY: Self = Self { damping: 0.7, response_ms: 500, }; pub const INSTANT: Self = Self { damping: 1.0, response_ms: 0, }; pub const fn new(damping: f32, response_ms: u32) -> Self { Self { damping, response_ms, } } pub const fn with_duration(mut self, millis: u32) -> Self { self.response_ms = millis; self } pub const fn with_damping(mut self, damping: f32) -> Self { self.damping = damping; self } } impl Default for SpringMotion { fn default() -> Self { Self::SMOOTH } } impl From for SpringMotion { fn from(motion: iced_anim::spring::Motion) -> Self { Self { damping: motion.damping, response_ms: motion.response.as_millis() as u32, } } } impl From for iced_anim::spring::Motion { fn from(m: SpringMotion) -> Self { iced_anim::spring::Motion { damping: m.damping, response: std::time::Duration::from_millis(m.response_ms as u64), } } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub struct Transition { pub curve: AnimationCurve, pub duration_ms: u32, pub reversible: bool, } impl Default for Transition { fn default() -> Self { Self { curve: AnimationCurve::default(), duration_ms: 130, reversible: false, } } } impl Transition { pub const fn new(curve: AnimationCurve, duration_ms: u32) -> Self { Self { curve, duration_ms, reversible: false, } } pub const fn linear(duration_ms: u32) -> Self { Self::new(AnimationCurve::Linear, duration_ms) } pub const fn ease(duration_ms: u32) -> Self { Self::new(AnimationCurve::Ease, duration_ms) } pub const fn ease_in(duration_ms: u32) -> Self { Self::new(AnimationCurve::EaseIn, duration_ms) } pub const fn ease_out(duration_ms: u32) -> Self { Self::new(AnimationCurve::EaseOut, duration_ms) } pub const fn ease_in_out(duration_ms: u32) -> Self { Self::new(AnimationCurve::EaseInOut, duration_ms) } pub const fn bezier(x1: f32, y1: f32, x2: f32, y2: f32, duration_ms: u32) -> Self { Self::new( AnimationCurve::Bezier(CubicBezier::new(x1, y1, x2, y2)), duration_ms, ) } pub const fn custom(func: extern "C" fn(f32) -> f32, duration_ms: u32) -> Self { Self::new(AnimationCurve::Custom(func), duration_ms) } pub const fn reversible(mut self, rev: bool) -> Self { self.reversible = rev; self } } impl From for Transition { fn from(easing: iced_anim::transition::Easing) -> Self { let curve = match easing.curve { iced_anim::transition::Curve::Linear => AnimationCurve::Linear, iced_anim::transition::Curve::Ease => AnimationCurve::Ease, iced_anim::transition::Curve::EaseIn => AnimationCurve::EaseIn, iced_anim::transition::Curve::EaseOut => AnimationCurve::EaseOut, iced_anim::transition::Curve::EaseInOut => AnimationCurve::EaseInOut, iced_anim::transition::Curve::Bezier(_) => AnimationCurve::Ease, iced_anim::transition::Curve::Custom(_) => AnimationCurve::Linear, }; Self { curve, duration_ms: easing.duration.as_millis() as u32, reversible: easing.reversible, } } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub enum AnimationMode { Spring(SpringMotion), Transition(Transition), } impl Default for AnimationMode { fn default() -> Self { AnimationMode::Transition(Transition::default()) } } impl From for AnimationMode { fn from(motion: SpringMotion) -> Self { AnimationMode::Spring(motion) } } impl From for AnimationMode { fn from(transition: Transition) -> Self { AnimationMode::Transition(transition) } } impl From for AnimationMode { fn from(easing: Easing) -> Self { AnimationMode::Transition(Transition { curve: easing.into(), duration_ms: 130, reversible: false, }) } } impl From for AnimationMode { fn from(curve: AnimationCurve) -> Self { AnimationMode::Transition(Transition { curve, duration_ms: 130, reversible: false, }) } } impl From for AnimationMode { fn from(mode: iced_anim::animated::Mode) -> Self { match mode { iced_anim::animated::Mode::Spring(m) => AnimationMode::Spring(m.into()), iced_anim::animated::Mode::Transition(e) => AnimationMode::Transition(e.into()), } } } impl From for AnimationMode { fn from(motion: iced_anim::spring::Motion) -> Self { AnimationMode::Spring(motion.into()) } } impl From for AnimationMode { fn from(easing: iced_anim::transition::Easing) -> Self { AnimationMode::Transition(easing.into()) } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug, PartialEq)] pub struct Animation { pub kind: AnimationKind, pub duration_ms: u32, pub easing: Easing, pub offset_x: f32, pub offset_y: f32, pub tween: bool, pub mode: AnimationMode, pub key: u64, } impl Animation { pub const fn slide(offset: (f32, f32)) -> Self { Self { kind: AnimationKind::Slide, duration_ms: 130, easing: Easing::EaseOut, offset_x: offset.0, offset_y: offset.1, tween: false, mode: AnimationMode::Transition(Transition::ease_out(130)), key: 0, } } pub const fn slide_in_edge(edge: AnimationEdge, distance: f32) -> Self { Self { kind: AnimationKind::SlideIn(edge), duration_ms: 130, easing: Easing::EaseOut, offset_x: distance, offset_y: distance, tween: false, mode: AnimationMode::Transition(Transition::ease_out(130)), key: 0, } } pub const fn slide_out_edge(edge: AnimationEdge, distance: f32) -> Self { Self { kind: AnimationKind::SlideOut(edge), duration_ms: 130, easing: Easing::EaseIn, offset_x: distance, offset_y: distance, tween: false, mode: AnimationMode::Transition(Transition::ease_in(130)), key: 0, } } pub const fn fade_in() -> Self { Self { kind: AnimationKind::FadeIn, duration_ms: 130, easing: Easing::Ease, offset_x: 0.0, offset_y: 0.0, tween: true, mode: AnimationMode::Transition(Transition::ease(130)), key: 0, } } pub const fn fade_out() -> Self { Self { kind: AnimationKind::FadeOut, duration_ms: 130, easing: Easing::Ease, offset_x: 0.0, offset_y: 0.0, tween: true, mode: AnimationMode::Transition(Transition::ease(130)), key: 0, } } pub const fn tween() -> Self { Self { kind: AnimationKind::None, duration_ms: 130, easing: Easing::Ease, offset_x: 0.0, offset_y: 0.0, tween: true, mode: AnimationMode::Transition(Transition::ease(130)), key: 0, } } pub const fn custom(func: extern "C" fn(f32) -> AnimationOffset) -> Self { Self { kind: AnimationKind::Custom(CustomAnimation::new(func)), duration_ms: 130, easing: Easing::EaseOut, offset_x: 0.0, offset_y: 0.0, tween: false, mode: AnimationMode::Transition(Transition::ease_out(130)), key: 0, } } pub fn from_icss( kind: icss::widgets::animation::AnimKind, duration: std::time::Duration, ) -> Self { let abi_kind = AnimationKind::from(kind); let duration_ms = duration.as_millis() as u32; match abi_kind { AnimationKind::SlideIn(edge) => Self::slide_in_edge(edge, 100.0).duration(duration_ms), AnimationKind::SlideOut(edge) => Self::slide_out_edge(edge, 100.0).duration(duration_ms), AnimationKind::FadeIn => Self::fade_in().duration(duration_ms), AnimationKind::FadeOut => Self::fade_out().duration(duration_ms), _ => Self::tween().duration(duration_ms), } } pub fn spring(motion: impl Into) -> Self { Self::tween().with_spring(motion) } pub fn with_spring(mut self, motion: impl Into) -> Self { let m = motion.into(); self.duration_ms = m.response_ms; self.mode = AnimationMode::Spring(m); self } pub fn transition(transition: impl Into) -> Self { Self::tween().with_transition(transition) } pub fn with_transition(mut self, transition: impl Into) -> Self { let t = transition.into(); self.duration_ms = t.duration_ms; self.mode = AnimationMode::Transition(t); self } pub fn mode(mut self, mode: impl Into) -> Self { self.mode = mode.into(); if let AnimationMode::Transition(t) = &self.mode { self.duration_ms = t.duration_ms; } else if let AnimationMode::Spring(s) = &self.mode { self.duration_ms = s.response_ms; } self } pub const fn smooth(mut self) -> Self { self.duration_ms = SpringMotion::SMOOTH.response_ms; self.mode = AnimationMode::Spring(SpringMotion::SMOOTH); self } pub const fn snappy(mut self) -> Self { self.duration_ms = SpringMotion::SNAPPY.response_ms; self.mode = AnimationMode::Spring(SpringMotion::SNAPPY); self } pub const fn bouncy(mut self) -> Self { self.duration_ms = SpringMotion::BOUNCY.response_ms; self.mode = AnimationMode::Spring(SpringMotion::BOUNCY); self } pub const fn instant(mut self) -> Self { self.duration_ms = SpringMotion::INSTANT.response_ms; self.mode = AnimationMode::Spring(SpringMotion::INSTANT); self } pub const fn bezier(mut self, x1: f32, y1: f32, x2: f32, y2: f32) -> Self { self.mode = AnimationMode::Transition(Transition { curve: AnimationCurve::Bezier(CubicBezier::new(x1, y1, x2, y2)), duration_ms: self.duration_ms, reversible: false, }); self } pub const fn custom_curve(mut self, func: extern "C" fn(f32) -> f32) -> Self { self.mode = AnimationMode::Transition(Transition { curve: AnimationCurve::Custom(func), duration_ms: self.duration_ms, reversible: false, }); self } pub const fn duration(mut self, millis: u32) -> Self { self.duration_ms = millis; match self.mode { AnimationMode::Transition(mut t) => { t.duration_ms = millis; self.mode = AnimationMode::Transition(t); } AnimationMode::Spring(mut s) => { s.response_ms = millis; self.mode = AnimationMode::Spring(s); } } self } pub const fn easing(mut self, easing: Easing) -> Self { self.easing = easing; self.mode = AnimationMode::Transition(Transition { curve: match easing { Easing::Linear => AnimationCurve::Linear, Easing::Ease => AnimationCurve::Ease, Easing::EaseIn => AnimationCurve::EaseIn, Easing::EaseOut => AnimationCurve::EaseOut, Easing::EaseInOut => AnimationCurve::EaseInOut, }, duration_ms: self.duration_ms, reversible: false, }); self } pub const fn curve(mut self, curve: AnimationCurve) -> Self { self.mode = AnimationMode::Transition(Transition { curve, duration_ms: self.duration_ms, reversible: false, }); self } } #[repr(C)] #[derive(StableAbi, Clone, Copy, Debug)] pub struct CanvasRef { pub ptr: *mut u8, pub len: usize, pub id: u64, } #[repr(C)] #[derive(StableAbi, Clone, Debug, PartialEq)] pub enum Image { Rgba { cache_key: u64, width: u32, height: u32, pixels: RVec, }, Encoded { cache_key: u64, bytes: RVec, }, Path(RString), } impl Image { pub fn rgba(width: u32, height: u32, pixels: impl Into>) -> Image { Image::Rgba { cache_key: 0, width, height, pixels: pixels.into().into(), } } pub fn rgba_cached(cache_key: u64, width: u32, height: u32, pixels: impl Into>) -> Image { Image::Rgba { cache_key, width, height, pixels: pixels.into().into(), } } pub fn encoded(bytes: impl Into>) -> Image { Image::Encoded { cache_key: 0, bytes: bytes.into().into(), } } pub fn encoded_cached(cache_key: u64, bytes: impl Into>) -> Image { Image::Encoded { cache_key, bytes: bytes.into().into(), } } pub fn path(path: impl Into) -> Image { Image::Path(path.into().into()) } } #[repr(C)] #[derive(StableAbi, Clone, Debug)] pub enum Node { Row { children: RVec, spacing: f32, align: Align, classes: RVec, }, Column { children: RVec, spacing: f32, align: Align, classes: RVec, }, Container { child: RBox, padding: Padding, align_x: Align, align_y: Align, style: ROption