xonecas.io
8447
Co

conveyor / files

conveyor / crates / conveyor-app / src / form.rs

4.1 KB
1//! The prompt a Ctrl+click opens: what was clicked, what's been said about that widget so far,
2//! and a field for what to do next. Enter sends, Shift+Enter breaks the line, Esc cancels.
3use crate::{app::State, client};
4use rune::{El, Key, ScrollSpec, TextEdit, TextStyle, Tx, col, text, text_input, tok};
5use serde_json::{Value, json};
6
7pub struct Form {
8    /// Where it opened, in window points.
9    pub at: (f32, f32),
10    pub kind: String,
11    /// The instance clicked, then its parents, each with the click in its own points.
12    pub targets: Vec<(String, f32, f32)>,
13    pub element: String,
14    pub text: TextEdit,
15}
16
17pub fn open(s: &mut State) {
18    let (px, py) = s.pointer;
19    let targets: Vec<_> = s.rects.borrow().iter().map(|(id, r)| (id.clone(), px - r[0], py - r[1])).collect();
20    let kind = targets.first().and_then(|t| s.live.kind(&t.0)).unwrap_or("root").to_owned();
21    let mut text = TextEdit::new("");
22    text.focused = true;
23    s.form = Some(Form { at: s.pointer, kind, targets, element: s.element.clone(), text });
24}
25
26fn submit(s: &mut State, tx: &mut Tx) {
27    tx.blur();
28    let Some(f) = s.form.take() else { return };
29    if f.text.text.trim().is_empty() { return; }
30    // conveyord copies the app's state into the ticket, so the agent can replay this moment.
31    s.live.save_state(f.targets.first().map_or("root", |t| t.0.as_str()), &f.element);
32    let (w, h) = s.window.get();
33    client::send(json!({"op": "widget", "prompt": f.text.text, "kind": f.kind, "targets": f.targets, "element": f.element,
34                        "tree": s.live.tree(), "size": [w, h]}).to_string());
35}
36
37/// What's been said about `kind`: each build asked from it or that changed it.
38fn thread<'a>(s: &'a State, kind: &str) -> Vec<&'a Value> {
39    s.builds.iter()
40        .filter(|b| b["kind"] == kind || b["changed"].as_array().is_some_and(|c| c.iter().any(|k| k == kind)))
41        .flat_map(|b| b["thread"].as_array().into_iter().flatten()).collect()
42}
43
44fn entry(e: &Value) -> El {
45    let t = tok::theme(true);
46    let kind = e["kind"].as_str().unwrap_or("");
47    let color = match kind { "prompt" => t.text, "block" | "error" => tok::accent(1, true), _ => t.text_secondary };
48    let who = if kind == "prompt" { "you".into() } else { format!("#{} {kind}", e["ticket"]) };
49    col().w_full().gap(2.0)
50        .child(text(who).font(tok::type_::LABEL).color(t.text_secondary))
51        .child(text(e["text"].as_str().unwrap_or("").to_owned()).color(color).w_full().wrap())
52}
53
54pub fn view(s: &State, f: &Form, rcx: &mut rune::Cx<State>) -> El {
55    let t = tok::theme(true);
56    let (ww, wh) = rcx.layout("app").map_or((1400.0, 900.0), |r| (r[2], r[3]));
57    let (w, h) = (420.0, 360.0);
58    let at = ((f.at.0).min(ww - w - 8.0).max(8.0), (f.at.1).min(wh - h - 8.0).max(8.0));
59    let target = f.targets.first().map_or("root", |t| t.0.as_str());
60    let mut history = col().w_full().gap(10.0);
61    let entries = thread(s, &f.kind);
62    if entries.is_empty() { history = history.child(text(format!("Nothing asked about `{}` yet.", f.kind)).color(t.text_secondary)); }
63    for e in entries { history = history.child(entry(e)); }
64    col().key("form").w(w).h(h).absolute(at.0, at.1).radius(10.0).fill(t.card).padding(12.0).gap(8.0).focus_ring(2.0, 0.0, tok::accent(0, true))
65        .child(text(format!("{}  ·  {target}", f.kind)).font(tok::type_::LABEL).color(t.text_secondary))
66        .child(col().key("form-thread").w_full().grow(1.0).min_h(0.0).scroll(ScrollSpec { stick_bottom: true }).child(history))
67        .child(text_input(&f.text, TextStyle::new(14.0, 400.0)).key("form-input").label("Prompt").w_full().h(84.0)
68            .padding(6.0).radius(6.0).fill(t.bg).color(t.text)
69            .placeholder("Change this, or ask for a new widget here…  Enter sends, Shift+Enter new line, Esc cancels", t.text_secondary)
70            .on_edit(|s: &mut State| { if s.form.is_none() { return &mut s.spare; } &mut s.form.as_mut().unwrap().text })
71            .on_submit(submit)
72            .on_input_key(|s: &mut State, e: &rune::KeyEvent, tx| {
73                if !matches!(e.key, Key::Escape) { return false; }
74                s.form = None;
75                tx.blur();
76                true
77            }))
78}