#![allow(non_snake_case)]
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use crate::composable;
use crate::modifier::Modifier;
use crate::widgets::box_widget::{Box, BoxSpec};
use crate::widgets::popup::Popup;
use cranpose_animation::{spring, Animatable, AnimationSpec, AnimationType};
use cranpose_core::{remember, with_current_composer};
use cranpose_ui_graphics::{
liquid_loupe_effect, GraphicsLayer, LayerShape, LiquidLoupeSpec, Point, Rect,
RoundedCornerShape, Size,
};
pub const LOUPE_WIDTH: f32 = 117.0;
pub const LOUPE_HEIGHT: f32 = 82.0;
pub const LOUPE_RISE: f32 = 75.0;
pub const LOUPE_MAGNIFICATION: f32 = 1.25;
const LOUPE_BIRTH_WIDTH_FRAC: f32 = 0.63;
const LOUPE_BIRTH_HEIGHT_FRAC: f32 = 0.93;
const LOUPE_BIRTH_RISE_FRAC: f32 = 0.82;
const LOUPE_DISSOLVE_SHRINK: f32 = 0.30;
const LOUPE_DISSOLVE_SINK: f32 = 0.35;
const LOUPE_DISSOLVE_ALPHA_FLOOR: f32 = 0.65;
const LOUPE_BIRTH_DELAY_MS: u64 = 120;
const LOUPE_LINE_GRAB_MARGIN: f32 = 0.15;
fn loupe_grow_spring() -> AnimationType {
spring(0.5, 310.0)
}
fn loupe_rise_spring() -> AnimationType {
spring(2.0, 1900.0)
}
fn loupe_birth_gate() -> AnimationType {
AnimationType::Tween(AnimationSpec::linear(LOUPE_BIRTH_DELAY_MS))
}
fn loupe_collapse_tween() -> AnimationType {
AnimationType::Tween(AnimationSpec::linear(55))
}
fn loupe_follow_spring() -> AnimationType {
spring(1.0, 625.0)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LoupeTarget {
pub focus_x: f32,
pub line_mid_y: f32,
pub dot_clearance: f32,
}
pub fn loupe_target_for_drag(
finger: Point,
line_bottom: f32,
line_height: f32,
) -> Option<LoupeTarget> {
let line_height = line_height.max(1.0);
if finger.y <= line_bottom + LOUPE_LINE_GRAB_MARGIN * line_height {
Some(LoupeTarget {
focus_x: finger.x,
line_mid_y: line_bottom - 0.5 * line_height,
dot_clearance: 0.5 * line_height + 2.0 * crate::text_selection::HANDLE_RADIUS
- crate::text_selection::HANDLE_DOT_LINE_OVERLAP
+ 1.0,
})
} else {
None
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct LoupePose {
width_frac: f32,
height_frac: f32,
rise_frac: f32,
}
fn grow_pose(p: f32, rise: f32) -> LoupePose {
let pc = p.clamp(0.0, 1.0);
LoupePose {
width_frac: LOUPE_BIRTH_WIDTH_FRAC + (1.0 - LOUPE_BIRTH_WIDTH_FRAC) * p.max(0.0),
height_frac: LOUPE_BIRTH_HEIGHT_FRAC + (1.0 - LOUPE_BIRTH_HEIGHT_FRAC) * pc,
rise_frac: LOUPE_BIRTH_RISE_FRAC + (1.0 - LOUPE_BIRTH_RISE_FRAC) * rise.clamp(0.0, 1.0),
}
}
fn dissolve_pose(released: LoupePose, q: f32) -> (LoupePose, f32) {
let d = 1.0 - q.clamp(0.0, 1.0);
let u = ((d - 0.15) / 0.85).clamp(0.0, 1.0);
let shrink = (u * 1.9).min(1.0);
let plunge = (u * 2.8).min(1.0);
let factor = 1.0 - LOUPE_DISSOLVE_SHRINK * shrink;
let pose = LoupePose {
width_frac: released.width_frac * factor,
height_frac: released.height_frac * factor,
rise_frac: released.rise_frac - LOUPE_DISSOLVE_SINK * plunge,
};
(pose, 1.0 - (1.0 - LOUPE_DISSOLVE_ALPHA_FLOOR) * plunge)
}
struct LoupeState {
progress: RefCell<Animatable<f32>>,
gate: RefCell<Animatable<f32>>,
rise: RefCell<Animatable<f32>>,
follow_x: RefCell<Animatable<f32>>,
shown: RefCell<Option<LoupeTarget>>,
released: Cell<Option<(f32, LoupePose)>>,
was_active: std::cell::Cell<bool>,
}
#[composable]
pub fn SelectionLoupe(target: Option<LoupeTarget>) {
let state = remember(|| {
let runtime = with_current_composer(|composer| composer.runtime_handle());
Rc::new(LoupeState {
progress: RefCell::new(Animatable::new(0.0, runtime.clone())),
gate: RefCell::new(Animatable::new(0.0, runtime.clone())),
rise: RefCell::new(Animatable::new(0.0, runtime.clone())),
follow_x: RefCell::new(Animatable::new(0.0, runtime)),
shown: RefCell::new(None),
released: Cell::new(None),
was_active: std::cell::Cell::new(false),
})
})
.with(Rc::clone);
let active = target.is_some();
if let Some(t) = target {
let fresh_grab = !state.was_active.get();
{
let mut follow = state.follow_x.borrow_mut();
if fresh_grab {
follow.snapTo(t.focus_x);
} else if (follow.target() - t.focus_x).abs() > f32::EPSILON {
follow.animateTo(t.focus_x, loupe_follow_spring());
}
}
state.shown.replace(Some(t));
if fresh_grab {
let mut gate = state.gate.borrow_mut();
gate.snapTo(0.0);
gate.animateTo(1.0, loupe_birth_gate());
state.progress.borrow_mut().snapTo(0.0);
state.rise.borrow_mut().snapTo(0.0);
state.released.set(None);
}
let born = state.gate.borrow().state().value() >= 1.0;
if born {
let mut progress = state.progress.borrow_mut();
if (progress.target() - 1.0).abs() > f32::EPSILON {
progress.animateTo(1.0, loupe_grow_spring());
state.rise.borrow_mut().animateTo(1.0, loupe_rise_spring());
}
}
} else {
let mut progress = state.progress.borrow_mut();
if progress.target() != 0.0 {
let p_rel = progress.state().value().max(1.0e-3);
let mut rise = state.rise.borrow_mut();
let rise_rel = rise.state().value();
rise.snapTo(rise_rel);
state
.released
.set(Some((p_rel, grow_pose(p_rel, rise_rel))));
progress.animateTo(0.0, loupe_collapse_tween());
}
}
state.was_active.set(active);
let progress_state = state.progress.borrow().state();
let rise_state = state.rise.borrow().state();
let follow_state = state.follow_x.borrow().state();
let p = progress_state.value().max(0.0);
let Some(shown) = *state.shown.borrow() else {
return;
};
let born = active && state.gate.borrow().state().value() >= 1.0;
if p <= 0.001 && !born && state.released.get().is_none() {
if !active {
state.shown.replace(None);
}
return;
}
let (pose, optic) = if active {
(grow_pose(p, rise_state.value()), 1.0)
} else {
let Some((p_rel, released)) = state.released.get() else {
state.shown.replace(None);
return;
};
let q = (p / p_rel).clamp(0.0, 1.0);
if q <= 0.02 {
state.shown.replace(None);
state.released.set(None);
return;
}
dissolve_pose(released, q)
};
let width = LOUPE_WIDTH * pose.width_frac;
let height = LOUPE_HEIGHT * pose.height_frac;
let center_x = follow_state.value();
let center_y = shown.line_mid_y - LOUPE_RISE * pose.rise_frac;
let focus_offset_y = shown.line_mid_y - center_y;
let corner_frac = if active {
0.38 + (0.5 - 0.38) * (p * 2.0).clamp(0.0, 1.0)
} else {
0.5
};
let corner_radius = height * corner_frac;
let spec = LiquidLoupeSpec {
magnification: LOUPE_MAGNIFICATION,
focus_offset: (0.0, focus_offset_y),
seam_lift: shown.dot_clearance,
corner_radius,
progress: optic,
..LiquidLoupeSpec::default()
};
let anchor = Rect {
x: center_x - width * 0.5,
y: center_y - height * 0.5,
width: 0.0,
height: 0.0,
};
Popup(anchor, Point { x: 0.0, y: 0.0 }, move || {
let spec = spec.clone();
Box(
Modifier::empty()
.size(Size { width, height })
.graphics_layer(move || GraphicsLayer {
backdrop_effect: Some(liquid_loupe_effect((width, height), &spec)),
shape: LayerShape::Rounded(RoundedCornerShape::uniform(corner_radius)),
clip: true,
..Default::default()
}),
BoxSpec::default(),
|| {},
);
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn loupe_shows_only_while_the_finger_covers_the_line() {
let line_bottom = 100.0;
let line_height = 20.0;
let on_line = loupe_target_for_drag(Point { x: 40.0, y: 95.0 }, line_bottom, line_height)
.expect("a finger on the line raises the loupe");
assert_eq!(on_line.focus_x, 40.0);
assert_eq!(on_line.line_mid_y, 90.0);
assert!(
loupe_target_for_drag(Point { x: 40.0, y: 102.0 }, line_bottom, line_height).is_some()
);
assert!(
loupe_target_for_drag(Point { x: 40.0, y: 106.0 }, line_bottom, line_height).is_none()
);
assert!(
loupe_target_for_drag(Point { x: 40.0, y: 70.0 }, line_bottom, line_height).is_some()
);
}
#[test]
fn birth_pose_is_a_low_near_square_squircle() {
let pose = grow_pose(0.0, 0.0);
assert!((pose.width_frac - LOUPE_BIRTH_WIDTH_FRAC).abs() < 1e-6);
assert!((pose.height_frac - LOUPE_BIRTH_HEIGHT_FRAC).abs() < 1e-6);
assert!((pose.rise_frac - LOUPE_BIRTH_RISE_FRAC).abs() < 1e-6);
let aspect = (LOUPE_WIDTH * pose.width_frac) / (LOUPE_HEIGHT * pose.height_frac);
assert!((0.9..=1.05).contains(&aspect), "birth aspect {aspect}");
}
#[test]
fn grow_width_carries_the_overshoot_and_height_clamps_it() {
let pose = grow_pose(1.16, 1.0);
assert!(pose.width_frac > 1.05 && pose.width_frac < 1.07);
assert!((pose.height_frac - 1.0).abs() < 1e-6);
assert!((pose.rise_frac - 1.0).abs() < 1e-6);
}
#[test]
fn dissolve_holds_still_then_plunges_shrinks_and_dims() {
let released = grow_pose(1.0, 1.0);
let (pose, alpha) = dissolve_pose(released, 0.9);
assert_eq!(pose, released);
assert_eq!(alpha, 1.0);
let (pose, alpha) = dissolve_pose(released, 0.55);
assert!(
pose.width_frac < 0.85 && pose.width_frac > 0.75,
"mid-fade width {}",
pose.width_frac
);
assert!(
released.rise_frac - pose.rise_frac > 0.30,
"the bubble plunges toward the line by +25 ms, sank {}",
released.rise_frac - pose.rise_frac
);
assert!(
(alpha - LOUPE_DISSOLVE_ALPHA_FLOOR).abs() < 0.02,
"content dimmed to the floor by +25 ms, got {alpha}"
);
let (pose, alpha) = dissolve_pose(released, 0.24);
assert!((pose.width_frac - 0.70).abs() < 0.02);
assert!((alpha - LOUPE_DISSOLVE_ALPHA_FLOOR).abs() < 1e-6);
assert!((released.rise_frac - pose.rise_frac - LOUPE_DISSOLVE_SINK).abs() < 1e-6);
}
#[test]
fn dissolving_a_newborn_shrinks_the_newborn_pose() {
let newborn = grow_pose(0.02, 0.05);
let (pose, optic) = dissolve_pose(newborn, 1.0);
assert_eq!(
pose, newborn,
"the fade starts exactly at the released pose"
);
assert_eq!(optic, 1.0);
let (pose, _) = dissolve_pose(newborn, 0.3);
assert!(pose.width_frac < newborn.width_frac);
assert!(pose.width_frac >= newborn.width_frac * (1.0 - LOUPE_DISSOLVE_SHRINK) - 1e-6);
}
#[test]
fn loupe_effect_keeps_optics_constant_and_fades_content_alpha() {
let dimmed = LiquidLoupeSpec {
progress: 0.65,
..LiquidLoupeSpec::default()
};
let effect = liquid_loupe_effect((LOUPE_WIDTH, LOUPE_HEIGHT), &dimmed);
let cranpose_ui_graphics::RenderEffect::Shader { shader } = effect else {
panic!("loupe must be a bare shader effect");
};
let u = shader.uniforms();
assert!(
(u[83] - LOUPE_MAGNIFICATION).abs() < 1e-6,
"magnification never animates, got {}",
u[83]
);
assert!(u[86] > 0.0, "dispersion never animates");
assert!(
(u[90] - 0.65).abs() < 1e-6,
"content alpha rides uniform 90"
);
assert!(
u[87] > 20.0,
"the fold floor clears the handle dot, got {}",
u[87]
);
let grown = LiquidLoupeSpec::default();
let effect = liquid_loupe_effect((LOUPE_WIDTH, LOUPE_HEIGHT), &grown);
let cranpose_ui_graphics::RenderEffect::Shader { shader } = effect else {
panic!("loupe must be a bare shader effect");
};
let u = shader.uniforms();
assert_eq!(u[80], 1.0, "loupe mode on");
assert!(
(u[83] - LOUPE_MAGNIFICATION).abs() < 1e-6,
"full magnification"
);
assert_eq!(u[81], 0.0, "focus x on the bubble center");
assert!((u[82] - 75.0).abs() < 1e-6, "focus 75dp below the center");
assert_eq!(
&u[0..2],
&[LOUPE_WIDTH, LOUPE_HEIGHT],
"container = node dp"
);
assert_eq!(u[6], -1.0, "capsule sentinel");
assert!(
shader.input_padding() >= 75.0,
"capture must cover the offset focus, got {}",
shader.input_padding()
);
}
}