@@ -60,6 +60,8 @@ pub struct Data {
6060
6161 /// Every widget kind in the library, as (file name, widget): what a host loads.
6262 pub type Kinds = &'static [(&'static str, &'static Widget)];
63+/// Each kind's description: the first paragraph of its file's `//!` comment.
64+pub type Abouts = &'static [(&'static str, &'static str)];
6365
6466 /// Each widget file declares `pub const WIDGET: Widget`.
6567 pub struct Widget {
@@ -119,6 +121,11 @@ impl Cx<'_> {
119121 self.live.render(kind, &format!("{}.{key}", self.id))
120122 }
121123
124+ /// What widget `kind` is, from the first paragraph of its file's `//!` comment.
125+ pub fn about(&self, kind: &str) -> &'static str {
126+ self.live.about.iter().find(|(n, _)| *n == kind).map_or("", |(_, a)| a)
127+ }
128+
122129 /// A value any widget can read, set with `share`: the selected project, say.
123130 pub fn shared(&self, key: &str) -> Option<&str> {
124131 self.live.shared.get(key).map(String::as_str)
@@ -168,7 +175,25 @@ pub fn get<'a, W: View>(h: &'a mut HostState, id: &str) -> Option<&'a mut W> {
168175 /// A handler on instance `id`'s state, for on_press_tx, on_submit, on_input_change and the like.
169176 pub fn on<W: View>(id: &str, f: impl Fn(&mut W, &mut Tx) + 'static) -> impl Fn(&mut HostState, &mut Tx) + 'static {
170177 let id = id.to_owned();
171- move |h: &mut HostState, tx: &mut Tx| if let Some(w) = get::<W>(h, &id) { f(w, tx) }
178+ move |h: &mut HostState, tx: &mut Tx| {
179+ let Some(w) = get::<W>(h, &id) else { return };
180+ if let Err(msg) = catch(|| f(w, tx)) { Host::live(h).broken.get_mut().insert(id.clone(), msg); }
181+ }
182+}
183+
184+/// Runs widget code, turning a panic into its message.
185+fn catch<T>(f: impl FnOnce() -> T) -> Result<T, String> {
186+ std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)).map_err(|e| {
187+ e.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| e.downcast_ref::<String>().cloned()).unwrap_or_else(|| "it panicked".into())
188+ })
189+}
190+
191+/// What a broken instance draws instead of itself.
192+fn stopped(kind: &str, msg: &str) -> El {
193+ let (t, red) = (rune::tok::theme(true), rune::Color { r: 0.9, g: 0.35, b: 0.3, a: 1.0 });
194+ col().w_full().h_full().padding(12.0).gap(4.0).radius(6.0).fill(t.card).border(1.0, red)
195+ .child(rune::text(format!("{kind} stopped: {msg}")).color(red).w_full().wrap())
196+ .child(rune::text("Ctrl+click it to have it fixed").font(rune::tok::type_::LABEL).color(t.text_secondary))
172197 }
173198
174199 /// Binds a text field to a TextEdit in instance `id`'s state:
@@ -243,6 +268,9 @@ pub fn quote(s: &str) -> String {
243268 pub struct Live {
244269 pub data: Data,
245270 kinds: Kinds,
271+ about: Abouts,
272+ /// Instances whose code panicked, with the message. They draw `stopped` until the next build.
273+ broken: RefCell<BTreeMap<String, String>>,
246274 base: PathBuf,
247275 instances: HashMap<String, (String, Box<dyn View>)>,
248276 shared: BTreeMap<String, String>,
@@ -323,14 +351,14 @@ impl Live {
323351
324352 impl Live {
325353 /// Instances of `kinds` keep their folders under `base`, and shared values in `base/shared.tsv`.
326- pub fn new(base: PathBuf, kinds: Kinds) -> Self {
354+ pub fn new(base: PathBuf, kinds: Kinds, about: Abouts) -> Self {
327355 let shared = std::fs::read_to_string(base.join("shared.tsv")).unwrap_or_default().lines()
328356 .filter_map(|l| l.split_once('\t').map(|(k, v)| (k.to_owned(), v.replace("\\n", "\n"))))
329357 .collect();
330358 // Instances that were keeping work going come back now, shown or not.
331359 let wanted: Vec<(String, String)> = std::fs::read_to_string(base.join("running.tsv")).unwrap_or_default().lines()
332360 .filter_map(|l| l.split_once('\t').map(|(id, kind)| (id.to_owned(), kind.to_owned()))).collect();
333- Live { data: Data::default(), kinds, base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
361+ Live { data: Data::default(), kinds, about, broken: RefCell::new(BTreeMap::new()), base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
334362 animate: Cell::new(false), outbox: vec![], spare: TextEdit::new(""), dirty: false,
335363 later: RefCell::new(vec![]), running: RefCell::new(BTreeSet::new()), kept: BTreeSet::new() }
336364 }
@@ -341,8 +369,13 @@ impl Live {
341369
342370 /// Instance `id` of `kind`, keyed `w:<id>` so the app can find it under the pointer.
343371 pub fn render(&self, kind: &str, id: &str) -> El {
372+ let broken = self.broken.borrow().get(id).cloned();
373+ if let Some(msg) = broken { return stopped(kind, &msg).key(format!("w:{id}")); }
344374 match self.instances.get(id) {
345- Some((k, w)) if k == kind => w.view(&self.cx(id, now())).key(format!("w:{id}")),
375+ Some((k, w)) if k == kind => match catch(|| w.view(&self.cx(id, now()))) {
376+ Ok(el) => el.key(format!("w:{id}")),
377+ Err(msg) => { self.broken.borrow_mut().insert(id.to_owned(), msg.clone()); stopped(kind, &msg).key(format!("w:{id}")) }
378+ },
346379 // Made before the next render. A kind the library doesn't have (a discarded build's)
347380 // draws nothing.
348381 _ => {
@@ -361,8 +394,10 @@ impl Live {
361394 if self.instances.get(&id).is_some_and(|(k, _)| *k == kind) { continue; }
362395 let cx = self.cx(&id, now());
363396 let _ = std::fs::create_dir_all(&cx.dir);
364- let state = (w.new)(&cx);
365- self.instances.insert(id, (kind, state));
397+ match catch(|| (w.new)(&cx)) {
398+ Ok(state) => { self.instances.insert(id, (kind, state)); }
399+ Err(msg) => { self.broken.get_mut().insert(id, msg); }
400+ }
366401 made = true;
367402 }
368403 made
@@ -376,14 +411,30 @@ impl Live {
376411 /// A new build of the library: every instance is dropped, then made again from its kind's
377412 /// new code, before the next render. What they asked with `cx.act` runs first, against
378413 /// the old instances. Kinds the new build doesn't have are gone.
379- pub fn reload(&mut self, kinds: Kinds) {
414+ pub fn reload(&mut self, kinds: Kinds, about: Abouts) {
380415 self.apply();
381416 let all: Vec<(String, String)> = self.instances.drain().map(|(id, (kind, _))| (id, kind)).collect();
382- self.kinds = kinds;
417+ (self.kinds, self.about) = (kinds, about);
418+ self.broken.get_mut().clear();
383419 self.wanted.get_mut().extend(all);
384420 self.make_wanted();
385421 }
386422
423+ /// A shared value, as `cx.shared` reads it.
424+ pub fn shared(&self, key: &str) -> Option<&str> {
425+ self.shared.get(key).map(String::as_str)
426+ }
427+
428+ /// Every instance made again, from the same build: a fresh start for broken ones.
429+ pub fn remake(&mut self) {
430+ self.reload(self.kinds, self.about);
431+ }
432+
433+ /// Why instance `id` stopped, if its code panicked.
434+ pub fn broken(&self, id: &str) -> Option<String> {
435+ self.broken.borrow().get(id).cloned()
436+ }
437+
387438 /// Instances of `kind`, or any instance if `kind` is empty.
388439 pub fn ids(&self, kind: &str) -> Vec<String> {
389440 self.instances.iter().filter(|(_, (k, _))| kind.is_empty() || k == kind).map(|(id, _)| id.clone()).collect()
@@ -421,10 +472,11 @@ impl Live {
421472
422473 /// Instance `id`'s state, writable, beside a Cx for it.
423474 fn with<T>(&mut self, id: &str, f: impl FnOnce(&mut dyn View, &Cx) -> T) -> Option<T> {
475+ if self.broken.get_mut().contains_key(id) { return None; }
424476 let (k, mut w) = self.instances.remove(id)?;
425- let out = f(w.as_mut(), &self.cx(id, now()));
477+ let out = catch(|| f(w.as_mut(), &self.cx(id, now())));
426478 self.instances.insert(id.to_owned(), (k, w));
427- Some(out)
479+ out.map_err(|msg| self.broken.get_mut().insert(id.to_owned(), msg)).ok()
428480 }
429481
430482 /// Writes what the preview needs to show the app as it is now: `data.tsv` beside the