Skip to content

Commit

Permalink
Use the prop! macro for all style properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Oct 31, 2023
1 parent 1254ade commit 9740eac
Show file tree
Hide file tree
Showing 17 changed files with 715 additions and 992 deletions.
17 changes: 15 additions & 2 deletions src/animate/anim_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::atomic::AtomicUsize;
use std::{rc::Rc, sync::atomic::AtomicUsize};

use crate::update::ANIM_UPDATE_MESSAGES;
use crate::{style::StyleProp, update::ANIM_UPDATE_MESSAGES};

use super::{anim_val::AnimValue, AnimPropKind, AnimUpdateMsg};

Expand Down Expand Up @@ -28,4 +28,17 @@ impl AnimId {
});
});
}

pub(crate) fn update_style_prop<P: StyleProp>(&self, _prop: P, val: P::Type) {
ANIM_UPDATE_MESSAGES.with(|msgs| {
let mut msgs = msgs.borrow_mut();
msgs.push(AnimUpdateMsg::Prop {
id: *self,
kind: AnimPropKind::Prop {
prop: P::prop_ref(),
},
val: AnimValue::Prop(Rc::new(val)),
});
});
}

Check warning on line 43 in src/animate/anim_id.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_id.rs#L32-L43

Added lines #L32 - L43 were not covered by tests
}
18 changes: 14 additions & 4 deletions src/animate/anim_val.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
use std::{any::Any, rc::Rc};

use peniko::Color;

#[derive(Debug, Clone)]
pub enum AnimValue {
Float(f64),
Color(Color),
Prop(Rc<dyn Any>),
}

impl AnimValue {
pub fn get_f32(self) -> f32 {
match self {
AnimValue::Float(v) => v as f32,
AnimValue::Color(_) => panic!(),
}
self.get_f64() as f32

Check warning on line 14 in src/animate/anim_val.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_val.rs#L14

Added line #L14 was not covered by tests
}

pub fn get_f64(self) -> f64 {
match self {
AnimValue::Float(v) => v,
AnimValue::Color(_) => panic!(),
AnimValue::Prop(prop) => *prop.downcast_ref::<f64>().unwrap(),

Check warning on line 21 in src/animate/anim_val.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_val.rs#L21

Added line #L21 was not covered by tests
}
}

pub fn get_color(self) -> Color {
match self {
AnimValue::Color(c) => c,
AnimValue::Float(_) => panic!(),
AnimValue::Prop(prop) => *prop.downcast_ref::<Color>().unwrap(),

Check warning on line 29 in src/animate/anim_val.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_val.rs#L29

Added line #L29 was not covered by tests
}
}

Check warning on line 31 in src/animate/anim_val.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_val.rs#L31

Added line #L31 was not covered by tests

pub fn get_any(self) -> Rc<dyn Any> {
match self {
AnimValue::Color(_) => panic!(),
AnimValue::Float(_) => panic!(),
AnimValue::Prop(prop) => prop.clone(),

Check warning on line 37 in src/animate/anim_val.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/anim_val.rs#L33-L37

Added lines #L33 - L37 were not covered by tests
}
}
}
13 changes: 6 additions & 7 deletions src/animate/animation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::style::{Background, BorderColor, BorderRadius, TextColor};

use super::{
anim_val::AnimValue, AnimId, AnimPropKind, AnimState, AnimStateKind, AnimatedProp, Easing,
EasingFn, EasingMode,
Expand Down Expand Up @@ -108,7 +110,7 @@ impl Animation {
let border_radius = border_radius_fn();

self.id
.update_prop(AnimPropKind::BorderRadius, AnimValue::Float(border_radius));
.update_style_prop(BorderRadius, border_radius.into());

Check warning on line 113 in src/animate/animation.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/animation.rs#L113

Added line #L113 was not covered by tests
});

self
Expand All @@ -118,8 +120,7 @@ impl Animation {
create_effect(move |_| {
let color = color_fn();

self.id
.update_prop(AnimPropKind::Color, AnimValue::Color(color));
self.id.update_style_prop(TextColor, Some(color));

Check warning on line 123 in src/animate/animation.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/animation.rs#L123

Added line #L123 was not covered by tests
});

self
Expand All @@ -129,8 +130,7 @@ impl Animation {
create_effect(move |_| {
let border_color = bord_color_fn();

self.id
.update_prop(AnimPropKind::BorderColor, AnimValue::Color(border_color));
self.id.update_style_prop(BorderColor, border_color);

Check warning on line 133 in src/animate/animation.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/animation.rs#L133

Added line #L133 was not covered by tests
});

self
Expand All @@ -140,8 +140,7 @@ impl Animation {
create_effect(move |_| {
let background = bg_fn();

self.id
.update_prop(AnimPropKind::Background, AnimValue::Color(background));
self.id.update_style_prop(Background, Some(background));

Check warning on line 143 in src/animate/animation.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/animation.rs#L143

Added line #L143 was not covered by tests
});

self
Expand Down
84 changes: 56 additions & 28 deletions src/animate/prop.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
use std::{any::Any, rc::Rc};

use peniko::Color;

use crate::animate::AnimDirection;
use crate::{animate::AnimDirection, style::StylePropRef, unit::Px};

use super::{anim_val::AnimValue, assert_valid_time, SizeUnit};

#[derive(Clone, Debug)]
pub enum AnimatedProp {
Width { from: f64, to: f64, unit: SizeUnit },
Height { from: f64, to: f64, unit: SizeUnit },
Scale { from: f64, to: f64 },
Width {
from: f64,
to: f64,
unit: SizeUnit,
},
Height {
from: f64,
to: f64,
unit: SizeUnit,
},
Scale {
from: f64,
to: f64,
},
// Opacity { from: f64, to: f64 },
// TranslateX,
// TranslateY,
Background { from: Color, to: Color },
BorderRadius { from: f64, to: f64 },
BorderWidth { from: f64, to: f64 },
BorderColor { from: Color, to: Color },
Color { from: Color, to: Color },
Prop {
prop: StylePropRef,
from: Rc<dyn Any>,
to: Rc<dyn Any>,
},
}

impl AnimatedProp {
pub(crate) fn from(&self) -> AnimValue {
match self {
AnimatedProp::Width { from, .. }
| AnimatedProp::Height { from, .. }
| AnimatedProp::BorderWidth { from, .. }
| AnimatedProp::BorderRadius { from, .. } => AnimValue::Float(*from),
AnimatedProp::Prop { from, .. } => AnimValue::Prop(from.clone()),
AnimatedProp::Width { from, .. } | AnimatedProp::Height { from, .. } => {
AnimValue::Float(*from)

Check warning on line 40 in src/animate/prop.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/prop.rs#L38-L40

Added lines #L38 - L40 were not covered by tests
}
AnimatedProp::Scale { .. } => todo!(),
AnimatedProp::Background { from, .. }
| AnimatedProp::BorderColor { from, .. }
| AnimatedProp::Color { from, .. } => AnimValue::Color(*from),
}
}

Expand Down Expand Up @@ -108,19 +118,40 @@ impl AnimatedProp {

pub(crate) fn animate(&self, time: f64, direction: AnimDirection) -> AnimValue {
match self {
AnimatedProp::Prop { prop, from, to } => {
if let Some(from) = from.downcast_ref::<Px>() {
let to = to.downcast_ref::<Px>().unwrap();
return AnimValue::Prop(Rc::new(Px(
self.animate_float(from.0, to.0, time, direction)
)));
}
if let Some(from) = from.downcast_ref::<f64>() {
let to = to.downcast_ref::<f64>().unwrap();
return AnimValue::Prop(Rc::new(
self.animate_float(*from, *to, time, direction),
));
}
if let Some(from) = from.downcast_ref::<Color>() {
let to = to.downcast_ref::<Color>().unwrap();
return AnimValue::Prop(Rc::new(
self.animate_color(*from, *to, time, direction),
));
}
if let Some(from) = from.downcast_ref::<Option<Color>>() {
let to = to.downcast_ref::<Option<Color>>().unwrap();
let from = from.unwrap();
let to = to.unwrap();
return AnimValue::Prop(Rc::new(Some(
self.animate_color(from, to, time, direction),
)));
}
panic!("unknown type for {prop:?}")

Check warning on line 148 in src/animate/prop.rs

View check run for this annotation

Codecov / codecov/patch

src/animate/prop.rs#L121-L148

Added lines #L121 - L148 were not covered by tests
}
AnimatedProp::Width { from, to, unit: _ }
| AnimatedProp::Height { from, to, unit: _ } => {
AnimValue::Float(self.animate_float(*from, *to, time, direction))
}
AnimatedProp::Background { from, to }
| AnimatedProp::BorderColor { from, to }
| AnimatedProp::Color { from, to } => {
AnimValue::Color(self.animate_color(*from, *to, time, direction))
}
AnimatedProp::Scale { .. } => todo!(),
AnimatedProp::BorderRadius { from, to } | AnimatedProp::BorderWidth { from, to } => {
AnimValue::Float(self.animate_float(*from, *to, time, direction))
}
}
}
}
Expand All @@ -131,9 +162,6 @@ pub enum AnimPropKind {
// TranslateX,
// TranslateY,
Width,
Background,
Color,
Height,
BorderRadius,
BorderColor,
Prop { prop: StylePropRef },
}
Loading

0 comments on commit 9740eac

Please sign in to comment.