8447
Co
conveyor / files
conveyor / templates / library / widgets / status.rs
5.8 KB
1//! Build status as a hover card under the title bar's running belt: what's running, widget 2//! builds in flight, and the agent's reply when one finishes, with Keep or Discard for a build on 3//! trial. A dot on the belt says a build waits on you; a failed or parked build's reply goes 4//! once it's clicked away. 5use super::{api::*, look, ui}; 6use rune::{Align, El, Side, col, el, row, text}; 7use std::{collections::BTreeSet, path::PathBuf, time::Duration}; 8 9pub const WIDGET: Widget = Widget { title: "Build status", size: (0.0, 0.0), new: |cx| Box::new(Status::load(cx)) }; 10 11/// The element the card hangs from: the running widget's instance in the title bar. 12const ICON: &str = "w:root.titlebar.running"; 13 14pub struct Status { 15 /// When this widget first ran (replies older than that aren't news), and replies clicked away. 16 born: u64, 17 seen: BTreeSet<u64>, 18 file: PathBuf, 19 /// The pointer is over the belt icon (set by the running widget), or over the card. 20 pub icon: bool, 21 card: bool, 22} 23 24impl Status { 25 fn load(cx: &Cx) -> Self { 26 let file = cx.dir.join("seen"); 27 let saved = std::fs::read_to_string(&file).unwrap_or_else(|_| (now() as u64).to_string()); 28 let mut ids = saved.split_whitespace().filter_map(|w| w.parse().ok()); 29 let s = Status { born: ids.next().unwrap_or(0), seen: ids.collect(), file, icon: false, card: false }; 30 s.save(); 31 s 32 } 33 34 fn save(&self) { 35 let ids: String = self.seen.iter().map(|i| format!(" {i}")).collect(); 36 let _ = std::fs::write(&self.file, format!("{}{ids}\n", self.born)); 37 } 38 39 /// A finished widget build whose reply hasn't been clicked away: on trial, failed or parked. 40 fn news(&self, b: &Ticket) -> bool { 41 b.project == "widgets" && match b.state.as_str() { 42 "review" => true, 43 "failed" | "parked" => b.since > self.born && !self.seen.contains(&(b.id as u64)), 44 _ => false, 45 } 46 } 47} 48 49/// How long the card lingers after the pointer leaves, so it can cross from the icon to the card. 50pub const LEAVE: Duration = Duration::from_millis(250); 51 52impl View for Status { 53 fn view(&self, cx: &Cx) -> El { 54 let t = look::of(cx); 55 let tickets = &cx.data.tickets; 56 let news = !cx.data.connected || tickets.iter().any(|b| self.news(b)); 57 let mut out = el().w_full().h_full(); 58 if news { 59 out = out.child(el().key("news").w(8.0).h(8.0).radius(4.0).fill(t.accent).anchored(ICON, Side::Bottom, Align::End, -12.0)); 60 } 61 if !(self.icon || self.card) { return out; } 62 let mut card = ui::popover(&t, col().key("builds").w(400.0).padding(10.0).gap(8.0)) 63 .anchored(ICON, Side::Bottom, Align::Center, 4.0) 64 .hover_delays(Duration::ZERO, LEAVE) 65 .on_hover_change(hover(cx.id, |s, inside| s.card = inside)); 66 if !cx.data.connected { card = card.child(text("conveyord isn't running").font(t.label).color(t.warn)); } 67 let mut any = false; 68 for b in tickets.iter().filter(|b| b.state == "running" || (b.project == "widgets" && b.state == "ready")) { 69 card = card.child(pill(&t, b, cx.now as u64)); 70 any = true; 71 } 72 for b in tickets.iter().filter(|b| self.news(b)) { 73 card = card.child(toast(b, cx)); 74 any = true; 75 } 76 if !any { card = card.child(text("Nothing running").font(t.label).color(t.text_secondary)); } 77 out.child(card) 78 } 79} 80 81/// A hover handler on instance `id`'s Status, told whether the pointer is inside: the running 82/// widget's icon passes `hover("status", |s, inside| s.icon = inside)`. 83pub fn hover(id: &str, f: impl Fn(&mut Status, bool) + 'static) -> impl Fn(&mut HostState, bool, &mut rune::Tx) + 'static { 84 let id = id.to_owned(); 85 move |h, inside, _| if let Some(s) = get::<Status>(h, &id) { f(s, inside) } 86} 87 88fn pill(t: &look::Look, b: &Ticket, now: u64) -> El { 89 let secs = now.saturating_sub(b.since); 90 let who = if b.project == "widgets" { format!("#{} {}", b.id, b.state) } else { format!("{} #{}", b.project, b.id) }; 91 row().key(format!("pill-{}", b.id)).w_full().min_h(28.0).px(10.0).gap(8.0).center_y().radius(14.0).fill(t.bg) 92 .child(text(who).font(t.label).color(t.accent)) 93 .child(text(b.title.clone()).font(t.label).color(t.text).grow(1.0).truncate()) 94 .child(text(format!("{}:{:02}", secs / 60, secs % 60)).font(t.label).color(t.text_secondary)) 95} 96 97fn toast(b: &Ticket, cx: &Cx) -> El { 98 let t = look::of(cx); 99 let (id, trial, color) = (b.id, b.state == "review", if b.state == "review" { t.accent } else { t.warn }); 100 let said = b.thread.iter().rev().find(|e| matches!(e.kind.as_str(), "reply" | "block" | "error")).map_or("", |e| &e.text); 101 let head = text(format!("#{id} {} · {}", if trial { "on trial" } else { &b.state }, b.title)) 102 .font(t.label).color(color).w_full().truncate(); 103 let toast = col().key(format!("toast-{id}")).w_full().radius(8.0).fill(t.bg).padding(10.0).gap(4.0).border(1.0, color) 104 .child(head).child(text(said.to_owned()).font(t.body).color(t.text).w_full().wrap()); 105 if trial { 106 return toast.child(row().w_full().gap(8.0).center_y() 107 .child(text("Try it, or Ctrl+click it to follow up").font(t.label).color(t.text_secondary).grow(1.0)) 108 .child(ui::button(&t, &format!("discard-{id}"), "Discard", true, move |h: &mut HostState, _| keep_build(h, id, false))) 109 .child(ui::primary(&t, &format!("keep-{id}"), "Keep", true, move |h: &mut HostState, _| keep_build(h, id, true)))); 110 } 111 toast.role("button").label("Dismiss").hover(t.hover) 112 .on_press_tx(on(cx.id, move |s: &mut Status, _| { s.seen.insert(id as u64); s.save(); })) 113 .child(text("Ctrl+click the widget to follow up · click to dismiss").font(t.label).color(t.text_secondary)) 114}