craft-ui: Outbox, the event channel out of a memoized view; replays note their widgets and nodes for the bypass walk

A run sends what its components reported in the pass; a replay sends nothing, which is complete because input
reaches only an engaged view, and an engaged view runs. The outbox feeds nothing into the key.

The testing notes of runtime-made widgets, nodes and scroll regions are journaled, so a replayed view's nodes
pass the bypass walk as its run's did.
This commit is contained in:
aiongg committed 2026-11-19 13:34:45 +00:00
1 parent 1657ce1b31
commit a9929bc06d
3 files changed
+61 -3

No files matched your search

+55 -2
View File
@@ -5,6 +5,7 @@ use super::lazy::{Lazy, Shown};
use super::memo::{MemoKey, MemoProps};
use crate::runtime::env::{Env, Scoped};
use egui::Ui;
use std::fmt;
use std::hash::Hash;
use std::sync::Arc;
@@ -42,8 +43,9 @@ use std::sync::Arc;
/// around it the view is one unit, sized by its content's measure.
///
/// What `show` returns holds layout facts only (a size, which part took the room). A replay returns the kept value
/// again, so an event returned through it would arrive once per pass. Events leave a view through its bindings, its
/// `Tracked` writes or commands. A measuring pass that takes the kept extent returns the kept value of the real run.
/// again, so an event returned through it would arrive once per pass. Events leave a view through an [`Outbox`] in its
/// props (picks, closes, drags: what its components report in the pass), its bindings, its `Tracked` writes or
/// commands. A measuring pass that takes the kept extent returns the kept value of the real run.
///
/// `show` is a `fn`: a closure that captures does not build.
///
@@ -119,3 +121,54 @@ impl<P: MemoProps, R: Clone + PartialEq + Send + Sync + 'static> MemoView<P, R>
key.finish()
}
}
/// The events a memoized view's run sends to the code around it, in the pass it runs: what its components reported (a
/// pick, a close, a drag that started). A run sends; a replay sends nothing. That is complete, because input reaches
/// only a view it engages (the pointer over it, focus in it, a held press, an intent routed to it), and an engaged view
/// runs. The view cannot read what it sent, so an outbox is no input and feeds nothing into the key.
///
/// ```
/// # extern crate craft_ui_contract as craft_ui;
/// use craft_ui::contract::{MemoProps, MemoView, Outbox};
/// #[derive(MemoProps)]
/// struct Row<'a> {
/// title: &'a str,
/// clicked: Outbox<'a, ()>,
/// }
/// fn row(props: &mut Row<'_>, ui: &mut egui::Ui) {
/// if ui.button(props.title).clicked() {
/// props.clicked.send(());
/// }
/// }
/// fn view(ui: &mut egui::Ui) -> bool {
/// let mut clicked = Vec::new();
/// let _ = MemoView::new("row", Row { title: "Open", clicked: Outbox::new(&mut clicked) }, row).show(ui);
/// !clicked.is_empty()
/// }
/// ```
pub struct Outbox<'a, E> {
sent: &'a mut Vec<E>,
}
impl<'a, E> Outbox<'a, E> {
/// An outbox whose events go into `sent`, after what it holds.
pub fn new(sent: &'a mut Vec<E>) -> Outbox<'a, E> {
Outbox { sent }
}
/// Sends `event`.
pub fn send(&mut self, event: E) {
self.sent.push(event);
}
}
impl<E> fmt::Debug for Outbox<'_, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Outbox").field("sent", &self.sent.len()).finish()
}
}
/// An outbox feeds nothing: the view only writes it.
impl<E> MemoProps for Outbox<'_, E> {
fn key(&self, _: &mut MemoKey) {}
}
+1 -1
View File
@@ -95,7 +95,7 @@ pub use lazy::Lazy;
pub use level::{Catalog, CatalogEntry, Level, StoryLayout, Surface, catalog, stale_table_rows, story_title};
pub use looks::Looks;
pub use memo::{MemoKey, MemoProps, Stateless};
pub use memo_view::MemoView;
pub use memo_view::{MemoView, Outbox};
pub use overlay::{Overlay, OverlayUi, Overlays, Popup, PopupHandle, SizeLimits, Trigger};
pub use pages::Pages;
pub use paint_ui::PaintUi;
@@ -47,6 +47,8 @@ pub(crate) fn note_widget(ctx: &Context, id: Id) {
noted::with(ctx, |pass| {
pass.widgets.insert(id);
});
// A replayed run notes it again, as it adds the widget again.
crate::runtime::replay::journal(ctx, || id, |ctx, &id| note_widget(ctx, id));
}
#[cfg(not(feature = "testing"))]
@@ -58,6 +60,8 @@ pub(crate) fn note_node(ctx: &Context, id: Id) {
noted::with(ctx, |pass| {
pass.nodes.insert(super::access::node_key(id));
});
// A replayed run notes it again, as it adds the node again.
crate::runtime::replay::journal(ctx, || id, |ctx, &id| note_node(ctx, id));
}
#[cfg(not(feature = "testing"))]
@@ -76,6 +80,7 @@ pub(crate) struct ScrollRegion {
#[cfg(feature = "testing")]
pub(crate) fn note_scroll_region(ctx: &Context, view: Rect, content: Rect) {
noted::with(ctx, |pass| pass.scrolls.push(ScrollRegion { view, content }));
crate::runtime::replay::journal(ctx, || (view, content), |ctx, &(view, content)| note_scroll_region(ctx, view, content));
}
#[cfg(not(feature = "testing"))]