xonecas.io
8447
Co

conveyor / files

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

3.9 KB
1//! The building tool's eye: while Ctrl is held, a layer over the whole window outlines what's
2//! under the pointer: the deepest keyed element, the widget it's in, and that widget's parents.
3//! The layer takes the click, so no widget's own controls can swallow it. A click opens the
4//! prompt about that widget and element. A corner button opens safe mode.
5use crate::{app::State, form};
6use rune::{El, Tx, el, text, tok};
7
8/// Finds the widget instances under the pointer (each renders keyed `w:<id>`), and the keyed
9/// elements inside the innermost one, as a key path such as `col-done/card-12`. The status
10/// widget lies over the content and fills the window, so only what it draws inside counts.
11pub fn pick(s: &mut State, tx: &mut Tx) {
12    let (x, y) = s.pointer;
13    tx.element_at_in("status-layer", x, y, move |s: &mut State, picked, tx| match picked.filter(|p| p.path().contains("/w:status/")) {
14        Some(p) => picked_at(s, &p),
15        None => tx.element_at_in("content", x, y, |s: &mut State, picked, _| match picked {
16            Some(p) => picked_at(s, &p),
17            None => (s.picked, s.element) = (vec![], String::new()),
18        }),
19    });
20}
21
22fn picked_at(s: &mut State, p: &rune::Picked) {
23    s.picked = p.path().split('/').filter_map(|seg| seg.strip_prefix("w:")).rev().map(String::from).collect();
24    let inner = p.path().rsplit_once("/w:").map_or("", |(_, rest)| rest);
25    let unkeyed = |seg: &&str| seg.starts_with('#') && seg[1..].bytes().all(|b| b.is_ascii_digit());
26    s.element = inner.split('/').skip(1).filter(|seg| !unkeyed(seg)).collect::<Vec<_>>().join("/");
27}
28
29pub fn view(s: &State, rcx: &mut rune::Cx<State>) -> El {
30    let accent = tok::accent(0, true);
31    let t = tok::theme(true);
32    let rects: Vec<(String, [f32; 4])> = s.picked.iter().filter_map(|id| Some((id.clone(), rcx.layout(&format!("w:{id}"))?))).collect();
33    let mut layer = el().key("inspect").w_full().h_full().absolute(0.0, 0.0)
34        .on_pointer_move_local(|s: &mut State, e: rune::PointerEvent, tx: &mut Tx| {
35            s.pointer = (e.window_x, e.window_y);
36            s.ctrl = tx.mods().ctrl;
37            pick(s, tx);
38        })
39        .on_press_tx(|s: &mut State, _| form::open(s))
40        // On macOS, Rune reports a Ctrl+click as a secondary press.
41        .on_secondary_press(|s: &mut State, _, _| form::open(s));
42    if crate::safe::offered(s) {
43        layer = layer.child(el().key("safe-mode").pin(Some(12.0), None, None, Some(12.0)).h(24.0).px(10.0).center_y().radius(12.0)
44            .fill(t.card).border(1.0, accent).role("button").label("Safe mode").help("Pause every widget, and keep or discard builds on trial")
45            .on_press_tx(|s: &mut State, _| s.safe = Some("You asked for it from the Ctrl inspector.".into()))
46            .child(text("Safe mode").font(tok::type_::LABEL).color(t.text)));
47    }
48    for (i, (_, [x, y, w, h])) in rects.iter().enumerate().rev() {
49        let (width, alpha) = if i == 0 { (1.5, 0.8) } else { (1.0, 0.3) };
50        layer = layer.child(el().w(*w).h(*h).absolute(*x, *y).border(width, accent.alpha(alpha)).radius(4.0));
51    }
52    let Some((id, inst)) = rects.first() else { *s.rects.borrow_mut() = rects; return layer };
53    let leaf = s.element.rsplit('/').next().unwrap_or("");
54    let part = if leaf.is_empty() { None } else { rcx.layout(&format!("w:{id}/{}", s.element)).or_else(|| rcx.layout(leaf)) };
55    if let Some([x, y, w, h]) = part {
56        layer = layer.child(el().w(w).h(h).absolute(x, y).border(2.5, accent).fill(accent.alpha(0.12)).radius(4.0));
57    }
58    let [x, y, ..] = part.unwrap_or(*inst);
59    let kind = s.live.kind(id).unwrap_or("?");
60    let name = if leaf.is_empty() { format!("{kind}  ·  {id}") } else { format!("{kind}  ›  {leaf}") };
61    layer = layer.child(el().absolute(x, (y - 22.0).max(0.0)).h(20.0).px(8.0).center_y().radius(4.0).fill(accent)
62        .child(text(name).font(tok::type_::LABEL).color(t.bg)));
63    *s.rects.borrow_mut() = rects;
64    layer
65}