Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

feat(onboarding): fly the pen round the icon after hello

RoversXadfb57300d3c
diff --git a/wezterm-gui/src/termwindow/onboarding.rs b/wezterm-gui/src/termwindow/onboarding.rs
index fbed61e5..cad6db6c 100644
--- a/wezterm-gui/src/termwindow/onboarding.rs
+++ b/wezterm-gui/src/termwindow/onboarding.rs
@@ -440,10 +440,14 @@ pub(crate) struct OnboardingView {
/// When the page opened, which bounds the wait for the running check.
opened: Instant,
curtain: Option<Curtain>,
- /// "hello." written out before the page appears. Gone once the page is
- /// fully in; until then the page takes no input, since none of it can
- /// be seen yet.
+ /// "hello." written out before the page appears, and the pen's flight
+ /// round the icon after. Until the page is fully in it takes no input,
+ /// since it cannot all be seen yet; the flight plays on over a page
+ /// that does.
intro: Option<hello::Intro>,
+ /// Where the page last laid its icon out, which the intro's pen flies
+ /// round; `None` while a window too short for it leaves it out.
+ mark: Option<RectF>,
/// What this page's strings shaped to. The curtain repaints the page
/// many times a second, and its text never changes in between.
shaped: ShapedText,
@@ -482,10 +486,16 @@ impl OnboardingView {
opened: Instant::now(),
curtain: None,
intro: None,
+ mark: None,
shaped: ShapedText::default(),
}
}
+ /// Whether the intro still keeps the page from taking input.
+ fn intro_hides_page(&self) -> bool {
+ self.intro.as_ref().is_some_and(|intro| !intro.page_shown())
+ }
+
fn space_name(&self) -> String {
let initial_name = self.initial_space_name.trim();
if !initial_name.is_empty() {
@@ -783,7 +793,7 @@ impl OnboardingView {
area.origin.y,
area.size.width,
area.size.height,
- palette.window_bg,
+ skin.ground,
)?;
let body_h = Self::text_h(font);
@@ -950,13 +960,14 @@ impl OnboardingView {
let mut y = area.origin.y + ((area.size.height - total_h) / 2.0).max(0.0);
// --- brand
+ self.mark = None;
if show_mark {
- ctx.draw_app_icon(
- layers,
- col_x + (col_w - fx(MARK_SIZE)) / 2.0,
- y,
- fx(MARK_SIZE),
- )?;
+ let mark_x = col_x + (col_w - fx(MARK_SIZE)) / 2.0;
+ ctx.draw_app_icon(layers, mark_x, y, fx(MARK_SIZE))?;
+ self.mark = Some(RectF::new(
+ euclid::point2(mark_x, y),
+ euclid::size2(fx(MARK_SIZE), fx(MARK_SIZE)),
+ ));
y += mark_h;
}
@@ -1930,7 +1941,7 @@ struct Skin {
impl Skin {
fn new(palette: UiPalette) -> Self {
- let ground = palette.window_bg;
+ let ground = page_ground(palette);
// Every fill below is opaque — see `mix`. The selected chip in
// particular sits only a little way from the ground, so the ordinary
// text colour still reads on it; pushing it towards the text colour
@@ -1954,12 +1965,25 @@ impl Skin {
// The mono inversion: the primary action is the highest-contrast
// thing on the page without introducing a hue.
primary_bg: palette.text,
- primary_text: palette.window_bg,
+ primary_text: ground,
focus: mix(ground, palette.text, 0.65),
}
}
}
+/// The page's ground: the window's own colour, except plain white for the
+/// light theme's own grey, where the first thing a new user sees should
+/// read bright rather than grey. A ground taken from a terminal scheme keeps
+/// its tint, so the page matches the chrome around it. The controls are
+/// mixed from it, so they follow.
+fn page_ground(palette: UiPalette) -> LinearRgba {
+ if palette.is_dark() || palette.derived {
+ palette.window_bg
+ } else {
+ LinearRgba::with_srgba(255, 255, 255, 255)
+ }
+}
+
/// Opaque blend from `from` towards `to`, **in sRGB space**.
///
/// Two things this has to get right:
@@ -2114,15 +2138,22 @@ impl ContentView for OnboardingView {
caption_font: &Rc<LoadedFont>,
_cursor_on: bool,
) -> anyhow::Result<()> {
+ let to_mark = self.mark.is_some();
let frame = self
.intro
.as_mut()
- .and_then(|intro| intro.frame(Instant::now()));
+ .and_then(|intro| intro.frame(Instant::now(), to_mark));
if frame.is_none() {
self.intro = None;
}
+ // From the moment the word starts going, the page is laid out under
+ // the still opaque ground every frame: where its icon is, which the
+ // pen flies to, has to be where it is now, not where the warm-up
+ // found it.
let page_under = match (self.intro.as_mut(), frame) {
- (Some(intro), Some(frame)) => frame.page_in.is_some() || intro.wants_warm_up(frame),
+ (Some(intro), Some(frame)) => {
+ frame.page_in.is_some() || frame.tail > 0.0 || intro.wants_warm_up(frame)
+ }
_ => true,
};
if page_under {
@@ -2141,22 +2172,28 @@ impl ContentView for OnboardingView {
self.widgets.clear();
}
match (&self.intro, frame) {
- (Some(intro), Some(frame)) => {
- intro.paint(ctx, layers, area, palette.window_bg, frame, page_under)
- }
+ (Some(intro), Some(frame)) => intro.paint(
+ ctx,
+ layers,
+ area,
+ page_ground(palette),
+ frame,
+ page_under,
+ self.mark,
+ ),
_ => Ok(()),
}
}
fn on_mouse(&mut self, x: f32, y: f32, kind: WMEK) -> ContentViewResponse {
- if self.intro.is_some() {
+ if self.intro_hides_page() {
return ContentViewResponse::Ignored;
}
self.on_mouse_impl(x, y, kind)
}
fn on_key(&mut self, key: KeyCode, mods: KeyModifiers) -> ContentViewResponse {
- if self.intro.is_some() {
+ if self.intro_hides_page() {
return ContentViewResponse::Ignored;
}
self.on_key_impl(key, mods)
@@ -2323,6 +2360,25 @@ mod tests {
));
}
+ /// Once the page is fully in, it answers while the pen's flight round
+ /// the icon is still playing over it; not a moment before.
+ #[test]
+ fn the_page_takes_input_once_it_is_in() {
+ let mut view = OnboardingView {
+ intro: Some(hello::Intro::new()),
+ ..view(found::Found::default())
+ };
+ assert!(matches!(
+ view.on_key(KeyCode::Escape, KeyModifiers::NONE),
+ ContentViewResponse::Ignored
+ ));
+ view.intro = Some(hello::Intro::with_page_shown());
+ assert!(matches!(
+ view.on_key(KeyCode::Escape, KeyModifiers::NONE),
+ ContentViewResponse::Run(_)
+ ));
+ }
+
/// The language menu takes the arrow keys and Escape while it is open, and
/// gives Escape back to the page once it is shut.
#[test]
diff --git a/wezterm-gui/src/termwindow/onboarding/hello.rs b/wezterm-gui/src/termwindow/onboarding/hello.rs
index b120e7f8..aa16e4d1 100644
--- a/wezterm-gui/src/termwindow/onboarding/hello.rs
+++ b/wezterm-gui/src/termwindow/onboarding/hello.rs
@@ -1,5 +1,8 @@
//! The intro the page opens with: "hello." written out in one stroke, then
-//! taken back up along the same path while the page fades in under it.
+//! taken back up along the same path while the page fades in under it. Just
+//! before the tail has it all, the pen sets off again: on from the word's
+//! end in one wide swoop to the page's icon, once round it, and the ring it
+//! closes breaks into sparks that drift away.
//!
//! The word is a single hand-drawn path. The pen is a chain of round stamps
//! along it, one every `STEP` units, each tinted on its own: the colour of a
@@ -7,11 +10,13 @@
//! the colours run along the ink exactly as fast as the pen moves and come
//! to rest when it stops, blue at the first stroke through green at the
//! last. Taking the word back up, a tail runs the same path after the pen
-//! and the colours run on with it. Stamps are one small disc sprite drawn many times, so a frame
-//! uploads nothing and draws the same on either renderer.
+//! and the colours run on with it. Stamps are one small disc sprite drawn
+//! many times, so a frame uploads nothing and draws the same on either
+//! renderer.
use crate::quad::TripleLayerQuadAllocator;
use crate::ui::DrawContext;
+use std::cell::RefCell;
use std::time::{Duration, Instant};
use window::color::LinearRgba;
use window::RectF;
@@ -87,6 +92,36 @@ const DOT_OUT_FROM: f32 = 0.9;
/// two barely cross, and finishes a little after the ink is gone.
const PAGE_AT: Duration = Duration::from_millis(1300);
const PAGE_IN: Duration = Duration::from_millis(700);
+/// When the pen leaves the word's end, counted from the start of taking it
+/// back up: with about two thirds of the word gone, so the pen sets off
+/// while the tail is still running towards it.
+const LAUNCH: Duration = Duration::from_millis(950);
+/// From the word's end round the icon until the ring closes.
+const FLIGHT: Duration = Duration::from_millis(1300);
+/// The closed ring goes this quickly once it has broken into sparks.
+const RING_FADE: f32 = 0.22;
+/// Sparks set off up to this late, in seconds, and drift for this long.
+const SPARK_DELAY: f32 = 0.16;
+const SPARK_LIFE: (f32, f32) = (0.9, 1.4);
+/// From the ring closing until the last spark is gone.
+const SPARKS_END: Duration = Duration::from_millis(1560);
+/// The swoop's shape was drawn for a word this many pixels wide.
+const SWOOP_WORD: f32 = 605.0;
+/// The icon's rounded square inside its image (512 square: 50 in from each
+/// side, corners about 100 round), as fractions of the image's side.
+const BODY_INSET: f32 = 50.0 / 512.0;
+const BODY_RADIUS: f32 = 100.0 / 512.0;
+/// The ring's distance outside the rounded square, as a fraction of it.
+const RING_GAP: f32 = 0.14;
+/// The pen's width on the ring, and a spark's, against the word's.
+const THIN: f32 = 0.28;
+const SPARK: f32 = 0.3;
+/// Past the word the rainbow turns this much faster than round the ring's
+/// length: on a line this short it would otherwise read as one colour.
+const TURN: f32 = 1.1;
+/// How fast the colours keep running round the closed ring, in turns a
+/// second.
+const RING_RUN: f32 = 0.4;
/// The longest step one paint advances the intro by. A window that is
/// covered or on another Space is not painted at all, so the intro waits
/// for it instead of playing to nobody.
@@ -111,6 +146,10 @@ pub(super) struct Intro {
elapsed: Duration,
last_paint: Option<Instant>,
warmed: bool,
+ /// Whether the pen flies to the icon, settled the moment it would set
+ /// off: a window resized afterwards does not cut the flight short or
+ /// start one halfway.
+ fly: Option<bool>,
}
impl Intro {
@@ -120,16 +159,36 @@ impl Intro {
elapsed: Duration::ZERO,
last_paint: None,
warmed: false,
+ fly: None,
}
}
/// Advance to `now` and say where the intro is; `None` once it is over.
- pub(super) fn frame(&mut self, now: Instant) -> Option<Frame> {
+ /// `to_mark`: the page shows its icon, so the pen flies round it.
+ pub(super) fn frame(&mut self, now: Instant, to_mark: bool) -> Option<Frame> {
if let Some(last) = self.last_paint {
self.elapsed += now.saturating_duration_since(last).min(MAX_STEP);
}
self.last_paint = Some(now);
- Frame::at(self.elapsed)
+ if self.fly.is_none() && self.elapsed >= WRITE + DOT_IN + HOLD + LAUNCH {
+ self.fly = Some(to_mark);
+ }
+ Frame::at(self.elapsed, self.fly.unwrap_or(to_mark))
+ }
+
+ /// An intro whose page has just come fully in, for tests.
+ #[cfg(test)]
+ pub(super) fn with_page_shown() -> Self {
+ Self {
+ elapsed: WRITE + DOT_IN + HOLD + PAGE_AT + PAGE_IN,
+ ..Self::new()
+ }
+ }
+
+ /// Whether the page is fully in. From then on it takes input while
+ /// whatever is left of the intro plays on over it.
+ pub(super) fn page_shown(&self) -> bool {
+ self.elapsed >= WRITE + DOT_IN + HOLD + PAGE_AT + PAGE_IN
}
/// Whether to paint the page, hidden, this once. Its first paint shapes
@@ -147,7 +206,8 @@ impl Intro {
}
/// `page_under`: the page was painted first this frame, so the ground
- /// goes over it rather than under everything.
+ /// goes over it rather than under everything. `mark`: where the page's
+ /// icon was last laid out.
pub(super) fn paint(
&self,
ctx: &DrawContext,
@@ -156,9 +216,10 @@ impl Intro {
ground: LinearRgba,
frame: Frame,
page_under: bool,
+ mark: Option<RectF>,
) -> anyhow::Result<()> {
self.hello
- .paint(ctx, layers, area, ground, frame, page_under)
+ .paint(ctx, layers, area, ground, frame, page_under, mark)
}
}
@@ -169,6 +230,10 @@ struct Hello {
length: f32,
/// One trip round the loop: blue, to green, back to blue.
colours: Vec<LinearRgba>,
+ /// The pen's way on to the icon, laid out once as it sets off and kept
+ /// to the end, gone with the intro: a page that moves its icon or loses
+ /// it mid-flight (a resize, another language) cannot make the pen jump.
+ course: RefCell<Option<Course>>,
}
/// Where the intro is at one moment.
@@ -183,31 +248,71 @@ pub(super) struct Frame {
pub dot: f32,
/// Once it has started, how far the page has faded in.
pub page_in: Option<f32>,
+ /// Once the pen has left the word for the icon.
+ pub flight: Option<Flight>,
+}
+
+/// The pen's way on from the word: the swoop to the icon, then once round.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub(super) struct Flight {
+ /// How far the pen has gone, as a fraction of swoop and ring together.
+ pub pen: f32,
+ /// How far the tail has followed onto the swoop, as a fraction of it.
+ /// It stops where the ring starts, so the ring is whole when it closes.
+ pub tail: f32,
+ /// Seconds since the ring closed.
+ pub closed: Option<f32>,
}
impl Frame {
- /// `None` once the page is fully in and the intro is over.
- pub(super) fn at(elapsed: Duration) -> Option<Self> {
+ /// `None` once the intro is over: the page is fully in and, when the
+ /// pen flies to the icon (`to_mark`), the last spark is gone.
+ pub(super) fn at(elapsed: Duration, to_mark: bool) -> Option<Self> {
let t = elapsed.as_secs_f32();
let phase = |start: Duration, length: Duration| {
((t - start.as_secs_f32()) / length.as_secs_f32()).clamp(0.0, 1.0)
};
let retract_at = WRITE + DOT_IN + HOLD;
let page_at = retract_at + PAGE_AT;
- if elapsed >= page_at + PAGE_IN {
+ let launch = retract_at + LAUNCH;
+ let close = launch + FLIGHT;
+ let end = if to_mark {
+ (close + SPARKS_END).max(page_at + PAGE_IN)
+ } else {
+ page_at + PAGE_IN
+ };
+ if elapsed >= end {
return None;
}
let tail = ease_in_steady_out(phase(retract_at, RETRACT));
let dot_out = ((tail - DOT_OUT_FROM) / (1.0 - DOT_OUT_FROM)).clamp(0.0, 1.0);
+ // The tail goes on onto the swoop once it has the whole word.
+ let word_gone = retract_at + RETRACT;
+ let flight = (to_mark && elapsed >= launch).then(|| Flight {
+ pen: ease_in_out(phase(launch, FLIGHT)),
+ tail: sine_in_out(phase(word_gone, close.saturating_sub(word_gone))),
+ closed: (elapsed >= close).then(|| (elapsed - close).as_secs_f32()),
+ });
Some(Self {
pen: ease_in_out(phase(Duration::ZERO, WRITE)),
tail,
dot: phase(WRITE, DOT_IN) - dot_out,
page_in: (elapsed >= page_at).then(|| ease_in_out(phase(page_at, PAGE_IN))),
+ flight,
})
}
}
+/// Gently off and gently in.
+fn sine_in_out(t: f32) -> f32 {
+ (1.0 - (std::f32::consts::PI * t).cos()) / 2.0
+}
+
+/// Quick off the mark, settling slowly.
+fn ease_out(t: f32) -> f32 {
+ 1.0 - (1.0 - t).powi(3)
+}
+
/// From rest, ending at the average speed: never faster than 4/3 of it.
fn ease_in_steady_out(t: f32) -> f32 {
t * t * (2.0 - t)
@@ -262,6 +367,7 @@ impl Hello {
stamps,
length,
colours,
+ course: RefCell::new(None),
}
}
@@ -301,6 +407,7 @@ impl Hello {
ground: LinearRgba,
frame: Frame,
page_under: bool,
+ mark: Option<RectF>,
) -> anyhow::Result<()> {
let page_in = frame.page_in.unwrap_or(0.0);
let (layer, cover) = if page_under {
@@ -308,15 +415,18 @@ impl Hello {
} else {
(0, ground)
};
- ctx.draw_rect(
- layers,
- layer,
- area.origin.x,
- area.origin.y,
- area.size.width,
- area.size.height,
- cover,
- )?;
+ // Once the page is fully in, the ground over it is gone.
+ if !page_under || page_in < 1.0 {
+ ctx.draw_rect(
+ layers,
+ layer,
+ area.origin.x,
+ area.origin.y,
+ area.size.width,
+ area.size.height,
+ cover,
+ )?;
+ }
let width = ctx
.px(WORD_W)
.min(area.size.width - ctx.px(WORD_PAD) * 2.0)
@@ -325,8 +435,49 @@ impl Hello {
let left = area.origin.x + (area.size.width - width) / 2.0 - VIEW_X * k;
let top = area.origin.y + (area.size.height - VIEW_H * k) / 2.0 - VIEW_Y * k;
- let (pen, tail) = (self.length * frame.pen, self.length * frame.tail);
let diameter = PEN_WIDTH * k;
+ if let (Some(_), Some(mark)) = (frame.flight, mark) {
+ let mut cached = self.course.borrow_mut();
+ if cached.is_none() {
+ let end = self.stamps[self.stamps.len() - 1];
+ let before = self.stamps[self.stamps.len().saturating_sub(4)];
+ let margin = ctx.px(16.0);
+ let bounds = RectF::new(
+ euclid::point2(area.min_x() + margin, area.min_y() + margin),
+ euclid::size2(
+ (area.size.width - 2.0 * margin).max(1.0),
+ (area.size.height - 2.0 * margin).max(1.0),
+ ),
+ );
+ let course = Course::new(
+ (left + end.0 * k, top + end.1 * k),
+ (end.0 - before.0, end.1 - before.1),
+ diameter,
+ width / SWOOP_WORD,
+ mark,
+ bounds,
+ );
+ *cached = Some(course);
+ }
+ }
+ let cached = self.course.borrow();
+ let course = frame.flight.zip(cached.as_ref());
+ // Past the word, a pixel of the course counts as this much of the
+ // word's length, so a whole turn of the rainbow spans the ring.
+ let warp = |course: &Course| 2.0 * self.length / (TURN * course.ring_length()).max(1.0);
+ let (pen, tail) = match &course {
+ Some((flight, course)) => {
+ let w = warp(course);
+ let tail = if frame.tail < 1.0 {
+ self.length * frame.tail
+ } else {
+ self.length + course.ring_at * flight.tail * w
+ };
+ let run = flight.closed.unwrap_or(0.0) * RING_RUN * 2.0 * self.length;
+ (self.length + course.length * flight.pen * w + run, tail)
+ }
+ None => (self.length * frame.pen, self.length * frame.tail),
+ };
for &(x, y, s) in self.shown(frame) {
ctx.draw_disc(
layers,
@@ -347,10 +498,302 @@ impl Hello {
self.ink(self.length, pen + tail).mul_alpha(frame.dot),
)?;
}
+ if let Some((flight, course)) = &course {
+ let w = warp(course);
+ let ink = |s: f32| self.ink(self.length + s * w, pen + tail);
+ let (head, from) = (course.length * flight.pen, course.ring_at * flight.tail);
+ let ring = match flight.closed {
+ Some(closed) => 1.0 - ease_out((closed / RING_FADE).min(1.0)),
+ None => 1.0,
+ };
+ // The ring and its sparks can reach past the page in a short
+ // window; what would cross its edge is left out rather than
+ // drawn over the window's chrome.
+ for &(x, y, s, d) in course.stamps.iter().take_while(|stamp| stamp.2 <= head) {
+ if s < from || ring <= 0.0 || !within(area, x, y, d) {
+ continue;
+ }
+ ctx.draw_disc(layers, 2, x, y, d, ink(s).mul_alpha(ring))?;
+ }
+ if let Some(closed) = flight.closed {
+ for spark in course.sparks(diameter, closed) {
+ if !within(area, spark.x, spark.y, spark.diameter) {
+ continue;
+ }
+ ctx.draw_disc(
+ layers,
+ 2,
+ spark.x,
+ spark.y,
+ spark.diameter,
+ ink(spark.s).mul_alpha(spark.alpha),
+ )?;
+ }
+ }
+ }
Ok(())
}
}
+/// The pen's way on from the word, in pixels for the page as it is laid out
+/// now: one swoop from the word's end, then once round the icon clockwise
+/// from the middle of its top edge.
+struct Course {
+ /// A stamp every so often: position, distance along from the word's
+ /// end, and the pen's width there, which narrows over the swoop.
+ stamps: Vec<(f32, f32, f32, f32)>,
+ /// How far along the ring starts, and the whole way.
+ ring_at: f32,
+ length: f32,
+ /// The icon's middle, which the sparks fly away from.
+ centre: (f32, f32),
+}
+
+/// One spark, drawn as a disc.
+struct Spark {
+ x: f32,
+ y: f32,
+ /// Where along the ring it came from, for its colour.
+ s: f32,
+ diameter: f32,
+ alpha: f32,
+}
+
+impl Course {
+ /// `from`: the word's end, leaving in direction `heading`, the pen
+ /// `pen` wide there. `scale`: the swoop's size against its design.
+ /// `mark`: the icon's image. The swoop's middle and handles are kept
+ /// inside `bounds`, and with them the curve between its two ends; the
+ /// ends are left where they are, so it starts on the word and lands
+ /// exactly where the ring begins. The ring itself is not kept inside:
+ /// what of it would cross the page's edge is left out when drawn.
+ fn new(
+ from: (f32, f32),
+ heading: (f32, f32),
+ pen: f32,
+ scale: f32,
+ mark: RectF,
+ bounds: RectF,
+ ) -> Self {
+ let side = mark.size.width.min(mark.size.height);
+ let body = side * (1.0 - 2.0 * BODY_INSET);
+ let gap = body * RING_GAP;
+ let (rx, ry) = (
+ mark.min_x() + side * BODY_INSET - gap,
+ mark.min_y() + side * BODY_INSET - gap,
+ );
+ let (rw, radius) = (body + 2.0 * gap, side * BODY_RADIUS + gap);
+ let centre = (rx + rw / 2.0, ry + rw / 2.0);
+ let thin = pen * THIN;
+ // Close enough that the discs read as one line, and never closer
+ // than a pixel, however small the window makes the pen.
+ let spacing = (thin / 4.0).max(1.0);
+
+ let inside = |(x, y): (f32, f32)| {
+ (
+ x.clamp(bounds.min_x(), bounds.max_x()),
+ y.clamp(bounds.min_y(), bounds.max_y()),
+ )
+ };
+ let length = (heading.0 * heading.0 + heading.1 * heading.1)
+ .sqrt()
+ .max(1e-3);
+ let dir = (heading.0 / length, heading.1 / length);
+ let (ex, ey) = from;
+ let (cx, top) = (centre.0, ry);
+ let lift = (40.0 * scale).max((ey - top) * 0.6);
+ let handle = (150.0 * scale, -10.0 * scale);
+ // Over the icon and down onto its top edge from the left needs room
+ // above it to turn round in. Where the page leaves too little, the
+ // pen comes up from the right onto the top edge instead and goes
+ // round the other way: no turn back, so no hairpin.
+ let room = top - bounds.min_y() - handle.1.abs();
+ let over = room >= 60.0 * scale;
+ let swoop = if over {
+ let mid = (
+ (cx + (ex - cx) * 0.35).clamp(
+ bounds.min_x() + handle.0,
+ (bounds.max_x() - handle.0).max(bounds.min_x() + handle.0),
+ ),
+ top - lift.min(room),
+ );
+ [
+ [
+ from,
+ inside((ex + dir.0 * 150.0 * scale, ey + dir.1 * 150.0 * scale)),
+ (mid.0 + handle.0, mid.1 + handle.1),
+ mid,
+ ],
+ [
+ mid,
+ (mid.0 - handle.0, mid.1 - handle.1),
+ inside((cx - 130.0 * scale, top - 4.0 * scale)),
+ (cx, top),
+ ],
+ ]
+ } else {
+ // One rising curve, split in two at its middle so both shapes
+ // share the code below.
+ let a1 = inside((ex + dir.0 * 150.0 * scale, ey + dir.1 * 150.0 * scale));
+ let a2 = inside((cx + 130.0 * scale, top - (4.0 * scale).min(room.max(0.0))));
+ let half = |p: (f32, f32), q: (f32, f32)| ((p.0 + q.0) / 2.0, (p.1 + q.1) / 2.0);
+ let (m01, m12, m23) = (half(from, a1), half(a1, a2), half(a2, (cx, top)));
+ let (m012, m123) = (half(m01, m12), half(m12, m23));
+ let mid = half(m012, m123);
+ [[from, m01, m012, mid], [mid, m123, m23, (cx, top)]]
+ };
+ let mut swoop_line = vec![from];
+ for [p0, p1, p2, p3] in swoop {
+ for i in 1..=FLATTEN {
+ let t = i as f32 / FLATTEN as f32;
+ let u = 1.0 - t;
+ let (a, b, c, d) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t);
+ swoop_line.push((
+ a * p0.0 + b * p1.0 + c * p2.0 + d * p3.0,
+ a * p0.1 + b * p1.1 + c * p2.1 + d * p3.1,
+ ));
+ }
+ }
+
+ // Round the ring clockwise from the middle of its top edge.
+ let mut ring_line = vec![(cx, top)];
+ let corner = |line: &mut Vec<(f32, f32)>, ax: f32, ay: f32, a0: f32| {
+ for i in 1..=24 {
+ let a = a0 + std::f32::consts::FRAC_PI_2 * i as f32 / 24.0;
+ line.push((ax + radius * a.cos(), ay + radius * a.sin()));
+ }
+ };
+ let (right, bottom) = (rx + rw, ry + rw);
+ ring_line.push((right - radius, ry));
+ corner(
+ &mut ring_line,
+ right - radius,
+ ry + radius,
+ -std::f32::consts::FRAC_PI_2,
+ );
+ ring_line.push((right, bottom - radius));
+ corner(&mut ring_line, right - radius, bottom - radius, 0.0);
+ ring_line.push((rx + radius, bottom));
+ corner(
+ &mut ring_line,
+ rx + radius,
+ bottom - radius,
+ std::f32::consts::FRAC_PI_2,
+ );
+ ring_line.push((rx, ry + radius));
+ corner(
+ &mut ring_line,
+ rx + radius,
+ ry + radius,
+ std::f32::consts::PI,
+ );
+ ring_line.push((cx, top));
+ if !over {
+ ring_line.reverse();
+ }
+
+ // Stamps every `spacing` along both, the swoop narrowing as it goes.
+ let swoop_length = polyline_length(&swoop_line);
+ let mut stamps = Vec::new();
+ let (mut along, mut next) = (0.0f32, 0.0f32);
+ let mut stamp_along = |line: &[(f32, f32)], width: &dyn Fn(f32) -> f32| {
+ for pair in line.windows(2) {
+ let ((x0, y0), (x1, y1)) = (pair[0], pair[1]);
+ let step = ((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt();
+ while step > 0.0 && next <= along + step {
+ let t = (next - along) / step;
+ stamps.push((x0 + (x1 - x0) * t, y0 + (y1 - y0) * t, next, width(next)));
+ next += spacing;
+ }
+ along += step;
+ }
+ };
+ stamp_along(&swoop_line, &|s| {
+ pen + (thin - pen) * sine_in_out((s / swoop_length.max(1e-3)).min(1.0))
+ });
+ let ring_at = swoop_length;
+ stamp_along(&ring_line, &|_| thin);
+ // A stamp exactly where the ring closes, however the spacing fell.
+ if next - spacing < along - 1e-3 {
+ stamps.push((cx, top, along, thin));
+ }
+ let length = stamps.last().map_or(ring_at, |stamp| stamp.2);
+ Self {
+ stamps,
+ ring_at,
+ length,
+ centre,
+ }
+ }
+
+ fn ring_length(&self) -> f32 {
+ self.length - self.ring_at
+ }
+
+ /// The ring's sparks `closed` seconds after it closed: one every so
+ /// often along it, each flying out from the icon at its own angle,
+ /// distance, start and pace, shrinking and fading as it goes. The same
+ /// every time: the spread comes from each spark's place, not chance.
+ fn sparks(&self, pen: f32, closed: f32) -> impl Iterator<Item = Spark> + '_ {
+ let every = (pen * 0.22).max(1.0);
+ let mut next = self.ring_at;
+ self.stamps
+ .iter()
+ .filter(move |stamp| {
+ if stamp.2 < next {
+ return false;
+ }
+ next = stamp.2 + every;
+ true
+ })
+ .enumerate()
+ .filter_map(move |(i, &(x, y, s, _))| {
+ let r = |salt: u32| scatter(i as u32 * 4 + salt);
+ let distance = pen * (1.06 + 2.0 * r(0));
+ let angle = (r(1) - 0.5) * 0.9;
+ let delay = SPARK_DELAY * r(2);
+ let life = SPARK_LIFE.0 + (SPARK_LIFE.1 - SPARK_LIFE.0) * r(3);
+ let u = (closed - delay) / life;
+ if !(0.0..1.0).contains(&u) {
+ return None;
+ }
+ let (dx, dy) = (x - self.centre.0, y - self.centre.1);
+ let len = (dx * dx + dy * dy).sqrt().max(1e-3);
+ let (c, sn) = (angle.cos(), angle.sin());
+ let (ux, uy) = ((dx * c - dy * sn) / len, (dx * sn + dy * c) / len);
+ let go = distance * ease_out(u);
+ Some(Spark {
+ x: x + ux * go,
+ y: y + uy * go,
+ s,
+ diameter: pen * SPARK * (1.0 - 0.7 * u),
+ alpha: 1.0 - u,
+ })
+ })
+ }
+}
+
+/// Whether a disc `d` wide at (`x`, `y`) lies wholly inside `area`.
+fn within(area: RectF, x: f32, y: f32, d: f32) -> bool {
+ let r = d / 2.0;
+ x - r >= area.min_x() && x + r <= area.max_x() && y - r >= area.min_y() && y + r <= area.max_y()
+}
+
+fn polyline_length(line: &[(f32, f32)]) -> f32 {
+ line.windows(2)
+ .map(|pair| ((pair[1].0 - pair[0].0).powi(2) + (pair[1].1 - pair[0].1).powi(2)).sqrt())
+ .sum()
+}
+
+/// A number in 0..1 that looks random but is fixed by `n`.
+fn scatter(n: u32) -> f32 {
+ let mut h = n.wrapping_mul(0x9E37_79B9) ^ 0x85EB_CA6B;
+ h ^= h >> 15;
+ h = h.wrapping_mul(0x2C1B_3C6D);
+ h ^= h >> 12;
+ (h & 0x00FF_FFFF) as f32 / 0x0100_0000 as f32
+}
+
/// `turn` round the loop, 0 and 1 both blue and 0.5 green, mixed in sRGB as
/// the design was.
fn colour_at(turn: f32) -> LinearRgba {
@@ -411,16 +854,16 @@ mod tests {
fn nothing_is_left_once_the_tail_arrives() {
let hello = Hello::new();
let retract_at = WRITE + DOT_IN + HOLD;
- let written = Frame::at(retract_at).unwrap();
+ let written = Frame::at(retract_at, false).unwrap();
assert_eq!(hello.shown(written).count(), hello.stamps.len());
- let halfway = Frame::at(retract_at + RETRACT / 2).unwrap();
+ let halfway = Frame::at(retract_at + RETRACT / 2, false).unwrap();
let left = hello.shown(halfway).count();
assert!(left > 0 && left < hello.stamps.len());
// From the moment the tail arrives until the page is fully in.
let after = retract_at + RETRACT + (PAGE_AT + PAGE_IN - RETRACT) / 2;
- assert!(Frame::at(after).is_some());
+ assert!(Frame::at(after, false).is_some());
for at in [retract_at + RETRACT, after] {
- let frame = Frame::at(at).unwrap();
+ let frame = Frame::at(at, false).unwrap();
assert_eq!(hello.shown(frame).count(), 0, "at {:?}", at);
assert!(frame.dot <= 1e-4);
}
@@ -429,9 +872,9 @@ mod tests {
#[test]
fn the_page_warms_up_once_while_the_word_is_still() {
let mut intro = Intro::new();
- let writing = Frame::at(WRITE / 2).unwrap();
+ let writing = Frame::at(WRITE / 2, false).unwrap();
assert!(!intro.wants_warm_up(writing));
- let still = Frame::at(WRITE + DOT_IN + HOLD / 2).unwrap();
+ let still = Frame::at(WRITE + DOT_IN + HOLD / 2, false).unwrap();
assert!(intro.wants_warm_up(still));
assert!(!intro.wants_warm_up(still));
}
@@ -440,12 +883,12 @@ mod tests {
fn the_clock_waits_while_nothing_is_painted() {
let mut intro = Intro::new();
let t0 = Instant::now();
- assert_eq!(intro.frame(t0).unwrap().pen, 0.0);
+ assert_eq!(intro.frame(t0, false).unwrap().pen, 0.0);
// A minute covered up moves the intro on by one step, not a minute.
- intro.frame(t0 + Duration::from_secs(60));
+ intro.frame(t0 + Duration::from_secs(60), false);
assert_eq!(intro.elapsed, MAX_STEP);
let mut now = t0 + Duration::from_secs(60);
- while intro.frame(now).is_some() {
+ while intro.frame(now, false).is_some() {
now += Duration::from_millis(16);
}
assert!(intro.elapsed >= WRITE + DOT_IN + HOLD + PAGE_AT + PAGE_IN);
@@ -453,31 +896,177 @@ mod tests {
#[test]
fn timeline_writes_then_takes_it_back_up_under_the_page() {
- let start = Frame::at(Duration::ZERO).unwrap();
+ let start = Frame::at(Duration::ZERO, false).unwrap();
assert_eq!((start.pen, start.tail, start.dot), (0.0, 0.0, 0.0));
assert_eq!(start.page_in, None);
- let written = Frame::at(WRITE + DOT_IN).unwrap();
+ let written = Frame::at(WRITE + DOT_IN, false).unwrap();
assert_eq!((written.pen, written.tail), (1.0, 0.0));
assert!(written.dot > 0.999);
let retract_at = WRITE + DOT_IN + HOLD;
// The page holds off until the tail is well along.
- let before_page = Frame::at(retract_at + PAGE_AT / 2).unwrap();
+ let before_page = Frame::at(retract_at + PAGE_AT / 2, false).unwrap();
assert!(before_page.tail > 0.0 && before_page.page_in.is_none());
- let page_starts = Frame::at(retract_at + PAGE_AT).unwrap();
+ let page_starts = Frame::at(retract_at + PAGE_AT, false).unwrap();
assert!(page_starts.tail > 0.55, "{}", page_starts.tail);
// The tail leaves at a steady pace, not a crawl: its last tenth of the
// path takes about a tenth of the time, and the full stop goes with
// it rather than after.
- let near_end = Frame::at(retract_at + RETRACT * 9 / 10).unwrap();
+ let near_end = Frame::at(retract_at + RETRACT * 9 / 10, false).unwrap();
assert!(near_end.tail < 0.9 && near_end.dot > 0.0);
- let ink_gone = Frame::at(retract_at + RETRACT).unwrap();
+ let ink_gone = Frame::at(retract_at + RETRACT, false).unwrap();
assert_eq!(ink_gone.tail, 1.0);
assert!(ink_gone.dot.abs() < 1e-4);
assert!(ink_gone.page_in.is_some_and(|p| p > 0.0 && p < 1.0));
- assert_eq!(Frame::at(retract_at + PAGE_AT + PAGE_IN), None);
+ assert_eq!(Frame::at(retract_at + PAGE_AT + PAGE_IN, false), None);
+ }
+
+ #[test]
+ fn the_pen_flies_round_the_icon_after_the_page_is_in() {
+ let retract_at = WRITE + DOT_IN + HOLD;
+ let launch = retract_at + LAUNCH;
+ let close = launch + FLIGHT;
+ // Up to the launch, flying changes nothing.
+ let before = Frame::at(launch - Duration::from_millis(1), true).unwrap();
+ assert_eq!(
+ before,
+ Frame::at(launch - Duration::from_millis(1), false).unwrap()
+ );
+ // The pen sets off while the tail is still on the word.
+ let off = Frame::at(launch + Duration::from_millis(100), true).unwrap();
+ let flight = off.flight.unwrap();
+ assert!(flight.pen > 0.0 && off.tail < 1.0 && flight.tail == 0.0);
+ // The page is in before the ring closes, and the intro runs on
+ // until the last spark is gone.
+ assert!(retract_at + PAGE_AT + PAGE_IN < close);
+ let closed = Frame::at(close, true).unwrap().flight.unwrap();
+ assert_eq!(
+ (closed.pen, closed.tail, closed.closed),
+ (1.0, 1.0, Some(0.0))
+ );
+ assert!(Frame::at(close + SPARKS_END - Duration::from_millis(1), true).is_some());
+ assert_eq!(Frame::at(close + SPARKS_END, true), None);
+ // No icon on the page: no flight, and over when the page is in.
+ assert_eq!(
+ Frame::at(launch + Duration::from_millis(100), false)
+ .unwrap()
+ .flight,
+ None
+ );
+ }
+
+ fn course() -> (Course, RectF) {
+ let bounds = RectF::new(euclid::point2(0.0, 0.0), euclid::size2(1600.0, 1100.0));
+ let mark = RectF::new(euclid::point2(728.0, 180.0), euclid::size2(144.0, 144.0));
+ let course = Course::new((1300.0, 520.0), (1.0, -0.6), 30.0, 2.0, mark, bounds);
+ (course, bounds)
+ }
+
+ #[test]
+ fn the_course_runs_from_the_word_to_a_closed_ring_inside_the_page() {
+ let (course, bounds) = course();
+ let first = course.stamps[0];
+ assert!((first.0 - 1300.0).abs() < 0.01 && (first.1 - 520.0).abs() < 0.01);
+ assert_eq!(first.2, 0.0);
+ assert!(course.ring_at > 0.0 && course.ring_at < course.length);
+ for pair in course.stamps.windows(2) {
+ assert!(pair[1].2 > pair[0].2);
+ }
+ // The pen narrows over the swoop and keeps a thin line round the ring.
+ assert_eq!(first.3, 30.0);
+ let last = course.stamps.last().unwrap();
+ assert!((last.3 - 30.0 * THIN).abs() < 0.01);
+ // The ring closes where it started: the middle of its top edge.
+ let start = course
+ .stamps
+ .iter()
+ .find(|stamp| stamp.2 >= course.ring_at)
+ .unwrap();
+ assert!((last.0 - start.0).abs() < 2.0 && (last.1 - start.1).abs() < 2.0);
+ assert!((start.0 - 800.0).abs() < 2.0);
+ for &(x, y, _, _) in &course.stamps {
+ assert!(bounds.contains(euclid::point2(x, y)), "({}, {})", x, y);
+ }
+ }
+
+ #[test]
+ fn sparks_fly_outwards_and_are_gone_by_the_end() {
+ let (course, _) = course();
+ let at = |closed: f32| course.sparks(30.0, closed).collect::<Vec<_>>();
+ assert!(!at(0.3).is_empty());
+ // The same every time.
+ let (a, b) = (at(0.5), at(0.5));
+ assert_eq!(a.len(), b.len());
+ assert!(a.iter().zip(&b).all(|(a, b)| a.x == b.x && a.y == b.y));
+ // Further from the icon later on than where they started.
+ let far = |sparks: &[Spark]| {
+ sparks
+ .iter()
+ .map(|s| ((s.x - course.centre.0).powi(2) + (s.y - course.centre.1).powi(2)).sqrt())
+ .sum::<f32>()
+ / sparks.len() as f32
+ };
+ assert!(far(&at(0.8)) > far(&at(0.2)));
+ assert!(at(SPARKS_END.as_secs_f32()).is_empty());
+ }
+
+ #[test]
+ fn the_flight_is_settled_when_the_pen_sets_off() {
+ let mut intro = Intro::new();
+ let mut now = Instant::now();
+ let launch = WRITE + DOT_IN + HOLD + LAUNCH;
+ while intro.elapsed < launch {
+ intro.frame(now, true);
+ now += Duration::from_millis(16);
+ }
+ // The icon leaving the page afterwards does not ground the pen.
+ let frame = intro.frame(now, false).unwrap();
+ assert!(frame.flight.is_some());
+ assert!(!intro.page_shown());
+ while !intro.page_shown() {
+ now += Duration::from_millis(16);
+ assert!(intro.frame(now, false).is_some());
+ }
+ // The intro outlives the page coming in: the sparks are still to come.
+ assert!(intro.frame(now, false).is_some());
+ }
+
+ #[test]
+ fn the_swoop_stays_smooth_when_the_page_cuts_its_arc() {
+ // The icon close under the page's top edge: the arc's high point
+ // is pulled down onto the page, and the curve must not kink there.
+ let bounds = RectF::new(euclid::point2(0.0, 0.0), euclid::size2(1600.0, 1100.0));
+ let mark = RectF::new(euclid::point2(728.0, 40.0), euclid::size2(144.0, 144.0));
+ let course = Course::new((1300.0, 900.0), (1.0, -0.6), 30.0, 2.0, mark, bounds);
+ let swoop: Vec<_> = course
+ .stamps
+ .iter()
+ .take_while(|stamp| stamp.2 <= course.ring_at)
+ .collect();
+ for w in swoop.windows(3) {
+ let a = (w[1].1 - w[0].1).atan2(w[1].0 - w[0].0);
+ let b = (w[2].1 - w[1].1).atan2(w[2].0 - w[1].0);
+ let mut turn = (b - a).abs();
+ if turn > std::f32::consts::PI {
+ turn = 2.0 * std::f32::consts::PI - turn;
+ }
+ assert!(turn < 0.35, "kink of {} at {:?}", turn, w[1]);
+ }
+ // It lands where the ring starts, so the pen does not jump: the two
+ // stamps either side of the join are no further apart than any two.
+ let ring = course
+ .stamps
+ .iter()
+ .find(|stamp| stamp.2 >= course.ring_at)
+ .unwrap();
+ let landed = swoop.last().unwrap();
+ let gap = ((ring.0 - landed.0).powi(2) + (ring.1 - landed.1).powi(2)).sqrt();
+ assert!(gap <= 30.0 * THIN / 4.0 + 0.01, "gap {}", gap);
+ for stamp in &swoop {
+ assert!(stamp.1 >= 0.0, "left the page at {:?}", stamp);
+ }
}
}