use crate::compositor::layout::cell::CellBuffer;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum AnimateAllocationSite {
FrameCollection,
TransitionCollection,
WasmSwapBuffer,
Payload,
BuildNotices,
}
#[cfg(test)]
thread_local! {
static REJECT_ANIMATE_ALLOCATION: std::cell::Cell<Option<AnimateAllocationSite>> =
const { std::cell::Cell::new(None) };
}
#[cfg(test)]
pub(crate) struct AnimateRejectionGuard;
#[cfg(test)]
impl AnimateRejectionGuard {
pub(crate) fn at(site: AnimateAllocationSite) -> Self {
REJECT_ANIMATE_ALLOCATION.with(|rejected| rejected.set(Some(site)));
Self
}
}
#[cfg(test)]
impl Drop for AnimateRejectionGuard {
fn drop(&mut self) {
REJECT_ANIMATE_ALLOCATION.with(|rejected| rejected.set(None));
}
}
#[cfg(test)]
pub(crate) fn reject_animate_allocation(site: AnimateAllocationSite) -> bool {
REJECT_ANIMATE_ALLOCATION.with(|rejected| rejected.get() == Some(site))
}
#[cfg(not(test))]
pub(crate) fn reject_animate_allocation(_site: AnimateAllocationSite) -> bool {
false
}
use crate::compositor::scene::{NodeId, Patch};
pub mod effect;
pub mod frame;
pub mod page_transition;
pub mod runtime;
pub mod transition;
pub mod tween;
pub mod wasm;
pub use effect::TextEffectAdapter;
pub use frame::FrameAnimationAdapter;
pub use page_transition::{PAGE_TRANSITION_ID, PageTransitionAdapter};
pub use runtime::{AnimState, AnimationRuntime};
pub use transition::TransitionAdapter;
pub use tween::TweenAdapter;
pub use wasm::WasmAnimationAdapter;
#[doc(hidden)]
pub struct AnimationResizeCandidate {
pub(crate) width: u16,
pub(crate) height: u16,
pub(crate) swap_buffer: CellBuffer,
pub(crate) output_buffer: CellBuffer,
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AnimationResizeRejected;
pub trait Animation {
fn id(&self) -> &str;
fn advance(&mut self, ctx: &mut AdvanceCtx) -> AdvanceResult;
fn advance_with_scene(
&mut self,
ctx: &mut AdvanceCtx,
_scene: &mut crate::compositor::scene::Scene,
) -> AdvanceResult {
self.advance(ctx)
}
fn finished(&self) -> bool;
fn state(&self) -> runtime::AnimState;
fn background(&self) -> bool {
false
}
fn fast_forwardable(&self) -> bool {
!self.background()
}
fn paints_buffer(&self) -> bool {
true
}
fn paint(&self, _scene: &mut crate::compositor::scene::Scene) {}
fn memory_bytes(&self) -> usize {
0
}
fn next_notice_capacity_bound(&self) -> usize {
0
}
fn trigger_start(&mut self, _now: std::time::Instant) {}
fn trigger_stop(&mut self) {}
fn skip(&mut self) -> AdvanceResult {
self.trigger_stop();
AdvanceResult::none()
}
fn skip_with_scene(&mut self, _scene: &mut crate::compositor::scene::Scene) -> AdvanceResult {
self.skip()
}
fn prepare_resize(
&self,
_scene: &crate::compositor::scene::Scene,
) -> Result<Option<AnimationResizeCandidate>, AnimationResizeRejected> {
Ok(None)
}
fn commit_resize(
&mut self,
_scene: &mut crate::compositor::scene::Scene,
_candidate: AnimationResizeCandidate,
) {
}
}
pub struct AdvanceCtx<'a> {
pub now: std::time::Instant,
pub viewport_offset: u16,
pub viewport_height: u16,
pub finished_ids: &'a [String],
}
impl<'a> AdvanceCtx<'a> {
pub fn new(
now: std::time::Instant,
viewport_offset: u16,
viewport_height: u16,
finished_ids: &'a [String],
) -> Self {
Self {
now,
viewport_offset,
viewport_height,
finished_ids,
}
}
}
#[derive(Debug, Default)]
pub struct AdvanceResult {
pub patch: Option<Patch>,
pub wrote_buffer: Option<NodeId>,
pub notice: Option<String>,
}
impl AdvanceResult {
pub fn none() -> Self {
Self::default()
}
pub fn with_buffer(node: NodeId) -> Self {
Self {
patch: None,
wrote_buffer: Some(node),
notice: None,
}
}
pub fn with_patch(patch: Patch) -> Self {
Self {
patch: Some(patch),
wrote_buffer: None,
notice: None,
}
}
pub fn is_noop(&self) -> bool {
self.patch.is_none() && self.wrote_buffer.is_none()
}
}
#[cfg(test)]
mod tests {
use super::*;
struct DummyAnim {
id: String,
done: bool,
}
impl Animation for DummyAnim {
fn id(&self) -> &str {
&self.id
}
fn advance(&mut self, _ctx: &mut AdvanceCtx) -> AdvanceResult {
self.done = true;
AdvanceResult::none()
}
fn finished(&self) -> bool {
self.done
}
fn state(&self) -> runtime::AnimState {
if self.done {
runtime::AnimState::Finished
} else {
runtime::AnimState::Running
}
}
}
#[test]
fn trait_dispatches_via_trait_object() {
let mut a: Box<dyn Animation> = Box::new(DummyAnim {
id: "d".into(),
done: false,
});
assert!(!a.finished());
let finished: Vec<String> = Vec::new();
let mut ctx = AdvanceCtx::new(std::time::Instant::now(), 0, 24, &finished);
let r = a.advance(&mut ctx);
assert!(r.is_noop());
assert!(a.finished());
}
#[test]
fn advance_result_helpers() {
assert!(AdvanceResult::none().is_noop());
let changed = AdvanceResult::with_patch(Patch::SetScroll { offset: 1 });
assert!(!changed.is_noop());
}
}