mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
style: Add a new Timer structure to the shared style context, and basic infrastructure for controlling animations.
This commit is contained in:
parent
2e68821014
commit
0b67b218d0
20 changed files with 178 additions and 26 deletions
|
@ -20,7 +20,7 @@ use selectors::matching::DeclarationBlock;
|
|||
use std::sync::Arc;
|
||||
use std::sync::mpsc::Sender;
|
||||
use string_cache::Atom;
|
||||
use time;
|
||||
use timer::Timer;
|
||||
use values::computed::Time;
|
||||
|
||||
/// This structure represents a keyframes animation current iteration state.
|
||||
|
@ -122,7 +122,9 @@ impl KeyframesAnimationState {
|
|||
///
|
||||
/// There are some bits of state we can't just replace, over all taking in
|
||||
/// account times, so here's that logic.
|
||||
pub fn update_from_other(&mut self, other: &Self) {
|
||||
pub fn update_from_other(&mut self,
|
||||
other: &Self,
|
||||
timer: &Timer) {
|
||||
use self::KeyframesRunningState::*;
|
||||
|
||||
debug!("KeyframesAnimationState::update_from_other({:?}, {:?})", self, other);
|
||||
|
@ -146,11 +148,11 @@ impl KeyframesAnimationState {
|
|||
// If we're pausing the animation, compute the progress value.
|
||||
match (&mut self.running_state, old_running_state) {
|
||||
(&mut Running, Paused(progress))
|
||||
=> new_started_at = time::precise_time_s() - (self.duration * progress),
|
||||
=> new_started_at = timer.seconds() - (self.duration * progress),
|
||||
(&mut Paused(ref mut new), Paused(old))
|
||||
=> *new = old,
|
||||
(&mut Paused(ref mut progress), Running)
|
||||
=> *progress = (time::precise_time_s() - old_started_at) / old_duration,
|
||||
=> *progress = (timer.seconds() - old_started_at) / old_duration,
|
||||
_ => {},
|
||||
}
|
||||
|
||||
|
@ -341,7 +343,8 @@ impl PropertyAnimation {
|
|||
pub fn start_transitions_if_applicable(new_animations_sender: &Sender<Animation>,
|
||||
node: OpaqueNode,
|
||||
old_style: &ComputedValues,
|
||||
new_style: &mut Arc<ComputedValues>)
|
||||
new_style: &mut Arc<ComputedValues>,
|
||||
timer: &Timer)
|
||||
-> bool {
|
||||
let mut had_animations = false;
|
||||
for i in 0..new_style.get_box().transition_property_count() {
|
||||
|
@ -355,7 +358,7 @@ pub fn start_transitions_if_applicable(new_animations_sender: &Sender<Animation>
|
|||
|
||||
// Kick off the animation.
|
||||
let box_style = new_style.get_box();
|
||||
let now = time::precise_time_s();
|
||||
let now = timer.seconds();
|
||||
let start_time =
|
||||
now + (box_style.transition_delay_mod(i).seconds() as f64);
|
||||
new_animations_sender
|
||||
|
@ -424,7 +427,7 @@ pub fn maybe_start_animations(context: &SharedStyleContext,
|
|||
}
|
||||
|
||||
let delay = box_style.animation_delay_mod(i).seconds();
|
||||
let now = time::precise_time_s();
|
||||
let now = context.timer.seconds();
|
||||
let animation_start = now + delay as f64;
|
||||
let duration = box_style.animation_duration_mod(i).seconds();
|
||||
let iteration_state = match box_style.animation_iteration_count_mod(i) {
|
||||
|
@ -497,7 +500,7 @@ where Damage: TRestyleDamage {
|
|||
match *animation {
|
||||
Animation::Transition(_, start_time, ref frame, _) => {
|
||||
debug!("update_style_for_animation: transition found");
|
||||
let now = time::precise_time_s();
|
||||
let now = context.timer.seconds();
|
||||
let mut new_style = (*style).clone();
|
||||
let updated_style = update_style_for_animation_frame(&mut new_style,
|
||||
now, start_time,
|
||||
|
@ -516,7 +519,7 @@ where Damage: TRestyleDamage {
|
|||
let started_at = state.started_at;
|
||||
|
||||
let now = match state.running_state {
|
||||
KeyframesRunningState::Running => time::precise_time_s(),
|
||||
KeyframesRunningState::Running => context.timer.seconds(),
|
||||
KeyframesRunningState::Paused(progress) => started_at + duration * progress,
|
||||
};
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ use std::cell::RefCell;
|
|||
use std::collections::HashMap;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, Mutex, RwLock};
|
||||
use timer::Timer;
|
||||
|
||||
/// This structure is used to create a local style context from a shared one.
|
||||
pub struct LocalStyleContextCreationInfo {
|
||||
|
@ -57,6 +58,10 @@ pub struct SharedStyleContext {
|
|||
|
||||
/// Data needed to create the local style context from the shared one.
|
||||
pub local_context_creation_data: Mutex<LocalStyleContextCreationInfo>,
|
||||
|
||||
/// The current timer for transitions and animations. This is needed to test
|
||||
/// them.
|
||||
pub timer: Timer,
|
||||
}
|
||||
|
||||
pub struct LocalStyleContext {
|
||||
|
|
|
@ -101,6 +101,7 @@ pub mod sink;
|
|||
pub mod str;
|
||||
pub mod stylesheets;
|
||||
mod tid;
|
||||
pub mod timer;
|
||||
pub mod traversal;
|
||||
#[macro_use]
|
||||
#[allow(non_camel_case_types)]
|
||||
|
|
|
@ -435,7 +435,8 @@ trait PrivateMatchMethods: TNode
|
|||
new_animations_sender,
|
||||
this_opaque,
|
||||
&**style,
|
||||
&mut this_style);
|
||||
&mut this_style,
|
||||
&shared_context.timer);
|
||||
}
|
||||
|
||||
cacheable = cacheable && !animations_started
|
||||
|
|
58
components/style/timer.rs
Normal file
58
components/style/timer.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
use time;
|
||||
|
||||
/// The `TimerMode` is used to determine what time should the `Timer` return,
|
||||
/// either a fixed value (in the `Test` mode), or the actual time (in the
|
||||
/// `Current` mode).
|
||||
#[derive(Debug, Clone)]
|
||||
enum TimerMode {
|
||||
Test(f64),
|
||||
Current,
|
||||
}
|
||||
|
||||
/// A `Timer` struct that takes care of giving the current time for animations.
|
||||
///
|
||||
/// This is needed to be allowed to hook the time in the animations' test-mode.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Timer {
|
||||
mode: TimerMode,
|
||||
}
|
||||
|
||||
impl Timer {
|
||||
/// Creates a new "normal" timer, i.e., a "Current" mode timer.
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
Timer {
|
||||
mode: TimerMode::Current,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new "test mode" timer, with initial time 0.
|
||||
#[inline]
|
||||
pub fn test_mode() -> Self {
|
||||
Timer {
|
||||
mode: TimerMode::Test(0.),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current time, at least from the caller's perspective. In
|
||||
/// test mode returns whatever the value is.
|
||||
pub fn seconds(&self) -> f64 {
|
||||
match self.mode {
|
||||
TimerMode::Test(test_value) => test_value,
|
||||
TimerMode::Current => time::precise_time_s(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Increments the current clock. Panics if the clock is not on test mode.
|
||||
pub fn increment(&mut self, by: f64) {
|
||||
match self.mode {
|
||||
TimerMode::Test(ref mut val)
|
||||
=> *val += by,
|
||||
TimerMode::Current
|
||||
=> panic!("Timer::increment called for a non-test mode timer. This is a bug."),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue