xonecas.io
8447
Co

conveyor / files

conveyor / templates / library / widgets / running.rs

2.4 KB
1//! A compact running indicator: a conveyor belt that moves while any ticket runs, and how many do.
2//! Hovering it opens the build status card (`status`). The title bar puts it at its center.
3use super::{api::*, look};
4use super::status::{LEAVE, hover};
5use std::time::Duration;
6use rune::{El, LineCap, Shape, el, row, text};
7
8pub const WIDGET: Widget = Widget { title: "Running", size: (0.0, 0.0), new: |_| Box::new(Running) };
9
10pub struct Running;
11
12impl View for Running {
13    // `animate` only wakes the host; an act each frame makes it draw that frame too.
14    fn update(&mut self, cx: &Cx) {
15        if cx.data.tickets.iter().any(|k| k.state == "running") { cx.act(|_| {}); }
16    }
17
18    fn view(&self, cx: &Cx) -> El {
19        let t = look::of(cx);
20        let running: Vec<&Ticket> = cx.data.tickets.iter().filter(|k| k.state == "running").collect();
21        let on = !running.is_empty();
22        if on { cx.animate(); }
23        let color = if on { t.accent } else { t.text_secondary };
24        let b = row().key("running").min_h(28.0).px(8.0).gap(6.0).center_y().radius(6.0).hover(t.hover)
25            .hover_delays(Duration::ZERO, LEAVE).on_hover_change(hover("status", |s, inside| s.icon = inside))
26            .child(el().key("belt").w(26.0).h(18.0).shape(belt(if on { (cx.now % 600.0) as f32 } else { 0.0 }, color)));
27        if on { b.child(text(running.len().to_string()).font(t.label).color(t.text)) } else { b }
28    }
29}
30
31/// A belt (a stadium with two rollers) carrying packages, in a 26×18 box. `t` seconds moves it.
32fn belt(t: f32, color: rune::Color) -> Shape {
33    let (l, r, y, rad) = (7.0, 19.0, 13.0, 3.5);
34    let arc = |cx: f32, from: f32| (0..=8).map(move |i| { let a = (from + i as f32 * 22.5).to_radians(); [cx + rad * a.cos(), y + rad * a.sin()] });
35    let stadium: Vec<[f32; 2]> = arc(r, -90.0).chain(arc(l, 90.0)).collect();
36    let mut s = Shape::new().stroke_with(stadium, true, 1.4, LineCap::Round, color);
37    for c in [l, r] {
38        let a = -t * 4.0;
39        let (dx, dy) = (1.6 * a.cos(), 1.6 * a.sin());
40        s = s.line([c - dx, y - dy], [c + dx, y + dy], 1.2, LineCap::Round, color);
41    }
42    // Packages 10 apart sliding right, cut off where they leave the belt.
43    let shift = (t * 6.0) % 10.0;
44    for k in 0..3 {
45        let x0 = (k as f32 * 10.0 - 7.0 + shift).max(3.5);
46        let x1 = (k as f32 * 10.0 - 2.0 + shift).min(22.5);
47        if x1 - x0 > 0.5 { s = s.fill(vec![[x0, 3.5], [x1, 3.5], [x1, 8.5], [x0, 8.5]], color); }
48    }
49    s
50}