leptos-node-graph 0.2.2

A reactive node graph editor component for Leptos
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};

use leptos::prelude::*;

use crate::registry::{ConnectionEntry, EditorRegistry, NodeEntry, PortEntry};
use crate::subway::{self, SubwayConnection, SubwayOptions, SubwayRect, SubwayRoutingStats};
use crate::types::*;
use crate::utils;

/// Visual endpoints for ports hidden inside collapsed compound rows.
/// Connections retain their original port ids for selection, deletion and editing.
#[derive(Clone)]
pub struct PortDisplayAliases<P: PortId>(pub Signal<HashMap<P, P>>);

fn display_ports<N: NodeId, P: PortId, T: PortType>(
    mut ports: HashMap<P, PortEntry<N, P, T>>,
    aliases: &HashMap<P, P>,
) -> HashMap<P, PortEntry<N, P, T>> {
    for (port, parent) in aliases {
        if let Some(mut parent) = ports.get(parent).cloned() {
            parent.id = port.clone();
            ports.insert(port.clone(), parent);
        }
    }
    ports
}

/// Corner rounding applied to routed polylines when drawing them.
const SUBWAY_CORNER_RADIUS: f64 = 6.0;
/// Frozen routes within this distance of a changed node are included in its incremental solve.
const SUBWAY_INCREMENTAL_PROXIMITY_PAD: f64 = 48.0;

/// How connections are routed between ports.
///
/// Provided reactively by the consumer as an `RwSignal<RoutingMode>` in
/// context. When no context is present the renderer falls back to
/// [`RoutingMode::Orthogonal`] so existing embeds are unchanged.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum RoutingMode {
    /// Right-angle "subway map" wiring (default).
    #[default]
    Orthogonal,
    /// Classic bezier curves.
    Bezier,
}

impl RoutingMode {
    /// Build the SVG path `d` string for this routing mode.
    fn path(self, start: Position, end: Position) -> String {
        match self {
            RoutingMode::Bezier => utils::bezier_path(start, end),
            RoutingMode::Orthogonal => utils::orthogonal_path(start, end),
        }
    }
}

/// The deterministic, pure input to one subway solve.
///
/// Keeping this value separately from the result lets reactive notifications
/// which did not change world geometry skip the router entirely.
#[derive(Clone, Debug, PartialEq)]
struct SubwayBatch<N, C> {
    rect_ids: Vec<N>,
    rects: Vec<SubwayRect>,
    connections: Vec<SubwayBatchConnection<N, C>>,
}

#[derive(Clone, Debug, PartialEq)]
struct SubwayBatchConnection<N, C> {
    id: C,
    start_node: N,
    end_node: N,
    input: SubwayConnection,
}

impl<N, C> SubwayBatch<N, C>
where
    N: NodeId,
    C: ConnectionId,
{
    fn subset(&self, ids: &HashSet<C>) -> Self {
        Self {
            rect_ids: self.rect_ids.clone(),
            rects: self.rects.clone(),
            connections: self
                .connections
                .iter()
                .filter(|connection| ids.contains(&connection.id))
                .cloned()
                .collect(),
        }
    }

    /// Return the nodes responsible for a geometry-only change. Node/connection
    /// population and endpoint ownership changes are structural and require a
    /// full solve; positions and rects can be handled incrementally.
    fn changed_nodes_since(&self, previous: &Self) -> Option<HashSet<N>> {
        if self.rect_ids != previous.rect_ids
            || self.connections.len() != previous.connections.len()
        {
            return None;
        }

        let mut changed = HashSet::new();
        for ((id, current), old) in self.rect_ids.iter().zip(&self.rects).zip(&previous.rects) {
            if current != old {
                changed.insert(id.clone());
            }
        }

        for (current, old) in self.connections.iter().zip(&previous.connections) {
            if current.id != old.id
                || current.start_node != old.start_node
                || current.end_node != old.end_node
                || current.input.start_rect != old.input.start_rect
                || current.input.end_rect != old.input.end_rect
            {
                return None;
            }
            if current.input.start != old.input.start {
                changed.insert(current.start_node.clone());
            }
            if current.input.end != old.input.end {
                changed.insert(current.end_node.clone());
            }
        }

        Some(changed)
    }
}

/// Collect routing geometry in stable debug-id order. Stable node order is as
/// important as stable connection order because endpoint rects are indices.
fn subway_batch<N, P, C, T>(
    conns: &HashMap<C, ConnectionEntry<P, C>>,
    ports: &HashMap<P, PortEntry<N, P, T>>,
    nodes: &HashMap<N, NodeEntry<N>>,
) -> SubwayBatch<N, C>
where
    N: NodeId,
    P: PortId,
    C: ConnectionId,
    T: PortType,
{
    let mut ordered_nodes: Vec<(&N, &NodeEntry<N>)> = nodes.iter().collect();
    ordered_nodes.sort_by_cached_key(|(id, _)| format!("{id:?}"));

    let mut rect_ids = Vec::with_capacity(ordered_nodes.len());
    let mut rects = Vec::with_capacity(ordered_nodes.len());
    let mut rect_of: HashMap<&N, usize> = HashMap::with_capacity(ordered_nodes.len());
    for (id, node) in ordered_nodes {
        rect_of.insert(id, rects.len());
        rect_ids.push(id.clone());
        rects.push(SubwayRect {
            x: node.position.x,
            y: node.position.y,
            w: node.size.width,
            h: node.size.height,
        });
    }

    let mut ordered: Vec<(&C, &ConnectionEntry<P, C>)> = conns.iter().collect();
    ordered.sort_by_cached_key(|(id, _)| format!("{id:?}"));

    let mut connections = Vec::with_capacity(ordered.len());
    for (id, conn) in ordered {
        let (Some(src), Some(tgt)) = (ports.get(&conn.source), ports.get(&conn.target)) else {
            continue;
        };
        connections.push(SubwayBatchConnection {
            id: id.clone(),
            start_node: src.node_id.clone(),
            end_node: tgt.node_id.clone(),
            input: SubwayConnection {
                start: src.position,
                end: tgt.position,
                start_rect: rect_of.get(&src.node_id).copied(),
                end_rect: rect_of.get(&tgt.node_id).copied(),
            },
        });
    }

    SubwayBatch {
        rect_ids,
        rects,
        connections,
    }
}

fn solve_subway_batch<N, C>(
    batch: &SubwayBatch<N, C>,
) -> (HashMap<C, Vec<Position>>, SubwayRoutingStats)
where
    N: NodeId,
    C: ConnectionId,
{
    let inputs: Vec<_> = batch
        .connections
        .iter()
        .map(|connection| connection.input)
        .collect();
    let (routes, stats) =
        subway::compute_subway_routes_with_stats(&batch.rects, &inputs, &SubwayOptions::default());
    let routes = batch
        .connections
        .iter()
        .map(|connection| connection.id.clone())
        .zip(routes)
        .collect();
    (routes, stats)
}

/// Choose the small batch needed for a node geometry change: every incident
/// wire plus any frozen route close enough that the moving obstacle affects it.
fn incremental_route_ids<N, C>(
    batch: &SubwayBatch<N, C>,
    previous: &SubwayBatch<N, C>,
    changed_nodes: &HashSet<N>,
    cached_routes: &HashMap<C, Vec<Position>>,
) -> HashSet<C>
where
    N: NodeId,
    C: ConnectionId,
{
    // A frozen route may be close only to where an obstacle used to be. Test
    // both sides of the move so large jumps also straighten old detours.
    let affected_rects: Vec<_> = batch
        .rect_ids
        .iter()
        .zip(&batch.rects)
        .chain(previous.rect_ids.iter().zip(&previous.rects))
        .filter_map(|(id, rect)| changed_nodes.contains(id).then_some(*rect))
        .collect();

    batch
        .connections
        .iter()
        .filter(|connection| {
            changed_nodes.contains(&connection.start_node)
                || changed_nodes.contains(&connection.end_node)
                || cached_routes.get(&connection.id).is_some_and(|route| {
                    affected_rects.iter().any(|rect| {
                        subway::route_intersects_rect(
                            route,
                            *rect,
                            SUBWAY_INCREMENTAL_PROXIMITY_PAD,
                        )
                    })
                })
        })
        .map(|connection| connection.id.clone())
        .collect()
}

#[derive(Debug, PartialEq)]
enum SubwaySolve<C: ConnectionId> {
    Cached,
    Full,
    Partial(HashSet<C>),
}

fn plan_subway_solve<N, C>(
    batch: &SubwayBatch<N, C>,
    previous: Option<&SubwayBatch<N, C>>,
    cached_routes: &HashMap<C, Vec<Position>>,
) -> SubwaySolve<C>
where
    N: NodeId,
    C: ConnectionId,
{
    let Some(previous) = previous else {
        return SubwaySolve::Full;
    };
    if batch == previous {
        return SubwaySolve::Cached;
    }
    match batch.changed_nodes_since(previous) {
        Some(changed_nodes) => SubwaySolve::Partial(incremental_route_ids(
            batch,
            previous,
            &changed_nodes,
            cached_routes,
        )),
        None => SubwaySolve::Full,
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct ConnectionEndpoints {
    source: Option<Position>,
    target: Option<Position>,
}

#[derive(Clone)]
struct ConnectionSignals {
    endpoints: ArcRwSignal<ConnectionEndpoints>,
    route: ArcRwSignal<Option<Vec<Position>>>,
}

struct SubwayCache<N, C> {
    geometry: Option<SubwayBatch<N, C>>,
    routes: HashMap<C, Vec<Position>>,
}

impl<N, C> Default for SubwayCache<N, C> {
    fn default() -> Self {
        Self {
            geometry: None,
            routes: HashMap::new(),
        }
    }
}

/// Style configuration for connections. Consumer provides this to customize appearance.
#[derive(Clone, Debug)]
pub struct ConnectionStyle {
    pub stroke: String,
    pub stroke_selected: String,
    pub stroke_draft: String,
    pub stroke_width: f64,
    pub stroke_width_selected: f64,
}

impl Default for ConnectionStyle {
    fn default() -> Self {
        Self {
            stroke: "#71717a".into(),
            stroke_selected: "#ef4444".into(),
            stroke_draft: "#22d3ee".into(),
            stroke_width: 2.0,
            stroke_width_selected: 3.0,
        }
    }
}

#[component]
pub fn ConnectionRenderer<N, P, C, T>(
    #[prop(optional)] _marker: PhantomData<(N, P, C, T)>,
) -> impl IntoView
where
    N: NodeId,
    P: PortId,
    C: ConnectionId,
    T: PortType,
{
    let registry = expect_context::<EditorRegistry<N, P, C, T>>();
    let style_config = use_context::<ConnectionStyle>().unwrap_or_default();
    let aliases = use_context::<PortDisplayAliases<P>>();
    let port_registry = registry.clone();
    let routing_aliases = aliases.clone();
    let rendered_ports = Signal::derive(move || {
        display_ports(
            port_registry.ports.get(),
            &aliases
                .as_ref()
                .map(|aliases| aliases.0.get())
                .unwrap_or_default(),
        )
    });
    // Reactive routing mode; absent context defaults to Orthogonal (subway).
    let routing_mode = use_context::<RwSignal<RoutingMode>>();
    let connection_signals = StoredValue::new(HashMap::<C, ConnectionSignals>::new());
    let subway_cache = StoredValue::new(SubwayCache::<N, C>::default());
    let last_subway_stats = StoredValue::new(None::<(SubwayRoutingStats, usize, usize)>);

    // Collapse the coarse registry maps into per-connection signals. A port map
    // update still costs one cheap walk, but only paths whose endpoints really
    // changed receive a reactive notification.
    {
        let reg = registry.clone();
        let active = Arc::new(AtomicBool::new(true));
        let pending = Arc::new(AtomicBool::new(false));
        let active_cleanup = active.clone();
        on_cleanup(move || active_cleanup.store(false, Ordering::Relaxed));
        let update_endpoints = Arc::new(move || {
            let conns = reg.connections.get_untracked();
            let ports = rendered_ports.get_untracked();
            connection_signals.update_value(|signals| {
                signals.retain(|id, _| conns.contains_key(id));
                for (id, connection) in &conns {
                    let endpoints = ConnectionEndpoints {
                        source: ports.get(&connection.source).map(|port| port.position),
                        target: ports.get(&connection.target).map(|port| port.position),
                    };
                    let signals = signals
                        .entry(id.clone())
                        .or_insert_with(|| ConnectionSignals {
                            endpoints: ArcRwSignal::new(endpoints),
                            route: ArcRwSignal::new(
                                subway_cache.with_value(|cache| cache.routes.get(id).cloned()),
                            ),
                        });
                    if signals.endpoints.get_untracked() != endpoints {
                        signals.endpoints.set(endpoints);
                    }
                }
            });
        });
        let reg = registry.clone();
        let aliases = routing_aliases.clone();
        Effect::new(move |_| {
            reg.connections.track();
            reg.ports.track();
            if let Some(aliases) = aliases.as_ref() {
                aliases.0.track();
            }
            if pending.swap(true, Ordering::Relaxed) {
                return;
            }
            let update_endpoints = update_endpoints.clone();
            let pending = pending.clone();
            let active = active.clone();
            crate::raf::request_animation_frame(move || {
                pending.store(false, Ordering::Relaxed);
                if active.load(Ordering::Relaxed) {
                    update_endpoints();
                }
            });
        });
    }

    // Solve routes separately from rendering. The cache filters identical
    // geometry, and node-only changes merge a small partial solve into frozen routes.
    {
        let reg = registry.clone();
        let active = Arc::new(AtomicBool::new(true));
        let pending = Arc::new(AtomicBool::new(false));
        let active_cleanup = active.clone();
        on_cleanup(move || active_cleanup.store(false, Ordering::Relaxed));
        let solve = Arc::new(move || {
            let mode = routing_mode
                .map(|mode| mode.get_untracked())
                .unwrap_or_default();
            let conns = reg.connections.get_untracked();
            let ports = rendered_ports.get_untracked();
            let nodes = reg.nodes.get_untracked();
            let ports_at_node_origin = ports
                .values()
                .filter(|port| {
                    nodes
                        .get(&port.node_id)
                        .is_some_and(|node| utils::distance(port.position, node.position) < 0.01)
                })
                .count();

            if mode == RoutingMode::Bezier {
                subway_cache.update_value(|cache| *cache = SubwayCache::default());
                connection_signals.with_value(|signals| {
                    for signals in signals.values() {
                        if signals.route.get_untracked().is_some() {
                            signals.route.set(None);
                        }
                    }
                });
                log_subway_stats(
                    &last_subway_stats,
                    SubwayRoutingStats::default(),
                    conns.len(),
                    ports_at_node_origin,
                    ports.len(),
                );
                return;
            }

            let batch = subway_batch(&conns, &ports, &nodes);
            let solve = subway_cache.with_value(|cache| {
                plan_subway_solve(&batch, cache.geometry.as_ref(), &cache.routes)
            });

            let stats = match solve {
                SubwaySolve::Cached => return,
                SubwaySolve::Full => {
                    let (routes, stats) = solve_subway_batch(&batch);
                    subway_cache.update_value(|cache| {
                        cache.geometry = Some(batch.clone());
                        cache.routes = routes.clone();
                    });
                    connection_signals.with_value(|signals| {
                        for (id, signals) in signals {
                            let route = routes.get(id).cloned();
                            if signals.route.get_untracked() != route {
                                signals.route.set(route);
                            }
                        }
                    });
                    stats
                }
                SubwaySolve::Partial(ids) => {
                    if ids.is_empty() {
                        subway_cache.update_value(|cache| {
                            cache.geometry = Some(batch);
                        });
                        return;
                    }
                    let partial_batch = batch.subset(&ids);
                    let (routes, stats) = solve_subway_batch(&partial_batch);
                    subway_cache.update_value(|cache| {
                        cache.geometry = Some(batch);
                        cache.routes.extend(routes.clone());
                    });
                    connection_signals.with_value(|signals| {
                        for (id, route) in routes {
                            if let Some(signals) = signals.get(&id)
                                && signals.route.get_untracked().as_ref() != Some(&route)
                            {
                                signals.route.set(Some(route));
                            }
                        }
                    });
                    stats
                }
            };

            log_subway_stats(
                &last_subway_stats,
                stats,
                conns.len(),
                ports_at_node_origin,
                ports.len(),
            );
        });
        let reg = registry.clone();
        Effect::new(move |_| {
            // Track invalidations without cloning maps or solving during layout.
            reg.connections.track();
            reg.ports.track();
            reg.nodes.track();
            if let Some(aliases) = routing_aliases.as_ref() {
                aliases.0.track();
            }
            if let Some(mode) = routing_mode {
                mode.track();
            }
            if pending.swap(true, Ordering::Relaxed) {
                return;
            }
            let solve = solve.clone();
            let pending = pending.clone();
            let active = active.clone();
            // RAF callbacks run before paint. The second frame gives newly
            // mounted nodes a paint before routing, and batches their measurements.
            crate::raf::request_animation_frame(move || {
                crate::raf::request_animation_frame(move || {
                    pending.store(false, Ordering::Relaxed);
                    if active.load(Ordering::Relaxed) {
                        solve();
                    }
                });
            });
        });
    }

    let reg_for_each = registry.clone();
    let reg_for_children = registry.clone();
    let sc_for_each = style_config.clone();
    let connections_view = view! {
        <For
            each=move || {
                let conns = reg_for_each.connections.get();
                let ports = rendered_ports.get_untracked();
                let mut ids: Vec<_> = conns.keys().cloned().collect();
                ids.sort_by_cached_key(|id| format!("{id:?}"));
                let mut items = Vec::with_capacity(ids.len());
                connection_signals.update_value(|signals| {
                    for id in ids {
                        let endpoints = conns.get(&id).map_or_else(
                            ConnectionEndpoints::default,
                            |connection| ConnectionEndpoints {
                                source: ports.get(&connection.source).map(|port| port.position),
                                target: ports.get(&connection.target).map(|port| port.position),
                            },
                        );
                        let signals = signals
                            .entry(id.clone())
                            .or_insert_with(|| ConnectionSignals {
                                endpoints: ArcRwSignal::new(endpoints),
                                route: ArcRwSignal::new(
                                    subway_cache.with_value(|cache| cache.routes.get(&id).cloned()),
                                ),
                            })
                            .clone();
                        items.push((id, signals));
                    }
                });
                items
            }
            key=|(id, _)| id.clone()
            children=move |(conn_id, signals)| {
                let endpoints_d = signals.endpoints.clone();
                let route_d = signals.route.clone();
                let normal_d = move || {
                    let endpoints = endpoints_d.get();
                    match (endpoints.source, endpoints.target) {
                        (Some(source), Some(target)) => {
                            let mode = routing_mode.map(|mode| mode.get()).unwrap_or_default();
                            match route_d.get() {
                                Some(route) if mode == RoutingMode::Orthogonal && route.len() >= 2 => {
                                    utils::rounded_polyline_path(&route, SUBWAY_CORNER_RADIUS)
                                }
                                _ => mode.path(source, target),
                            }
                        }
                        _ => String::new(),
                    }
                };

                let endpoints_normal_style = signals.endpoints.clone();
                let reg_normal_style = reg_for_children.clone();
                let normal_style_id = conn_id.clone();
                let normal_sc = sc_for_each.clone();
                let normal_style = move || {
                    let selected = reg_normal_style
                        .selected_connections
                        .with(|selected| selected.contains(&normal_style_id));
                    let (stroke, width) = if selected {
                        (&normal_sc.stroke_selected, normal_sc.stroke_width_selected)
                    } else {
                        (&normal_sc.stroke, normal_sc.stroke_width)
                    };
                    let display = if matches!(
                        endpoints_normal_style.get(),
                        ConnectionEndpoints { source: Some(_), target: Some(_) }
                    ) {
                        ""
                    } else {
                        "display: none;"
                    };
                    format!(
                        "{display} pointer-events: stroke; cursor: pointer; stroke: {stroke}; stroke-width: {width};"
                    )
                };

                let endpoints_data = signals.endpoints.clone();
                let reg_click = reg_for_children.clone();
                let click_id = conn_id.clone();

                let endpoints_stub_d = signals.endpoints.clone();
                let stub_d = move || dangling_geometry(endpoints_stub_d.get()).map_or_else(
                    String::new,
                    |(start, finish, _, _, _)| {
                        routing_mode
                            .map(|mode| mode.get())
                            .unwrap_or_default()
                            .path(start, finish)
                    },
                );
                let endpoints_stub_style = signals.endpoints.clone();
                let reg_stub_style = reg_for_children.clone();
                let stub_style_id = conn_id.clone();
                let stub_sc = sc_for_each.clone();
                let stub_style = move || {
                    let selected = reg_stub_style
                        .selected_connections
                        .with(|selected| selected.contains(&stub_style_id));
                    let (stroke, width) = if selected {
                        (&stub_sc.stroke_selected, stub_sc.stroke_width_selected)
                    } else {
                        (&stub_sc.stroke, stub_sc.stroke_width)
                    };
                    let display = if dangling_geometry(endpoints_stub_style.get()).is_some() {
                        ""
                    } else {
                        "display: none;"
                    };
                    format!(
                        "{display} pointer-events: none; stroke: {stroke}; stroke-width: {width}; stroke-dasharray: 4 3; opacity: 0.5;"
                    )
                };

                let endpoints_q_x = signals.endpoints.clone();
                let endpoints_q_y = signals.endpoints.clone();
                let endpoints_anchor = signals.endpoints.clone();
                let endpoints_text_style = signals.endpoints.clone();
                let reg_text_style = reg_for_children.clone();
                let text_style_id = conn_id.clone();
                let text_sc = sc_for_each.clone();

                view! {
                    <path
                        d=normal_d
                        fill="none"
                        style=normal_style
                        data-connection=move || matches!(
                            endpoints_data.get(),
                            ConnectionEndpoints { source: Some(_), target: Some(_) }
                        ).then_some("")
                        on:mousedown=move |ev: web_sys::MouseEvent| {
                            ev.stop_propagation();
                            if ev.shift_key() {
                                reg_click.selected_connections.update(|selected| {
                                    if !selected.remove(&click_id) {
                                        selected.insert(click_id.clone());
                                    }
                                });
                            } else {
                                reg_click.select_connection(click_id.clone());
                            }
                        }
                    />
                    <path
                        d=stub_d
                        fill="none"
                        style=stub_style
                        data-connection-dangling=move || {
                            dangling_geometry(signals.endpoints.get()).is_some().then_some("")
                        }
                    />
                    <text
                        x=move || dangling_geometry(endpoints_q_x.get()).map_or(0.0, |(_, _, x, _, _)| x)
                        y=move || dangling_geometry(endpoints_q_y.get()).map_or(0.0, |(_, _, _, y, _)| y)
                        style=move || {
                            let selected = reg_text_style
                                .selected_connections
                                .with(|selected| selected.contains(&text_style_id));
                            let stroke = if selected {
                                &text_sc.stroke_selected
                            } else {
                                &text_sc.stroke
                            };
                            let display = if dangling_geometry(endpoints_text_style.get()).is_some() {
                                ""
                            } else {
                                "display: none;"
                            };
                            let anchor = dangling_geometry(endpoints_anchor.get())
                                .map_or("start", |(_, _, _, _, anchor)| anchor);
                            format!(
                                "{display} font-size: 10px; fill: {stroke}; opacity: 0.5; font-weight: 600; pointer-events: none; text-anchor: {anchor};"
                            )
                        }
                    >
                        "?"
                    </text>
                }
            }
        />
    };

    let reg_draft = registry.clone();
    let draft_sc = style_config.clone();
    let draft_view = move || {
        let mode = routing_mode.map(|mode| mode.get()).unwrap_or_default();
        let draft = reg_draft.draft_connection.get();
        let sc = draft_sc.clone();
        draft.map(|draft| {
            // When dragging from an input, swap so the curve flows left→right
            let (start, end) = if draft.origin_direction == PortDirection::Input {
                (draft.current_end, draft.source_position)
            } else {
                (draft.source_position, draft.current_end)
            };
            let path_d = mode.path(start, end);
            let style = format!(
                "pointer-events: none; stroke: {}; stroke-width: {}; stroke-dasharray: 6 4;",
                sc.stroke_draft, sc.stroke_width
            );
            view! {
                <path
                    d=path_d
                    fill="none"
                    style=style
                    data-connection-draft=""
                />
            }
        })
    };

    view! {
        <svg style="position: absolute; top: 0; left: 0; width: 10000px; height: 10000px; pointer-events: none; overflow: visible;">
            {connections_view}
            {draft_view}
        </svg>
    }
}

fn dangling_geometry(
    endpoints: ConnectionEndpoints,
) -> Option<(Position, Position, f64, f64, &'static str)> {
    let (position, source_present) = match (endpoints.source, endpoints.target) {
        (Some(position), None) => (position, true),
        (None, Some(position)) => (position, false),
        _ => return None,
    };
    let end_x = position.x + if source_present { 30.0 } else { -30.0 };
    let end = Position::new(end_x, position.y);
    let (start, finish) = if source_present {
        (position, end)
    } else {
        (end, position)
    };
    Some((
        start,
        finish,
        end_x + if source_present { 6.0 } else { -6.0 },
        position.y + 4.0,
        if source_present { "start" } else { "end" },
    ))
}

fn log_subway_stats(
    last_subway_stats: &StoredValue<Option<(SubwayRoutingStats, usize, usize)>>,
    stats: SubwayRoutingStats,
    connection_count: usize,
    ports_at_node_origin: usize,
    port_count: usize,
) {
    let signature = (stats, ports_at_node_origin, port_count);
    let changed = last_subway_stats.with_value(|previous| *previous != Some(signature));
    if changed {
        last_subway_stats.set_value(Some(signature));
        leptos::logging::log!(
            "[subway] connections={} ports_at_node_origin={}/{} grid={}x{} ({} cells) expansions={} routed={} fallbacks={} (grid={} budget={} no_path={})",
            connection_count,
            ports_at_node_origin,
            port_count,
            stats.grid_width,
            stats.grid_height,
            stats.grid_cells,
            stats.expansions,
            stats.routed,
            stats.fallbacks(),
            stats.grid_fallbacks,
            stats.budget_fallbacks,
            stats.no_path_fallbacks,
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[derive(Clone, Debug, PartialEq)]
    struct TestPort;
    impl PortType for TestPort {
        fn compatible(_: &Self, _: &Self) -> bool {
            true
        }
        fn type_id(&self) -> String {
            "any".into()
        }
        fn from_type_id(_: &str) -> Self {
            Self
        }
    }

    #[test]
    fn collapsed_alias_changes_display_geometry_without_changing_registry_or_connection() {
        let parent = PortEntry {
            id: "radius".to_owned(),
            node_id: "node".to_owned(),
            direction: PortDirection::Input,
            port_type: TestPort,
            position: Position::new(120.0, 40.0),
            slot_index: 0,
            offset: None,
        };
        let ports = HashMap::from([("radius".to_owned(), parent)]);
        let connection = ConnectionEntry {
            id: "wire".to_owned(),
            source: "wave".to_owned(),
            target: "radius.y".to_owned(),
        };
        let collapsed = display_ports(
            ports.clone(),
            &HashMap::from([("radius.y".to_owned(), "radius".to_owned())]),
        );
        assert_eq!(
            collapsed.get(&connection.target).map(|port| port.position),
            Some(Position::new(120.0, 40.0))
        );
        assert!(!ports.contains_key(&connection.target));
        assert_eq!(connection.target, "radius.y");
        let expanded = display_ports(ports, &HashMap::new());
        assert!(!expanded.contains_key(&connection.target));
    }

    fn batch() -> SubwayBatch<String, String> {
        SubwayBatch {
            rect_ids: vec!["moving".into(), "fixed".into(), "other".into()],
            rects: vec![
                SubwayRect {
                    x: 0.0,
                    y: 0.0,
                    w: 20.0,
                    h: 20.0,
                },
                SubwayRect {
                    x: 100.0,
                    y: 0.0,
                    w: 20.0,
                    h: 20.0,
                },
                SubwayRect {
                    x: 200.0,
                    y: 0.0,
                    w: 20.0,
                    h: 20.0,
                },
            ],
            connections: vec![
                SubwayBatchConnection {
                    id: "incident".into(),
                    start_node: "moving".into(),
                    end_node: "fixed".into(),
                    input: SubwayConnection {
                        start: Position::new(20.0, 10.0),
                        end: Position::new(100.0, 10.0),
                        start_rect: Some(0),
                        end_rect: Some(1),
                    },
                },
                SubwayBatchConnection {
                    id: "nearby".into(),
                    start_node: "fixed".into(),
                    end_node: "other".into(),
                    input: SubwayConnection {
                        start: Position::new(120.0, 10.0),
                        end: Position::new(200.0, 10.0),
                        start_rect: Some(1),
                        end_rect: Some(2),
                    },
                },
            ],
        }
    }

    fn cached_routes() -> HashMap<String, Vec<Position>> {
        HashMap::from([
            (
                "incident".to_string(),
                vec![Position::new(20.0, 10.0), Position::new(100.0, 10.0)],
            ),
            (
                "nearby".to_string(),
                vec![Position::new(-50.0, 30.0), Position::new(50.0, 30.0)],
            ),
        ])
    }

    #[test]
    fn programmatic_single_node_move_plans_incident_and_nearby_routes() {
        let previous = batch();
        let mut current = previous.clone();
        current.rects[0].x += 5.0;
        current.connections[0].input.start.x += 5.0;

        assert_eq!(
            plan_subway_solve(&current, Some(&previous), &cached_routes()),
            SubwaySolve::Partial(HashSet::from([
                "incident".to_string(),
                "nearby".to_string(),
            ]))
        );
    }

    #[test]
    fn incremental_plan_includes_routes_near_only_the_previous_rect() {
        let previous = batch();
        let mut current = previous.clone();
        current.rects[0].x += 500.0;
        current.connections[0].input.start.x += 500.0;
        let routes = cached_routes();
        let nearby = routes.get("nearby").unwrap();

        assert!(subway::route_intersects_rect(
            nearby,
            previous.rects[0],
            SUBWAY_INCREMENTAL_PROXIMITY_PAD,
        ));
        assert!(!subway::route_intersects_rect(
            nearby,
            current.rects[0],
            SUBWAY_INCREMENTAL_PROXIMITY_PAD,
        ));
        assert_eq!(
            plan_subway_solve(&current, Some(&previous), &routes),
            SubwaySolve::Partial(HashSet::from([
                "incident".to_string(),
                "nearby".to_string(),
            ]))
        );
    }

    #[test]
    fn endpoint_diffs_are_mapped_back_to_their_nodes() {
        let previous = batch();
        let mut current = previous.clone();
        current.connections[1].input.end.y += 2.0;

        assert_eq!(
            current.changed_nodes_since(&previous),
            Some(HashSet::from(["other".to_string()]))
        );
    }

    #[test]
    fn final_drop_with_identical_geometry_is_a_cache_hit() {
        let geometry = batch();
        assert_eq!(
            plan_subway_solve(&geometry, Some(&geometry), &cached_routes()),
            SubwaySolve::Cached
        );
    }

    #[test]
    fn structural_changes_still_request_a_full_solve() {
        let previous = batch();
        let mut current = previous.clone();
        current.connections.pop();

        assert_eq!(
            plan_subway_solve(&current, Some(&previous), &cached_routes()),
            SubwaySolve::Full
        );
    }
}