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