@@ -2,7 +2,7 @@
22 //! when it changes. Everything in widgets/ is the library's, starting with root.rs: the app
33 //! draws `root` and nothing else, so the whole UI is widgets.
44 use rune::{El, TextEdit, Tx, col};
5-use std::{any::Any, cell::{Cell, RefCell}, collections::{BTreeMap, BTreeSet, HashMap}, path::{Path, PathBuf}};
5+use std::{any::Any, cell::{Cell, OnceCell, RefCell}, rc::Rc, collections::{BTreeMap, BTreeSet, HashMap}, path::{Path, PathBuf}};
66
77 // ── What widgets read: conveyor's state ─────────────────────────────────────────────────────
88
@@ -121,6 +121,13 @@ impl Cx<'_> {
121121 self.live.render(kind, &format!("{}.{key}", self.id))
122122 }
123123
124+ /// The app's image cache: decodes off the UI thread and redraws when an image is ready.
125+ /// Keep the handle (`images: cx.images()`) to load from handlers too.
126+ pub fn images(&self) -> Rc<rune::ImageCache> {
127+ let live = self.live;
128+ live.images.get_or_init(|| Rc::new(rune::ImageCache::new(live.waker.clone()))).clone()
129+ }
130+
124131 /// What widget `kind` is, from the first paragraph of its file's `//!` comment.
125132 pub fn about(&self, kind: &str) -> &'static str {
126133 self.live.about.iter().find(|(n, _)| *n == kind).map_or("", |(_, a)| a)
@@ -269,6 +276,9 @@ pub struct Live {
269276 pub data: Data,
270277 kinds: Kinds,
271278 about: Abouts,
279+ /// The host's waker and the image cache made with it, shared by every widget.
280+ waker: rune::Waker,
281+ images: OnceCell<Rc<rune::ImageCache>>,
272282 /// Instances whose code panicked, with the message. They draw `stopped` until the next build.
273283 broken: RefCell<BTreeMap<String, String>>,
274284 base: PathBuf,
@@ -358,7 +368,7 @@ impl Live {
358368 // Instances that were keeping work going come back now, shown or not.
359369 let wanted: Vec<(String, String)> = std::fs::read_to_string(base.join("running.tsv")).unwrap_or_default().lines()
360370 .filter_map(|l| l.split_once('\t').map(|(id, kind)| (id.to_owned(), kind.to_owned()))).collect();
361- Live { data: Data::default(), kinds, about, broken: RefCell::new(BTreeMap::new()), base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
371+ Live { data: Data::default(), kinds, about, broken: RefCell::new(BTreeMap::new()), waker: std::sync::Arc::new(|| {}), images: OnceCell::new(), base, instances: HashMap::new(), shared, wanted: RefCell::new(wanted),
362372 animate: Cell::new(false), outbox: vec![], spare: TextEdit::new(""), dirty: false,
363373 later: RefCell::new(vec![]), running: RefCell::new(BTreeSet::new()), kept: BTreeSet::new() }
364374 }
@@ -420,6 +430,11 @@ impl Live {
420430 self.make_wanted();
421431 }
422432
433+ /// How widgets wake the host from other threads (images decoded). Set it before rendering.
434+ pub fn set_waker(&mut self, waker: rune::Waker) {
435+ self.waker = waker;
436+ }
437+
423438 /// A shared value, as `cx.shared` reads it.
424439 pub fn shared(&self, key: &str) -> Option<&str> {
425440 self.shared.get(key).map(String::as_str)