Skip to main content

hypen_engine/reconcile/
patch.rs

1use crate::ir::NodeId;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4
5/// Platform-agnostic patch operations for updating the UI
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(tag = "type", rename_all = "camelCase")]
8pub enum Patch {
9    /// Create a new node
10    Create {
11        id: String,
12        element_type: String,
13        props: indexmap::IndexMap<String, Value>,
14    },
15
16    /// Set a property on an existing node
17    SetProp {
18        id: String,
19        name: String,
20        value: Value,
21    },
22
23    /// Set text content of a node
24    SetText {
25        id: String,
26        text: String,
27    },
28
29    /// Insert a node into the tree
30    Insert {
31        parent_id: String,
32        id: String,
33        before_id: Option<String>,
34    },
35
36    /// Move a node to a new position
37    Move {
38        parent_id: String,
39        id: String,
40        before_id: Option<String>,
41    },
42
43    /// Remove a node from the tree
44    Remove {
45        id: String,
46    },
47
48    // Event handling is now done at the renderer level
49}
50
51impl Patch {
52    pub fn create(id: NodeId, element_type: String, props: indexmap::IndexMap<String, Value>) -> Self {
53        Self::Create {
54            id: format!("{:?}", id),
55            element_type,
56            props,
57        }
58    }
59
60    pub fn set_prop(id: NodeId, name: String, value: Value) -> Self {
61        Self::SetProp {
62            id: format!("{:?}", id),
63            name,
64            value,
65        }
66    }
67
68    pub fn set_text(id: NodeId, text: String) -> Self {
69        Self::SetText {
70            id: format!("{:?}", id),
71            text,
72        }
73    }
74
75    pub fn insert(parent_id: NodeId, id: NodeId, before_id: Option<NodeId>) -> Self {
76        Self::Insert {
77            parent_id: format!("{:?}", parent_id),
78            id: format!("{:?}", id),
79            before_id: before_id.map(|id| format!("{:?}", id)),
80        }
81    }
82
83    /// Insert a root node into the "root" container
84    pub fn insert_root(id: NodeId) -> Self {
85        Self::Insert {
86            parent_id: "root".to_string(),
87            id: format!("{:?}", id),
88            before_id: None,
89        }
90    }
91
92    pub fn move_node(parent_id: NodeId, id: NodeId, before_id: Option<NodeId>) -> Self {
93        Self::Move {
94            parent_id: format!("{:?}", parent_id),
95            id: format!("{:?}", id),
96            before_id: before_id.map(|id| format!("{:?}", id)),
97        }
98    }
99
100    pub fn remove(id: NodeId) -> Self {
101        Self::Remove {
102            id: format!("{:?}", id),
103        }
104    }
105
106    // Event handling methods removed - now done at renderer level
107}