use std::collections::HashMap;
use arc_tokens::{ArcMotionTokens, SpringSpec};
use gpui_kit::{
App, Bounds, Corners, ElementId, Global, Pixels, Point, SharedString, Window, bounds, point,
px, size,
};
use crate::{clock::motion_now, spring::SpringSolver};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LayoutSnapshot {
pub bounds: Bounds<Pixels>,
pub corner_radii: Corners<Pixels>,
}
impl LayoutSnapshot {
pub fn new(bounds: Bounds<Pixels>, corner_radii: Corners<Pixels>) -> Self {
Self {
bounds,
corner_radii,
}
}
pub fn uniform_radius(bounds: Bounds<Pixels>, radius: Pixels) -> Self {
Self {
bounds,
corner_radii: Corners::all(radius),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LayoutTarget {
pub bounds: Bounds<Pixels>,
pub corner_radii: Corners<Pixels>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LayoutFlipSample {
pub bounds: Bounds<Pixels>,
pub corner_radii: Corners<Pixels>,
pub settled: bool,
}
#[derive(Clone, Debug)]
struct ActiveFlip {
from: LayoutSnapshot,
to: LayoutSnapshot,
started_at_secs: f64,
spec: SpringSpec,
}
impl ActiveFlip {
fn sample(&self, now_secs: f64) -> LayoutFlipSample {
let elapsed = (now_secs - self.started_at_secs).max(0.0);
let x = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.bounds.origin.x)),
f64::from(f32::from(self.to.bounds.origin.x)),
0.0,
)
.sample(elapsed);
let y = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.bounds.origin.y)),
f64::from(f32::from(self.to.bounds.origin.y)),
0.0,
)
.sample(elapsed);
let w = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.bounds.size.width)),
f64::from(f32::from(self.to.bounds.size.width)),
0.0,
)
.sample(elapsed);
let h = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.bounds.size.height)),
f64::from(f32::from(self.to.bounds.size.height)),
0.0,
)
.sample(elapsed);
let r_tl = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.corner_radii.top_left)),
f64::from(f32::from(self.to.corner_radii.top_left)),
0.0,
)
.sample(elapsed);
let r_tr = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.corner_radii.top_right)),
f64::from(f32::from(self.to.corner_radii.top_right)),
0.0,
)
.sample(elapsed);
let r_br = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.corner_radii.bottom_right)),
f64::from(f32::from(self.to.corner_radii.bottom_right)),
0.0,
)
.sample(elapsed);
let r_bl = SpringSolver::new(
self.spec,
f64::from(f32::from(self.from.corner_radii.bottom_left)),
f64::from(f32::from(self.to.corner_radii.bottom_left)),
0.0,
)
.sample(elapsed);
let settled = x.settled && y.settled && w.settled && h.settled;
LayoutFlipSample {
bounds: bounds(
point(px(x.value), px(y.value)),
size(px(w.value.max(0.0)), px(h.value.max(0.0))),
),
corner_radii: Corners {
top_left: px(r_tl.value.max(0.0)),
top_right: px(r_tr.value.max(0.0)),
bottom_right: px(r_br.value.max(0.0)),
bottom_left: px(r_bl.value.max(0.0)),
},
settled,
}
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct LayoutKey {
scope: SharedString,
layout_id: SharedString,
}
#[derive(Clone, Debug)]
struct Entry {
last_rendered: LayoutSnapshot,
active_target: LayoutSnapshot,
flip: Option<ActiveFlip>,
}
#[derive(Default)]
pub struct LayoutRegistry {
entries: HashMap<LayoutKey, Entry>,
}
impl Global for LayoutRegistry {}
impl LayoutRegistry {
pub fn sample(
&mut self,
scope: impl Into<SharedString>,
layout_id: impl Into<SharedString>,
target: LayoutSnapshot,
spec: SpringSpec,
now_secs: f64,
reduced_motion: bool,
) -> LayoutFlipSample {
let key = LayoutKey {
scope: scope.into(),
layout_id: layout_id.into(),
};
let Some(entry) = self.entries.get_mut(&key) else {
self.entries.insert(
key,
Entry {
last_rendered: target,
active_target: target,
flip: None,
},
);
return LayoutFlipSample {
bounds: target.bounds,
corner_radii: target.corner_radii,
settled: true,
};
};
if reduced_motion {
entry.last_rendered = target;
entry.active_target = target;
entry.flip = None;
return LayoutFlipSample {
bounds: target.bounds,
corner_radii: target.corner_radii,
settled: true,
};
}
if !snapshots_close(&entry.active_target, &target) {
let from = entry.last_rendered;
entry.active_target = target;
entry.flip = Some(ActiveFlip {
from,
to: target,
started_at_secs: now_secs,
spec,
});
}
if let Some(flip) = &entry.flip {
let sample = flip.sample(now_secs);
entry.last_rendered = LayoutSnapshot::new(sample.bounds, sample.corner_radii);
if sample.settled {
entry.flip = None;
}
sample
} else {
entry.last_rendered = target;
LayoutFlipSample {
bounds: target.bounds,
corner_radii: target.corner_radii,
settled: true,
}
}
}
pub fn remove(&mut self, scope: &str, layout_id: &str) {
self.entries.remove(&LayoutKey {
scope: SharedString::from(scope.to_owned()),
layout_id: SharedString::from(layout_id.to_owned()),
});
}
}
fn snapshots_close(a: &LayoutSnapshot, b: &LayoutSnapshot) -> bool {
let eps = 0.25_f32;
let dx = (f32::from(a.bounds.origin.x) - f32::from(b.bounds.origin.x)).abs();
let dy = (f32::from(a.bounds.origin.y) - f32::from(b.bounds.origin.y)).abs();
let dw = (f32::from(a.bounds.size.width) - f32::from(b.bounds.size.width)).abs();
let dh = (f32::from(a.bounds.size.height) - f32::from(b.bounds.size.height)).abs();
let dr = (f32::from(a.corner_radii.top_left) - f32::from(b.corner_radii.top_left)).abs();
dx < eps && dy < eps && dw < eps && dh < eps && dr < eps
}
pub fn sample_shared_layout(
scope: impl Into<SharedString>,
layout_id: impl Into<SharedString>,
target: LayoutSnapshot,
spec: Option<SpringSpec>,
window: &mut Window,
cx: &mut App,
) -> LayoutFlipSample {
let now = motion_now(cx);
let reduced = cx.reduce_motion();
let spec = spec.unwrap_or(ArcMotionTokens::DEFAULT.spring.morph);
if !cx.has_global::<LayoutRegistry>() {
cx.set_global(LayoutRegistry::default());
}
let sample = cx
.global_mut::<LayoutRegistry>()
.sample(scope, layout_id, target, spec, now, reduced);
if !sample.settled {
window.request_animation_frame();
}
sample
}
pub fn flip_offset(current: Bounds<Pixels>, target: Bounds<Pixels>) -> Point<Pixels> {
point(
current.origin.x - target.origin.x,
current.origin.y - target.origin.y,
)
}
pub fn layout_scope_key(id: &ElementId) -> SharedString {
SharedString::from(format!("{id:?}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shared_layout_handoff_interpolates_position_size_and_radius() {
let mut reg = LayoutRegistry::default();
let spec = ArcMotionTokens::DEFAULT.spring.morph;
let tab_a = LayoutSnapshot::uniform_radius(
bounds(point(px(0.0), px(0.0)), size(px(80.0), px(36.0))),
px(15.0),
);
let tab_b = LayoutSnapshot::uniform_radius(
bounds(point(px(90.0), px(0.0)), size(px(120.0), px(36.0))),
px(18.0),
);
let initial = reg.sample("tabs-1", "selection", tab_a, spec, 0.0, false);
assert!(initial.settled);
assert_eq!(initial.bounds, tab_a.bounds);
let start = reg.sample("tabs-1", "selection", tab_b, spec, 0.0, false);
assert!(!start.settled);
assert!((f32::from(start.bounds.origin.x) - 0.0).abs() < 1.0);
let mid = reg.sample("tabs-1", "selection", tab_b, spec, 0.12, false);
let mid_x = f32::from(mid.bounds.origin.x);
let mid_w = f32::from(mid.bounds.size.width);
let mid_r = f32::from(mid.corner_radii.top_left);
assert!(mid_x > 10.0 && mid_x < 90.0, "mid_x={mid_x}");
assert!(mid_w > 85.0 && mid_w < 120.0, "mid_w={mid_w}");
assert!(mid_r > 15.0 && mid_r < 18.0, "mid_r={mid_r}");
let end = reg.sample("tabs-1", "selection", tab_b, spec, 1.0, false);
assert!(end.settled);
assert_eq!(end.bounds, tab_b.bounds);
assert_eq!(end.corner_radii, tab_b.corner_radii);
}
#[test]
fn reduced_motion_snaps_shared_layout_immediately() {
let mut reg = LayoutRegistry::default();
let spec = ArcMotionTokens::DEFAULT.spring.morph;
let a = LayoutSnapshot::uniform_radius(
bounds(point(px(0.0), px(0.0)), size(px(80.0), px(36.0))),
px(15.0),
);
let b = LayoutSnapshot::uniform_radius(
bounds(point(px(160.0), px(0.0)), size(px(100.0), px(36.0))),
px(15.0),
);
reg.sample("tabs-2", "selection", a, spec, 0.0, true);
let snapped = reg.sample("tabs-2", "selection", b, spec, 0.01, true);
assert!(snapped.settled);
assert_eq!(snapped.bounds, b.bounds);
}
}