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 std::rc::Rc;
33
34/// Setup helper that bundles NodeEditorController and automatic model updates.
35///
36/// This helper eliminates boilerplate by:
37/// - Managing the controller lifecycle
38/// - Calling your drag-commit closure automatically when a drag ends
39///
40/// The setup knows nothing about selection. A finished drag hands your closure
41/// the node that was dragged, and committing it — including moving whatever
42/// else is selected — is [`GraphLogic::commit_drag`], which reads `selected`
43/// off the model rows.
44///
45/// [`GraphLogic::commit_drag`]: crate::GraphLogic::commit_drag
46pub struct NodeEditorSetup<F>
47where
48 F: Fn(i32, f32, f32) + 'static,
49{
50 controller: Rc<NodeEditorController>,
51 on_drag_committed: Rc<F>,
52}
53
54impl<F> NodeEditorSetup<F>
55where
56 F: Fn(i32, f32, f32) + 'static,
57{
58 /// Create a new setup helper with a drag-commit callback.
59 ///
60 /// The callback receives `(dragged_node_id, delta_x, delta_y)` once, when a
61 /// node drag ends. This is the ONLY callback you need to provide - everything
62 /// else is handled internally.
63 pub fn new(on_drag_committed: F) -> Self {
64 Self {
65 controller: Rc::new(NodeEditorController::new()),
66 on_drag_committed: Rc::new(on_drag_committed),
67 }
68 }
69
70 /// Get the underlying controller for advanced operations.
71 pub fn controller(&self) -> &Rc<NodeEditorController> {
72 &self.controller
73 }
74
75 /// Callback for `NodeEditorInternalCallbacks.on_report_node_rect`.
76 pub fn report_node_rect(&self) -> impl Fn(i32, f32, f32, f32, f32) + 'static {
77 let ctrl = self.controller.clone();
78 move |id, x, y, w, h| {
79 ctrl.handle_node_rect(id, x, y, w, h);
80 }
81 }
82
83 /// Callback for `NodeEditorInternalCallbacks.on_report_pin_position`.
84 pub fn report_pin_position(&self) -> impl Fn(i32, i32, i32, f32, f32, bool) + 'static {
85 let ctrl = self.controller.clone();
86 move |pin_id, node_id, pin_type, x, y, hit_testable| {
87 ctrl.handle_pin_position_with_hit_testable(
88 pin_id,
89 node_id,
90 pin_type,
91 x,
92 y,
93 hit_testable,
94 );
95 }
96 }
97
98 /// Callback for retiring a node and all of its projected pins.
99 pub fn remove_node(&self) -> impl Fn(i32) + 'static {
100 let ctrl = self.controller.clone();
101 move |node_id| {
102 ctrl.remove_node(node_id);
103 }
104 }
105
106 /// Callback for retiring one projected pin.
107 pub fn remove_pin(&self) -> impl Fn(i32) + 'static {
108 let ctrl = self.controller.clone();
109 move |pin_id| {
110 ctrl.remove_pin(pin_id);
111 }
112 }
113
114 /// Callback for clearing all projected graph state.
115 pub fn reset_graph(&self) -> impl Fn() + 'static {
116 let ctrl = self.controller.clone();
117 move || ctrl.reset_graph()
118 }
119
120 /// Callback for `NodeEditorInternalCallbacks.on_start_node_drag`.
121 pub fn start_node_drag(&self) -> impl Fn(i32, bool, f32, f32) + 'static {
122 let ctrl = self.controller.clone();
123 move |node_id, _already_selected, _world_x, _world_y| {
124 ctrl.handle_node_drag_started(node_id);
125 }
126 }
127
128 /// Callback for a node drag cancelled before its normal end.
129 pub fn cancel_node_drag(&self) -> impl Fn() + 'static {
130 let ctrl = self.controller.clone();
131 move || ctrl.handle_node_drag_finished()
132 }
133
134 /// Callback for `NodeEditorInternalCallbacks.on_end_node_drag`.
135 ///
136 /// Calls your drag-commit closure once, with the node the user dragged.
137 pub fn end_node_drag(&self) -> impl Fn(i32, f32, f32) + 'static {
138 let ctrl = self.controller.clone();
139 let on_committed = self.on_drag_committed.clone();
140 move |node_id, delta_x, delta_y| {
141 ctrl.handle_node_drag_finished();
142 on_committed(node_id, delta_x, delta_y);
143 }
144 }
145}