@@ -1,12 +1,18 @@
11 //! The contract between this library and conveyor. Conveyor owns this file and replaces it
22 //! when it changes. The library's own code is frame.rs and widgets/.
3-use rune::{El, Tx};
3+use rune::{El, TextEdit, Tx};
4+use std::{any::Any, path::Path};
45
5-/// What a widget can read. The app fills it from conveyord; the preview with sample data.
6+/// What a widget gets each render. The app fills it from conveyord; the preview with samples.
67 pub struct Cx<'a> {
8+ /// This widget's id on the canvas: pass it to `on`, `edit` and `get`.
9+ pub id: u32,
710 /// Unix time in seconds. The app re-renders every second.
811 pub now: f64,
912 pub tickets: &'a [TicketInfo],
13+ /// This widget's own folder. What it keeps here survives restarts, and the app restarts
14+ /// every time the library changes.
15+ pub dir: &'a Path,
1016 }
1117
1218 pub struct TicketInfo {
@@ -17,12 +23,49 @@ pub struct TicketInfo {
1723 }
1824
1925 /// Each widget file declares `pub const WIDGET: Widget`. A widget is content: the frame
20-/// around it, its position and its size on the canvas are the frame's and the user's.
26+/// around it, its position and size on the canvas are the frame's and the user's.
2127 pub struct Widget {
2228 pub title: &'static str,
2329 /// Width and height in points when not maximized. Lay out to fill whatever box you get.
2430 pub size: (f32, f32),
25- pub view: fn(&Cx) -> El,
31+ /// Makes the widget's state, once per widget on the canvas, each time the app starts.
32+ pub new: fn(&Cx) -> Box<dyn View>,
33+}
34+
35+/// A widget's state. It renders itself; its handlers reach it again through `on`, `edit` or `get`.
36+pub trait View: Any {
37+ fn view(&self, cx: &Cx) -> El;
38+}
39+
40+/// The state Rune hands every handler: the app's, or the preview's.
41+pub type HostState = crate::HostState;
42+
43+/// The app, or the preview.
44+pub trait Host: 'static {
45+ fn act(&mut self, id: u32, a: Act);
46+ fn widget(&mut self, id: u32) -> Option<&mut dyn View>;
47+ /// A field to bind to while a widget isn't there, so `edit` always has one.
48+ fn spare(&mut self) -> &mut TextEdit;
49+}
50+
51+/// Widget `id`'s state, inside any handler:
52+/// `.on_key(move |h: &mut HostState, e, tx| get::<Todo>(h, id).map_or(false, |t| t.key(e)))`.
53+pub fn get<W: View>(h: &mut HostState, id: u32) -> Option<&mut W> {
54+ let w: &mut dyn Any = Host::widget(h, id)?;
55+ w.downcast_mut()
56+}
57+
58+/// A handler on widget `id`'s state, for on_press_tx, on_submit, on_input_change and the like.
59+pub fn on<W: View>(id: u32, f: impl Fn(&mut W, &mut Tx) + 'static) -> impl Fn(&mut HostState, &mut Tx) + 'static {
60+ move |h: &mut HostState, tx: &mut Tx| if let Some(w) = get::<W>(h, id) { f(w, tx) }
61+}
62+
63+/// Binds a text field to a TextEdit in widget `id`'s state: `.on_edit(edit(cx.id, |p: &mut Pad| &mut p.text))`.
64+pub fn edit<W: View>(id: u32, f: impl Fn(&mut W) -> &mut TextEdit + 'static) -> impl for<'a> Fn(&'a mut HostState) -> &'a mut TextEdit + 'static {
65+ rune::edit_access(move |h: &mut HostState| {
66+ if get::<W>(h, id).is_none() { return Host::spare(h); }
67+ f(get::<W>(h, id).unwrap())
68+ })
2669 }
2770
2871 /// One widget on the canvas, as its frame sees it. frame.rs draws it.
@@ -44,7 +87,7 @@ pub struct Frame<'a> {
4487
4588 /// One step of a widget's conversation. `kind` is one of: prompt (the user's words),
4689 /// decide, deviate, block (what the agent chose, changed or couldn't do), reply (the agent's
47-/// last message), error (conveyor's, when a run failed).
90+/// last message), conveyor (a note from conveyor), error (when a run failed).
4891 pub struct Entry {
4992 pub ticket: u32,
5093 pub kind: String,
@@ -54,8 +97,7 @@ pub struct Entry {
5497 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
5598 pub enum Chrome { Shown, Hover, Hidden }
5699
57-/// What a frame can ask of the app. These are the only capabilities there are: if a ticket
58-/// needs another, it's conveyor's to add, so block and say which.
100+/// What a frame can ask of the app: the controls every widget shares.
59101 #[derive(Clone, Copy, PartialEq, Debug)]
60102 pub enum Act {
61103 /// Fill the canvas, or go back to its own size and place.
@@ -69,12 +111,7 @@ pub enum Act {
69111 Notes,
70112 }
71113
72-/// The app, or the preview: whatever carries out acts.
73-pub trait Host: 'static {
74- fn act(&mut self, id: u32, a: Act);
75-}
76-
77114 /// A press handler that asks for `a`: `button.on_press_tx(act(f.id, Act::Maximize))`.
78-pub fn act(id: u32, a: Act) -> impl Fn(&mut crate::HostState, &mut Tx) + 'static {
79- move |h: &mut crate::HostState, _: &mut Tx| Host::act(h, id, a)
115+pub fn act(id: u32, a: Act) -> impl Fn(&mut HostState, &mut Tx) + 'static {
116+ move |h: &mut HostState, _: &mut Tx| Host::act(h, id, a)
80117 }