use ferrokit_core::kept::{UiCell, UiLocal};
use std::cell::{Cell, RefCell};
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject};
use objc2::{MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{NSAnimationContext, NSView, NSWorkspace};
use objc2_foundation::{NSPoint, NSRect, NSSize};
use crate::Edge;
use crate::signal::{Signal, batch};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Animation {
duration: f64,
curve: Curve,
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Curve {
EaseInOut,
EaseOut,
EaseIn,
Linear,
Spring { response: f64, damping: f64 },
}
impl Curve {
fn eased(self, t: f64) -> f64 {
let t = t.clamp(0.0, 1.0);
let (x1, y1, x2, y2) = match self {
Curve::EaseInOut => (0.42, 0.0, 0.58, 1.0),
Curve::EaseOut => (0.0, 0.0, 0.58, 1.0),
Curve::EaseIn => (0.42, 0.0, 1.0, 1.0),
Curve::Linear | Curve::Spring { .. } => return t,
};
let bez = |a: f64, b: f64, u: f64| 3.0 * a * u * (1.0 - u).powi(2) + 3.0 * b * u * u * (1.0 - u) + u.powi(3);
let (mut lo, mut hi) = (0.0, 1.0);
for _ in 0..40 {
let mid = (lo + hi) / 2.0;
if bez(x1, x2, mid) < t { lo = mid } else { hi = mid }
}
bez(y1, y2, (lo + hi) / 2.0)
}
}
fn spring_at(t: f64, response: f64, damping: f64) -> f64 {
let w0 = std::f64::consts::TAU / response.max(0.01);
if damping < 1.0 {
let wd = w0 * (1.0 - damping * damping).sqrt();
1.0 - (-damping * w0 * t).exp() * ((wd * t).cos() + damping * w0 / wd * (wd * t).sin())
} else {
1.0 - (-w0 * t).exp() * (1.0 + w0 * t)
}
}
impl Default for Animation {
fn default() -> Self {
Animation { duration: 0.25, curve: Curve::EaseInOut }
}
}
impl Animation {
pub fn ease_in_out(seconds: f64) -> Animation {
Animation { duration: seconds, curve: Curve::EaseInOut }
}
pub fn ease_out(seconds: f64) -> Animation {
Animation { duration: seconds, curve: Curve::EaseOut }
}
pub fn ease_in(seconds: f64) -> Animation {
Animation { duration: seconds, curve: Curve::EaseIn }
}
pub fn linear(seconds: f64) -> Animation {
Animation { duration: seconds, curve: Curve::Linear }
}
pub fn spring(response: f64, damping: f64) -> Animation {
let (response, damping) = (response.max(0.05), damping.clamp(0.05, 1.0));
let w0 = std::f64::consts::TAU / response;
let settle = if damping < 1.0 { 6.9 / (damping * w0) } else { 9.3 / w0 };
Animation { duration: settle.min(4.0), curve: Curve::Spring { response, damping } }
}
pub fn bouncy() -> Animation {
Animation::spring(0.5, 0.6)
}
pub fn snappy() -> Animation {
Animation::spring(0.3, 0.85)
}
pub fn smooth() -> Animation {
Animation::spring(0.5, 1.0)
}
pub fn progress(&self, seconds: f64) -> f64 {
match self.curve {
Curve::Spring { response, damping } if seconds < self.duration => spring_at(seconds, response, damping),
Curve::Spring { .. } => 1.0,
c => c.eased(if self.duration > 0.0 { seconds / self.duration } else { 1.0 }),
}
}
fn is_spring(&self) -> bool {
matches!(self.curve, Curve::Spring { .. })
}
pub fn duration(&self) -> f64 {
self.duration
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Transition {
Fade,
Slide(Edge),
}
const SLIDE: f64 = 24.0;
struct Entrance {
views: Vec<Retained<NSView>>,
transition: Transition,
alpha: f64,
}
thread_local! {
static PENDING: Cell<Option<Animation>> = const { Cell::new(None) };
static RUNNING: Cell<bool> = const { Cell::new(false) };
static QUEUED: UiCell<Vec<Box<dyn FnOnce()>>> = UiCell::new(Vec::new());
static ENTERING: UiCell<Vec<Entrance>> = const { UiCell::new(Vec::new()) };
static EXITING: UiCell<Vec<(Vec<Retained<NSView>>, Transition)>> = const { UiCell::new(Vec::new()) };
static LEAVING: Cell<usize> = const { Cell::new(0) };
static REDUCE_MOTION: Cell<Option<bool>> = const { Cell::new(None) };
static SPRING: Cell<Option<Animation>> = const { Cell::new(None) };
static FRAMES: UiCell<std::collections::HashMap<usize, FrameJob>> = UiCell::new(std::collections::HashMap::new());
static MATCH_FROM: UiCell<std::collections::HashMap<String, NSRect>> = UiCell::new(std::collections::HashMap::new());
static MATCH_TO: UiCell<Vec<(String, Vec<Retained<NSView>>)>> = const { UiCell::new(Vec::new()) };
static PLACED: UiCell<std::collections::HashMap<usize, objc2::rc::Weak<NSView>>> = UiCell::new(std::collections::HashMap::new());
}
fn key(view: &NSView) -> usize {
view as *const NSView as usize
}
fn placed(view: &NSView) -> bool {
PLACED.with_borrow(|p| p.get(&key(view)).and_then(|w| w.load()).is_some_and(|v| std::ptr::eq(&*v, view)))
}
fn mark_placed(view: &NSView) -> bool {
if placed(view) {
return false;
}
PLACED.with_borrow_mut(|p| {
if p.len() >= SWEEP_AT.with(Cell::get) {
p.retain(|_, w| w.load().is_some());
SWEEP_AT.with(|s| s.set((p.len() * 2).max(4096)));
}
p.insert(key(view), objc2::rc::Weak::from(view));
});
true
}
pub fn animated<R>(f: impl FnOnce() -> R) -> R {
animated_with(Animation::default(), f)
}
pub fn animated_with<R>(animation: Animation, f: impl FnOnce() -> R) -> R {
PENDING.with(|p| p.set(Some(animation)));
batch(f)
}
pub fn reduce_motion() -> bool {
REDUCE_MOTION.with(Cell::get).unwrap_or_else(|| NSWorkspace::sharedWorkspace().accessibilityDisplayShouldReduceMotion())
}
pub(crate) fn set_reduce_motion(on: Option<bool>) {
REDUCE_MOTION.with(|r| r.set(on));
}
pub(crate) fn animating() -> bool {
PENDING.try_with(Cell::get).ok().flatten().is_some() || RUNNING.try_with(Cell::get).unwrap_or(false)
}
pub(crate) fn apply(change: impl FnOnce() + 'static) {
if PENDING.with(Cell::get).is_some() {
QUEUED.with_borrow_mut(|q| q.push(Box::new(change)));
} else {
change();
}
}
pub(crate) fn enter(views: Vec<Retained<NSView>>, transition: Transition, alpha: f64) {
for v in &views {
v.setWantsLayer(true);
v.setAlphaValue(0.0);
}
ENTERING.with_borrow_mut(|e| e.push(Entrance { views, transition, alpha }));
}
pub(crate) fn exit(views: Vec<Retained<NSView>>, transition: Transition) {
EXITING.with_borrow_mut(|e| e.push((views, transition)));
}
pub(crate) fn leaving() -> usize {
LEAVING.with(Cell::get)
}
pub(crate) fn set_frame(view: &NSView, frame: NSRect) {
let unplaced = mark_placed(view);
let flying = FRAMES.try_with(|f| f.borrow().get(&key(view)).map(|j| j.to)).ok().flatten();
if flying == Some(frame) {
return;
}
if flying.is_some() {
FRAMES.with_borrow_mut(|f| f.remove(&key(view)));
}
match (RUNNING.with(Cell::get), SPRING.with(Cell::get)) {
(true, _) if unplaced => without_animation(|| view.setFrame(frame)),
(true, Some(spring)) if view.frame() != frame => fly(view, view.frame(), frame, spring),
_ => view.setFrame(frame),
}
}
struct FrameJob {
view: objc2::rc::Weak<NSView>,
from: NSRect,
to: NSRect,
start: f64,
animation: Animation,
}
fn fly(view: &NSView, from: NSRect, to: NSRect, spring: Animation) {
without_animation(|| view.setFrame(from));
let job = FrameJob { view: objc2::rc::Weak::from(view), from, to, start: now(), animation: spring };
FRAMES.with_borrow_mut(|f| f.insert(key(view), job));
every_frame(FRAMES_KEY, Box::new(|_| {
let t = now();
let mut done = Vec::new();
without_animation(|| {
FRAMES.with_borrow(|frames| {
for (k, j) in frames {
let Some(v) = j.view.load() else {
done.push(*k);
continue;
};
let elapsed = t - j.start;
if elapsed >= j.animation.duration {
v.setFrame(j.to);
done.push(*k);
} else {
v.setFrame(lerp(j.from, j.to, j.animation.progress(elapsed)));
}
}
});
});
FRAMES.with_borrow_mut(|f| {
for k in done {
f.remove(&k);
}
!f.is_empty()
})
}));
}
fn lerp(a: NSRect, b: NSRect, p: f64) -> NSRect {
let l = |x: f64, y: f64| x + (y - x) * p;
NSRect::new(NSPoint::new(l(a.origin.x, b.origin.x), l(a.origin.y, b.origin.y)), NSSize::new(l(a.size.width, b.size.width).max(0.0), l(a.size.height, b.size.height).max(0.0)))
}
pub(crate) fn matched_leaving(id: &str, views: &[Retained<NSView>]) {
if !animating() {
return;
}
if let Some(rect) = union_in_window(views) {
let _ = MATCH_FROM.try_with(|m| m.borrow_mut().insert(id.to_string(), rect));
}
}
pub(crate) fn matched_entering(id: &str, views: Vec<Retained<NSView>>) {
if animating() {
MATCH_TO.with_borrow_mut(|m| m.push((id.to_string(), views)));
}
}
fn in_window(v: &NSView) -> Option<NSRect> {
let sup = unsafe { v.superview() }?;
Some(sup.convertRect_toView(v.frame(), None))
}
fn union_in_window(views: &[Retained<NSView>]) -> Option<NSRect> {
views.iter().filter_map(|v| in_window(v)).reduce(|a, b| {
let (x0, y0) = (a.origin.x.min(b.origin.x), a.origin.y.min(b.origin.y));
let (x1, y1) = ((a.origin.x + a.size.width).max(b.origin.x + b.size.width), (a.origin.y + a.size.height).max(b.origin.y + b.size.height));
NSRect::new(NSPoint::new(x0, y0), NSSize::new(x1 - x0, y1 - y0))
})
}
fn run_matched(reduce: bool, spring: Option<Animation>) {
let arrivals = MATCH_TO.with_borrow_mut(std::mem::take);
let mut from = MATCH_FROM.with_borrow_mut(std::mem::take);
if reduce {
return;
}
for (id, views) in arrivals {
let (Some(old), Some(new)) = (from.remove(&id), union_in_window(&views)) else { continue };
let (sx, sy) = (old.size.width / new.size.width.max(1.0), old.size.height / new.size.height.max(1.0));
for v in &views {
let (Some(sup), Some(target)) = (unsafe { v.superview() }, in_window(v)) else { continue };
let start = NSRect::new(
NSPoint::new(old.origin.x + (target.origin.x - new.origin.x) * sx, old.origin.y + (target.origin.y - new.origin.y) * sy),
NSSize::new(target.size.width * sx, target.size.height * sy),
);
let start = sup.convertRect_fromView(start, None);
let end = v.frame();
match spring {
Some(s) => fly(v, start, end, s),
None => {
without_animation(|| v.setFrame(start));
v.setFrame(end);
}
}
}
}
}
#[link(name = "QuartzCore", kind = "framework")]
unsafe extern "C" {
fn CACurrentMediaTime() -> f64;
}
const FRAMES_KEY: u64 = u64::MAX;
type Step = Box<dyn FnMut(f64) -> bool>;
thread_local! {
static JOBS: UiCell<Vec<(u64, f64, Step)>> = const { UiCell::new(Vec::new()) };
static STOPPED: UiCell<Vec<u64>> = const { UiCell::new(Vec::new()) };
static SWEEP_AT: Cell<usize> = const { Cell::new(4096) };
static TIMER: UiCell<Option<Retained<AnyObject>>> = const { UiCell::new(None) };
static SKEW: Cell<f64> = const { Cell::new(0.0) };
}
fn now() -> f64 {
(unsafe { CACurrentMediaTime() }) + SKEW.with(Cell::get)
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitMotionTicker"]
struct Ticker;
impl Ticker {
#[unsafe(method(tick:))]
fn tick(&self, _timer: &AnyObject) {
crate::failure::guard("an animation frame (tick)", (), || {
tick();
})
}
}
);
fn every_frame(key: u64, step: Step) {
let start = now();
JOBS.with_borrow_mut(|j| {
j.retain(|(k, ..)| *k != key);
j.push((key, start, step));
});
if TIMER.with_borrow(Option::is_none) {
let mtm = crate::mtm();
let target = Ticker::alloc(mtm).set_ivars(());
let timer: Retained<AnyObject> = unsafe {
let target: Retained<Ticker> = msg_send![super(target), init];
let timer: Retained<AnyObject> = msg_send![objc2::class!(NSTimer), timerWithTimeInterval: 1.0 / 120.0, target: &*target, selector: sel!(tick:), userInfo: Option::<&AnyObject>::None, repeats: true];
let run_loop: Retained<AnyObject> = msg_send![objc2::class!(NSRunLoop), mainRunLoop];
let () = msg_send![&*run_loop, addTimer: &*timer, forMode: objc2_foundation::NSRunLoopCommonModes];
timer
};
TIMER.with_borrow_mut(|t| *t = Some(timer));
}
}
fn tick() {
let t = now();
let running = JOBS.with_borrow_mut(std::mem::take);
STOPPED.with_borrow_mut(Vec::clear);
let mut kept = Vec::new();
crate::failure::entry("an animation frame", (), || {
for (key, start, mut step) in running {
if step(t - start) {
kept.push((key, start, step));
}
}
});
JOBS.with_borrow_mut(|j| {
let stopped = STOPPED.with_borrow_mut(std::mem::take);
kept.retain(|(k, ..)| !j.iter().any(|(n, ..)| n == k) && !stopped.contains(k));
j.extend(kept);
if j.is_empty() {
TIMER.with_borrow_mut(|timer| {
if let Some(timer) = timer.take() {
let () = unsafe { msg_send![&*timer, invalidate] };
}
});
}
});
}
pub(crate) fn step(seconds: f64) {
SKEW.with(|s| s.set(s.get() + seconds));
tick();
}
pub(crate) fn running() -> usize {
JOBS.with_borrow(Vec::len)
}
pub fn animate(value: &Signal<f64>, to: f64, animation: Animation) {
keyframes(value, Keyframes { start: None, steps: vec![(to, animation)], repeat: false });
}
#[derive(Debug, Clone, PartialEq)]
pub struct Keyframes {
start: Option<f64>,
steps: Vec<(f64, Animation)>,
repeat: bool,
}
impl Keyframes {
pub fn new(value: f64) -> Keyframes {
Keyframes { start: Some(value), steps: Vec::new(), repeat: false }
}
pub fn from_current() -> Keyframes {
Keyframes { start: None, steps: Vec::new(), repeat: false }
}
pub fn to(self, value: f64, seconds: f64) -> Keyframes {
self.with(value, Animation::ease_in_out(seconds))
}
pub fn linear(self, value: f64, seconds: f64) -> Keyframes {
self.with(value, Animation::linear(seconds))
}
pub fn with(mut self, value: f64, animation: Animation) -> Keyframes {
self.steps.push((value, animation));
self
}
pub fn repeating(mut self) -> Keyframes {
self.repeat = true;
self
}
pub fn duration(&self) -> f64 {
self.steps.iter().map(|(_, a)| a.duration).sum()
}
}
pub fn keyframes(value: &Signal<f64>, frames: Keyframes) {
let value = *value;
if !value.is_alive() || frames.steps.is_empty() {
return;
}
if reduce_motion() {
let rest = if frames.repeat { frames.start.unwrap_or_else(|| value.get_untracked()) } else { frames.steps.last().map(|s| s.0).unwrap_or(0.0) };
value.set(rest);
return;
}
let start = frames.start.unwrap_or_else(|| value.get_untracked());
let total = frames.duration().max(1e-6);
every_frame(value.key(), Box::new(move |elapsed| {
if !value.is_alive() {
return false;
}
let mut t = if frames.repeat { elapsed % total } else { elapsed };
let mut from = start;
for (to, animation) in &frames.steps {
if t < animation.duration {
value.set(from + (to - from) * animation.progress(t));
return true;
}
t -= animation.duration;
from = *to;
}
value.set(from);
frames.repeat
}));
}
pub fn stop_animation(value: &Signal<f64>) {
let key = value.key();
JOBS.with_borrow_mut(|j| j.retain(|(k, ..)| *k != key));
STOPPED.with_borrow_mut(|s| s.push(key));
}
fn without_animation(f: impl FnOnce()) {
NSAnimationContext::beginGrouping();
let cx = NSAnimationContext::currentContext();
cx.setDuration(0.0);
cx.setAllowsImplicitAnimation(false);
f();
NSAnimationContext::endGrouping();
}
pub(crate) fn run_layout(layout: impl FnOnce()) {
let Some(animation) = PENDING.with(|p| p.take()) else {
layout();
return;
};
let reduce = reduce_motion();
let mut layout = Some(layout);
if reduce {
if let Some(l) = layout.take() {
l();
}
}
NSAnimationContext::beginGrouping();
let cx = NSAnimationContext::currentContext();
let spring = animation.is_spring().then_some(animation);
cx.setDuration(match animation.curve {
Curve::Spring { response, .. } => response * 0.7,
_ => animation.duration,
});
cx.setAllowsImplicitAnimation(true);
let name = unsafe {
use objc2_quartz_core::*;
match animation.curve {
Curve::EaseInOut => kCAMediaTimingFunctionEaseInEaseOut,
Curve::EaseOut | Curve::Spring { .. } => kCAMediaTimingFunctionEaseOut,
Curve::EaseIn => kCAMediaTimingFunctionEaseIn,
Curve::Linear => kCAMediaTimingFunctionLinear,
}
};
cx.setTimingFunction(Some(&objc2_quartz_core::CAMediaTimingFunction::functionWithName(name)));
RUNNING.with(|r| r.set(true));
SPRING.with(|s| s.set(if reduce { None } else { spring }));
if let Some(l) = layout.take() {
l();
}
run_matched(reduce, spring);
for change in QUEUED.with_borrow_mut(std::mem::take) {
change();
}
for e in ENTERING.with_borrow_mut(std::mem::take) {
if let (Transition::Slide(edge), false) = (e.transition, reduce) {
for v in e.views.iter().filter(|v| placed(v)) {
let end = v.frame();
without_animation(|| v.setFrame(offset(end, edge)));
v.setFrame(end);
}
}
for v in &e.views {
v.setAlphaValue(e.alpha);
}
}
let exits = EXITING.with_borrow_mut(std::mem::take);
if !exits.is_empty() {
let gone: Vec<Retained<NSView>> = exits.iter().flat_map(|(v, _)| v.iter().cloned()).collect();
LEAVING.with(|l| l.set(l.get() + gone.len()));
let gone = RefCell::new(Some(gone));
let done = block2::RcBlock::new(move || {
if let Some(views) = gone.borrow_mut().take() {
LEAVING.with(|l| l.set(l.get().saturating_sub(views.len())));
for v in views {
v.removeFromSuperview();
}
}
});
cx.setCompletionHandler(Some(&done));
}
for (views, transition) in exits {
for v in &views {
if let (Transition::Slide(edge), false) = (transition, reduce) {
v.setFrame(offset(v.frame(), edge));
}
v.setAlphaValue(0.0);
}
}
RUNNING.with(|r| r.set(false));
SPRING.with(|s| s.set(None));
NSAnimationContext::endGrouping();
}
fn offset(mut frame: NSRect, edge: Edge) -> NSRect {
let edge = match (edge, crate::right_to_left()) {
(Edge::Leading, true) => Edge::Trailing,
(Edge::Trailing, true) => Edge::Leading,
(e, _) => e,
};
match edge {
Edge::Above => frame.origin.y -= SLIDE,
Edge::Below => frame.origin.y += SLIDE,
Edge::Leading => frame.origin.x -= SLIDE,
Edge::Trailing => frame.origin.x += SLIDE,
}
frame
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn springs_settle() {
let bouncy = Animation::bouncy();
assert_eq!(bouncy.progress(0.0), 0.0);
assert_eq!(bouncy.progress(bouncy.duration()), 1.0);
let peak = (1..200).map(|i| bouncy.progress(i as f64 * 0.01)).fold(0.0, f64::max);
assert!(peak > 1.05 && peak < 1.3, "peak {peak}");
assert!((bouncy.progress(bouncy.duration() - 0.01) - 1.0).abs() < 0.01);
let smooth = Animation::smooth();
assert!((1..400).all(|i| smooth.progress(i as f64 * 0.01) <= 1.0 + 1e-9));
assert!(Animation::snappy().duration() < bouncy.duration());
}
#[test]
fn easings_meet_their_ends() {
for a in [Animation::ease_in_out(1.0), Animation::ease_in(1.0), Animation::ease_out(1.0), Animation::linear(1.0)] {
assert!(a.progress(0.0).abs() < 1e-6 && (a.progress(1.0) - 1.0).abs() < 1e-6, "{a:?}");
}
assert!(Animation::ease_out(1.0).progress(0.25) > 0.25 && Animation::ease_in(1.0).progress(0.25) < 0.25);
assert!((Animation::ease_in_out(1.0).progress(0.5) - 0.5).abs() < 1e-3);
}
}