use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use cranpose_animation::{Animatable, AnimationSpec, AnimationType, Easing, spring};
use cranpose_core::{remember, with_current_composer};
use cranpose_ui_graphics::{
GraphicsLayer, LayerShape, LiquidLoupeSpec, Point, Rect, RoundedCornerShape, Size,
liquid_loupe_effect,
};
use crate::{
composable,
modifier::Modifier,
widgets::{
box_widget::{Box, BoxSpec},
popup::{Popup, local_popup_viewport},
},
};
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_COLLAPSE_MS: u64 = 120;
fn loupe_grow_spring() -> AnimationType {
spring(0.55, 320.0)
}
fn loupe_collapse_tween() -> AnimationType {
AnimationType::Tween(AnimationSpec::tween(LOUPE_COLLAPSE_MS, Easing::EaseInOut))
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LoupeTarget {
pub focus_x: f32,
pub line_mid_y: 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);
Some(LoupeTarget {
focus_x: finger.x,
line_mid_y: line_bottom - 0.5 * line_height,
})
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct LoupePose {
width_frac: f32,
height_frac: f32,
rise_frac: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LoupePhase {
Birth,
Collapse,
}
fn loupe_pose(progress: f32, phase: LoupePhase) -> LoupePose {
let p = progress.max(0.0);
let bounded = p.min(1.0);
let (width_exponent, height_exponent, rise_exponent) = match phase {
LoupePhase::Birth => (0.60, 0.18, 0.60),
LoupePhase::Collapse => (0.80, 0.50, 1.0),
};
let width = if p <= 1.0 {
bounded.powf(width_exponent)
} else {
p
};
LoupePose {
width_frac: width,
height_frac: bounded.powf(height_exponent),
rise_frac: bounded.powf(rise_exponent),
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
struct LoupePlacement {
x: f32,
y: f32,
width: f32,
height: f32,
focus_offset: (f32, f32),
}
fn loupe_below_line(line_mid_y: f32, window: Size) -> bool {
let fit = (window.width / LOUPE_WIDTH)
.min(window.height / LOUPE_HEIGHT)
.min(1.0);
let room_above = line_mid_y;
let room_below = window.height - line_mid_y;
let needed_above = LOUPE_RISE + 0.5 * LOUPE_HEIGHT * fit;
room_above < needed_above && room_below > room_above
}
fn place_loupe(
line_mid_y: f32,
center_x: f32,
(width, height): (f32, f32),
rise: f32,
below: bool,
window: Option<Size>,
) -> LoupePlacement {
let center_y = if below {
line_mid_y + rise
} else {
line_mid_y - rise
};
let Some(window) = window else {
return LoupePlacement {
x: center_x - width * 0.5,
y: center_y - height * 0.5,
width,
height,
focus_offset: (0.0, line_mid_y - center_y),
};
};
let fit = (window.width / width).min(window.height / height).min(1.0);
let (width, height) = (
(width * fit).min(window.width),
(height * fit).min(window.height),
);
let wanted_x = center_x - width * 0.5;
let wanted_y = center_y - height * 0.5;
let x = wanted_x.max(0.0).min(window.width - width);
let y = wanted_y.max(0.0).min(window.height - height);
LoupePlacement {
x,
y,
width,
height,
focus_offset: (wanted_x - x, (line_mid_y - center_y) + (wanted_y - y)),
}
}
fn loupe_optical_activity(progress: f32) -> f32 {
smoothstep01(progress)
}
fn smoothstep01(value: f32) -> f32 {
let t = value.clamp(0.0, 1.0);
t * t * (3.0 - 2.0 * t)
}
struct LoupeState {
progress: RefCell<Animatable<f32>>,
follow_x: RefCell<Animatable<f32>>,
shown: RefCell<Option<LoupeTarget>>,
was_active: 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())),
follow_x: RefCell::new(Animatable::new(f32::NAN, runtime)),
shown: RefCell::new(None),
was_active: Cell::new(false),
})
})
.with(Rc::clone);
let viewport = local_popup_viewport().current().get();
let window = (viewport.width > 0.0 && viewport.height > 0.0).then_some(viewport);
let active = target.is_some();
if let Some(t) = target {
let fresh_grab = !state.was_active.get();
state.shown.replace(Some(t));
if fresh_grab {
let mut progress = state.progress.borrow_mut();
progress.snapTo(0.0);
progress.animateTo(1.0, loupe_grow_spring());
}
} else if state.was_active.get() {
let mut progress = state.progress.borrow_mut();
if progress.state().value() > 0.001 {
progress.animateTo(0.0, loupe_collapse_tween());
} else {
state.shown.replace(None);
}
}
state.was_active.set(active);
let progress_state = state.progress.borrow().state();
let p = progress_state.value().max(0.0);
let Some(shown) = *state.shown.borrow() else {
return;
};
if p <= 0.001 {
if !active {
state.shown.replace(None);
}
return;
}
let pose = loupe_pose(
p,
if active {
LoupePhase::Birth
} else {
LoupePhase::Collapse
},
);
let optic = loupe_optical_activity(p);
{
let mut follow_anim = state.follow_x.borrow_mut();
if !follow_anim.state().value().is_finite() {
follow_anim.snapTo(shown.focus_x);
} else if (follow_anim.target() - shown.focus_x).abs() > f32::EPSILON {
let velocity = follow_anim.velocity();
follow_anim.animate_to_with_velocity(shown.focus_x, velocity, spring(1.0, 1050.0));
}
}
let follow = state.follow_x.borrow().state().value();
let trail = shown.focus_x - follow;
let stretch = 1.0 + (trail.abs() * 0.004).clamp(0.0, 0.12);
let below = window.is_some_and(|window| loupe_below_line(shown.line_mid_y, window));
let LoupePlacement {
x,
y,
width,
height,
focus_offset,
} = place_loupe(
shown.line_mid_y,
follow,
(
LOUPE_WIDTH * pose.width_frac * stretch,
LOUPE_HEIGHT * pose.height_frac / stretch,
),
LOUPE_RISE * pose.rise_frac,
below,
window,
);
let corner_radius = 0.5 * width.min(height);
let spec = LiquidLoupeSpec {
magnification: LOUPE_MAGNIFICATION,
focus_offset,
corner_radius,
activity: optic,
..LiquidLoupeSpec::default()
};
let anchor = Rect {
x,
y,
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)]
#[path = "tests/loupe_tests.rs"]
mod tests;