8447
Co
conveyor / files
conveyor / crates / conveyor-app / src / safe.rs
2.9 KB
1//! Safe mode: the core alone, with every widget paused, for when the library on trial is broken 2//! (root or status panicked, or the user asked from the Ctrl inspector, or `--safe`). It lists the 3//! builds on trial with Keep and Discard, and leaving it makes every widget again. 4//! The shared value `safe_mode = off` stops offering it; `--safe` still opens it. 5use crate::app::State; 6use rune::{El, col, el, row, text, tok}; 7 8/// Whether the inspector offers it and a broken root or status opens it. 9pub fn offered(s: &State) -> bool { 10 s.live.shared("safe_mode") != Some("off") 11} 12 13/// Opens it when root or status stopped. 14pub fn check(s: &mut State) { 15 if s.safe.is_some() || !offered(s) { return; } 16 if let Some(msg) = ["root", "status"].iter().find_map(|id| Some(format!("`{id}` stopped: {}", s.live.broken(id)?))) { 17 s.safe = Some(msg); 18 } 19} 20 21fn button(key: String, label: &'static str, fill: rune::Color, ink: rune::Color, f: impl Fn(&mut State) + 'static) -> El { 22 row().key(key).h(28.0).px(12.0).center_y().radius(6.0).fill(fill).role("button").label(label) 23 .on_press_tx(move |s: &mut State, _| f(s)) 24 .child(text(label).font(tok::type_::LABEL).color(ink)) 25} 26 27pub fn view(s: &State, why: &str) -> El { 28 let t = tok::theme(true); 29 let trial: Vec<_> = s.builds.iter().filter(|b| b["state"] == "review").collect(); 30 let mut list = col().w_full().gap(8.0); 31 if trial.is_empty() { list = list.child(text("No builds on trial.").color(t.text_secondary)); } 32 for b in trial { 33 let id = b["id"].as_u64().unwrap_or(0); 34 let decide = move |keep: bool| move |s: &mut State| s.live.keep_build(id as u32, keep); 35 list = list.child(row().key(format!("build-{id}")).w_full().gap(8.0).center_y() 36 .child(text(format!("#{id} {}", b["title"].as_str().unwrap_or(""))).color(t.text).grow(1.0).min_w(0.0).truncate()) 37 .child(button(format!("discard-{id}"), "Discard", t.hover, t.text, decide(false))) 38 .child(button(format!("keep-{id}"), "Keep", tok::accent(0, true), t.bg, decide(true)))); 39 } 40 let offer = offered(s); 41 let card = col().key("safe").w(560.0).padding(24.0).gap(12.0).radius(10.0).fill(t.card) 42 .child(text("Safe mode").font(tok::type_::TITLE).color(t.text)) 43 .child(text(why.to_owned()).color(t.text_secondary).w_full().wrap()) 44 .child(text("Your widgets are paused. Builds on trial:").color(t.text)) 45 .child(list) 46 .child(row().w_full().gap(8.0).center_y() 47 .child(button("offer".into(), if offer { "Don't offer safe mode" } else { "Offer safe mode" }, t.hover, t.text, 48 move |s| s.live.share("safe_mode", if offer { "off" } else { "on" }))) 49 .child(el().grow(1.0)) 50 .child(button("leave".into(), "Leave safe mode", tok::accent(0, true), t.bg, |s| { s.safe = None; s.live.remake(); }))); 51 col().key("safe-mode").w_full().h_full().fill(t.bg).center().window_drag().child(card) 52}