xonecas.ioDon't click! Sean Caetano Martin Sign in
cast / src / workflow / dag.rs 152 lines4.6 kB Raw
1use crate::app::App;
2use crate::exec::machine::Status;
3use crate::ui::theme::{self, pal};
4use crate::workflow::model::Workflow;
5use rune::{Color, El, LineCap, Path, Shape, canvas, col, el, text};
6
7const NW: f32 = 188.0;
8const NH: f32 = 56.0;
9const GX: f32 = 48.0;
10const GY: f32 = 12.0;
11const PAD: f32 = 8.0;
12
13pub struct Look<'a> {
14    pub status: Option<&'a [Status]>,
15    pub selected: Option<usize>,
16    pub elapsed: &'a dyn Fn(usize) -> Option<String>,
17}
18
19pub fn glyph(s: Status) -> &'static str {
20    match s {
21        Status::Done => "✓",
22        Status::Running => "◐",
23        Status::Queued => "○",
24        Status::Failed => "✕",
25        Status::Cancelled => "■",
26    }
27}
28
29pub fn tint(s: Status) -> Color {
30    let p = pal();
31    match s {
32        Status::Done => p.ok,
33        Status::Running => p.accent,
34        Status::Queued => p.faint,
35        Status::Failed => p.danger,
36        Status::Cancelled => p.muted,
37    }
38}
39
40fn layout(wf: &Workflow) -> (Vec<[f32; 2]>, f32, f32) {
41    let steps = wf.steps();
42    let rows = steps
43        .iter()
44        .map(|s| wf.in_step(*s).count())
45        .max()
46        .unwrap_or(1) as f32;
47    let h = rows * (NH + GY) - GY + 2.0 * PAD;
48    let w = steps.len() as f32 * (NW + GX) - GX + 2.0 * PAD;
49    let mut pos = vec![[0.0; 2]; wf.tasks.len()];
50    for (c, s) in steps.iter().enumerate() {
51        let col: Vec<usize> = wf.in_step(*s).map(|(i, _)| i).collect();
52        let top = (h - (col.len() as f32 * (NH + GY) - GY)) / 2.0;
53        for (r, i) in col.into_iter().enumerate() {
54            pos[i] = [PAD + c as f32 * (NW + GX), top + r as f32 * (NH + GY)];
55        }
56    }
57    (pos, w, h)
58}
59
60pub fn view(wf: &Workflow, look: Look, on: impl Fn(&mut App, usize) + Clone + 'static) -> El {
61    let p = pal();
62    let (pos, w, h) = layout(wf);
63    let status = |i: usize| look.status.map(|s| s[i]);
64    let mut shape = Shape::new();
65    for (a, ta) in wf.tasks.iter().enumerate() {
66        for (b, _) in wf
67            .tasks
68            .iter()
69            .enumerate()
70            .filter(|(_, tb)| tb.step == ta.step + 1)
71        {
72            let (from, to) = (
73                [pos[a][0] + NW, pos[a][1] + NH / 2.0],
74                [pos[b][0], pos[b][1] + NH / 2.0],
75            );
76            let m = (from[0] + to[0]) / 2.0;
77            let color = match status(b) {
78                Some(Status::Done) => p.ok.alpha(0.5),
79                Some(Status::Running) => p.accent.alpha(0.8),
80                _ => p.muted.alpha(0.35),
81            };
82            shape = shape.stroke_path(
83                &Path::new()
84                    .move_to(from)
85                    .cubic_to([m, from[1]], [m, to[1]], to),
86                1.5,
87                LineCap::Round,
88                color,
89            );
90        }
91    }
92    let nodes = wf.tasks.iter().enumerate().map(|(i, t)| {
93        let st = status(i);
94        let color = st.map_or(p.fg, tint);
95        let sel = look.selected == Some(i);
96        let head = match st {
97            Some(s) => format!(
98                "{} {}{}",
99                t.id,
100                glyph(s),
101                (look.elapsed)(i)
102                    .map(|e| format!(" {e}"))
103                    .unwrap_or_default()
104            ),
105            None => t.id.clone(),
106        };
107        let on = on.clone();
108        let node = col()
109            .key(format!("node-{}", t.id))
110            .absolute(pos[i][0], pos[i][1])
111            .w(NW)
112            .h(NH)
113            .px(8.0)
114            .justify_center()
115            .radius(4.0)
116            .fill(p.bg)
117            .border(
118                if sel { 2.0 } else { 1.0 },
119                if sel { p.accent } else { color.alpha(0.45) },
120            )
121            .role("button")
122            .label(t.title.clone())
123            .focusable(true)
124            .cursor(rune::Cursor::Pointer)
125            .on_press(move |s: &mut App| on(s, i))
126            .child(
127                text(head)
128                    .font(theme::SMALL)
129                    .color(if st.is_some() { color } else { p.faint }),
130            )
131            .child(
132                text(t.title.clone())
133                    .font(theme::STRONG)
134                    .color(if st.is_some() { color } else { p.fg })
135                    .truncate(),
136            );
137        if st == Some(Status::Running) {
138            node.child(
139                el().absolute(0.0, NH - 3.0)
140                    .w(NW)
141                    .h(3.0)
142                    .fill(p.accent.alpha(0.6)),
143            )
144        } else {
145            node
146        }
147    });
148    el().w(w)
149        .h(h)
150        .child(canvas(shape).absolute(0.0, 0.0).w(w).h(h))
151        .children(nodes)
152}