Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 239 additions & 6 deletions crates/gpui/examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::time::Duration;

use anyhow::Result;
use gpui::{
Animation, AnimationExt as _, App, AssetSource, Bounds, Context, SharedString, Transformation,
Window, WindowBounds, WindowOptions, bounce, div, ease_in_out, percentage, prelude::*, px,
size, svg,
Animation, AnimationExt as _, AnimationPhase, App, AssetSource, Bounds, Context, MouseButton,
MouseDownEvent, MouseMoveEvent, Pixels, SharedString, SpringAnimation, SpringConfig,
Transformation, Window, WindowBounds, WindowOptions, bounce, div, ease_in_out, percentage,
prelude::*, px, relative, rgba, size, svg,
};
use gpui_platform::application;

Expand Down Expand Up @@ -36,10 +37,26 @@ const ARROW_CIRCLE_SVG: &str = concat!(
"/examples/image/arrow_circle.svg"
);

struct AnimationExample {}
struct AnimationExample {
spring_phase: u8,
spring_damping: f32,
spring_damping_drag: Option<(Pixels, f32)>,
}

impl Render for AnimationExample {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
const MINIMUM_DAMPING: f32 = 2.0;
const MAXIMUM_DAMPING: f32 = 32.0;
const DAMPING_SLIDER_WIDTH: Pixels = px(224.0);

let spring_phase = self.spring_phase;
let spring_position = px(98.0 * f32::from(spring_phase));
let spring_damping = self.spring_damping;
let spring = SpringConfig::new(170.0, spring_damping, 1.0);
let (_, damping_ratio) = spring.canonical();
let damping_fraction =
(spring_damping - MINIMUM_DAMPING) / (MAXIMUM_DAMPING - MINIMUM_DAMPING);

div()
.flex()
.flex_col()
Expand Down Expand Up @@ -67,6 +84,190 @@ impl Render for AnimationExample {
.text_xl()
.gap_4()
.child("Hello Animation")
.child(
div()
.id("spring-demo")
.flex()
.flex_col()
.gap_2()
.p_2()
.w(px(240.0))
.rounded_md()
.bg(rgba(0x00000010))
.cursor_pointer()
.on_mouse_down(
MouseButton::Left,
cx.listener(|this, _, _, cx| {
this.spring_phase = (this.spring_phase + 1) % 3;
cx.notify();
}),
)
.child(format!(
"Target phase {spring_phase}: click rapidly to redirect momentum"
))
.child(
div()
.id("spring-damping-controls")
.flex()
.flex_col()
.gap_1()
.text_sm()
.on_click(|_, _, cx| cx.stop_propagation())
.child(format!(
"Drag damping: {spring_damping:.1} (ζ {damping_ratio:.2})"
))
.child(
div()
.id("spring-damping")
.relative()
.h(px(20.0))
.w(DAMPING_SLIDER_WIDTH)
.cursor_pointer()
.on_mouse_down(
MouseButton::Left,
cx.listener(
|this,
event: &MouseDownEvent,
_,
cx| {
this.spring_damping_drag = Some((
event.position.x,
this.spring_damping,
));
cx.stop_propagation();
},
),
)
.on_mouse_move(cx.listener(
|this,
event: &MouseMoveEvent,
_,
cx| {
let Some((
start_position,
start_damping,
)) = this.spring_damping_drag
else {
return;
};

let delta = (event.position.x
- start_position)
/ DAMPING_SLIDER_WIDTH;
this.spring_damping = (start_damping
+ delta
* (MAXIMUM_DAMPING
- MINIMUM_DAMPING))
.clamp(
MINIMUM_DAMPING,
MAXIMUM_DAMPING,
);
cx.stop_propagation();
cx.notify();
},
))
.on_mouse_up(
MouseButton::Left,
cx.listener(|this, _, _, cx| {
this.spring_damping_drag = None;
cx.stop_propagation();
}),
)
.on_mouse_up_out(
MouseButton::Left,
cx.listener(|this, _, _, cx| {
this.spring_damping_drag = None;
cx.stop_propagation();
}),
)
.child(
div()
.absolute()
.top(px(8.0))
.h(px(4.0))
.w_full()
.rounded_full()
.bg(rgba(0x00000030)),
)
.child(
div()
.absolute()
.top(px(8.0))
.h(px(4.0))
.w(relative(damping_fraction))
.rounded_full()
.bg(rgba(0x3b82f6ff)),
)
.child(
div()
.absolute()
.top(px(3.0))
.left(relative(damping_fraction))
.ml(px(-7.0))
.size(px(14.0))
.rounded_full()
.bg(rgba(0x2563ebff)),
),
),
)
.child(
div().relative().h(px(32.0)).w_full().child(
div()
.absolute()
.top_1()
.size(px(24.0))
.rounded_full()
.bg(rgba(0x3b82f6ff))
.cursor_pointer()
.debug_selector(|| "spring-position".into())
.with_spring(
"spring-position",
SpringAnimation::new(spring)
.to(spring_position)
.with_epsilon(0.25),
|this, left| this.left(left),
),
),
)
.child(
div().h(px(24.0)).rounded_sm().with_spring(
"spring-phase",
SpringAnimation::new(spring)
.to(AnimationPhase(f32::from(spring_phase)))
.with_epsilon(0.001),
|this, phase| {
let (width, color) = if phase.0 <= 1.0 {
(
phase.interpolate_between(
0.0..=1.0,
px(48.0),
px(224.0),
),
phase.interpolate_between_clamped(
0.0..=1.0,
rgba(0xf97316ff),
rgba(0x22c55eff),
),
)
} else {
(
phase.interpolate_between(
1.0..=2.0,
px(224.0),
px(96.0),
),
phase.interpolate_between_clamped(
1.0..=2.0,
rgba(0x22c55eff),
rgba(0xa855f7ff),
),
)
};
this.w(width).bg(color)
},
),
),
)
.child(
svg()
.size_20()
Expand Down Expand Up @@ -115,7 +316,11 @@ fn run_example() {
};
cx.open_window(options, |_, cx| {
cx.activate(false);
cx.new(|_| AnimationExample {})
cx.new(|_| AnimationExample {
spring_phase: 0,
spring_damping: 14.0,
spring_damping_drag: None,
})
})
.unwrap();
});
Expand All @@ -132,3 +337,31 @@ pub fn start() {
gpui_platform::web_init();
run_example();
}

#[cfg(all(test, feature = "test-support"))]
mod tests {
use gpui::{Modifiers, TestAppContext};

use super::*;

#[gpui::test]
fn clicking_spring_position_changes_the_target(cx: &mut TestAppContext) {
let (view, cx) = cx.add_window_view(|_, _| AnimationExample {
spring_phase: 0,
spring_damping: 14.0,
spring_damping_drag: None,
});
cx.simulate_resize(size(px(300.0), px(300.0)));
cx.run_until_parked();

let position_bounds = cx
.debug_bounds("spring-position")
.expect("spring position should be rendered");
let position = position_bounds.center();
cx.simulate_mouse_move(position, None, Modifiers::default());
cx.simulate_click(position, Modifiers::default());
cx.run_until_parked();

assert_eq!(cx.update(|_, cx| view.read(cx).spring_phase), 1);
}
}
Loading
Loading