2018-06-06 08:51:34 -04:00
|
|
|
use std::fmt;
|
2018-06-11 16:54:47 -04:00
|
|
|
use std::sync::{Arc, Weak, Mutex, RwLock};
|
2018-04-25 19:01:52 -04:00
|
|
|
use futures_core::Async;
|
|
|
|
use futures_core::future::Future;
|
|
|
|
use futures_core::task::{Context, Waker};
|
|
|
|
use futures_signals::CancelableFutureHandle;
|
|
|
|
use futures_signals::signal::{Signal, SignalExt, WaitFor, MutableSignal, Mutable};
|
|
|
|
use futures_signals::signal_vec::{SignalVec, VecDiff};
|
2018-03-15 06:54:18 -04:00
|
|
|
use stdweb::Value;
|
2018-04-25 19:01:52 -04:00
|
|
|
use discard::DiscardOnDrop;
|
|
|
|
use operations::spawn_future;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
|
|
|
|
2018-03-12 20:09:44 -04:00
|
|
|
// TODO generalize this so it works for any target, not just JS
|
|
|
|
struct Raf(Value);
|
|
|
|
|
|
|
|
impl Raf {
|
|
|
|
#[inline]
|
2018-03-16 02:50:21 -04:00
|
|
|
fn new<F>(callback: F) -> Self where F: FnMut(f64) + 'static {
|
2018-03-12 20:09:44 -04:00
|
|
|
Raf(js!(
|
|
|
|
var callback = @{callback};
|
|
|
|
|
|
|
|
function loop(time) {
|
2018-03-16 02:50:21 -04:00
|
|
|
value.id = requestAnimationFrame(loop);
|
|
|
|
callback(time);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var value = {
|
|
|
|
callback: callback,
|
|
|
|
id: requestAnimationFrame(loop)
|
|
|
|
};
|
|
|
|
|
|
|
|
return value;
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Raf {
|
|
|
|
#[inline]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
js! { @(no_return)
|
2018-03-15 06:54:18 -04:00
|
|
|
var self = @{&self.0};
|
2018-03-16 02:50:21 -04:00
|
|
|
cancelAnimationFrame(self.id);
|
|
|
|
self.callback.drop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
struct TimestampsInner {
|
2018-03-16 02:50:21 -04:00
|
|
|
raf: Option<Raf>,
|
|
|
|
// TODO make this more efficient
|
2018-06-11 16:54:47 -04:00
|
|
|
states: Vec<Weak<Mutex<TimestampsState>>>,
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TimestampsGlobal {
|
2018-06-11 16:54:47 -04:00
|
|
|
inner: Mutex<TimestampsInner>,
|
|
|
|
value: Arc<RwLock<Option<f64>>>,
|
2018-03-17 07:17:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TimestampsEnum {
|
|
|
|
First,
|
|
|
|
Changed,
|
|
|
|
NotChanged,
|
2018-03-16 02:50:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TimestampsState {
|
2018-04-25 19:01:52 -04:00
|
|
|
state: TimestampsEnum,
|
|
|
|
waker: Option<Waker>,
|
2018-03-16 02:50:21 -04:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub struct Timestamps {
|
2018-06-11 16:54:47 -04:00
|
|
|
state: Arc<Mutex<TimestampsState>>,
|
|
|
|
// TODO verify that there aren't any Arc cycles
|
|
|
|
value: Arc<RwLock<Option<f64>>>,
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-16 02:50:21 -04:00
|
|
|
|
|
|
|
impl Signal for Timestamps {
|
|
|
|
type Item = Option<f64>;
|
|
|
|
|
2018-03-29 21:48:20 -04:00
|
|
|
// TODO implement Async::Ready(None)
|
2018-04-25 19:01:52 -04:00
|
|
|
fn poll_change(&mut self, cx: &mut Context) -> Async<Option<Self::Item>> {
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.state.lock().unwrap();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
match lock.state {
|
2018-03-17 07:17:06 -04:00
|
|
|
TimestampsEnum::Changed => {
|
2018-04-25 19:01:52 -04:00
|
|
|
lock.state = TimestampsEnum::NotChanged;
|
2018-06-11 16:54:47 -04:00
|
|
|
Async::Ready(Some(*self.value.read().unwrap()))
|
2018-03-17 07:17:06 -04:00
|
|
|
},
|
|
|
|
TimestampsEnum::First => {
|
2018-04-25 19:01:52 -04:00
|
|
|
lock.state = TimestampsEnum::NotChanged;
|
2018-03-29 21:48:20 -04:00
|
|
|
Async::Ready(Some(None))
|
2018-03-17 07:17:06 -04:00
|
|
|
},
|
|
|
|
TimestampsEnum::NotChanged => {
|
2018-04-25 19:01:52 -04:00
|
|
|
lock.waker = Some(cx.waker().clone());
|
|
|
|
Async::Pending
|
2018-03-17 07:17:06 -04:00
|
|
|
},
|
2018-03-16 02:50:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
lazy_static! {
|
|
|
|
static ref TIMESTAMPS: Arc<TimestampsGlobal> = Arc::new(TimestampsGlobal {
|
|
|
|
inner: Mutex::new(TimestampsInner {
|
2018-04-25 19:01:52 -04:00
|
|
|
raf: None,
|
|
|
|
states: vec![],
|
|
|
|
}),
|
2018-06-11 16:54:47 -04:00
|
|
|
value: Arc::new(RwLock::new(None)),
|
2018-04-25 19:01:52 -04:00
|
|
|
});
|
2018-03-16 09:40:18 -04:00
|
|
|
}
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-03-16 09:40:18 -04:00
|
|
|
pub fn timestamps() -> Timestamps {
|
2018-06-11 16:54:47 -04:00
|
|
|
let timestamps = Timestamps {
|
|
|
|
state: Arc::new(Mutex::new(TimestampsState {
|
|
|
|
state: TimestampsEnum::First,
|
|
|
|
waker: None,
|
|
|
|
})),
|
|
|
|
value: TIMESTAMPS.value.clone(),
|
|
|
|
};
|
2018-03-16 09:40:18 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
{
|
|
|
|
let mut lock = TIMESTAMPS.inner.lock().unwrap();
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
lock.states.push(Arc::downgrade(×tamps.state));
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
if let None = lock.raf {
|
|
|
|
let global = TIMESTAMPS.clone();
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
lock.raf = Some(Raf::new(move |time| {
|
|
|
|
let mut lock = global.inner.lock().unwrap();
|
|
|
|
let mut value = global.value.write().unwrap();
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
*value = Some(time);
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
lock.states.retain(|state| {
|
|
|
|
if let Some(state) = state.upgrade() {
|
|
|
|
let mut lock = state.lock().unwrap();
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
lock.state = TimestampsEnum::Changed;
|
2018-03-17 07:17:06 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
if let Some(waker) = lock.waker.take() {
|
|
|
|
drop(lock);
|
|
|
|
waker.wake();
|
2018-03-17 07:17:06 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
false
|
2018-03-17 07:17:06 -04:00
|
|
|
}
|
2018-06-11 16:54:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if lock.states.len() == 0 {
|
|
|
|
lock.raf = None;
|
|
|
|
// TODO is this a good idea ?
|
|
|
|
lock.states = vec![];
|
|
|
|
}
|
|
|
|
}));
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-06-11 16:54:47 -04:00
|
|
|
}
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
timestamps
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
pub trait AnimatedSignalVec: SignalVec {
|
2018-04-25 19:01:52 -04:00
|
|
|
type Animation;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
|
|
|
fn animated_map<A, F>(self, duration: f64, f: F) -> AnimatedMap<Self, F>
|
2018-04-25 19:01:52 -04:00
|
|
|
where F: FnMut(Self::Item, Self::Animation) -> A,
|
2018-03-15 06:54:18 -04:00
|
|
|
Self: Sized;
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: SignalVec> AnimatedSignalVec for S {
|
2018-04-25 19:01:52 -04:00
|
|
|
type Animation = AnimatedMapBroadcaster;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-03-12 20:09:44 -04:00
|
|
|
#[inline]
|
2018-03-15 06:54:18 -04:00
|
|
|
fn animated_map<A, F>(self, duration: f64, f: F) -> AnimatedMap<Self, F>
|
2018-04-25 19:01:52 -04:00
|
|
|
where F: FnMut(Self::Item, Self::Animation) -> A {
|
2018-03-12 20:09:44 -04:00
|
|
|
AnimatedMap {
|
|
|
|
duration: duration,
|
|
|
|
animations: vec![],
|
|
|
|
signal: Some(self),
|
|
|
|
callback: f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub struct AnimatedMapBroadcaster(MutableAnimation);
|
|
|
|
|
|
|
|
impl AnimatedMapBroadcaster {
|
|
|
|
// TODO it should return a custom type
|
|
|
|
#[inline]
|
|
|
|
pub fn signal(&self) -> MutableAnimationSignal {
|
|
|
|
self.0.signal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-12 20:09:44 -04:00
|
|
|
struct AnimatedMapState {
|
|
|
|
animation: MutableAnimation,
|
2018-04-25 19:01:52 -04:00
|
|
|
removing: Option<WaitFor<MutableAnimationSignal>>,
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO move this into signals crate and also generalize it to work with any future, not just animations
|
|
|
|
pub struct AnimatedMap<A, B> {
|
|
|
|
duration: f64,
|
|
|
|
animations: Vec<AnimatedMapState>,
|
|
|
|
signal: Option<A>,
|
|
|
|
callback: B,
|
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
impl<A, F, S> AnimatedMap<S, F>
|
2018-03-12 20:09:44 -04:00
|
|
|
where S: SignalVec,
|
2018-04-25 19:01:52 -04:00
|
|
|
F: FnMut(S::Item, AnimatedMapBroadcaster) -> A {
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
fn animated_state(&self) -> AnimatedMapState {
|
|
|
|
let state = AnimatedMapState {
|
|
|
|
animation: MutableAnimation::new(self.duration),
|
|
|
|
removing: None,
|
|
|
|
};
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
state.animation.animate_to(Percentage::new_unchecked(1.0));
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
state
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
fn remove_index(&mut self, index: usize) -> Async<Option<VecDiff<A>>> {
|
2018-03-12 20:09:44 -04:00
|
|
|
if index == (self.animations.len() - 1) {
|
|
|
|
self.animations.pop();
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::Pop {}))
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
self.animations.remove(index);
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::RemoveAt { index }))
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
fn should_remove(&mut self, cx: &mut Context, index: usize) -> bool {
|
2018-03-15 06:54:18 -04:00
|
|
|
let state = &mut self.animations[index];
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
state.animation.animate_to(Percentage::new_unchecked(0.0));
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
let mut future = state.animation.signal().wait_for(Percentage::new_unchecked(0.0));
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
if future.poll(cx).unwrap().is_ready() {
|
2018-03-15 06:54:18 -04:00
|
|
|
true
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
state.removing = Some(future);
|
2018-03-15 06:54:18 -04:00
|
|
|
false
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_index(&self, parent_index: usize) -> Option<usize> {
|
|
|
|
let mut seen = 0;
|
|
|
|
|
|
|
|
// TODO is there a combinator that can simplify this ?
|
|
|
|
self.animations.iter().position(|state| {
|
|
|
|
if state.removing.is_none() {
|
|
|
|
if seen == parent_index {
|
|
|
|
true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
seen += 1;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn find_last_index(&self) -> Option<usize> {
|
|
|
|
self.animations.iter().rposition(|state| state.removing.is_none())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
impl<A, F, S> SignalVec for AnimatedMap<S, F>
|
2018-03-12 20:09:44 -04:00
|
|
|
where S: SignalVec,
|
2018-04-25 19:01:52 -04:00
|
|
|
F: FnMut(S::Item, AnimatedMapBroadcaster) -> A {
|
2018-03-15 06:54:18 -04:00
|
|
|
type Item = A;
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
// TODO this can probably be implemented more efficiently
|
2018-04-25 19:01:52 -04:00
|
|
|
fn poll_vec_change(&mut self, cx: &mut Context) -> Async<Option<VecDiff<Self::Item>>> {
|
2018-03-15 06:54:18 -04:00
|
|
|
let mut is_done = true;
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
// TODO is this loop correct ?
|
2018-04-25 19:01:52 -04:00
|
|
|
while let Some(result) = self.signal.as_mut().map(|signal| signal.poll_vec_change(cx)) {
|
2018-03-29 21:48:20 -04:00
|
|
|
match result {
|
|
|
|
Async::Ready(Some(change)) => return match change {
|
|
|
|
// TODO maybe it should play remove / insert animations for this ?
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::Replace { values } => {
|
2018-03-29 21:48:20 -04:00
|
|
|
self.animations = Vec::with_capacity(values.len());
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::Replace {
|
2018-03-29 21:48:20 -04:00
|
|
|
values: values.into_iter().map(|value| {
|
|
|
|
let state = AnimatedMapState {
|
|
|
|
animation: MutableAnimation::new_with_initial(self.duration, Percentage::new_unchecked(1.0)),
|
|
|
|
removing: None,
|
|
|
|
};
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
let value = (self.callback)(value, AnimatedMapBroadcaster(state.animation.raw_clone()));
|
2018-03-29 21:48:20 -04:00
|
|
|
|
|
|
|
self.animations.push(state);
|
|
|
|
|
|
|
|
value
|
|
|
|
}).collect()
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::InsertAt { index, value } => {
|
2018-03-29 21:48:20 -04:00
|
|
|
let index = self.find_index(index).unwrap_or_else(|| self.animations.len());
|
|
|
|
let state = self.animated_state();
|
2018-04-25 19:01:52 -04:00
|
|
|
let value = (self.callback)(value, AnimatedMapBroadcaster(state.animation.raw_clone()));
|
2018-03-29 21:48:20 -04:00
|
|
|
self.animations.insert(index, state);
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::InsertAt { index, value }))
|
2018-03-29 21:48:20 -04:00
|
|
|
},
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::Push { value } => {
|
2018-03-29 21:48:20 -04:00
|
|
|
let state = self.animated_state();
|
2018-04-25 19:01:52 -04:00
|
|
|
let value = (self.callback)(value, AnimatedMapBroadcaster(state.animation.raw_clone()));
|
2018-03-29 21:48:20 -04:00
|
|
|
self.animations.push(state);
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::Push { value }))
|
2018-03-29 21:48:20 -04:00
|
|
|
},
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::UpdateAt { index, value } => {
|
2018-03-29 21:48:20 -04:00
|
|
|
let index = self.find_index(index).expect("Could not find value");
|
|
|
|
let state = &self.animations[index];
|
2018-04-25 19:01:52 -04:00
|
|
|
let value = (self.callback)(value, AnimatedMapBroadcaster(state.animation.raw_clone()));
|
|
|
|
Async::Ready(Some(VecDiff::UpdateAt { index, value }))
|
2018-03-29 21:48:20 -04:00
|
|
|
},
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO test this
|
|
|
|
// TODO should this be treated as a removal + insertion ?
|
|
|
|
VecDiff::Move { old_index, new_index } => {
|
|
|
|
let old_index = self.find_index(old_index).expect("Could not find value");
|
|
|
|
|
|
|
|
let state = self.animations.remove(old_index);
|
|
|
|
|
|
|
|
let new_index = self.find_index(new_index).unwrap_or_else(|| self.animations.len());
|
|
|
|
|
|
|
|
self.animations.insert(new_index, state);
|
|
|
|
|
|
|
|
Async::Ready(Some(VecDiff::Move { old_index, new_index }))
|
|
|
|
},
|
|
|
|
|
|
|
|
VecDiff::RemoveAt { index } => {
|
2018-03-29 21:48:20 -04:00
|
|
|
let index = self.find_index(index).expect("Could not find value");
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
if self.should_remove(cx, index) {
|
2018-03-29 21:48:20 -04:00
|
|
|
self.remove_index(index)
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-03-29 21:48:20 -04:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::Pop {} => {
|
2018-03-29 21:48:20 -04:00
|
|
|
let index = self.find_last_index().expect("Cannot pop from empty vec");
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
if self.should_remove(cx, index) {
|
2018-03-29 21:48:20 -04:00
|
|
|
self.remove_index(index)
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-03-29 21:48:20 -04:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO maybe it should play remove animation for this ?
|
2018-04-25 19:01:52 -04:00
|
|
|
VecDiff::Clear {} => {
|
2018-03-29 21:48:20 -04:00
|
|
|
self.animations.clear();
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Ready(Some(VecDiff::Clear {}))
|
2018-03-29 21:48:20 -04:00
|
|
|
},
|
2018-03-12 20:09:44 -04:00
|
|
|
},
|
|
|
|
Async::Ready(None) => {
|
2018-03-29 21:48:20 -04:00
|
|
|
self.signal = None;
|
2018-03-12 20:09:44 -04:00
|
|
|
break;
|
|
|
|
},
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Pending => {
|
2018-03-12 20:09:44 -04:00
|
|
|
is_done = false;
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
let mut is_removing = false;
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
// TODO make this more efficient (e.g. using a similar strategy as FuturesUnordered)
|
2018-04-25 19:01:52 -04:00
|
|
|
// This uses rposition so that way it will return VecDiff::Pop in more situations
|
2018-03-15 06:54:18 -04:00
|
|
|
let index = self.animations.iter_mut().rposition(|state| {
|
|
|
|
if let Some(ref mut future) = state.removing {
|
2018-03-12 20:09:44 -04:00
|
|
|
is_removing = true;
|
2018-04-25 19:01:52 -04:00
|
|
|
future.poll(cx).unwrap().is_ready()
|
2018-03-12 20:09:44 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
match index {
|
|
|
|
Some(index) => {
|
|
|
|
self.remove_index(index)
|
|
|
|
},
|
|
|
|
None => if is_done && !is_removing {
|
|
|
|
Async::Ready(None)
|
|
|
|
|
|
|
|
} else {
|
2018-04-25 19:01:52 -04:00
|
|
|
Async::Pending
|
2018-03-12 20:09:44 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2018-03-12 20:09:44 -04:00
|
|
|
pub struct Percentage(f64);
|
|
|
|
|
|
|
|
impl Percentage {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(input: f64) -> Self {
|
|
|
|
debug_assert!(input >= 0.0 && input <= 1.0);
|
|
|
|
Self::new_unchecked(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn new_unchecked(input: f64) -> Self {
|
|
|
|
Percentage(input)
|
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn map<F>(self, f: F) -> Self where F: FnOnce(f64) -> f64 {
|
|
|
|
Self::new(f(self.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn map_unchecked<F>(self, f: F) -> Self where F: FnOnce(f64) -> f64 {
|
|
|
|
Self::new_unchecked(f(self.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn invert(self) -> Self {
|
|
|
|
// TODO use new instead ?
|
|
|
|
Self::new_unchecked(1.0 - self.0)
|
|
|
|
}
|
|
|
|
|
2018-03-12 20:09:44 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn range_inclusive(&self, low: f64, high: f64) -> f64 {
|
2018-03-15 06:54:18 -04:00
|
|
|
range_inclusive(self.0, low, high)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO figure out better name
|
|
|
|
#[inline]
|
|
|
|
pub fn into_f64(self) -> f64 {
|
|
|
|
self.0
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-06-08 12:00:33 -04:00
|
|
|
|
2018-06-08 12:05:15 -04:00
|
|
|
pub fn none_if(self, percentage: f64) -> Option<Self> {
|
|
|
|
if self.0 == percentage {
|
2018-06-08 12:00:33 -04:00
|
|
|
None
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
#[inline]
|
|
|
|
fn range_inclusive(percentage: f64, low: f64, high: f64) -> f64 {
|
|
|
|
low + (percentage * (high - low))
|
|
|
|
}
|
|
|
|
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub struct MutableAnimationSignal(MutableSignal<Percentage>);
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
impl Signal for MutableAnimationSignal {
|
|
|
|
type Item = Percentage;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
fn poll_change(&mut self, cx: &mut Context) -> Async<Option<Self::Item>> {
|
|
|
|
self.0.poll_change(cx)
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct MutableAnimationState {
|
|
|
|
playing: bool,
|
|
|
|
duration: f64,
|
|
|
|
end: Percentage,
|
|
|
|
animating: Option<DiscardOnDrop<CancelableFutureHandle>>,
|
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
struct MutableAnimationInner {
|
2018-06-11 16:54:47 -04:00
|
|
|
state: Mutex<MutableAnimationState>,
|
2018-04-25 19:01:52 -04:00
|
|
|
value: Mutable<Percentage>,
|
|
|
|
}
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub struct MutableAnimation {
|
2018-06-11 16:54:47 -04:00
|
|
|
inner: Arc<MutableAnimationInner>,
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-06-06 08:51:34 -04:00
|
|
|
impl fmt::Debug for MutableAnimation {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-11 16:54:47 -04:00
|
|
|
let state = self.inner.state.lock().unwrap();
|
2018-06-06 08:51:34 -04:00
|
|
|
|
|
|
|
fmt.debug_struct("MutableAnimation")
|
|
|
|
.field("playing", &state.playing)
|
|
|
|
.field("duration", &state.duration)
|
|
|
|
.field("current", &self.inner.value.get())
|
|
|
|
.field("end", &state.end)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
impl MutableAnimation {
|
|
|
|
#[inline]
|
|
|
|
pub fn new_with_initial(duration: f64, initial: Percentage) -> Self {
|
|
|
|
debug_assert!(duration >= 0.0);
|
|
|
|
|
|
|
|
Self {
|
2018-06-11 16:54:47 -04:00
|
|
|
inner: Arc::new(MutableAnimationInner {
|
|
|
|
state: Mutex::new(MutableAnimationState {
|
2018-04-25 19:01:52 -04:00
|
|
|
playing: true,
|
|
|
|
duration: duration,
|
|
|
|
end: initial,
|
|
|
|
animating: None,
|
|
|
|
}),
|
2018-03-12 20:09:44 -04:00
|
|
|
value: Mutable::new(initial),
|
2018-04-25 19:01:52 -04:00
|
|
|
}),
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn new(duration: f64) -> Self {
|
|
|
|
Self::new_with_initial(duration, Percentage::new_unchecked(0.0))
|
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
fn raw_clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: self.inner.clone(),
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
fn stop_animating(lock: &mut MutableAnimationState) {
|
|
|
|
lock.animating = None;
|
|
|
|
}
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
fn start_animating(&self, lock: &mut MutableAnimationState) {
|
|
|
|
if lock.playing {
|
|
|
|
// TODO use Copy constraint to make value.get() faster ?
|
2018-05-24 06:22:47 -04:00
|
|
|
let start: f64 = self.inner.value.get().into_f64();
|
|
|
|
let end: f64 = lock.end.into_f64();
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
if start != end {
|
|
|
|
if lock.duration > 0.0 {
|
|
|
|
let duration = (end - start).abs() * lock.duration;
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
let state = self.raw_clone();
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
let mut starting_time = None;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
lock.animating = Some(spawn_future(
|
|
|
|
timestamps()
|
|
|
|
.for_each(move |current_time| {
|
|
|
|
if let Some(current_time) = current_time {
|
|
|
|
let starting_time = *starting_time.get_or_insert(current_time);
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
let diff = (current_time - starting_time) / duration;
|
2018-03-16 02:50:21 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO don't update if the new value is the same as the old value
|
|
|
|
if diff >= 1.0 {
|
|
|
|
{
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = state.inner.state.lock().unwrap();
|
2018-04-25 19:01:52 -04:00
|
|
|
Self::stop_animating(&mut lock);
|
2018-03-16 02:50:21 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
state.inner.value.set(Percentage::new_unchecked(end));
|
2018-03-16 02:50:21 -04:00
|
|
|
|
|
|
|
} else {
|
2018-04-25 19:01:52 -04:00
|
|
|
state.inner.value.set(Percentage::new_unchecked(range_inclusive(diff, start, end)));
|
2018-03-16 02:50:21 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
));
|
2018-03-15 06:54:18 -04:00
|
|
|
|
|
|
|
} else {
|
2018-04-25 19:01:52 -04:00
|
|
|
Self::stop_animating(lock);
|
|
|
|
self.inner.value.set(Percentage::new_unchecked(end));
|
2018-03-15 06:54:18 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO is this necessary ?
|
|
|
|
Self::stop_animating(lock);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn set_duration(&self, duration: f64) {
|
|
|
|
debug_assert!(duration >= 0.0);
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.inner.state.lock().unwrap();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
if lock.duration != duration {
|
|
|
|
lock.duration = duration;
|
|
|
|
self.start_animating(&mut lock);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn pause(&self) {
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.inner.state.lock().unwrap();
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
if lock.playing {
|
|
|
|
lock.playing = false;
|
|
|
|
Self::stop_animating(&mut lock);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn play(&self) {
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.inner.state.lock().unwrap();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
if !lock.playing {
|
|
|
|
lock.playing = true;
|
|
|
|
self.start_animating(&mut lock);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
fn _jump_to(mut lock: &mut MutableAnimationState, mutable: &Mutable<Percentage>, end: Percentage) {
|
|
|
|
Self::stop_animating(&mut lock);
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
lock.end = end;
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
// TODO use Copy constraint to make value.get() faster ?
|
|
|
|
if mutable.get() != end {
|
|
|
|
mutable.set(end);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
pub fn jump_to(&self, end: Percentage) {
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.inner.state.lock().unwrap();
|
2018-03-12 20:09:44 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
Self::_jump_to(&mut lock, &self.inner.value, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn animate_to(&self, end: Percentage) {
|
2018-06-11 16:54:47 -04:00
|
|
|
let mut lock = self.inner.state.lock().unwrap();
|
2018-04-25 19:01:52 -04:00
|
|
|
|
|
|
|
if lock.end != end {
|
|
|
|
if lock.duration <= 0.0 {
|
|
|
|
Self::_jump_to(&mut lock, &self.inner.value, end);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
lock.end = end;
|
|
|
|
self.start_animating(&mut lock);
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 19:01:52 -04:00
|
|
|
}
|
2018-03-15 06:54:18 -04:00
|
|
|
|
2018-04-25 19:01:52 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn signal(&self) -> MutableAnimationSignal {
|
|
|
|
MutableAnimationSignal(self.inner.value.signal())
|
2018-03-15 06:54:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub mod easing {
|
|
|
|
use super::Percentage;
|
|
|
|
|
|
|
|
// TODO should this use map rather than map_unchecked ?
|
|
|
|
#[inline]
|
|
|
|
pub fn powi(p: Percentage, n: i32) -> Percentage {
|
|
|
|
p.map_unchecked(|p| p.powi(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn cubic(p: Percentage) -> Percentage {
|
|
|
|
powi(p, 3)
|
|
|
|
}
|
|
|
|
|
2018-03-17 07:17:06 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn out<F>(p: Percentage, f: F) -> Percentage where F: FnOnce(Percentage) -> Percentage {
|
|
|
|
f(p.invert()).invert()
|
|
|
|
}
|
|
|
|
|
2018-03-15 06:54:18 -04:00
|
|
|
pub fn in_out<F>(p: Percentage, f: F) -> Percentage where F: FnOnce(Percentage) -> Percentage {
|
|
|
|
p.map_unchecked(|p| {
|
|
|
|
if p <= 0.5 {
|
|
|
|
f(Percentage::new_unchecked(p * 2.0)).into_f64() / 2.0
|
|
|
|
|
|
|
|
} else {
|
|
|
|
1.0 - (f(Percentage::new_unchecked((1.0 - p) * 2.0)).into_f64() / 2.0)
|
|
|
|
}
|
|
|
|
})
|
2018-03-12 20:09:44 -04:00
|
|
|
}
|
|
|
|
}
|