@@ -1,23 +1,28 @@
1-//! The canvas: widgets where they were asked for, a card for each widget still being built,
2-//! and the Ctrl+click prompt that asks for a new one.
3-use crate::{client::{self, Snapshot}, titlebar, widgets};
4-use rune::{Drag, El, Phase, TextEdit, TextStyle, Tx, Waker, col, el, text, text_input, tok};
5-use std::{path::{Path, PathBuf}, sync::{Arc, Mutex}, time::{Duration, SystemTime, UNIX_EPOCH}};
1+//! The canvas: the app's state, its link to conveyord, and the widgets drawn in stacking order.
2+use crate::{client::{self, Snapshot}, menu, place, prompt, titlebar, widgets};
3+use rune::{El, TextEdit, Tx, Waker, col, el, text, tok};
4+use std::{collections::HashSet, path::{Path, PathBuf}, sync::{Arc, Mutex}, time::{Duration, SystemTime, UNIX_EPOCH}};
65
76 pub struct State {
8- snap: Snapshot,
7+ pub snap: Snapshot,
98 connected: bool,
10- pointer: (f32, f32),
11- /// Where the prompt is open, and the widget ticket it follows up on, if a card was clicked.
12- prompt_at: Option<(f32, f32, Option<u32>)>,
13- prompt: TextEdit,
9+ /// The pointer, in canvas points.
10+ pub pointer: (f32, f32),
11+ /// Where the prompt is open, and the widget it follows up on, if one was Ctrl+clicked.
12+ pub prompt_at: Option<(f32, f32, Option<u32>)>,
13+ pub prompt: TextEdit,
1414 inbox: Arc<Mutex<Option<Option<Snapshot>>>>,
1515 /// The binary at launch and its mtime. Read once: after cargo replaces it,
1616 /// current_exe() names the deleted file.
1717 exe: Option<PathBuf>,
1818 built: Option<SystemTime>,
1919 /// A widget's position when the drag on it started.
20- drag: Option<(f32, f32)>,
20+ pub drag: Option<(f32, f32)>,
21+ pub hovered: Option<u32>,
22+ /// Widgets whose conversation is open.
23+ pub notes: HashSet<u32>,
24+ /// A widget's right-click menu, and where.
25+ pub menu: Option<(u32, f32, f32)>,
2126 }
2227
2328 fn mtime(p: &Path) -> Option<SystemTime> {
@@ -25,7 +30,8 @@ fn mtime(p: &Path) -> Option<SystemTime> {
2530 }
2631
2732 pub fn init() -> State {
28- State { snap: Snapshot::default(), connected: false, pointer: (0.0, 0.0), prompt_at: None, prompt: TextEdit::new(""), inbox: Default::default(), exe: None, built: None, drag: None }
33+ State { snap: Snapshot::default(), connected: false, pointer: (0.0, 0.0), prompt_at: None, prompt: TextEdit::new(""),
34+ inbox: Default::default(), exe: None, built: None, drag: None, hovered: None, notes: HashSet::new(), menu: None }
2935 }
3036
3137 /// Polls conveyord once a second on a thread. Every poll also re-renders, for clocks.
@@ -55,117 +61,29 @@ pub fn poll(s: &mut State, _t: f64, _tx: &mut Tx) -> bool {
5561 true
5662 }
5763
58-fn submit(s: &mut State, tx: &mut Tx) {
59- if let Some((x, y, about)) = s.prompt_at.take() {
60- let prompt = s.prompt.text.clone();
61- s.prompt = TextEdit::new("");
62- if !prompt.trim().is_empty() && client::widget(&prompt, x, y, about).is_none() {
63- eprintln!("conveyord isn't running; the prompt was not sent");
64- }
65- }
66- tx.blur();
67-}
68-
69-fn open_prompt(s: &mut State, x: f32, y: f32, about: Option<u32>) {
70- s.prompt_at = Some((x, y, about));
71- s.prompt = TextEdit::new("");
72- s.prompt.focused = true;
73-}
74-
75-/// Moves a widget with the pointer, and tells conveyord where it landed.
76-/// Ctrl+press instead opens a prompt about the widget.
77-fn drag(s: &mut State, id: u32, d: &Drag, tx: &Tx) {
78- let Some(p) = s.snap.canvas.iter_mut().find(|p| p.ticket == id) else { return };
79- match (d.phase, s.drag) {
80- (Phase::Down, _) if tx.mods().ctrl => { let (x, y) = (p.x + d.x, p.y + d.y); open_prompt(s, x, y, Some(id)) }
81- (Phase::Down, _) => s.drag = Some((p.x, p.y)),
82- (Phase::Move, Some((x, y))) => (p.x, p.y) = ((x + d.dx).max(0.0), (y + d.dy).max(0.0)),
83- (Phase::Up, Some((x, y))) => {
84- s.drag = None;
85- if d.cancelled() { (p.x, p.y) = (x, y) } else { client::move_to(id, p.x, p.y); }
86- }
87- _ => {}
88- }
89-}
90-
91-fn card(key: String, w: f32, h: f32) -> El {
92- let t = tok::theme(true);
93- col().key(key).w(w).h(h).radius(10.0).fill(t.card).padding(12.0).gap(6.0)
94-}
95-
96-fn state_color(state: &str) -> rune::Color {
97- if state == "failed" || state == "parked" { tok::accent(3, true) } else { tok::accent(0, true) }
98-}
99-
100-fn pending(id: u32, title: &str, state: &str) -> El {
101- let t = tok::theme(true);
102- let color = state_color(state);
103- card(format!("pending-{id}"), 260.0, 90.0).label(format!("widget #{id} {state}"))
104- .child(text(format!("#{id} · {state}")).font(tok::type_::LABEL).color(color))
105- .child(text(title.to_owned()).font(tok::type_::BODY).color(t.text).wrap())
106-}
107-
108-fn placed(s: &State, cx: &widgets::api::Cx) -> Vec<El> {
109- s.snap.canvas.iter().map(|p| {
110- let built = p.widget.as_deref().and_then(|w| widgets::ALL.iter().find(|(n, _)| *n == w));
111- let t = s.snap.tickets.iter().find(|t| t.project == "widgets" && t.id == p.ticket);
112- let el = match built {
113- Some((name, w)) => {
114- let card = card(format!("w-{}", p.ticket), w.size.0, w.size.1).label(format!("widget {name}")).child((w.view)(cx));
115- // A follow-up in flight: the widget stays, with its ticket's state on top.
116- match t.filter(|t| t.state != "done") {
117- Some(t) => card.child(text(format!("#{} · {}", t.id, t.state)).font(tok::type_::LABEL).color(state_color(&t.state))
118- .padding(4.0).radius(6.0).fill(tok::theme(true).bg).absolute(8.0, 8.0)),
119- None => card,
120- }
121- }
122- None => {
123- let state = match (&p.widget, t) { (Some(_), _) => "installing", (_, Some(t)) => t.state.as_str(), _ => "?" };
124- pending(p.ticket, t.map_or("", |t| t.title.as_str()), state)
125- }
126- };
127- let id = p.ticket;
128- el.absolute(p.x, p.y).on_drag(move |s: &mut State, d: &mut Drag, tx: &mut Tx| drag(s, id, d, tx))
129- }).collect()
130-}
131-
132-fn prompt_box(s: &State, x: f32, y: f32, about: Option<u32>) -> El {
133- let t = tok::theme(true);
134- let heading = match about.and_then(|a| s.snap.canvas.iter().find(|p| p.ticket == a)) {
135- Some(p) => format!("Change {}", p.widget.clone().unwrap_or_else(|| format!("#{}", p.ticket))),
136- None => "New widget".into(),
137- };
138- card("prompt".into(), 360.0, 120.0).absolute(x, y).focus_ring(2.0, 0.0, tok::accent(0, true))
139- .child(text(heading).font(tok::type_::LABEL).color(t.text_secondary))
140- .child(text_input(&s.prompt, TextStyle::new(14.0, 400.0)).key("prompt-input").label("Widget prompt").w_full().grow(1.0)
141- .padding(6.0).radius(6.0).fill(t.bg).color(t.text).placeholder("A clock with the date… Enter to build, Shift+Enter for a new line, Esc to cancel", t.text_secondary)
142- .on_edit(|s: &mut State| &mut s.prompt)
143- .on_submit(submit)
144- .on_input_key(|s: &mut State, e: &rune::KeyEvent, tx| {
145- if !matches!(e.key, rune::Key::Escape) { return false; }
146- s.prompt_at = None;
147- tx.blur();
148- true
149- }))
150-}
151-
152-pub fn view(s: &State, _: &mut rune::Cx<State>) -> El {
64+pub fn view(s: &State, rcx: &mut rune::Cx<State>) -> El {
15365 let t = tok::theme(true);
15466 let tickets: Vec<widgets::api::TicketInfo> = s.snap.tickets.iter()
15567 .map(|t| widgets::api::TicketInfo { project: t.project.clone(), id: t.id, state: t.state.clone(), title: t.title.clone() })
15668 .collect();
15769 let now = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0.0, |d| d.as_secs_f64());
15870 let cx = widgets::api::Cx { now, tickets: &tickets };
71+ let area = rcx.layout("canvas").map_or((1400.0, 864.0), |r| (r[2], r[3]));
15972 let mut canvas = el().key("canvas").role("canvas").label("Canvas").w_full().grow(1.0).min_h(0.0).fill(t.bg)
16073 .on_pointer_move_local(|s: &mut State, e: rune::PointerEvent, tx: &mut Tx| { s.pointer = (e.x, e.y); tx.live_only(); })
16174 .on_press_tx(|s: &mut State, tx: &mut Tx| {
162- if tx.mods().ctrl { open_prompt(s, s.pointer.0, s.pointer.1, None) }
75+ s.menu = None;
76+ if tx.mods().ctrl { let at = s.pointer; prompt::open(s, at, None) }
16377 });
16478 if s.snap.canvas.is_empty() && s.prompt_at.is_none() {
16579 canvas = canvas.child(col().w_full().h_full().center().child(text("Ctrl+click anywhere to make a widget").color(t.text_secondary)));
16680 }
167- for e in placed(s, &cx) { canvas = canvas.child(e); }
168- if let Some((x, y, about)) = s.prompt_at { canvas = canvas.child(prompt_box(s, x, y, about)); }
81+ let mut order: Vec<_> = s.snap.canvas.iter().collect();
82+ // A widget with its conversation open draws on top, so the panel isn't covered.
83+ order.sort_by_key(|p| (s.notes.contains(&p.id), p.z));
84+ for p in order { canvas = canvas.child(place::view(s, p, &cx, area)); }
85+ if let Some((x, y, about)) = s.prompt_at { canvas = canvas.child(prompt::view(s, x, y, about)); }
86+ if let Some((id, x, y)) = s.menu { canvas = canvas.child(menu::view(s, id, x, y)); }
16987 let status = if s.connected { String::new() } else { "conveyord isn't running".into() };
17088 col().key("root").w_full().h_full().fill(t.bg).child(titlebar::bar(status)).child(canvas)
17189 }