Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions animation/src/animated.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::time::Duration;

use parking_lot::Mutex;

use crate::{AnimationCoordinator, BlendedAnimation, Interpolatable, Interpolation};

/// `Animated` represents an animated value over time.
Expand All @@ -12,20 +10,14 @@ use crate::{AnimationCoordinator, BlendedAnimation, Interpolatable, Interpolatio
pub struct Animated<T: Send> {
coordinator: AnimationCoordinator,
/// The current value and the current state of the animation.
///
/// A mutex, because we want to access it through `&self` but modify it through the animator.
inner: Mutex<AnimatedInner<T>>,
inner: AnimatedInner<T>,
}

impl<T: Interpolatable + Send> Animated<T> {
pub(crate) fn new(coordinator: AnimationCoordinator, value: T) -> Self {
Self {
coordinator,
inner: AnimatedInner {
value,
animation: Default::default(),
}
.into(),
inner: AnimatedInner::new(value),
}
}

Expand All @@ -40,12 +32,8 @@ impl<T: Interpolatable + Send> Animated<T> {
) where
T: 'static + PartialEq,
{
self.inner.lock().animate_if_changed(
&self.coordinator,
target_value,
duration,
interpolation,
);
self.inner
.animate_if_changed(&self.coordinator, target_value, duration, interpolation);
}

/// Animate to a target value in the given duration.
Expand All @@ -59,13 +47,12 @@ impl<T: Interpolatable + Send> Animated<T> {
T: 'static,
{
self.inner
.lock()
.animate(&self.coordinator, target_value, duration, interpolation);
}

/// Stop all animations, and set the current value.
pub fn set_immediately(&mut self, value: T) {
self.inner.lock().set_immediately(value);
self.inner.set_immediately(value);
}

/// Finish all animations.
Expand All @@ -75,20 +62,24 @@ impl<T: Interpolatable + Send> Animated<T> {
///
/// Does nothing when no animation is active.
pub fn finish(&mut self) {
self.inner.lock().finish();
self.inner.finish();
}

/// The current value of this animated value.
///
/// If an animation is active, this computes the current value from the animation.
pub fn value(&self) -> T {
self.inner.lock().value(&self.coordinator)
/// The current value of this animated value, progressing active animations first.
pub fn value(&mut self) -> &T {
self.inner.progress(&self.coordinator);
self.latest_value()
}

/// The latest stored value of this animated value without progressing active animations.
pub fn latest_value(&self) -> &T {
self.inner.value()
}

/// The final value of this animated value after all current animations ran through or the
/// current value one if no animations are active.
pub fn final_value(&self) -> T {
self.inner.lock().final_value().clone()
pub fn final_value(&self) -> &T {
self.inner.final_value()
}

/// `true` if this is currently animating.
Expand All @@ -102,12 +93,12 @@ impl<T: Interpolatable + Send> Animated<T> {
///
/// Ergonomics: Foolproof!
pub fn is_animating(&self) -> bool {
self.inner.lock().is_animating()
self.inner.is_animating()
}

/// Returns the number of active animation blendings.
pub fn animation_count(&self) -> usize {
self.inner.lock().animation_count()
self.inner.animation_count()
}
}

Expand All @@ -123,6 +114,13 @@ where
}

impl<T: Send + Interpolatable> AnimatedInner<T> {
pub fn new(value: T) -> Self {
Self {
value,
animation: Default::default(),
}
}

pub fn animate_if_changed(
&mut self,
coordinator: &AnimationCoordinator,
Comment thread
pragmatrix marked this conversation as resolved.
Expand Down Expand Up @@ -169,14 +167,17 @@ impl<T: Send + Interpolatable> AnimatedInner<T> {
self.animation.final_value().unwrap_or(&self.value)
}

pub fn value(&mut self, coordinator: &AnimationCoordinator) -> T {
pub fn value(&self) -> &T {
&self.value
}

pub fn progress(&mut self, coordinator: &AnimationCoordinator) {
if self.animation.is_active() {
let instant = coordinator.current_cycle_time();
if let Some(new_value) = self.animation.proceed(instant) {
self.value = new_value;
}
}
self.value.clone()
}

pub fn is_animating(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Desktop {

// Get the camera, build the frame, and submit it to the renderer.
{
let camera = self.system.camera();
let camera = *self.system.camera();
let mut frame = self.scene.begin_frame().with_camera(camera);
if self.instance_manager.effective_pacing() == RenderPacing::Smooth {
frame = frame.with_pacing(RenderPacing::Smooth);
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/desktop_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl DesktopSystem {
self.aggregates.instances.contains_key(instance)
}

pub fn camera(&self) -> PixelCamera {
pub fn camera(&mut self) -> &PixelCamera {
self.camera.value()
}

Expand Down
5 changes: 3 additions & 2 deletions desktop/src/desktop_system/layout_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl DesktopSystem {
) -> Result<()> {
if !effects_mode.permit_camera_moves() {
// Lock camera motion immediately, including already running camera animations.
let camera = self.camera.value();
// Ergonomics: There should probably be a function for that in Animated.
let camera = *self.camera.value();
self.camera.set_immediately(camera);
}

Expand Down Expand Up @@ -360,7 +361,7 @@ impl DesktopSystem {
placement.transform = Transform::compose_with_anchor(
launcher_placement.transform,
launcher_anchor,
instance_presenter.layout_transform_animation.value(),
*instance_presenter.layout_transform_animation.latest_value(),
instance_anchor,
);

Expand Down
2 changes: 1 addition & 1 deletion desktop/src/desktop_system/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl DesktopSystem {
};

let initial_center_translation =
originating_presenter.map(|op| op.layout_transform_animation.value().translate);
originating_presenter.map(|op| op.layout_transform_animation.latest_value().translate);

let presenter = InstancePresenter::new(
initial_center_translation,
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/instance_presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl InstancePresenter {

pub fn apply_animations(&mut self) {
let layout_transform = self.layout_transform_animation.value();
self.instance_transform.update_if_changed(layout_transform);
self.instance_transform.update_if_changed(*layout_transform);

// Feature: Hiding animation.
let Some(view) = self.state.view_mut() else {
Expand All @@ -222,7 +222,7 @@ impl InstancePresenter {

let alpha = view.alpha.value();
location.update_if_changed_with(|location| {
location.alpha = alpha;
location.alpha = *alpha;
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions desktop/src/projects/launcher_presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl LauncherPresenter {
}

fn presents_instance(&self) -> bool {
self.fader.final_value() == 0.0
*self.fader.final_value() == 0.0
}

pub fn set_layout(&mut self, size: SizePx, layout_transform: Transform, animate: bool) {
Expand Down Expand Up @@ -332,7 +332,7 @@ impl LauncherPresenter {
self.background.update_if_changed_with(|visual| {
visual.shapes = [background_shape(
size.to_rect(),
BACKGROUND_COLOR.with_alpha(alpha),
BACKGROUND_COLOR.with_alpha(*alpha),
)]
.into()
});
Expand All @@ -342,7 +342,7 @@ impl LauncherPresenter {
visual.shapes = match &*visual.shapes {
[Shape::GlyphRun(gr)] => [gr
.clone()
.with_color(TEXT_COLOR.with_alpha(alpha))
.with_color(TEXT_COLOR.with_alpha(*alpha))
.into_shape()]
.into(),
rest => rest.into(),
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/projects/project_presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl DesktopPresenter {

let size = hover_placement.rect.size;
let local_rect = Rect::from_size((size[0] as f64, size[1] as f64));
let rect_alpha = (alpha != 0.0).then_some((local_rect, alpha));
let rect_alpha = (*alpha != 0.0).then_some((local_rect, *alpha));

// Position the hover visual in world space using the placement's center-based transform.
let local_center = local_rect.center();
Expand Down
9 changes: 5 additions & 4 deletions examples/logs/examples/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Logs {
}

fn apply_animations(&mut self) {
let v_center = self.vertical_center.value();
let v_center = *self.vertical_center.value();
self.vertical_center_transform
.update((0., v_center, 0.).into());

Expand All @@ -270,7 +270,7 @@ impl Logs {

while let Some(line) = self.lines.front() {
if line.fading_out && !line.fader.is_animating() {
debug!("faded out at: {}", line.fader.value());
debug!("faded out at: {}", line.fader.latest_value());
self.lines.pop_front();
update_v_alignment = true;
} else {
Expand All @@ -290,9 +290,10 @@ impl Logs {
}

fn update_content_transform(&mut self) {
let content_height = *self.content_height.value();
let new_transform = self
.application
.get_transform((self.content_width, self.content_height.value() as u32));
.get_transform((self.content_width, content_height as u32));
self.content_transform.update_if_changed(new_transform);
}
}
Expand Down Expand Up @@ -348,7 +349,7 @@ impl LogLine {
return;
}

let fading = self.fader.value();
let fading = *self.fader.value();

self.visual.update_with(|v| {
v.shapes = v
Expand Down
Loading