xonecas.ioDon't click! Sean Caetano Martin Sign in
cast / src / workflow / task_view.rs 148 lines4.6 kB Raw
1use crate::agent::Agent;
2use crate::app::App;
3use crate::ui::theme::{self, pal};
4use crate::ui::widgets::{Kind, btn, lbl, link, mono, ph, seg};
5use crate::workflow::review::relations;
6use crate::workflow::task_form::{Field, LISTS};
7use rune::{El, col, row};
8use rune_kit::{Kit, TextInput};
9
10fn field(s: &App, stem: &str, f: Field, k: &Kit) -> El {
11    let Some(r) = s.review(stem) else {
12        return col();
13    };
14    let edit = match f {
15        Field::Title => &r.form.title,
16        Field::Item(l, n) => &r.form.lists[l][n],
17    };
18    let (a, b) = (stem.to_string(), stem.to_string());
19    TextInput::new(edit)
20        .text_style(theme::BODY)
21        .height(theme::CONTROL)
22        .width(None)
23        .on_edit(move |s: &mut App| s.form_field(&a, f))
24        .on_change(move |s: &mut App, _| s.form_changed(&b))
25        .build(k)
26}
27
28pub fn view(s: &App, stem: &str, k: &Kit) -> El {
29    let p = pal();
30    let Some(r) = s.review(stem) else {
31        return col();
32    };
33    let t = &r.wf.tasks[r.selected];
34    let at = |f: fn(&mut App, &str)| {
35        let st = stem.to_string();
36        move |s: &mut App| f(s, &st)
37    };
38    let par = r.wf.in_step(t.step).count() > 1;
39    let step = row()
40        .gap(6.0)
41        .center_y()
42        .child(
43            mono(
44                format!("{} · {}", t.step, if par { "parallel" } else { "own step" }),
45                p.fg,
46            )
47            .truncate()
48            .grow(1.0)
49            .min_w(0.0),
50        )
51        .child(btn(
52            "◀ Earlier",
53            Kind::Ghost,
54            Some(at(|s, k| s.reorder(k, -1))),
55        ))
56        .child(btn(
57            "Later ▶",
58            Kind::Ghost,
59            Some(at(|s, k| s.reorder(k, 1))),
60        ))
61        .child(btn("Split", Kind::Ghost, Some(at(|s, k| s.reorder(k, 0)))));
62    let opts: Vec<(Option<Agent>, &str)> = std::iter::once((None, "default"))
63        .chain(Agent::ALL.iter().map(|a| (Some(*a), a.label())))
64        .collect();
65    let st = stem.to_string();
66    let agent = seg(&opts, t.agent, move |s: &mut App, a| {
67        if let Some(r) = s.review_mut(&st) {
68            let i = r.selected;
69            r.wf.tasks[i].agent = a;
70        }
71        s.save_review(&st);
72    });
73    let mut form = col()
74        .grow(1.0)
75        .min_w(0.0)
76        .padding(16.0)
77        .gap(12.0)
78        .scroll(rune::ScrollSpec {
79            stick_bottom: false,
80        })
81        .child(
82            col()
83                .gap(4.0)
84                .child(lbl("Title"))
85                .child(field(s, stem, Field::Title, k)),
86        )
87        .child(
88            row()
89                .gap(12.0)
90                .child(col().gap(4.0).grow(1.0).child(lbl("Step")).child(step))
91                .child(col().gap(4.0).child(lbl("Agent override")).child(agent)),
92        );
93    for (l, (title, noun)) in LISTS.iter().enumerate() {
94        let items = r.form.lists[l].iter().enumerate().map(|(n, _)| {
95            let st = stem.to_string();
96            row()
97                .gap(6.0)
98                .center_y()
99                .child(field(s, stem, Field::Item(l, n), k).grow(1.0).min_w(0.0))
100                .child(link("✕", move |s: &mut App| s.form_remove(&st, l, n)).px(4.0))
101        });
102        let st = stem.to_string();
103        let add = link(&format!("+ add {noun}"), move |s: &mut App| {
104            s.form_add(&st, l)
105        });
106        form = form.child(
107            col()
108                .gap(4.0)
109                .child(lbl(&format!("{title} · {}", r.form.lists[l].len())))
110                .children(items)
111                .child(row().child(add)),
112        );
113    }
114    form = form.child(row().justify_end().child(btn(
115        "Delete task",
116        Kind::Danger,
117        Some(at(|s, k| s.delete_task(k))),
118    )));
119    let (after, with, before) = relations(&r.wf, t);
120    let dash = |v: Vec<String>| {
121        if v.is_empty() {
122            "—".to_string()
123        } else {
124            v.join(" ")
125        }
126    };
127    let kv = |k: &str, v: String| {
128        row()
129            .justify_between()
130            .child(mono(k, p.muted))
131            .child(mono(v, p.fg))
132    };
133    let order = col()
134        .w(280.0)
135        .h_full()
136        .fill(p.surface)
137        .child(ph(lbl("Workflow order"), row()))
138        .child(
139            col()
140                .padding(12.0)
141                .gap(4.0)
142                .child(kv("step", format!("{} of {}", t.step, r.wf.steps().len())))
143                .child(kv("after", dash(after)))
144                .child(kv("before", dash(before)))
145                .child(kv("parallel with", dash(with))),
146        );
147    row().w_full().h_full().child(form).child(order)
148}