Skip to main content

slint_node_editor/
setup.rs

1//! Simplified setup helpers for NodeEditor with globals architecture.
2//!
3//! The [`NodeEditorSetup`] provides automatic callback handling. You only need
4//! to provide a closure that commits a finished drag into your model.
5//!
6//! # Generated-UI integration
7//!
8//! This fragment uses the generated types and model from the
9//! [compiled quick start](https://github.com/tilladam/slint-node-editor/tree/main/smoke/downstream).
10//!
11//! ```text
12//! use slint_node_editor::{GraphLogic, NodeEditorSetup, wire_node_editor};
13//!
14//! slint::include_modules!();
15//!
16//! fn main() {
17//!     let window = MainWindow::new().unwrap();
18//!
19//!     let setup = NodeEditorSetup::new({
20//!         let nodes = nodes.clone();
21//!         move |dragged, delta_x, delta_y| {
22//!             GraphLogic::commit_drag(&nodes, dragged, delta_x, delta_y);
23//!         }
24//!     });
25//!
26//!     wire_node_editor!(window, setup);
27//!     window.run().unwrap();
28//! }
29//! ```
30
31use crate::controller::NodeEditorController;
32use crate::groups::Dragged;
33use std::cell::RefCell;
34use std::rc::Rc;
35
36type GroupDragCommit = Rc<dyn Fn(i32, f32, f32)>;
37
38/// Setup helper that bundles NodeEditorController and automatic model updates.
39///
40/// This helper eliminates boilerplate by:
41/// - Managing the controller lifecycle
42/// - Calling your drag-commit closure automatically when a drag ends
43///
44/// The setup knows nothing about selection. A finished drag hands your closure
45/// the node that was dragged, and committing it — including moving whatever
46/// else is selected — is [`GraphLogic::commit_drag`], which reads `selected`
47/// off the model rows.
48///
49/// [`GraphLogic::commit_drag`]: crate::GraphLogic::commit_drag
50pub struct NodeEditorSetup<F>
51where
52    F: Fn(i32, f32, f32) + 'static,
53{
54    controller: Rc<NodeEditorController>,
55    on_drag_committed: Rc<F>,
56    /// Set by [`Self::on_group_drag_committed`] (or `wire_groups!`). A host
57    /// without groups never sees a group drag, so this is optional.
58    on_group_drag_committed: Rc<RefCell<Option<GroupDragCommit>>>,
59}
60
61impl NodeEditorSetup<Box<dyn Fn(i32, f32, f32)>> {
62    /// Create a setup helper with one drag commit for both gestures.
63    ///
64    /// The closure receives what was dragged ([`Dragged::Node`] or
65    /// [`Dragged::Group`]) and the delta, once per finished drag. With
66    /// [`GroupLogic::commit_drag`](crate::groups::GroupLogic::commit_drag) it
67    /// is the whole movement policy for a host with groups:
68    ///
69    /// ```text
70    /// let setup = NodeEditorSetup::with_drag_commit(move |dragged, dx, dy| {
71    ///     GroupLogic::commit_drag(&nodes, &groups, dragged, dx, dy)
72    /// });
73    /// ```
74    pub fn with_drag_commit(on_committed: impl Fn(Dragged, f32, f32) + 'static) -> Self {
75        let on_committed = Rc::new(on_committed);
76        let setup = Self::new({
77            let on_committed = on_committed.clone();
78            Box::new(move |node_id, dx, dy| on_committed(Dragged::Node(node_id), dx, dy))
79                as Box<dyn Fn(i32, f32, f32)>
80        });
81        setup.on_group_drag_committed(move |group_id, dx, dy| {
82            on_committed(Dragged::Group(group_id), dx, dy)
83        });
84        setup
85    }
86}
87
88impl<F> NodeEditorSetup<F>
89where
90    F: Fn(i32, f32, f32) + 'static,
91{
92    /// Create a new setup helper with a drag-commit callback.
93    ///
94    /// The callback receives `(dragged_node_id, delta_x, delta_y)` once, when a
95    /// node drag ends. This is the ONLY callback you need to provide - everything
96    /// else is handled internally.
97    pub fn new(on_drag_committed: F) -> Self {
98        Self {
99            controller: Rc::new(NodeEditorController::new()),
100            on_drag_committed: Rc::new(on_drag_committed),
101            on_group_drag_committed: Rc::new(RefCell::new(None)),
102        }
103    }
104
105    /// Install the commit for a finished group sheet drag, when the node and
106    /// group commits are separate closures. [`Self::with_drag_commit`] is the
107    /// usual way to get both from one rule.
108    pub fn on_group_drag_committed(&self, on_committed: impl Fn(i32, f32, f32) + 'static) {
109        *self.on_group_drag_committed.borrow_mut() = Some(Rc::new(on_committed));
110    }
111
112    /// Get the underlying controller for advanced operations.
113    pub fn controller(&self) -> &Rc<NodeEditorController> {
114        &self.controller
115    }
116
117    /// Callback for `NodeEditorInternalCallbacks.on_report_node_rect`.
118    pub fn report_node_rect(&self) -> impl Fn(i32, f32, f32, f32, f32) + 'static {
119        let ctrl = self.controller.clone();
120        move |id, x, y, w, h| {
121            ctrl.handle_node_rect(id, x, y, w, h);
122        }
123    }
124
125    /// Callback for `NodeEditorInternalCallbacks.on_report_pin_position`.
126    pub fn report_pin_position(&self) -> impl Fn(i32, i32, i32, f32, f32, bool) + 'static {
127        let ctrl = self.controller.clone();
128        move |pin_id, node_id, pin_type, x, y, hit_testable| {
129            ctrl.handle_pin_position_with_hit_testable(
130                pin_id,
131                node_id,
132                pin_type,
133                x,
134                y,
135                hit_testable,
136            );
137        }
138    }
139
140    /// Callback for retiring a node and all of its projected pins.
141    pub fn remove_node(&self) -> impl Fn(i32) + 'static {
142        let ctrl = self.controller.clone();
143        move |node_id| {
144            ctrl.remove_node(node_id);
145        }
146    }
147
148    /// Callback for retiring one projected pin.
149    pub fn remove_pin(&self) -> impl Fn(i32) + 'static {
150        let ctrl = self.controller.clone();
151        move |pin_id| {
152            ctrl.remove_pin(pin_id);
153        }
154    }
155
156    /// Callback for clearing all projected graph state.
157    pub fn reset_graph(&self) -> impl Fn() + 'static {
158        let ctrl = self.controller.clone();
159        move || ctrl.reset_graph()
160    }
161
162    /// Callback for `NodeEditorInternalCallbacks.on_start_node_drag`.
163    pub fn start_node_drag(&self) -> impl Fn(i32, bool, f32, f32) + 'static {
164        let ctrl = self.controller.clone();
165        move |node_id, _already_selected, _world_x, _world_y| {
166            ctrl.handle_node_drag_started(node_id);
167        }
168    }
169
170    /// Callback for a node drag cancelled before its normal end.
171    pub fn cancel_node_drag(&self) -> impl Fn() + 'static {
172        let ctrl = self.controller.clone();
173        move || ctrl.handle_node_drag_finished()
174    }
175
176    /// Callback for `NodeEditorInternalCallbacks.on_end_node_drag`.
177    ///
178    /// Calls your drag-commit closure once, with the node the user dragged.
179    pub fn end_node_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
180        let ctrl = self.controller.clone();
181        let on_committed = self.on_drag_committed.clone();
182        move |node_id, delta_x, delta_y| {
183            ctrl.handle_node_drag_finished();
184            on_committed(node_id, delta_x, delta_y);
185        }
186    }
187
188    /// Callback for `NodeEditorInternalCallbacks.on_report_group_rect`.
189    pub fn report_group_rect(&self) -> impl Fn(i32, f32, f32, f32, f32) + 'static {
190        let ctrl = self.controller.clone();
191        move |id, x, y, w, h| {
192            ctrl.handle_group_rect(id, x, y, w, h);
193        }
194    }
195
196    /// Callback for retiring a group sheet's projected rectangle.
197    pub fn remove_group(&self) -> impl Fn(i32) + 'static {
198        let ctrl = self.controller.clone();
199        move |group_id| {
200            ctrl.remove_group(group_id);
201        }
202    }
203
204    /// Callback for `NodeEditorInternalCallbacks.on_set_node_hidden`.
205    pub fn set_node_hidden(&self) -> impl Fn(i32, bool) + 'static {
206        let ctrl = self.controller.clone();
207        move |node_id, hidden| {
208            ctrl.set_node_hidden(node_id, hidden);
209        }
210    }
211
212    /// Callback for `NodeEditorInternalCallbacks.on_start_group_drag`.
213    pub fn start_group_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
214        let ctrl = self.controller.clone();
215        move |group_id, _world_x, _world_y| {
216            ctrl.handle_group_drag_started(group_id);
217        }
218    }
219
220    /// Callback for `NodeEditorInternalCallbacks.on_end_group_drag`.
221    ///
222    /// Calls the group drag commit from [`Self::with_drag_commit`] or
223    /// [`Self::on_group_drag_committed`], if any, once per finished sheet drag.
224    pub fn end_group_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
225        let ctrl = self.controller.clone();
226        let on_committed = self.on_group_drag_committed.clone();
227        move |group_id, delta_x, delta_y| {
228            ctrl.handle_node_drag_finished();
229            let commit = on_committed.borrow().clone();
230            if let Some(commit) = commit {
231                commit(group_id, delta_x, delta_y);
232            }
233        }
234    }
235}