git clone https://garage44.eu/jeroen/frits.gitcartopolis-agent · 2026-11-27T08:44:22Z
feat(frits): a field sends what was typed in it A view could send only fixed messages, so a text field had no way to tell the script what was typed. A bare message variant that carries one Str (on_input=Typed) now becomes AttrInput(tag), and the host builds the message with the control's text (view::AttrValue::message). A constructor reference was chosen over closures in attributes: a closure cannot cross to the host or from a server to a browser, a variant tag can.
crates/frits/src/compile.rs | 36 ++++++++++++++++++++++++++++++++++++
crates/frits/src/prelude.rs | 5 +++--
crates/frits/src/tests.rs | 29 +++++++++++++++++++++++++++++
crates/frits/src/view.rs | 16 ++++++++++++++++
4 files changed, 84 insertions(+), 2 deletions(-)
diff --git a/crates/frits/src/compile.rs b/crates/frits/src/compile.rs
index bfc567c..adeb452 100644
--- a/crates/frits/src/compile.rs
+++ b/crates/frits/src/compile.rs
@@ -2956,6 +2956,19 @@ impl<'p, 'a> Compiler<'p, 'a> {
self.variants[variant].1
}
+ /// An attribute naming, bare, a message variant that carries one Str
+ /// (`on_input=Typed`): the host fills in what was typed. Its tag.
+ fn input_variant(&self, e: &Expr, msg: &Type) -> Option<u32> {
+ let (ExprKind::Ident(n), Type::Enum(id, _)) = (&e.kind, msg) else {
+ return None;
+ };
+ if self.lookup(n).is_some() {
+ return None;
+ }
+ let &(en, tag) = self.variants.get(n)?;
+ (en == *id && self.enums[en as usize].variants[tag as usize].1 == [Type::Str]).then_some(tag)
+ }
+
/// `<Tag attr=value …>children</Tag>`: a `ViewEl(tag, attributes, children)`.
fn markup(&mut self, mk: &Markup, r: Reg, want: Option<&Type>, line: u32) -> Type {
let view = self.view_type(want);
@@ -2973,6 +2986,29 @@ impl<'p, 'a> Compiler<'p, 'a> {
let pair = self.alloc_n(2);
let k = self.string(name);
self.emit(Op::LoadK { dst: pair, k });
+ if let Some(tag) = self.input_variant(e, &msg) {
+ let v = self.alloc();
+ let k = self.konst(Value::Int(i64::from(tag)));
+ self.emit(Op::LoadK { dst: v, k });
+ let input = self.tag_of("AttrInput");
+ self.emit(Op::MakeVariant {
+ dst: pair + 1,
+ tag: input,
+ base: v,
+ n: 1,
+ });
+ let tuple = self.alloc();
+ self.emit(Op::MakeRecord {
+ dst: tuple,
+ base: pair,
+ n: 2,
+ });
+ self.emit(Op::ListPushMut {
+ list: attrs,
+ val: tuple,
+ });
+ continue;
+ }
let (v, t) = self.expr_any(e, Some(&msg));
let variant = match &t {
Type::Str => "AttrStr",
diff --git a/crates/frits/src/prelude.rs b/crates/frits/src/prelude.rs
index 71fa06e..cc66a6c 100644
--- a/crates/frits/src/prelude.rs
+++ b/crates/frits/src/prelude.rs
@@ -9,9 +9,10 @@ type Vec3 = { x: Float, y: Float, z: Float }
// A user interface, written as markup: `<Column gap=8> <Button on_press=Inc>…</Button> </Column>`.
// The host draws it with its own components. `M` is what pressing something
-// sends to the script's `on receive`.
+// sends to the script's `on receive`; a message that carries one Str, named
+// bare (`on_input=Typed`), sends what was typed in it.
enum View<M> { ViewEl(Str, List<(Str, Attr<M>)>, List<View<M>>), ViewText(Str) }
-enum Attr<M> { AttrStr(Str), AttrInt(Int), AttrFloat(Float), AttrBool(Bool), AttrSend(M) }
+enum Attr<M> { AttrStr(Str), AttrInt(Int), AttrFloat(Float), AttrBool(Bool), AttrSend(M), AttrInput(Int) }
// Showing a view: `render(v)`.
effect Ui {
diff --git a/crates/frits/src/tests.rs b/crates/frits/src/tests.rs
index 438c849..16c853a 100644
--- a/crates/frits/src/tests.rs
+++ b/crates/frits/src/tests.rs
@@ -976,6 +976,35 @@ fn a_view_is_rendered_and_a_press_comes_back_as_a_message() {
assert!(e[0].contains("closed by `</Tex>`"), "{e:?}");
}
+#[test]
+fn a_field_sends_what_was_typed_in_it() {
+ let mut rt = Runtime::new(platform());
+ let m = rt
+ .platform
+ .compile(
+ "script / Out, Ui
+ enum Msg { Typed(Str), Add }
+ var draft = \"\"
+ on go() { render(<Field value=draft on_input=Typed on_submit=Add/>) }
+ on receive(m: Msg) {
+ match m { Typed(t) => draft = t, Add => draft = \"\" }
+ render(<Field value=draft on_input=Typed on_submit=Add/>)
+ }",
+ )
+ .unwrap_or_else(|e| panic!("{e:?}"));
+ let pid = rt.spawn(m, Log::default());
+ rt.post(pid, "go", Vec::new());
+ rt.tick();
+ let v = rt.get_mut(pid).unwrap().take_view().unwrap();
+ let tree = view::decode(&v).unwrap();
+ let typed = tree.attr("on_input").unwrap().message("milk").unwrap();
+ assert!(tree.attr("on_submit").unwrap().message("ignored").is_some());
+ rt.post(pid, "receive", vec![typed]);
+ rt.tick();
+ let v = rt.get_mut(pid).unwrap().take_view().unwrap();
+ assert_eq!(view::decode(&v).unwrap().attr("value"), Some(&view::AttrValue::Str("milk".into())));
+}
+
#[test]
fn untrusted_text_is_shown_but_must_be_checked_before_it_acts() {
let mut rt = Runtime::new(platform());
diff --git a/crates/frits/src/view.rs b/crates/frits/src/view.rs
index 6e9623f..50784e9 100644
--- a/crates/frits/src/view.rs
+++ b/crates/frits/src/view.rs
@@ -22,6 +22,21 @@ pub enum AttrValue {
Bool(bool),
/// A message: post it to the script as `receive` when this is used.
Send(HostValue),
+ /// A message that carries text: post [`AttrValue::message`] with what
+ /// was typed.
+ Input(u32),
+}
+
+impl AttrValue {
+ /// The message this attribute sends, given what the control holds now
+ /// (only an `Input` reads it).
+ pub fn message(&self, text: &str) -> Option<HostValue> {
+ match self {
+ AttrValue::Send(m) => Some(m.clone()),
+ AttrValue::Input(tag) => Some(HostValue::Variant(*tag, vec![HostValue::Str(text.to_owned())])),
+ _ => None,
+ }
+ }
}
/// The tree in a rendered view, or None if it is not one.
@@ -51,6 +66,7 @@ pub fn decode(v: &HostValue) -> Option<Node> {
(2, HostValue::Float(x)) => AttrValue::Float(*x),
(3, HostValue::Bool(b)) => AttrValue::Bool(*b),
(4, m) => AttrValue::Send(m.clone()),
+ (5, HostValue::Int(tag)) => AttrValue::Input(u32::try_from(*tag).ok()?),
_ => return None,
};
Some((name.clone(), value))