use super::{EmptyScene, Phase};
use arc_motion::arc_spring;
use arc_tokens::{ActiveArcTheme as _, BlockTokens};
use gpui_kit::{
App, ElementId, InteractiveElement as _, IntoElement, ParentElement, PathBuilder, Styled,
TestSupportExt as _, Window, canvas, div, point, px,
};
struct ArtPrim {
kind: &'static str,
tone: &'static str,
geo: &'static [f32],
fx: [f32; 2],
}
#[path = "empty_art_frames.rs"]
mod frames;
fn frame(scene: EmptyScene, phase: Phase) -> &'static [ArtPrim] {
use frames::*;
match (scene, phase) {
(EmptyScene::Search, Phase::Idle) => SEARCH_IDLE,
(EmptyScene::Search, Phase::Loading) => SEARCH_LOADING,
(EmptyScene::Search, Phase::Done) => SEARCH_DONE,
(EmptyScene::Offline, Phase::Idle) => OFFLINE_IDLE,
(EmptyScene::Offline, Phase::Loading) => OFFLINE_LOADING,
(EmptyScene::Offline, Phase::Done) => OFFLINE_DONE,
(EmptyScene::Inbox, Phase::Idle) => INBOX_IDLE,
(EmptyScene::Inbox, Phase::Loading) => INBOX_LOADING,
(EmptyScene::Inbox, Phase::Done) => INBOX_DONE,
(EmptyScene::NotFound, Phase::Idle) => MAP_IDLE,
(EmptyScene::NotFound, Phase::Loading) => MAP_LOADING,
(EmptyScene::NotFound, Phase::Done) => MAP_DONE,
}
}
fn tile_points(g: &[f32]) -> Vec<(f32, f32)> {
let hw = g[2].max(0.0) / 2.0;
let hh = g[3].max(0.0) / 2.0;
let r = g[4].max(0.0).min(hw).min(hh);
let tan = g[6].to_radians().tan();
let (sin, cos) = g[5].to_radians().sin_cos();
let mut points = Vec::with_capacity(28);
for (cx, cy, start) in [
(hw - r, -hh + r, -90.0),
(hw - r, hh - r, 0.0),
(-hw + r, hh - r, 90.0),
(-hw + r, -hh + r, 180.0),
] {
for step in 0..=6 {
let a = (start + step as f32 * 15.0).to_radians();
let x = cx + r * a.cos();
let y = cy + r * a.sin() + x * tan;
points.push((x * cos - y * sin + g[0], x * sin + y * cos + g[1]));
}
}
points
}
fn paint(
points: &[(f32, f32)],
closed: bool,
fill: bool,
color: gpui_kit::Hsla,
origin: gpui_kit::Point<gpui_kit::Pixels>,
scale: f32,
window: &mut Window,
) {
if points.len() < 2 || color.a < 0.001 {
return;
}
let mut path = if fill {
PathBuilder::fill()
} else {
PathBuilder::stroke(px(1.5))
};
if let gpui_kit::PathStyle::Stroke(options) = &mut path.style {
options.line_cap = gpui_kit::LineCap::Round;
options.line_join = gpui_kit::LineJoin::Round;
}
for (i, &(x, y)) in points.iter().enumerate() {
let p = point(origin.x + px(x * scale), origin.y + px(y * scale));
if i == 0 {
path.move_to(p);
} else {
path.line_to(p);
}
}
if closed {
path.close();
}
if let Ok(path) = path.build() {
window.paint_path(path, color);
}
}
fn traced(points: &[(f32, f32)], draw: f32, gap: f32) -> Vec<Vec<(f32, f32)>> {
let length: f32 = points
.windows(2)
.map(|p| (p[1].0 - p[0].0).hypot(p[1].1 - p[0].1))
.sum();
let limit = length * draw.clamp(0.0, 1.0);
let mut at = 0.0;
let mut paths: Vec<Vec<(f32, f32)>> = Vec::new();
for pair in points.windows(2) {
let (a, b) = (pair[0], pair[1]);
let len = (b.0 - a.0).hypot(b.1 - a.1);
if len < 0.0001 {
continue;
}
let mut pos = 0.0;
while pos < len && at < limit {
let on = gap <= 0.2 || at % (3.0 + gap) < 3.0;
let chunk = if gap <= 0.2 {
len - pos
} else {
let phase = at % (3.0 + gap);
(if on { 3.0 - phase } else { 3.0 + gap - phase })
.max(0.001)
.min(len - pos)
}
.min(limit - at);
let lerp = |t: f32| (a.0 + (b.0 - a.0) * t / len, a.1 + (b.1 - a.1) * t / len);
if on {
let start = lerp(pos);
let end = lerp(pos + chunk);
if let Some(last) = paths.last_mut()
&& last
.last()
.is_some_and(|&(x, y)| (x - start.0).hypot(y - start.1) < 0.0001)
{
last.push(end);
} else {
paths.push(vec![start, end]);
}
}
pos += chunk;
at += chunk;
}
if at >= limit {
break;
}
}
paths
}
pub(super) fn artwork(
id: &ElementId,
scene: EmptyScene,
phase: Phase,
width: f32,
w: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let colors = cx.arc_theme().colors;
let spring = cx.arc_theme().motion.spring.morph;
let mut shapes = Vec::new();
for (index, p) in frame(scene, phase).iter().enumerate() {
let geo: Vec<_> = p
.geo
.iter()
.enumerate()
.map(|(axis, &target)| {
arc_spring(
(
super::child_id(id, format!("art-{index}-{axis}")),
"geometry",
),
target,
spring,
w,
cx,
)
})
.collect();
let alpha = arc_spring(
(super::child_id(id, format!("art-{index}")), "alpha"),
p.fx[0],
spring,
w,
cx,
)
.clamp(0.0, 1.0);
let draw = arc_spring(
(super::child_id(id, format!("art-{index}")), "draw"),
p.fx[1],
spring,
w,
cx,
)
.clamp(0.0, 1.0);
shapes.push((p.kind, p.tone, geo, alpha, draw));
}
let stage_width = width.clamp(0.0, 360.0);
div()
.id(super::child_id(id, "artwork"))
.test_support()
.w(px(stage_width))
.flex_none()
.h(px(
stage_width * BlockTokens::ART_HEIGHT / BlockTokens::ART_WIDTH
))
.child(
canvas(
|_, _, _| {},
move |rect, _, window, _| {
let scale = f32::from(rect.size.width) / 320.0;
for (kind, tone, g, alpha, draw) in &shapes {
let color = match *tone {
"tuck" | "slot" | "subtle" | "bar" => colors.border_strong,
"back" | "muted" | "query" => colors.text_muted,
"success" => colors.success,
"warning" => colors.warning,
_ => colors.foreground,
};
let mut stroke = color;
stroke.a *= alpha;
if *kind == "tile" || *kind == "body" {
let points = if *kind == "tile" {
tile_points(g)
} else {
g[..352].chunks_exact(2).map(|p| (p[0], p[1])).collect()
};
let mut fill = match *tone {
"shade" => colors.surface_muted,
"dot" | "bar" | "query" => color,
_ => colors.surface,
};
fill.a *= alpha;
paint(&points, true, true, fill, rect.origin, scale, window);
if !matches!(*tone, "dot" | "bar" | "query") {
paint(&points, true, false, stroke, rect.origin, scale, window);
}
} else {
let count = (g.len() - 3) / 2;
let points: Vec<_> = g[..count * 2]
.chunks_exact(2)
.map(|p| (p[0], p[1]))
.collect();
let parts: Vec<_> = if *kind == "mark" {
vec![&points[..12], &points[12..]]
} else {
vec![points.as_slice()]
};
let length = |points: &[(f32, f32)]| {
points
.windows(2)
.map(|pair| {
(pair[1].0 - pair[0].0).hypot(pair[1].1 - pair[0].1)
})
.sum::<f32>()
};
let mut budget =
draw * parts.iter().map(|part| length(part)).sum::<f32>();
for part in parts {
let len = length(part);
let fraction = if len > 0.0001 {
(budget / len).clamp(0.0, 1.0)
} else {
0.0
};
for path in traced(part, fraction, g[g.len() - 3]) {
paint(&path, false, false, stroke, rect.origin, scale, window);
}
budget -= len;
}
}
}
},
)
.size_full(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn traced_keeps_continuous_strokes_and_real_dash_lengths() {
let points = [(0.0, 0.0), (2.0, 0.0), (10.0, 0.0)];
let solid = traced(&points, 0.5, 0.0);
assert_eq!(solid.len(), 1);
assert_eq!(solid[0].last(), Some(&(5.0, 0.0)));
let dashed = traced(&points, 1.0, 5.0);
assert_eq!(dashed.len(), 2);
assert_eq!(dashed[0].last(), Some(&(3.0, 0.0)));
assert_eq!(dashed[1].first(), Some(&(8.0, 0.0)));
}
#[test]
fn upstream_frames_preserve_ten_compatible_distinct_shapes() {
for scene in [
EmptyScene::Search,
EmptyScene::Offline,
EmptyScene::Inbox,
EmptyScene::NotFound,
] {
let idle = frame(scene, Phase::Idle);
assert_eq!(idle.len(), 10);
for phase in [Phase::Loading, Phase::Done] {
for (a, b) in idle.iter().zip(frame(scene, phase)) {
assert_eq!(a.kind, b.kind);
assert_eq!(a.geo.len(), b.geo.len());
}
}
}
assert_ne!(
frame(EmptyScene::Search, Phase::Idle)[2].geo,
frame(EmptyScene::Offline, Phase::Idle)[2].geo
);
assert_ne!(
frame(EmptyScene::Inbox, Phase::Idle)[2].geo,
frame(EmptyScene::NotFound, Phase::Idle)[2].geo
);
assert_eq!(frame(EmptyScene::NotFound, Phase::Idle)[7].geo[80], 5.0);
}
}