LinearInterpolation now requires PartialEq

This also means that if an animation is animating over discrete values
and the actual value has not changed, the Dynamic will no longer detect
a change because it's now using update instead of set.
This commit is contained in:
Jonathan Johnson 2023-11-09 07:46:02 -08:00
parent a818cc41fd
commit 58b98a9a16
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
5 changed files with 7 additions and 9 deletions

View file

@ -13,12 +13,12 @@ fn main() -> gooey::Result {
Resize::width(Lp::points(100), Label::new(label)) Resize::width(Lp::points(100), Label::new(label))
.and(Button::new("+").on_click(counter.with_clone(|counter| { .and(Button::new("+").on_click(counter.with_clone(|counter| {
move |_| { move |_| {
counter.set(counter.get() + 1); *counter.lock() += 1;
} }
}))) })))
.and(Button::new("-").on_click(counter.with_clone(|counter| { .and(Button::new("-").on_click(counter.with_clone(|counter| {
move |_| { move |_| {
counter.set(counter.get() - 1); *counter.lock() -= 1;
} }
}))), }))),
) )

View file

@ -210,11 +210,11 @@ where
fn update(&self, percent: f32) { fn update(&self, percent: f32) {
self.change self.change
.dynamic .dynamic
.set(self.start.lerp(&self.change.new_value, percent)); .update(self.start.lerp(&self.change.new_value, percent));
} }
fn finish(&self) { fn finish(&self) {
self.change.dynamic.set(self.change.new_value.clone()); self.change.dynamic.update(self.change.new_value.clone());
} }
} }
@ -606,7 +606,7 @@ impl Animate for Duration {
} }
/// Performs a linear interpolation between two values. /// Performs a linear interpolation between two values.
pub trait LinearInterpolate { pub trait LinearInterpolate: PartialEq {
/// Interpolate linearly between `self` and `target` using `percent`. /// Interpolate linearly between `self` and `target` using `percent`.
#[must_use] #[must_use]
fn lerp(&self, target: &Self, percent: f32) -> Self; fn lerp(&self, target: &Self, percent: f32) -> Self;

View file

@ -136,7 +136,7 @@ impl<T> Dynamic<T> {
/// equal to the currently stored value. /// equal to the currently stored value.
pub fn update(&self, new_value: T) pub fn update(&self, new_value: T)
where where
T: Eq, T: PartialEq,
{ {
self.0.map_mut(|value, changed| { self.0.map_mut(|value, changed| {
if *value == new_value { if *value == new_value {

View file

@ -108,7 +108,7 @@ impl Button {
.spawn(); .spawn();
} }
(true, Some(dynamic)) => { (true, Some(dynamic)) => {
dynamic.set(background_color); dynamic.update(background_color);
self.background_color_animation.clear(); self.background_color_animation.clear();
} }
(_, None) => { (_, None) => {

View file

@ -736,8 +736,6 @@ where
) { ) {
match event { match event {
WindowCommand::Redraw => { WindowCommand::Redraw => {
// TODO we should attempt to batch redraw events so that we're
// not constantly sending them from animations.
window.set_needs_redraw(); window.set_needs_redraw();
} }
} }