A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! The Start button of the left island: a gradient Windows-style logo whose four squares move on their own, drawn by our own code
//! (vector shapes, nothing taken from Windows' resources).
//!
//! * THE LOGO: four squares in a 2 x 2 grid with a small gap and slightly rounded corners, filled with ONE diagonal gradient across
//! the whole logo: on the dark glass white at the top left to a soft silver blue at the bottom right, on the light glass a graphite
//! one (white would not show there). The colours are [`GRADIENT_FROM`], [`GRADIENT_TO`] and their `_LIGHT` pair, the only place to
//! tune them. The toolkit fills one shape with one gradient, so each square gets the piece of the big gradient that its place has
//! ([`square_stops`]); next to each other the four pieces read as one gradient.
//! * NO PLATE: nothing is drawn behind the logo (a plate behind it read as a separate glass box; the dock's
//! other icons have none either). The logo itself answers.
//! * THE MOTION (the squares move on their own, and the gap is there only in the animation): every square has its own size and its own SPREAD, how far it stands out from the middle, and
//! the logo its GLOW. The three diagonals (top left; top right and bottom left; bottom right) follow each other [`STAGGER_MS`]
//! apart, so every move runs over the logo as a wave from the top left. At rest, under the pointer and while the Start panel is
//! open the squares are in their place: the gap opens only while a press or a release moves them.
//! - under the pointer the squares grow a little ([`HOVER_SCALE`]) and nothing else; the logo is lit only while the Start panel is open;
//! - pressed, the squares close the gap and shrink ([`SPREAD_PRESSED`], [`PRESS_SCALE`]): for a moment the logo is one small tile;
//! - released, they burst out a little past their place ([`SPREAD_OVERSHOOT`], [`OVERSHOOT`]) and close again in about
//! [`RELEASE_MS`], at the size under the pointer.
//! When the panel opens by some other way than a click on the logo (the keyboard, the search field) the same press and release
//! plays once. With animations off every state changes at once.
//! * PIXELS: GPUI snaps every quad's edges to device pixels, so every length here is a whole number of device pixels ([`layout`]):
//! the squares move by whole pixels and never overlap, and at rest the layout is exactly [`logo_geometry`]'s.
//!
//! WHAT IS PURE (and tested): [`StartButton`], the states and what they look like at a moment in time, and [`layout`]. The view calls
//! the button's events (`enter`, `leave`, `press`, `release`, `panel_changed`) with the time of the event and asks
//! [`StartButton::frames`] for each diagonal's size, spread and glow at the time of a frame. [`StartButton::moving`] says whether anything is
//! still in motion: the island asks for frames only then, so a button at rest costs nothing.
use gpui_kit::*;
use rf_win::dock::magnify::ease_in_out;
use super::look::Tint;
// ---------------------------------------------------------------- the numbers (all in one place)
/// The gradient's colour at the top left of the logo on the dark glass, 0xRRGGBB: white (instead of the blue).
pub const GRADIENT_FROM: u32 = 0xFFFFFF;
/// The gradient's colour at the bottom right of the logo on the dark glass: a soft silver blue.
pub const GRADIENT_TO: u32 = 0xB9C6D8;
/// The same gradient on the light glass, where white would not show: graphite, lighter at the top left.
pub const GRADIENT_FROM_LIGHT: u32 = 0x56606E;
/// See [`GRADIENT_FROM_LIGHT`].
pub const GRADIENT_TO_LIGHT: u32 = 0x1D2229;
/// How much the logo lights up when it glows: its colours go this far towards white (towards black on the light glass). A burst
/// adds half as much again per 1.0 of spread.
pub const LIFT: f32 = 0.30;
/// One square's side, logical px.
pub const SQUARE: f32 = 11.0;
/// The gap between the squares, logical px.
pub const GAP: f32 = 1.0;
/// The squares' corner radius, logical px ("very slightly rounded").
pub const CORNER: f32 = 0.75;
/// The whole logo's side at rest, logical px.
pub const LOGO_SIDE: f32 = 2.0 * SQUARE + GAP;
/// How far a square moves out from the middle per 1.0 of spread, logical px (along each axis).
pub const SPREAD_PX: f32 = 1.0;
/// The squares' size under the pointer.
pub const HOVER_SCALE: f32 = 1.08;
/// How long growing to it or back takes.
pub const HOVER_MS: u32 = 160;
/// The spread while pressed: below 0 the gap closes, at -1 the squares touch.
pub const SPREAD_PRESSED: f32 = -1.0;
/// The spread the squares burst out to on release.
pub const SPREAD_OVERSHOOT: f32 = 1.3;
/// How long closing the gap takes on a press.
pub const SPREAD_PRESS_MS: u32 = 90;
/// How long the glow takes to come or go.
pub const GLOW_MS: u32 = 180;
/// The delay from one diagonal to the next.
pub const STAGGER_MS: u32 = 30;
/// The size the squares shrink to while pressed.
pub const PRESS_SCALE: f32 = 0.86;
/// How long the shrink takes.
pub const PRESS_MS: u32 = 90;
/// The size the squares spring past on release.
pub const OVERSHOOT: f32 = 1.16;
/// The way up from the pressed size (and spread) to the overshoot.
pub const RELEASE_UP_MS: u32 = 120;
/// The way back from the overshoot to the place and to the size at rest (or under the pointer).
pub const RELEASE_SETTLE_MS: u32 = 220;
/// The whole release of the first diagonal (the two stretches above; the tests check the total).
#[cfg(test)]
pub const RELEASE_MS: u32 = RELEASE_UP_MS + RELEASE_SETTLE_MS;
/// A release outside the button (or the pointer leaving while pressed) just eases back, without the burst.
pub const OUTSIDE_MS: u32 = 160;
/// A panel that opens this soon after the logo was pressed was opened by that press: its own press and release is the animation.
pub const OWN_CLICK_MS: u64 = 1500;
/// The number of diagonals: the top left square, the top right and bottom left ones, the bottom right one.
const DIAGONALS: usize = 3;
// ---------------------------------------------------------------- the gradient
/// The colour of the big gradient at `t` (0 at the top left of the logo, 1 at the bottom right) on the dark glass.
#[cfg(test)]
pub fn gradient_at(t: f32) -> u32 {
gradient_at_on(t, 0.0)
}
/// [`gradient_at`] on a glass of lightness `light` (the palette's mix: 0 dark, 1 light): the white gradient on the dark glass, the
/// graphite one on the light glass, and between them in between.
pub fn gradient_at_on(t: f32, light: f32) -> u32 {
let light = light.clamp(0.0, 1.0);
let from = Tint::mix(Tint::new(GRADIENT_FROM, 1.0), Tint::new(GRADIENT_FROM_LIGHT, 1.0), light);
let to = Tint::mix(Tint::new(GRADIENT_TO, 1.0), Tint::new(GRADIENT_TO_LIGHT, 1.0), light);
Tint::mix(from, to, t).rgb
}
/// A square's colour `c` lit up by the logo's `glow` (0..1) and a burst's `spread` (nothing at a spread of 0 or below): towards white
/// on the dark glass, towards black on the light one.
pub fn lifted(c: u32, glow: f32, spread: f32, light: f32) -> u32 {
let amount = ((glow.clamp(0.0, 1.0) + 0.5 * spread.max(0.0)) * LIFT).min(1.0);
if amount <= 0.0 {
return c;
}
let toward = Tint::mix(Tint::new(0xFFFFFF, 1.0), Tint::new(0x000000, 1.0), light.clamp(0.0, 1.0));
Tint::mix(Tint::new(c, 1.0), toward, amount).rgb
}
/// Where a square's own gradient reaches its two end colours, as a fraction of the shader's 0..1 run. The shader runs a 135 degree
/// gradient as `0.5 + (dx + dy) / (sqrt 2 * side)` for a point `dx`, `dy` from the square's centre and clamps it to 0..1. A corner of
/// the square is `dx + dy = +-side` from the centre, which is `0.5 +- 1/sqrt 2` there: with the ends at these two fractions (outside
/// 0..1, which the shader takes as they are) no corner is cut off and no square has a flat patch.
pub const STOP_START: f32 = 0.5 - std::f32::consts::FRAC_1_SQRT_2;
/// See [`STOP_START`].
pub const STOP_END: f32 = 0.5 + std::f32::consts::FRAC_1_SQRT_2;
/// The big gradient's colour at the two corners of one square on the dark glass ([`square_stops_on`]).
#[cfg(test)]
pub fn square_stops(row: usize, col: usize, square: f32, gap: f32) -> (u32, u32) {
square_stops_on(row, col, square, gap, 0.0)
}
/// The big gradient's colour at the two corners of one square, the top left one and the bottom right one (these are the end colours
/// of that square's own gradient, set at [`STOP_START`] and [`STOP_END`]), on a glass of lightness `light`. A square at (`row`,
/// `col`), both 0 or 1, of side `square` with `gap` between the squares (any one unit, the same for both); the big gradient runs
/// `(X + Y) / (2 * logo side)` across the logo, so the corner colours follow from where the corners are, and the logo's own corners
/// show exactly the two tuned colours.
pub fn square_stops_on(row: usize, col: usize, square: f32, gap: f32, light: f32) -> (u32, u32) {
let logo_side = 2.0 * square + gap;
let (x0, y0) = (col as f32 * (square + gap), row as f32 * (square + gap));
let t_start = (x0 + y0) / (2.0 * logo_side);
let t_end = (x0 + y0 + 2.0 * square) / (2.0 * logo_side);
(gradient_at_on(t_start, light), gradient_at_on(t_end, light))
}
// ---------------------------------------------------------------- pixels
/// The logo's squares at rest in whole DEVICE pixels.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LogoGeometry {
/// One square's side, device px.
pub square: u32,
/// The gap between the squares, device px (the same across and down).
pub gap: u32,
}
/// The squares and the gap of the logo at `scale` (the dock's size) on a screen of `scale_factor` device px per logical px, each a
/// whole number of device pixels (a gap of at least one). The side is rounded first and the gap after it, so the logo is within a
/// pixel or so of its logical size.
pub fn logo_geometry(scale: f32, scale_factor: f32) -> LogoGeometry {
let k = scale_factor.max(0.25) * scale.max(0.0);
let side = (LOGO_SIDE * k).round().max(3.0);
let gap = (GAP * k).round().max(1.0);
let square = ((side - gap) / 2.0).round().max(1.0);
LogoGeometry { square: square as u32, gap: gap as u32 }
}
/// One square of the logo in device px, from the top left corner of the logo's box.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Placed {
pub x: i32,
pub y: i32,
pub side: u32,
}
/// The logo's box and its four squares (top left, top right, bottom left, bottom right) for the diagonals' `frames`, at the dock's
/// size `k` on a screen of `scale_factor`. The box is the same at every moment (room for the largest burst on every side), so the
/// logo never moves the island's layout. Each square's inner corner stays at the middle of the logo: a spread above 0 moves it out
/// by whole pixels, one below 0 closes its half of the gap (never more, so two squares at most touch), and its size grows or shrinks
/// outward from that corner.
pub fn layout(frames: [Frame; DIAGONALS], k: f32, scale_factor: f32) -> (u32, [Placed; 4], LogoGeometry) {
let rest = logo_geometry(k, scale_factor);
let kd = k.max(0.0) * scale_factor.max(0.25);
let (sq, gap) = (rest.square as i32, rest.gap as i32);
// the room the largest burst needs on each side: the same roundings as below at the largest spread and size
let pad = (SPREAD_OVERSHOOT * SPREAD_PX * kd).round() as i32 + ((sq as f32 * OVERSHOOT).round() as i32 - sq).max(0);
let side = 2 * pad + 2 * sq + gap;
// the inner edge of the left (top) squares at rest; the right (bottom) ones begin a gap after it
let a = pad + sq;
let (near_cap, far_cap) = (gap / 2, gap - gap / 2);
// how far a square's inner edge stands out from its place at rest, along one axis (`cap` is its half of the gap)
let out = |spread: f32, cap: i32| -> i32 {
if spread >= 0.0 { (spread * SPREAD_PX * kd).round() as i32 } else { -((-spread).min(1.0) * cap as f32).round() as i32 }
};
let mut placed = [Placed { x: 0, y: 0, side: 0 }; 4];
for (i, p) in placed.iter_mut().enumerate() {
let (row, col) = (i / 2, i % 2);
let f = frames[row + col];
let f = Frame { scale: f.scale.clamp(0.0, OVERSHOOT), spread: f.spread.min(SPREAD_OVERSHOOT), glow: f.glow };
let s = ((sq as f32) * f.scale.max(0.0)).round().max(1.0) as i32;
let at = |far: bool| if far { a + gap + out(f.spread, far_cap) } else { a - out(f.spread, near_cap) - s };
*p = Placed { x: at(col == 1), y: at(row == 1), side: s as u32 };
}
(side as u32, placed, rest)
}
// ---------------------------------------------------------------- tracks
#[derive(Clone, Copy, Debug, PartialEq)]
enum Ease {
/// Fast at the start, slow at the end: a press.
Out,
/// Slow at both ends.
InOut,
}
impl Ease {
fn at(self, t: f32) -> f32 {
match self {
Ease::Out => 1.0 - (1.0 - t.clamp(0.0, 1.0)).powi(3),
Ease::InOut => ease_in_out(t),
}
}
}
/// One stretch of a value's way: from `from` to `to` over `dur` ms, beginning at `start` ms.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Seg {
start: u64,
dur: u32,
from: f32,
to: f32,
ease: Ease,
}
/// A value over time: it is `base` until the first stretch begins and follows the stretches one after the other. A new set of
/// stretches always begins at the value the track has at that moment, so nothing ever jumps.
#[derive(Clone, Debug, PartialEq)]
struct Track {
base: f32,
segs: Vec<Seg>,
}
impl Track {
fn at_rest(v: f32) -> Track {
Track { base: v, segs: Vec::new() }
}
fn value(&self, now: u64) -> f32 {
let mut v = self.base;
for s in &self.segs {
if now < s.start {
return v;
}
let elapsed = now - s.start;
if elapsed >= u64::from(s.dur) {
v = s.to;
continue;
}
return s.from + (s.to - s.from) * s.ease.at(elapsed as f32 / s.dur as f32);
}
v
}
/// Still moving, or about to: a stretch has not ended yet.
fn busy(&self, now: u64) -> bool {
self.segs.last().is_some_and(|s| now < s.start + u64::from(s.dur))
}
/// Continue from the value at `now` along `steps` (`(delay from now, ms, to, ease)`, one after the other). No stretches at all, or
/// a total of 0 ms, and the track is at its last value at once.
fn go(&mut self, now: u64, steps: &[(u32, u32, f32, Ease)]) {
let mut from = self.value(now);
let mut at = now;
let mut segs = Vec::with_capacity(steps.len());
for &(delay, dur, to, ease) in steps {
at += u64::from(delay);
segs.push(Seg { start: at, dur, from, to, ease });
at += u64::from(dur);
from = to;
}
if steps.iter().all(|s| s.0 == 0 && s.1 == 0) {
self.base = from;
self.segs.clear();
return;
}
self.base = self.value(now);
self.segs = segs;
}
}
/// The same steps for the three diagonals, each [`STAGGER_MS`] after the one before (all at once with animations off).
fn go_wave(tracks: &mut [Track; DIAGONALS], now: u64, animate: bool, steps: &[(u32, u32, f32, Ease)]) {
for (d, track) in tracks.iter_mut().enumerate() {
let mut steps = steps.to_vec();
if animate && let Some(first) = steps.first_mut() {
first.0 += d as u32 * STAGGER_MS;
}
track.go(now, &steps);
}
}
// ---------------------------------------------------------------- the button
/// What one diagonal of the logo looks like at one moment.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Frame {
/// The squares' size, 1.0 at rest.
pub scale: f32,
/// How far the squares stand out from the middle: 0 in their place, above 0 apart (only while moving), [`SPREAD_PRESSED`]
/// pressed (the gap closed).
pub spread: f32,
/// How lit the logo is: 0, or 1 while the Start panel is open.
pub glow: f32,
}
/// The states of the Start button and their motion. Every event takes the time it happened (ms on any clock that does not run back)
/// and whether animations are on; the answers depend on nothing else.
#[derive(Clone, Debug, PartialEq)]
pub struct StartButton {
hovered: bool,
pressed: bool,
panel_open: bool,
scale: [Track; DIAGONALS],
spread: [Track; DIAGONALS],
glow: [Track; DIAGONALS],
/// The glow the tracks are heading for (not to start the same fade again).
glow_goal: f32,
last_press: Option<u64>,
}
impl Default for StartButton {
fn default() -> Self {
StartButton::new()
}
}
impl StartButton {
pub fn new() -> StartButton {
StartButton {
hovered: false,
pressed: false,
panel_open: false,
scale: std::array::from_fn(|_| Track::at_rest(1.0)),
spread: std::array::from_fn(|_| Track::at_rest(0.0)),
glow: std::array::from_fn(|_| Track::at_rest(0.0)),
glow_goal: 0.0,
last_press: None,
}
}
/// The first diagonal (the top left square) at `now`: the one every move begins with.
#[cfg(test)]
pub fn frame(&self, now: u64) -> Frame {
self.frames(now)[0]
}
/// The three diagonals at `now`.
pub fn frames(&self, now: u64) -> [Frame; DIAGONALS] {
std::array::from_fn(|d| Frame { scale: self.scale[d].value(now), spread: self.spread[d].value(now), glow: self.glow[d].value(now) })
}
/// Whether anything is still moving at `now` (the island asks for another frame only then).
pub fn moving(&self, now: u64) -> bool {
self.scale.iter().chain(&self.spread).chain(&self.glow).any(|t| t.busy(now))
}
pub fn is_pressed(&self) -> bool {
self.pressed
}
pub fn is_hovered(&self) -> bool {
self.hovered
}
/// The size the squares come to rest at now: a little larger under the pointer.
fn rest(&self) -> f32 {
if self.hovered { HOVER_SCALE } else { 1.0 }
}
/// The logo lights up or goes back as the states ask: lit while the Start panel is open.
fn retarget_glow(&mut self, now: u64, animate: bool) {
let goal = if self.panel_open { 1.0 } else { 0.0 };
if goal == self.glow_goal {
return;
}
self.glow_goal = goal;
let ease = if goal > 0.0 { Ease::Out } else { Ease::InOut };
go_wave(&mut self.glow, now, animate, &[(0, if animate { GLOW_MS } else { 0 }, goal, ease)]);
}
/// The squares burst out from where they are, a little past their place, and close again at the size at rest.
fn burst(&mut self, now: u64) {
let rest = self.rest();
go_wave(&mut self.scale, now, true, &[(0, RELEASE_UP_MS, OVERSHOOT, Ease::Out), (0, RELEASE_SETTLE_MS, rest, Ease::InOut)]);
go_wave(&mut self.spread, now, true, &[(0, RELEASE_UP_MS, SPREAD_OVERSHOOT, Ease::Out), (0, RELEASE_SETTLE_MS, 0.0, Ease::InOut)]);
}
/// The pointer came onto the button: the squares grow a little, in a wave (not while a press holds them small).
pub fn enter(&mut self, now: u64, animate: bool) {
if self.hovered {
return;
}
self.hovered = true;
if !self.pressed {
go_wave(&mut self.scale, now, animate, &[(0, if animate { HOVER_MS } else { 0 }, HOVER_SCALE, Ease::Out)]);
}
}
/// The pointer left the button. A press that is still held goes on (the logo stays closed and lit): the mouse button coming up
/// decides, inside it bursts out, outside it eases back, and the pointer coming back in meanwhile is just hovering again.
pub fn leave(&mut self, now: u64, animate: bool) {
if !self.hovered {
return;
}
self.hovered = false;
if !self.pressed {
go_wave(&mut self.scale, now, animate, &[(0, if animate { HOVER_MS } else { 0 }, 1.0, Ease::InOut)]);
}
}
/// The mouse button went down on the button: the squares close the gap and shrink.
pub fn press(&mut self, now: u64, animate: bool) {
if self.pressed {
return;
}
self.pressed = true;
self.last_press = Some(now);
go_wave(&mut self.scale, now, animate, &[(0, if animate { PRESS_MS } else { 0 }, PRESS_SCALE, Ease::Out)]);
go_wave(&mut self.spread, now, animate, &[(0, if animate { SPREAD_PRESS_MS } else { 0 }, SPREAD_PRESSED, Ease::Out)]);
}
/// The mouse button went up: inside the button the squares burst out and close again; outside they ease back into their place.
pub fn release(&mut self, now: u64, inside: bool, animate: bool) {
if !self.pressed {
return;
}
self.pressed = false;
if animate && inside {
self.burst(now);
} else {
let ms = if animate { OUTSIDE_MS } else { 0 };
let rest = self.rest();
go_wave(&mut self.scale, now, animate, &[(0, ms, rest, Ease::InOut)]);
go_wave(&mut self.spread, now, animate, &[(0, ms, 0.0, Ease::InOut)]);
}
}
/// The Start panel opened or closed. Open keeps the logo lit. When it opened without a press on the logo (the keyboard, the
/// search field) the press and release plays once; when the user pressed the logo a moment ago that press was it.
pub fn panel_changed(&mut self, open: bool, now: u64, animate: bool) {
if open == self.panel_open {
return;
}
self.panel_open = open;
let own = self.pressed || self.last_press.is_some_and(|t| now.saturating_sub(t) < OWN_CLICK_MS);
if open && animate && !own {
let rest = self.rest();
go_wave(
&mut self.scale,
now,
true,
&[(0, PRESS_MS, PRESS_SCALE, Ease::Out), (0, RELEASE_UP_MS, OVERSHOOT, Ease::Out), (0, RELEASE_SETTLE_MS, rest, Ease::InOut)],
);
go_wave(
&mut self.spread,
now,
true,
&[
(0, SPREAD_PRESS_MS, SPREAD_PRESSED, Ease::Out),
(PRESS_MS.saturating_sub(SPREAD_PRESS_MS), RELEASE_UP_MS, SPREAD_OVERSHOOT, Ease::Out),
(0, RELEASE_SETTLE_MS, 0.0, Ease::InOut),
],
);
}
self.retarget_glow(now, animate);
}
}
// ---------------------------------------------------------------- drawing
/// The logo for the diagonals' `frames` at the dock's size `k` on a screen of `scale_factor` device px per logical px, on a glass of
/// lightness `light`: four squares of the gradient placed by [`layout`] in a box of a fixed size.
pub fn logo(frames: [Frame; DIAGONALS], k: f32, scale_factor: f32, light: f32) -> impl IntoElement {
let (side, placed, rest) = layout(frames, k, scale_factor);
let logical = move |device: f32| px(device / scale_factor);
let radius = px(CORNER * k);
let mut squares = Vec::with_capacity(4);
for (i, p) in placed.into_iter().enumerate() {
let (row, col) = (i / 2, i % 2);
let f = frames[row + col];
let (from, to) = square_stops_on(row, col, rest.square as f32, rest.gap as f32, light);
let (from, to) = (lifted(from, f.glow, f.spread, light), lifted(to, f.glow, f.spread, light));
squares.push(
div()
.absolute()
.left(logical(p.x as f32))
.top(logical(p.y as f32))
.size(logical(p.side as f32))
.rounded(radius)
.bg(linear_gradient(135., linear_color_stop(rgb(from), STOP_START), linear_color_stop(rgb(to), STOP_END))),
);
}
div().relative().flex_none().size(logical(side as f32)).children(squares)
}
#[cfg(test)]
mod tests {
// (not `super::*`: the toolkit's glob brings a `test` macro that would shadow `#[test]`)
use super::{
CORNER, DIAGONALS, Frame, GAP, GLOW_MS, GRADIENT_FROM, GRADIENT_FROM_LIGHT, GRADIENT_TO, GRADIENT_TO_LIGHT, HOVER_MS,
HOVER_SCALE, LOGO_SIDE, OUTSIDE_MS, OVERSHOOT, PRESS_MS, PRESS_SCALE, Placed, RELEASE_MS, RELEASE_UP_MS, SPREAD_OVERSHOOT,
SPREAD_PRESS_MS, SPREAD_PRESSED, SQUARE, STAGGER_MS, STOP_END, STOP_START, StartButton, Tint, gradient_at, gradient_at_on, layout,
lifted, logo_geometry, square_stops,
};
const ON: bool = true;
const OFF: bool = false;
/// The last diagonal has finished a move this long after the first one.
const WAVE: u64 = 2 * STAGGER_MS as u64;
fn near(a: f32, b: f32) -> bool {
(a - b).abs() < 1e-3
}
fn sample(b: &StartButton, from: u64, to: u64) -> Vec<f32> {
(from..=to).map(|t| b.frame(t).scale).collect()
}
/// Every diagonal's spread at every ms.
fn spreads(b: &StartButton, from: u64, to: u64) -> Vec<f32> {
(from..=to).flat_map(|t| b.frames(t).map(|f| f.spread)).collect()
}
fn max(v: &[f32]) -> f32 {
v.iter().cloned().fold(f32::MIN, f32::max)
}
fn f(scale: f32, spread: f32, glow: f32) -> Frame {
Frame { scale, spread, glow }
}
fn at_rest() -> Frame {
f(1.0, 0.0, 0.0)
}
/// In place and lit: open, once the motion is over.
fn lit() -> Frame {
f(1.0, 0.0, 1.0)
}
/// Under the pointer, once the motion is over: grown, nothing else.
fn hovered() -> Frame {
f(HOVER_SCALE, 0.0, 0.0)
}
fn all_near(b: &StartButton, t: u64, want: Frame) -> bool {
b.frames(t).iter().all(|g| near(g.scale, want.scale) && near(g.spread, want.spread) && near(g.glow, want.glow))
}
#[test]
fn at_rest_nothing_moves_and_the_squares_are_in_place() {
let b = StartButton::new();
assert_eq!(b.frames(0), [at_rest(); DIAGONALS]);
assert_eq!(b.frame(10_000), at_rest());
assert!(!b.moving(0));
assert!(!b.moving(10_000));
}
#[test]
fn the_pointer_coming_on_grows_the_squares_a_little_in_a_wave_and_nothing_else() {
let mut b = StartButton::new();
b.enter(1000, ON);
assert!(b.moving(1010));
// the first diagonal starts at once, the others one and two staggers later
let g = b.frames(1000 + u64::from(STAGGER_MS));
assert!(g[0].scale > 1.0 && g[1].scale == 1.0 && g[2].scale == 1.0, "{g:?}");
assert!(near(b.frame(1000 + u64::from(HOVER_MS)).scale, HOVER_SCALE));
let end = 1000 + u64::from(HOVER_MS) + WAVE;
assert!(all_near(&b, end, hovered()), "{:?}", b.frames(end));
assert!(!b.moving(end));
// never past it, and no gap and no light
assert!(sample(&b, 1000, 1600).iter().all(|s| *s >= 1.0 && *s <= HOVER_SCALE + 1e-4));
assert!(spreads(&b, 1000, 1600).iter().all(|s| *s == 0.0));
assert!((1000..1600).all(|t| b.frames(t).iter().all(|g| g.glow == 0.0)));
// leaving: back to the size at rest
b.leave(2000, ON);
let mid = b.frame(2000 + u64::from(HOVER_MS) / 2).scale;
assert!(mid > 1.0 && mid < HOVER_SCALE);
assert!(all_near(&b, 2000 + u64::from(HOVER_MS) + WAVE, at_rest()));
assert!(!b.moving(2000 + u64::from(HOVER_MS) + WAVE));
}
#[test]
fn pressing_closes_the_gap_and_shrinks_fast_at_first() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
assert!(b.is_pressed());
// eased out: past half of the way after half of the time
let half = b.frame(1000 + u64::from(PRESS_MS) / 2).scale;
assert!(half < (1.0 + PRESS_SCALE) / 2.0, "eased out, not linear: {half}");
assert!(near(b.frame(1000 + u64::from(PRESS_MS)).scale, PRESS_SCALE));
assert!(near(b.frame(1000 + u64::from(SPREAD_PRESS_MS)).spread, SPREAD_PRESSED));
// it holds there for as long as the button is down, every square
assert!(all_near(&b, 9000, f(PRESS_SCALE, SPREAD_PRESSED, 0.0)));
assert!(!b.moving(9000));
// never smaller than the pressed size on the way
assert!(sample(&b, 1000, 1200).iter().all(|s| *s >= PRESS_SCALE - 1e-4));
}
#[test]
fn releasing_inside_bursts_out_a_little_and_closes_again_in_about_340_ms() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.release(2000, true, ON);
assert_eq!(RELEASE_MS, 340);
let s = sample(&b, 2000, 2600);
assert!(near(max(&s), OVERSHOOT), "springs to {}", max(&s));
assert!(s.iter().all(|v| *v >= PRESS_SCALE - 1e-4 && *v <= OVERSHOOT + 1e-4));
assert!(near(max(&spreads(&b, 2000, 2600)), SPREAD_OVERSHOOT));
// the peak is at the end of the way up
assert!(near(b.frame(2000 + u64::from(RELEASE_UP_MS)).scale, OVERSHOOT));
assert!(near(b.frame(2000 + u64::from(RELEASE_UP_MS)).spread, SPREAD_OVERSHOOT));
// then back in place at the size under the pointer: the gap is gone again
let end = 2000 + u64::from(RELEASE_MS);
assert!(near(b.frame(end).scale, HOVER_SCALE) && near(b.frame(end).spread, 0.0));
assert!(b.moving(end + WAVE - 1));
assert!(!b.moving(end + WAVE));
assert!(all_near(&b, end + WAVE, hovered()));
}
#[test]
fn releasing_outside_eases_back_into_place_without_the_burst() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.release(1500, false, ON);
// (the pointer is still told to be on it: back to the size under the pointer, never past it)
let s = sample(&b, 1500, 1900);
assert!(s.iter().all(|v| *v <= HOVER_SCALE + 1e-4), "no overshoot");
assert!(s.windows(2).all(|w| w[1] >= w[0] - 1e-5), "only up");
assert!(spreads(&b, 1500, 1900).iter().all(|v| *v <= 1e-4), "never apart");
assert!(all_near(&b, 1500 + u64::from(OUTSIDE_MS) + WAVE, hovered()));
}
#[test]
fn leaving_while_held_does_not_end_the_press_and_the_release_inside_still_bursts() {
// GPUI reports the pointer as gone on the first mouse move while the button is down, even one pixel inside the button:
// that must not take the burst away.
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.leave(1300, ON);
assert!(b.is_pressed(), "still held");
assert!(!b.is_hovered());
assert!(all_near(&b, 2000, f(PRESS_SCALE, SPREAD_PRESSED, 0.0)), "still small and closed");
// the pointer is told to be back; the button comes up inside
b.enter(2100, ON);
b.release(2100, true, ON);
assert!(near(max(&sample(&b, 2100, 2600)), OVERSHOOT), "the burst plays");
assert!(all_near(&b, 2100 + u64::from(RELEASE_MS) + WAVE, hovered()));
}
#[test]
fn leaving_while_held_and_releasing_outside_eases_back_to_rest() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.leave(1300, ON);
b.release(1700, false, ON);
assert!(!b.is_pressed());
assert!(sample(&b, 1700, 2000).iter().all(|v| *v <= 1.0 + 1e-4), "no burst outside");
assert!(all_near(&b, 1700 + u64::from(OUTSIDE_MS) + WAVE, at_rest()));
// a second mouse-up changes nothing
let before = b.clone();
b.release(1800, false, ON);
assert_eq!(b, before);
}
#[test]
fn the_pointer_leaving_and_returning_while_held_is_still_one_press_and_one_burst() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(100, ON);
b.leave(150, ON);
b.enter(170, ON);
assert!(b.is_pressed() && b.is_hovered());
assert!(all_near(&b, 1000, f(PRESS_SCALE, SPREAD_PRESSED, 0.0)), "no growing while held");
b.release(1000, true, ON);
assert!(near(max(&sample(&b, 1000, 1400)), OVERSHOOT));
}
#[test]
fn a_press_during_a_release_starts_from_where_every_square_is_without_a_jump() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.release(2000, true, ON);
// in the middle of the wave: the diagonals are at different points of the burst
let at = 2000 + 45;
let before = b.frames(at);
assert!(before[0].scale != before[2].scale);
b.press(at, ON);
assert_eq!(b.frames(at), before, "no jump");
assert!(b.frame(at + 10).scale <= before[0].scale);
assert!(b.frames(at + u64::from(PRESS_MS) + WAVE).iter().all(|g| near(g.scale, PRESS_SCALE)));
// and once more on the way up from the overshoot
b.release(at + 500, true, ON);
let t = at + 500 + u64::from(RELEASE_UP_MS) + 10;
let high = b.frames(t);
assert!(high[0].scale > 1.0);
b.press(t, ON);
assert_eq!(b.frames(t), high);
}
#[test]
fn an_open_panel_keeps_the_logo_lit_with_the_squares_in_place_and_a_closed_one_lets_the_glow_go() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.release(1100, true, ON);
b.panel_changed(true, 1200, ON);
b.leave(1300, ON);
assert!(all_near(&b, 5000, lit()), "lit while open, in place");
b.panel_changed(false, 6000, ON);
let back = 6000 + u64::from(GLOW_MS) + WAVE;
assert!(all_near(&b, back, at_rest()));
assert!(!b.moving(back));
}
#[test]
fn a_panel_opened_by_the_logos_own_click_does_not_play_the_press_again() {
let mut b = StartButton::new();
b.enter(0, ON);
b.press(1000, ON);
b.release(1090, true, ON);
// the panel's window takes its time to appear, up to a second
b.panel_changed(true, 1900, ON);
// the release's burst is over by then: the squares do not move again, only the light comes
assert!(all_near(&b, 1900, hovered()));
assert!(sample(&b, 1900, 2400).iter().all(|s| near(*s, HOVER_SCALE)));
assert!(spreads(&b, 1900, 2400).iter().all(|s| *s == 0.0));
assert!(all_near(&b, 1900 + u64::from(GLOW_MS) + WAVE, f(HOVER_SCALE, 0.0, 1.0)));
// while the button is still down it is the press itself
let mut c = StartButton::new();
c.press(100, ON);
c.panel_changed(true, 150, ON);
assert!(near(c.frame(5000).scale, PRESS_SCALE));
}
#[test]
fn a_panel_opened_from_the_keyboard_or_the_search_field_plays_one_press_and_burst() {
let mut b = StartButton::new();
b.panel_changed(true, 5000, ON);
assert!(b.moving(5010));
assert!(near(b.frame(5000 + u64::from(PRESS_MS)).scale, PRESS_SCALE));
assert!(near(b.frame(5000 + u64::from(SPREAD_PRESS_MS)).spread, SPREAD_PRESSED));
let s = sample(&b, 5000, 5600);
assert!(near(max(&s), OVERSHOOT));
assert!(s.iter().all(|v| *v >= PRESS_SCALE - 1e-4));
assert!(near(max(&spreads(&b, 5000, 5600)), SPREAD_OVERSHOOT));
let end = 5000 + u64::from(PRESS_MS + RELEASE_MS) + WAVE;
assert!(all_near(&b, end, lit()));
assert!(!b.moving(end));
// only once: the same call twice does not restart it, closing and opening again later plays again
b.panel_changed(true, end + 10, ON);
assert!(!b.moving(end + 10));
b.panel_changed(false, end + 20, ON);
b.panel_changed(true, end + 5000, ON);
assert!(b.moving(end + 5010));
}
#[test]
fn with_animations_off_every_state_changes_at_once() {
let mut b = StartButton::new();
b.enter(100, OFF);
assert_eq!(b.frames(100), [hovered(); DIAGONALS]);
assert!(!b.moving(100));
b.press(200, OFF);
assert_eq!(b.frames(200), [f(PRESS_SCALE, SPREAD_PRESSED, 0.0); DIAGONALS]);
assert!(!b.moving(200));
b.release(300, true, OFF);
assert_eq!(b.frames(300), [hovered(); DIAGONALS]);
assert!(sample(&b, 300, 399).iter().all(|v| *v == HOVER_SCALE), "no burst");
b.panel_changed(true, 400, OFF);
b.leave(500, OFF);
assert_eq!(b.frames(500), [lit(); DIAGONALS]);
b.panel_changed(false, 600, OFF);
assert_eq!(b.frames(600), [at_rest(); DIAGONALS]);
assert!(!b.moving(600));
}
#[test]
fn a_panel_opened_with_animations_off_plays_nothing() {
let mut b = StartButton::new();
b.panel_changed(true, 100, OFF);
assert!(!b.moving(100));
assert_eq!(b.frame(100), lit());
}
#[test]
fn the_same_event_twice_changes_nothing() {
let mut b = StartButton::new();
b.enter(0, ON);
let once = b.clone();
b.enter(50, ON);
assert_eq!(b, once);
b.press(100, ON);
let pressed = b.clone();
b.press(120, ON);
assert_eq!(b, pressed);
b.release(300, true, ON);
let released = b.clone();
b.release(310, true, ON);
assert_eq!(b, released);
b.leave(400, ON);
let left = b.clone();
b.leave(410, ON);
assert_eq!(b, left);
}
// ---- pixels
fn rects_overlap(a: Placed, b: Placed) -> bool {
let (a1, b1) = (a.side as i32, b.side as i32);
a.x < b.x + b1 && b.x < a.x + a1 && a.y < b.y + b1 && b.y < a.y + a1
}
#[test]
fn at_rest_the_layout_is_the_logo_geometry_with_its_gap() {
for sf in [1.0, 1.25, 1.5, 2.0, 3.0] {
for k in [0.85, 1.0, 1.2, 1.4] {
let (side, p, rest) = layout([at_rest(); DIAGONALS], k, sf);
assert_eq!(rest, logo_geometry(k, sf));
let s = rest.square;
assert!(p.iter().all(|q| q.side == s));
// the grid: equal gaps across and down, centred in the box
assert_eq!(p[1].x - (p[0].x + s as i32), rest.gap as i32);
assert_eq!(p[2].y - (p[0].y + s as i32), rest.gap as i32);
assert_eq!((p[0].x, p[0].y), (p[2].x, p[1].y));
assert_eq!(p[0].x, side as i32 - (p[1].x + s as i32), "as much room on the left as on the right");
assert!(rest.gap >= 1);
// lit is the same place
assert_eq!(layout([lit(); DIAGONALS], k, sf).1, p);
}
}
}
#[test]
fn at_every_moment_the_squares_stay_in_the_box_never_overlap_and_the_box_fits_the_button() {
for sf in [1.0, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0] {
for k in [0.85, 1.0, 1.2, 1.4] {
let button = (super::super::plan::LOGO * k * sf).floor() as u32;
let mut box_side = None;
let mut scale = PRESS_SCALE - 0.01;
while scale <= OVERSHOOT + 0.05 {
let mut spread = SPREAD_PRESSED - 0.2;
while spread <= SPREAD_OVERSHOOT + 0.5 {
// the diagonals at different points of a wave too
for frames in [
[f(scale, spread, 0.0); DIAGONALS],
[f(scale, spread, 1.0), at_rest(), f(OVERSHOOT, SPREAD_PRESSED, 0.0)],
[f(PRESS_SCALE, SPREAD_OVERSHOOT, 0.0), f(scale, spread, 0.5), f(1.0, SPREAD_PRESSED, 1.0)],
] {
let (side, p, _) = layout(frames, k, sf);
assert_eq!(*box_side.get_or_insert(side), side, "the box never changes");
assert!(side <= button, "sf {sf} k {k}: box {side} in a button of {button}");
for (i, a) in p.iter().enumerate() {
assert!(a.side >= 1);
assert!(
a.x >= 0 && a.y >= 0 && a.x + a.side as i32 <= side as i32 && a.y + a.side as i32 <= side as i32,
"sf {sf} k {k} {frames:?}: {a:?} out of {side}"
);
for b in &p[i + 1..] {
assert!(!rects_overlap(*a, *b), "sf {sf} k {k} {frames:?}: {a:?} {b:?}");
}
}
}
spread += 0.05;
}
scale += 0.01;
}
}
}
}
#[test]
fn hovering_grows_every_square_a_pixel_a_spread_moves_them_out_and_pressing_closes_the_gap() {
// on the small dock at 100 % too: the growth is a whole pixel, outward from the middle
for k in [0.85, 1.0] {
let (_, rest, _) = layout([at_rest(); DIAGONALS], k, 1.0);
let (_, big, _) = layout([hovered(); DIAGONALS], k, 1.0);
assert_eq!(big[0].side, rest[0].side + 1, "k {k}");
assert_eq!(big[0].x + big[0].side as i32, rest[0].x + rest[0].side as i32);
assert_eq!(big[3].x, rest[3].x);
}
let k = 1.0;
let (_, rest, _) = layout([at_rest(); DIAGONALS], k, 1.0);
let (_, apart, _) = layout([f(1.0, 1.0, 1.0); DIAGONALS], k, 1.0);
let (_, closed, _) = layout([f(1.0, SPREAD_PRESSED, 1.0); DIAGONALS], k, 1.0);
// apart: every square one pixel further out on both axes, no more (a little gap, only while moving)
assert_eq!((apart[0].x, apart[0].y), (rest[0].x - 1, rest[0].y - 1));
assert_eq!((apart[3].x, apart[3].y), (rest[3].x + 1, rest[3].y + 1));
let (_, burst, _) = layout([f(1.0, SPREAD_OVERSHOOT, 1.0); DIAGONALS], k, 1.0);
assert!(rest[0].x - burst[0].x <= 2, "a burst is a pixel or two");
// closed: the top two touch, the left two touch
assert_eq!(closed[0].x + closed[0].side as i32, closed[1].x);
assert_eq!(closed[0].y + closed[0].side as i32, closed[2].y);
// smaller squares keep their inner corner at the middle: the logo shrinks towards its centre
let (_, small, _) = layout([f(PRESS_SCALE, 0.0, 0.0); DIAGONALS], k, 1.0);
assert_eq!(small[0].x + small[0].side as i32, rest[0].x + rest[0].side as i32);
assert_eq!(small[3].x, rest[3].x);
assert!(small[0].side < rest[0].side);
}
// ---- colours
fn channels(c: u32) -> [i32; 3] {
[((c >> 16) & 0xFF) as i32, ((c >> 8) & 0xFF) as i32, (c & 0xFF) as i32]
}
fn close_to(a: u32, b: u32, tol: i32) -> bool {
channels(a).iter().zip(channels(b)).all(|(x, y)| (x - y).abs() <= tol)
}
fn sum(c: u32) -> i32 {
channels(c).iter().sum()
}
#[test]
fn the_four_squares_read_as_one_gradient_from_white_to_silver() {
for (square, gap) in [(8.0, 1.5), (8.0, 2.0), (13.0, 2.0), (17.0, 3.0)] {
let side = 2.0 * square + gap;
let all = [square_stops(0, 0, square, gap), square_stops(0, 1, square, gap), square_stops(1, 0, square, gap), square_stops(1, 1, square, gap)];
// the corners of the whole logo show exactly the two tuned colours: nothing is clamped away
assert_eq!(all[0].0, GRADIENT_FROM);
assert_eq!(all[3].1, GRADIENT_TO);
// the other two squares are the same colours (the gradient is along the diagonal) and each piece goes light to deep
assert_eq!(all[1], all[2]);
for (a, b) in all {
assert!(channels(a)[0] > channels(b)[0], "{a:06x} {b:06x}");
}
// each square's pieces start where the big gradient is at its top left corner and end at its bottom right one
let want = |x: f32, y: f32| gradient_at((x + y) / (2.0 * side));
assert_eq!(all[1].0, want(square + gap, 0.0));
assert_eq!(all[1].1, want(square + gap + square, square));
// the middle of a square's piece is the big gradient at the middle of that square
let (a, b) = all[3];
let mid = square + gap + square / 2.0;
assert!(close_to(Tint::mix(Tint::new(a, 1.0), Tint::new(b, 1.0), 0.5).rgb, want(mid, mid), 1));
}
assert_eq!(gradient_at(0.0), GRADIENT_FROM);
assert_eq!(gradient_at(1.0), GRADIENT_TO);
// the ends of the shader's run sit a corner away from the centre, so that no corner is clamped
assert!((STOP_START - (0.5 - 1.0 / 2f32.sqrt())).abs() < 1e-6 && (STOP_END - (0.5 + 1.0 / 2f32.sqrt())).abs() < 1e-6);
const { assert!(STOP_START < 0.0 && STOP_END > 1.0) };
}
#[test]
fn on_the_light_glass_the_logo_is_graphite_and_in_between_it_is_in_between() {
assert_eq!(gradient_at_on(0.0, 0.0), GRADIENT_FROM);
assert_eq!(gradient_at_on(1.0, 0.0), GRADIENT_TO);
assert_eq!(gradient_at_on(0.0, 1.0), GRADIENT_FROM_LIGHT);
assert_eq!(gradient_at_on(1.0, 1.0), GRADIENT_TO_LIGHT);
assert!(sum(GRADIENT_FROM_LIGHT) < sum(GRADIENT_TO) && sum(GRADIENT_TO_LIGHT) < sum(GRADIENT_FROM_LIGHT));
let half = sum(gradient_at_on(0.5, 0.5));
assert!(half < sum(gradient_at_on(0.5, 0.0)) && half > sum(gradient_at_on(0.5, 1.0)));
// a mix out of range is taken as its end
assert_eq!(gradient_at_on(0.0, 7.0), GRADIENT_FROM_LIGHT);
assert_eq!(gradient_at_on(0.0, -1.0), GRADIENT_FROM);
}
#[test]
fn a_lit_logo_is_brighter_on_the_dark_glass_and_deeper_on_the_light_one_and_a_burst_flashes() {
// nothing at rest, pressed alone adds nothing beyond its glow
assert_eq!(lifted(GRADIENT_TO, 0.0, 0.0, 0.0), GRADIENT_TO);
assert_eq!(lifted(GRADIENT_TO, 0.0, SPREAD_PRESSED, 0.0), GRADIENT_TO);
let lit = lifted(GRADIENT_TO, 1.0, 0.0, 0.0);
let flash = lifted(GRADIENT_TO, 1.0, SPREAD_OVERSHOOT, 0.0);
assert!(sum(GRADIENT_TO) < sum(lit) && sum(lit) < sum(flash));
assert_eq!(lifted(0xFFFFFF, 1.0, 0.0, 0.0), 0xFFFFFF, "white stays white");
// on the light glass towards black
assert!(sum(lifted(GRADIENT_FROM_LIGHT, 1.0, 0.0, 1.0)) < sum(GRADIENT_FROM_LIGHT));
}
#[test]
fn the_logo_is_a_little_larger_than_two_squares_and_slightly_rounded() {
assert_eq!(LOGO_SIDE, 2.0 * SQUARE + GAP);
const { assert!(CORNER > 0.0 && CORNER < SQUARE / 4.0, "slightly rounded") };
const { assert!(SPREAD_PRESSED < 0.0 && SPREAD_OVERSHOOT > 0.0) };
const { assert!(PRESS_SCALE < 1.0 && 1.0 < HOVER_SCALE && HOVER_SCALE < OVERSHOOT, "the burst goes past the size under the pointer") };
}
}