Skip to content

Commit

Permalink
Fix animation delay (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton authored Feb 23, 2025
1 parent 7e5e78a commit ded3c62
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
29 changes: 20 additions & 9 deletions src/animate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ pub struct Animation {
pub(crate) effect_states: EffectStateVec,
pub(crate) auto_reverse: bool,
pub(crate) delay: Duration,
pub(crate) delay_on_reverse: bool,
pub(crate) duration: Duration,
pub(crate) repeat_mode: RepeatMode,
/// How many times the animation has been repeated so far
Expand Down Expand Up @@ -391,6 +392,7 @@ impl Default for Animation {
effect_states: SmallVec::new(),
auto_reverse: false,
delay: Duration::ZERO,
delay_on_reverse: false,
duration: Duration::from_millis(200),
repeat_mode: RepeatMode::Times(1),
repeat_count: 0,
Expand Down Expand Up @@ -625,6 +627,12 @@ impl Animation {
self
}

/// Sets whether the animation should delay when reversing.
pub const fn delay_on_reverse(mut self, on_reverse: bool) -> Self {
self.delay_on_reverse = on_reverse;
self
}

/// Sets if the animation should the repeat forever.
pub const fn repeat(mut self, repeat: bool) -> Self {
self.repeat_mode = if repeat {
Expand Down Expand Up @@ -808,6 +816,7 @@ impl Animation {

/// Advance the animation.
pub fn advance(&mut self) {
let use_delay = self.use_delay();
match &mut self.state {
AnimState::Idle => {
self.start_mut();
Expand All @@ -822,11 +831,13 @@ impl Animation {
let og_elapsed = elapsed;
elapsed = duration;

let temp_elapsed = if elapsed <= self.delay {
let temp_elapsed = if elapsed <= self.delay && use_delay {
// The animation hasn't started yet
Duration::ZERO
} else {
} else if use_delay {
elapsed - self.delay
} else {
elapsed
};

if temp_elapsed >= self.duration {
Expand Down Expand Up @@ -957,13 +968,8 @@ impl Animation {
return 0.;
}
let mut elapsed = self.elapsed().unwrap_or(Duration::ZERO);
// don't account for delay when reversing
if !self.reverse_once.is_rev() && elapsed < self.delay {
// The animation hasn't started yet
return 0.0;
}
if !self.reverse_once.is_rev() {
elapsed -= self.delay;
if self.use_delay() {
elapsed = elapsed.saturating_sub(self.delay);
}
let percent = elapsed.as_secs_f64() / self.duration.as_secs_f64();

Expand All @@ -978,6 +984,11 @@ impl Animation {
self.reverse_once.is_rev()
}

fn use_delay(&self) -> bool {
// going forward or if we are still supposed to delay on reverse
!self.is_reversing() || self.delay_on_reverse
}

/// Get the lower and upper keyframe ids from the cache for a prop and then resolve those id's into a pair of `KeyFrameProp`s that contain the prop value and easing function
pub(crate) fn get_current_kf_props(
&self,
Expand Down
6 changes: 3 additions & 3 deletions src/views/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ impl<T: Clone> Dropdown<T> {
/// * `active_item` - A function that returns the currently selected item.
///
/// * `main_view` - A function that takes a value of type `T` and returns an `AnyView`
/// to be used as the main dropdown display.
/// to be used as the main dropdown display.
///
/// * `iterator` - An iterator that provides the items to be displayed in the dropdown list.
///
/// * `list_item_fn` - A function that takes a value of type `T` and returns an `AnyView`
/// to be used for each item in the dropdown list.
/// to be used for each item in the dropdown list.
pub fn custom<MF, I, LF, AIF>(
active_item: AIF,
main_view: MF,
Expand Down Expand Up @@ -461,7 +461,7 @@ impl<T: Clone> Dropdown<T> {
/// # Arguments
///
/// * `active_item` - A read-write signal representing the currently selected item.
/// It must implement `SignalGet<T>` and `SignalUpdate<T>`.
/// It must implement `SignalGet<T>` and `SignalUpdate<T>`.
///
/// * `iterator` - An iterator that provides the items to be displayed in the dropdown list.
pub fn new_rw<AI, I>(active_item: AI, iterator: I) -> Dropdown<T>
Expand Down
2 changes: 1 addition & 1 deletion src/views/editor/visual_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl Lines {
/// - Would require tracking that but might not be too hard to do it whenever we create a
/// text layout
/// - `is_linear` could be up to some line, which allows us to make at least the earliest parts
/// before any wrapping were faster. However, early lines are faster to calculate anyways.
/// before any wrapping were faster. However, early lines are faster to calculate anyways.
pub fn is_linear(&self, text_prov: impl TextLayoutProvider) -> bool {
self.wrap.get() == ResolvedWrap::None && !text_prov.has_multiline_phantom()
}
Expand Down
5 changes: 3 additions & 2 deletions tiny_skia/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,9 @@ impl<W: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle

// Copy from `tiny_skia::Pixmap` to the format specified by `softbuffer::Buffer`.
for (out_pixel, pixel) in (buffer.iter_mut()).zip(self.pixmap.pixels().iter()) {
*out_pixel =
(pixel.red() as u32) << 16 | (pixel.green() as u32) << 8 | (pixel.blue() as u32);
*out_pixel = ((pixel.red() as u32) << 16)
| ((pixel.green() as u32) << 8)
| (pixel.blue() as u32);
}

buffer
Expand Down
2 changes: 1 addition & 1 deletion vello/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Renderer for VelloRenderer {
let font_id = glyph.font_id;
let metadata = glyph.metadata;

if current_run.as_ref().map_or(true, |run| {
if current_run.as_ref().is_none_or(|run| {
run.color != color
|| run.font_size != font_size
|| run.font_id != font_id
Expand Down

0 comments on commit ded3c62

Please sign in to comment.