pub mod nodeeditor {
include!(concat!(env!("OUT_DIR"), "/node-editor.rs"));
}
pub mod controller;
pub mod graph;
pub mod grid;
pub mod groups;
pub mod hit_test;
#[cfg(feature = "layout")]
pub mod layout;
pub mod links;
pub mod path;
pub mod selection;
pub mod setup;
pub mod state;
pub mod tracking;
pub use nodeeditor::{
BoxSelectionModifier, GroupData, LinkCreationState, LinkData, LinkPath, MinimapNode,
MinimapPosition,
};
pub use grid::generate_grid_commands;
pub use hit_test::{
distance_to_polyline, find_link_at, find_link_route_at, find_pin_at, links_in_selection_box,
nodes_in_selection_box, BezierLinkRoute, LinkGeometry, LinkRoute, NodeGeometry, PinGeometry,
PolylineLinkRoute, SimpleLinkGeometry, SimpleNodeGeometry,
};
pub use path::{generate_bezier_path, generate_partial_bezier_path};
pub use state::{GeometryCache, StoredPin};
pub use controller::NodeEditorController;
pub use graph::{
validate_and_normalize_link,
BasicLinkValidator,
CompositeValidator,
GraphLogic,
LinkModel,
LinkValidator,
MovableNode,
NoDuplicatesValidator,
NormalizedLink,
SimpleLink,
ValidationError,
ValidationResult,
};
pub use groups::{Bounds, Dragged, GroupLogic, GroupMember, GroupModel};
#[cfg(feature = "layout")]
pub use layout::{
sugiyama_layout, sugiyama_layout_from_cache, Direction, NodePosition, RankAlignment,
SugiyamaConfig,
};
pub use links::LinkManager;
pub use setup::NodeEditorSetup;
pub use tracking::GeometryTracker;
#[macro_export]
macro_rules! wire_node_editor {
($window:expr, $setup:expr) => {{
let gc = $window.global::<NodeEditorInternalCallbacks>();
gc.on_report_node_rect($setup.report_node_rect());
gc.on_report_pin_position($setup.report_pin_position());
gc.on_remove_node_from_cache($setup.remove_node());
gc.on_remove_pin_from_cache($setup.remove_pin());
gc.on_reset_graph_cache($setup.reset_graph());
gc.on_start_node_drag($setup.start_node_drag());
gc.on_cancel_node_drag($setup.cancel_node_drag());
gc.on_end_node_drag($setup.end_node_drag());
gc.on_report_group_rect($setup.report_group_rect());
gc.on_remove_group_from_cache($setup.remove_group());
gc.on_set_node_hidden($setup.set_node_hidden());
gc.on_start_group_drag($setup.start_group_drag());
gc.on_end_group_drag($setup.end_group_drag());
let w = $window.as_weak();
gc.on_take_editor_focus(move || {
if let Some(w) = w.upgrade() {
w.invoke_focus_editor();
}
});
let computations = $window.global::<NodeEditorComputations>();
computations.on_compute_link_path($setup.controller().compute_link_path_callback(
|commands, x, y, width, height| LinkPath {
commands: commands.into(),
x,
y,
width,
height,
},
));
let ctrl = $setup.controller().clone();
computations
.on_compute_pin_at(move |x, y, radius| ctrl.cache().borrow().find_pin_at(x, y, radius));
computations.on_compute_link_preview_path(
|start_x, start_y, end_x, end_y, zoom, bezier_offset| {
$crate::generate_bezier_path(start_x, start_y, end_x, end_y, zoom, bezier_offset)
.into()
},
);
$setup
.controller()
.set_grid_spacing(computations.get_grid_spacing());
$setup
.controller()
.set_bezier_offset(computations.get_bezier_min_offset());
let ctrl = $setup.controller().clone();
computations.on_configuration_changed(move |grid_spacing, bezier_offset| {
ctrl.set_grid_spacing(grid_spacing);
ctrl.set_bezier_offset(bezier_offset);
});
let ctrl = $setup.controller().clone();
computations.on_viewport_changed(move |zoom, pan_x, pan_y| {
ctrl.set_viewport(zoom, pan_x, pan_y);
});
let ctrl = $setup.controller().clone();
let w = $window.as_weak();
computations.on_request_grid_update(move || {
if let Some(w) = w.upgrade() {
w.set_grid_commands(ctrl.generate_current_grid(w.get_width_(), w.get_height_()));
}
});
let ctrl = $setup.controller().clone();
let w = $window.as_weak();
if let Some(w) = w.upgrade() {
w.set_grid_commands(ctrl.generate_current_grid(w.get_width_(), w.get_height_()));
}
}};
}
#[macro_export]
macro_rules! wire_selection {
(@click $window:expr, $on:ident, $target:expr, [$($model:expr),*]) => {{
$window.$on({
let target = $target.clone();
let clearers: Vec<Box<dyn Fn()>> = vec![$({
let model = $model.clone();
Box::new(move || $crate::selection::clear_selection(&*model, |r| &mut r.selected))
as Box<dyn Fn()>
}),*];
move |id, shift| {
if !shift {
for clear in &clearers {
clear();
}
}
$crate::selection::apply_click(&*target, |r| r.id, |r| &mut r.selected, id, shift)
}
});
}};
(@clear $window:expr, [$($model:expr),*]) => {{
$window.on_selection_cleared({
let clearers: Vec<Box<dyn Fn()>> = vec![$({
let model = $model.clone();
Box::new(move || $crate::selection::clear_selection(&*model, |r| &mut r.selected))
as Box<dyn Fn()>
}),*];
move || {
for clear in &clearers {
clear();
}
}
});
}};
(@box $window:expr, $setup:expr, $links:expr, [$(($model:expr, $hits:expr)),*]) => {{
$window.on_box_selection_committed({
let ctrl = $setup.controller().clone();
let links = $links.clone();
let kinds: Vec<($crate::BoxApply, $crate::BoxHits)> = vec![$({
let model = $model.clone();
(
$crate::box_apply(move |hits, shift| {
$crate::selection::apply_box(&*model, |r| r.id, |r| &mut r.selected, hits, shift)
}),
$crate::box_hits($hits),
)
}),*];
move |x, y, w, h, shift| {
let rows: Vec<(i32, i32, i32)> = (0..slint::Model::row_count(&*links))
.filter_map(|i| slint::Model::row_data(&*links, i))
.map(|l| (l.id, l.start_pin_id, l.end_pin_id))
.collect();
let hits: Vec<Vec<i32>> = {
let cache = ctrl.cache();
let cache = cache.borrow();
kinds.iter().map(|(_, find)| find(&cache, x, y, w, h, &rows)).collect()
};
for ((apply, _), hits) in kinds.iter().zip(hits) {
apply(hits, shift);
}
}
});
}};
($window:expr, $setup:expr, $nodes:expr) => {{
$crate::wire_selection!(@click $window, on_node_selected, $nodes, []);
$crate::wire_selection!(@clear $window, [$nodes]);
$window.on_box_selection_committed({
let nodes = $nodes.clone();
let ctrl = $setup.controller().clone();
move |x, y, w, h, shift| {
let hits = ctrl.cache().borrow().nodes_in_selection_box(x, y, w, h);
$crate::selection::apply_box(&nodes, |n| n.id, |n| &mut n.selected, hits, shift)
}
});
}};
($window:expr, $setup:expr, $nodes:expr, $links:expr) => {{
$crate::wire_selection!(@click $window, on_node_selected, $nodes, [$links]);
$crate::wire_selection!(@click $window, on_select_link, $links, [$nodes]);
$crate::wire_selection!(@clear $window, [$nodes, $links]);
$crate::wire_selection!(@box $window, $setup, $links, [
($nodes, |cache, x, y, w, h, _rows| cache.nodes_in_selection_box(x, y, w, h)),
($links, |cache, x, y, w, h, rows| cache.links_in_selection_box(x, y, w, h, rows.iter().copied()))
]);
}};
($window:expr, $setup:expr, $nodes:expr, $links:expr, $groups:expr) => {{
$crate::wire_selection!(@click $window, on_node_selected, $nodes, [$links, $groups]);
$crate::wire_selection!(@click $window, on_select_link, $links, [$nodes, $groups]);
$crate::wire_selection!(@click $window, on_group_selected, $groups, [$nodes, $links]);
$crate::wire_selection!(@clear $window, [$nodes, $links, $groups]);
$crate::wire_selection!(@box $window, $setup, $links, [
($nodes, |cache, x, y, w, h, _rows| cache.nodes_in_selection_box(x, y, w, h)),
($links, |cache, x, y, w, h, rows| cache.links_in_selection_box(x, y, w, h, rows.iter().copied())),
($groups, |cache, x, y, w, h, _rows| cache.groups_in_selection_box(x, y, w, h))
]);
}};
}
#[doc(hidden)]
pub type BoxApply = Box<dyn Fn(Vec<i32>, bool)>;
#[doc(hidden)]
pub type BoxHits = Box<dyn Fn(&GeometryCache, f32, f32, f32, f32, &[(i32, i32, i32)]) -> Vec<i32>>;
#[doc(hidden)]
pub fn box_apply(f: impl Fn(Vec<i32>, bool) + 'static) -> BoxApply {
Box::new(f)
}
#[doc(hidden)]
pub fn box_hits(
f: impl Fn(&GeometryCache, f32, f32, f32, f32, &[(i32, i32, i32)]) -> Vec<i32> + 'static,
) -> BoxHits {
Box::new(f)
}
#[macro_export]
macro_rules! wire_groups {
($window:expr, $groups:expr, $nodes:expr) => {{
let computations = $window.global::<NodeEditorComputations>();
computations.on_group_in_drag({
let groups = $groups.clone();
move |group_id, dragged_node_id, dragged_group_id| {
$crate::groups::GroupLogic::group_moves(
&*groups,
group_id,
$crate::groups::Dragged::from_ids(dragged_node_id, dragged_group_id),
)
}
});
computations.on_group_collapsed({
let groups = $groups.clone();
move |group_id, _version| $crate::groups::GroupLogic::is_collapsed(&*groups, group_id)
});
$window
.global::<NodeEditorEvents>()
.on_group_collapse_requested({
let groups = $groups.clone();
let nodes = $nodes.clone();
move |group_id, collapsed| {
$crate::groups::GroupLogic::set_collapsed(
&groups,
&nodes,
group_id,
collapsed,
|n| &mut n.selected,
);
}
});
}};
}