@@ -1,8 +1,8 @@
11 //! The canvas: widgets where they were asked for, a card for each widget still being built,
22 //! and the Ctrl+click prompt that asks for a new one.
33 use crate::{client::{self, Snapshot}, titlebar, widgets};
4-use rune::{El, TextEdit, TextStyle, Tx, Waker, col, el, text, text_input, tok};
5-use std::{sync::{Arc, Mutex}, time::{Duration, SystemTime, UNIX_EPOCH}};
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}};
66
77 pub struct State {
88 snap: Snapshot,
@@ -11,19 +11,26 @@ pub struct State {
1111 prompt_at: Option<(f32, f32)>,
1212 prompt: TextEdit,
1313 inbox: Arc<Mutex<Option<Option<Snapshot>>>>,
14+ /// The binary at launch and its mtime. Read once: after cargo replaces it,
15+ /// current_exe() names the deleted file.
16+ exe: Option<PathBuf>,
1417 built: Option<SystemTime>,
18+ /// A widget's position when the drag on it started.
19+ drag: Option<(f32, f32)>,
1520 }
1621
17-fn exe_time() -> Option<SystemTime> {
18- std::fs::metadata(std::env::current_exe().ok()?).ok()?.modified().ok()
22+fn mtime(p: &Path) -> Option<SystemTime> {
23+ std::fs::metadata(p).ok()?.modified().ok()
1924 }
2025
2126 pub fn init() -> State {
22- State { snap: Snapshot::default(), connected: false, pointer: (0.0, 0.0), prompt_at: None, prompt: TextEdit::new(""), inbox: Default::default(), built: exe_time() }
27+ 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 }
2328 }
2429
2530 /// Polls conveyord once a second on a thread. Every poll also re-renders, for clocks.
2631 pub fn setup(s: &mut State, waker: Waker) {
32+ s.exe = std::env::current_exe().ok();
33+ s.built = s.exe.as_deref().and_then(mtime);
2734 let inbox = s.inbox.clone();
2835 std::thread::spawn(move || loop {
2936 *inbox.lock().unwrap() = Some(client::state());
@@ -35,13 +42,14 @@ pub fn setup(s: &mut State, waker: Waker) {
3542 pub fn poll(s: &mut State, _t: f64, _tx: &mut Tx) -> bool {
3643 let Some(snap) = s.inbox.lock().unwrap().take() else { return false };
3744 s.connected = snap.is_some();
38- if let Some(snap) = snap { s.snap = snap; }
45+ // Mid-drag, the daemon's copy would snap the widget back.
46+ if let Some(snap) = snap && s.drag.is_none() { s.snap = snap; }
3947 // conveyord rebuilt the app with a new widget: become the new binary, in place.
40- if exe_time() != s.built {
48+ if let Some(exe) = &s.exe && let Some(t) = mtime(exe) && Some(t) != s.built {
4149 use std::os::unix::process::CommandExt;
42- let err = std::process::Command::new(std::env::current_exe().unwrap()).args(std::env::args().skip(1)).exec();
50+ let err = std::process::Command::new(exe).args(std::env::args().skip(1)).exec();
4351 eprintln!("restart failed: {err}");
44- s.built = exe_time();
52+ s.built = Some(t);
4553 }
4654 true
4755 }
@@ -57,6 +65,20 @@ fn submit(s: &mut State, tx: &mut Tx) {
5765 tx.blur();
5866 }
5967
68+/// Moves a widget with the pointer, and tells conveyord where it landed.
69+fn drag(s: &mut State, id: u32, d: &Drag) {
70+ let Some(p) = s.snap.canvas.iter_mut().find(|p| p.ticket == id) else { return };
71+ match (d.phase, s.drag) {
72+ (Phase::Down, _) => s.drag = Some((p.x, p.y)),
73+ (Phase::Move, Some((x, y))) => (p.x, p.y) = ((x + d.dx).max(0.0), (y + d.dy).max(0.0)),
74+ (Phase::Up, Some((x, y))) => {
75+ s.drag = None;
76+ if d.cancelled() { (p.x, p.y) = (x, y) } else { client::move_to(id, p.x, p.y); }
77+ }
78+ _ => {}
79+ }
80+}
81+
6082 fn card(key: String, w: f32, h: f32) -> El {
6183 let t = tok::theme(true);
6284 col().key(key).w(w).h(h).radius(10.0).fill(t.card).padding(12.0).gap(6.0)
@@ -81,7 +103,8 @@ fn placed(s: &State, cx: &widgets::api::Cx) -> Vec<El> {
81103 pending(p.ticket, t.map_or("", |t| t.title.as_str()), state)
82104 }
83105 };
84- el.absolute(p.x, p.y)
106+ let id = p.ticket;
107+ el.absolute(p.x, p.y).on_drag(move |s: &mut State, d: &mut Drag, _: &mut Tx| drag(s, id, d))
85108 }).collect()
86109 }
87110
@@ -90,10 +113,10 @@ fn prompt_box(s: &State, x: f32, y: f32) -> El {
90113 card("prompt".into(), 360.0, 120.0).absolute(x, y).focus_ring(2.0, 0.0, tok::accent(0, true))
91114 .child(text("New widget").font(tok::type_::LABEL).color(t.text_secondary))
92115 .child(text_input(&s.prompt, TextStyle::new(14.0, 400.0)).key("prompt-input").label("Widget prompt").w_full().grow(1.0)
93- .padding(6.0).radius(6.0).fill(t.bg).color(t.text).placeholder("A clock with the date… Enter to build, Esc to cancel", t.text_secondary)
116+ .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)
94117 .on_edit(|s: &mut State| &mut s.prompt)
95118 .on_submit(submit)
96- .on_key(|s: &mut State, e: &rune::KeyEvent, tx| {
119+ .on_input_key(|s: &mut State, e: &rune::KeyEvent, tx| {
97120 if !matches!(e.key, rune::Key::Escape) { return false; }
98121 s.prompt_at = None;
99122 tx.blur();
@@ -122,6 +145,6 @@ pub fn view(s: &State, _: &mut rune::Cx<State>) -> El {
122145 }
123146 for e in placed(s, &cx) { canvas = canvas.child(e); }
124147 if let Some((x, y)) = s.prompt_at { canvas = canvas.child(prompt_box(s, x, y)); }
125- let status = if s.connected { format!("{} widgets", widgets::ALL.len()) } else { "conveyord isn't running".into() };
148+ let status = if s.connected { String::new() } else { "conveyord isn't running".into() };
126149 col().key("root").w_full().h_full().fill(t.bg).child(titlebar::bar(status)).child(canvas)
127150 }