nandi/jolt-nativepublic Fork 0
4706c920e45ce80b11ee106d05c16d9eacc99fc7
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

glimmer-cosmic: a libcosmic backend for glimmer (spike) 6a3304d · on 4706c920e45ce80b11ee106d05c16d9eacc99fc7 · nandi · 8d ago
tree.rs · 349 lines · 10.2 KBRust Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! The node arena: what the reconciler patches, and what `view` reads.
//!
//! Plain data with no libcosmic in it, so the edit half of the ABI is testable
//! without a window, a GPU or a display. libvidya and libjolttui keep the same
//! shape; what is different here is who reads it. There, the caller's thread
//! paints the tree; here iced's thread does, so the arena sits behind a mutex
//! and `view` works from a snapshot taken at the last commit.

use std::collections::BTreeMap;
use std::fmt::Write;

#[derive(Clone, Debug, PartialEq)]
pub enum Prop {
    Str(String),
    Num(f64),
    Bool(bool),
}

#[derive(Clone, Debug)]
pub struct Node {
    pub tag: String,
    pub props: BTreeMap<String, Prop>,
    pub children: Vec<i32>,
    pub parent: i32,
}

impl Node {
    pub fn str(&self, key: &str) -> &str {
        match self.props.get(key) {
            Some(Prop::Str(s)) => s,
            _ => "",
        }
    }

    pub fn num(&self, key: &str) -> Option<f64> {
        match self.props.get(key) {
            Some(Prop::Num(n)) => Some(*n),
            _ => None,
        }
    }

    pub fn bool(&self, key: &str) -> Option<bool> {
        match self.props.get(key) {
            Some(Prop::Bool(b)) => Some(*b),
            _ => None,
        }
    }

    /// The label a text-shaped widget shows: `label`, or `text` failing that.
    pub fn label(&self) -> &str {
        match self.str("label") {
            "" => self.str("text"),
            s => s,
        }
    }
}

/// `hbox` and `vbox` are one node here, as in libvidya; the tag only implies
/// an orientation, and the jolt side writes it as a prop.
fn canonical(tag: &str) -> &str {
    match tag {
        "hbox" | "vbox" => "box",
        "checkbox" => "checkbutton",
        other => other,
    }
}

#[derive(Clone, Debug, Default)]
pub struct Tree {
    /// Slot `i` holds handle `i + 1`; `None` is a freed handle waiting for reuse.
    slots: Vec<Option<Node>>,
    free: Vec<i32>,
    root: i32,
}

impl Tree {
    pub fn get(&self, id: i32) -> Option<&Node> {
        if id <= 0 {
            return None;
        }
        self.slots.get(id as usize - 1)?.as_ref()
    }

    fn get_mut(&mut self, id: i32) -> Option<&mut Node> {
        if id <= 0 {
            return None;
        }
        self.slots.get_mut(id as usize - 1)?.as_mut()
    }

    pub fn exists(&self, id: i32) -> bool {
        self.get(id).is_some()
    }

    pub fn root(&mut self) -> i32 {
        if !self.exists(self.root) {
            self.root = self.new_node("window");
        }
        self.root
    }

    /// The root without creating it, for a reader holding a snapshot.
    pub fn root_id(&self) -> i32 {
        self.root
    }

    pub fn new_node(&mut self, tag: &str) -> i32 {
        let node = Node {
            tag: canonical(tag).to_owned(),
            props: BTreeMap::new(),
            children: Vec::new(),
            parent: 0,
        };
        match self.free.pop() {
            Some(id) => {
                self.slots[id as usize - 1] = Some(node);
                id
            }
            None => {
                self.slots.push(Some(node));
                self.slots.len() as i32
            }
        }
    }

    /// Free `id` and everything under it, detaching it from its parent first.
    pub fn free(&mut self, id: i32) {
        let Some(node) = self.get(id) else { return };
        let parent = node.parent;
        if let Some(p) = self.get_mut(parent) {
            p.children.retain(|c| *c != id);
        }
        self.free_subtree(id);
    }

    fn free_subtree(&mut self, id: i32) {
        let Some(node) = self.slots.get_mut(id as usize - 1).and_then(Option::take) else {
            return;
        };
        for child in node.children {
            self.free_subtree(child);
        }
        if id == self.root {
            self.root = 0;
        }
        self.free.push(id);
    }

    pub fn set(&mut self, id: i32, key: &str, value: Prop) {
        if let Some(n) = self.get_mut(id) {
            n.props.insert(key.to_owned(), value);
        }
    }

    pub fn clear_props(&mut self, id: i32) {
        if let Some(n) = self.get_mut(id) {
            n.props.clear();
        }
    }

    fn detach(&mut self, child: i32) {
        let parent = match self.get(child) {
            Some(n) => n.parent,
            None => return,
        };
        if let Some(p) = self.get_mut(parent) {
            p.children.retain(|c| *c != child);
        }
        if let Some(c) = self.get_mut(child) {
            c.parent = 0;
        }
    }

    /// A node may not become its own ancestor; the reconciler never asks, but
    /// a cycle would make `view` recurse forever, so it is refused here.
    fn is_ancestor(&self, ancestor: i32, mut id: i32) -> bool {
        while id > 0 {
            if id == ancestor {
                return true;
            }
            id = self.get(id).map_or(0, |n| n.parent);
        }
        false
    }

    pub fn append(&mut self, parent: i32, child: i32) -> bool {
        if !self.exists(parent) || !self.exists(child) || self.is_ancestor(child, parent) {
            return false;
        }
        self.detach(child);
        self.get_mut(parent).unwrap().children.push(child);
        self.get_mut(child).unwrap().parent = parent;
        true
    }

    /// Unparents and frees `child`, under `parent` only.
    pub fn remove(&mut self, parent: i32, child: i32) {
        if self.get(child).is_some_and(|n| n.parent == parent) {
            self.free(child);
        }
    }

    /// Move `child` after `sibling`; `sibling` 0 is the first position.
    pub fn insert_after(&mut self, parent: i32, child: i32, sibling: i32) -> bool {
        if !self.exists(parent) || !self.exists(child) || self.is_ancestor(child, parent) {
            return false;
        }
        if sibling != 0 && self.get(sibling).is_none_or(|n| n.parent != parent) {
            return false;
        }
        self.detach(child);
        let p = self.get_mut(parent).unwrap();
        let at = if sibling == 0 {
            0
        } else {
            p.children.iter().position(|c| *c == sibling).unwrap() + 1
        };
        p.children.insert(at, child);
        self.get_mut(child).unwrap().parent = parent;
        true
    }

    /// Put `new` where `old` was under `parent`, and free `old`.
    pub fn replace(&mut self, parent: i32, old: i32, new: i32) -> bool {
        if !self.exists(new) || self.get(old).is_none_or(|n| n.parent != parent) {
            return false;
        }
        if self.is_ancestor(new, parent) {
            return false;
        }
        self.detach(new);
        let p = self.get_mut(parent).unwrap();
        let at = p.children.iter().position(|c| *c == old).unwrap();
        p.children[at] = new;
        self.get_mut(new).unwrap().parent = parent;
        // `old` is no longer among the parent's children, so free only its subtree.
        self.get_mut(old).unwrap().parent = 0;
        self.free_subtree(old);
        true
    }

    /// The subtree at `id` as hiccup, one node to a line, props sorted — so two
    /// dumps of the same tree compare as text.
    pub fn dump(&self, id: i32) -> String {
        let mut out = String::new();
        self.dump_into(id, 0, &mut out);
        out
    }

    fn dump_into(&self, id: i32, depth: usize, out: &mut String) {
        let Some(n) = self.get(id) else { return };
        let _ = write!(out, "{}[:{} {{", "  ".repeat(depth), n.tag);
        for (i, (k, v)) in n.props.iter().enumerate() {
            if i > 0 {
                out.push(' ');
            }
            let _ = match v {
                Prop::Str(s) => write!(out, ":{k} {s:?}"),
                Prop::Num(x) => write!(out, ":{k} {x}"),
                Prop::Bool(b) => write!(out, ":{k} {b}"),
            };
        }
        out.push('}');
        for child in &n.children {
            out.push('\n');
            self.dump_into(*child, depth + 1, out);
        }
        out.push(']');
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_removed_subtree_frees_every_node_under_it() {
        let mut t = Tree::default();
        let root = t.root();
        let card = t.new_node("card");
        let label = t.new_node("label");
        assert!(t.append(root, card));
        assert!(t.append(card, label));
        t.remove(root, card);
        assert!(!t.exists(card));
        assert!(!t.exists(label));
        assert!(t.get(root).unwrap().children.is_empty());
    }

    #[test]
    fn insert_after_zero_moves_to_the_front() {
        let mut t = Tree::default();
        let root = t.root();
        let (a, b, c) = (
            t.new_node("label"),
            t.new_node("label"),
            t.new_node("label"),
        );
        for n in [a, b, c] {
            t.append(root, n);
        }
        assert!(t.insert_after(root, c, 0));
        assert_eq!(t.get(root).unwrap().children, vec![c, a, b]);
        assert!(t.insert_after(root, c, b));
        assert_eq!(t.get(root).unwrap().children, vec![a, b, c]);
    }

    #[test]
    fn replace_keeps_the_position_and_frees_the_old_node() {
        let mut t = Tree::default();
        let root = t.root();
        let (a, b, c) = (
            t.new_node("label"),
            t.new_node("button"),
            t.new_node("label"),
        );
        t.append(root, a);
        t.append(root, b);
        assert!(t.replace(root, a, c));
        assert_eq!(t.get(root).unwrap().children, vec![c, b]);
        assert!(!t.exists(a));
    }

    #[test]
    fn a_node_cannot_be_appended_under_itself() {
        let mut t = Tree::default();
        let root = t.root();
        let card = t.new_node("card");
        t.append(root, card);
        assert!(!t.append(card, root));
    }

    #[test]
    fn dump_reads_back_as_hiccup() {
        let mut t = Tree::default();
        let root = t.root();
        let row = t.new_node("hbox");
        t.set(row, "orientation", Prop::Str("horizontal".into()));
        let b = t.new_node("button");
        t.set(b, "label", Prop::Str("+ 1".into()));
        t.set(b, "kind", Prop::Str("primary".into()));
        t.append(root, row);
        t.append(row, b);
        assert_eq!(
            t.dump(root),
            "[:window {}\n  [:box {:orientation \"horizontal\"}\n    [:button {:kind \"primary\" :label \"+ 1\"}]]]"
        );
    }
}