Skip to main content

webizen_studio/render/
node_graph.rs

1//! Node-relational presentation — SVG graph of panes and their bindings.
2
3use crate::canvas_graph::{derive_graph_edges, edge_visual};
4use crate::canvas_model::{Page, PanePlacement};
5use aether::prelude::*;
6
7/// Pixels per layout point (matches grid and spatial canvases).
8const POINT_SCALE: f64 = 6.0;
9
10#[derive(Clone, Debug, PartialEq)]
11struct NodeLayout {
12    idx: usize,
13    cx: f64,
14    cy: f64,
15    w: f64,
16    h: f64,
17}
18
19fn pane_rect(pane: &PanePlacement) -> (f64, f64, f64, f64) {
20    let w = (pane.w.max(20) as f64) * POINT_SCALE;
21    let h = (pane.h.max(10) as f64) * POINT_SCALE;
22    let x = pane.x as f64 * POINT_SCALE;
23    let y = pane.y as f64 * POINT_SCALE;
24    (x, y, w, h)
25}
26
27fn cubic_path(x1: f64, y1: f64, x2: f64, y2: f64) -> String {
28    let dx = (x2 - x1).abs().max(40.0);
29    format!(
30        "M {x1:.1} {y1:.1} C {cx1:.1} {y1:.1}, {cx2:.1} {y2:.1}, {x2:.1} {y2:.1}",
31        cx1 = x1 + dx * 0.4,
32        cx2 = x2 - dx * 0.4,
33    )
34}
35
36#[component]
37/// Aether `NodeGraphCanvas` component.
38pub fn NodeGraphCanvas(page: Page) -> Element {
39    let layouts: Vec<NodeLayout> = page
40        .panes
41        .iter()
42        .enumerate()
43        .map(|(idx, pane)| {
44            let (x, y, w, h) = pane_rect(pane);
45            NodeLayout {
46                idx,
47                cx: x + w * 0.5,
48                cy: y + h * 0.5,
49                w,
50                h,
51            }
52        })
53        .collect();
54
55    let edges = derive_graph_edges(&page.panes);
56
57    let (svg_w, svg_h) = layouts.iter().fold((640.0_f64, 480.0_f64), |(mw, mh), n| {
58        (
59            mw.max(n.cx + n.w * 0.5 + 24.0),
60            mh.max(n.cy + n.h * 0.5 + 24.0),
61        )
62    });
63
64    rsx! {
65        style {
66            r#"
67            @keyframes node-edge-flow {{
68                from {{ stroke-dashoffset: 24; }}
69                to {{ stroke-dashoffset: 0; }}
70            }}
71            .node-graph-edge-strong {{
72                animation: node-edge-flow 2.4s linear infinite;
73            }}
74            "#
75        }
76        div {
77            style: "position: relative; width: 100%; height: 100%; min-height: 500px; background: var(--qualia-bg, #0a0a0a); border: 1px solid var(--qualia-border, #333); border-radius: 12px; overflow: auto;",
78
79            svg {
80                width: "{svg_w}",
81                height: "{svg_h}",
82                style: "position: absolute; inset: 0; pointer-events: none; z-index: 0;",
83                defs {
84                    marker {
85                        id: "node-graph-arrow",
86                        marker_width: "8",
87                        marker_height: "8",
88                        ref_x: "6",
89                        ref_y: "4",
90                        orient: "auto",
91                        path {
92                            d: "M0,0 L8,4 L0,8 Z",
93                            fill: "var(--qualia-accent, #f59e0b)",
94                            opacity: "0.7",
95                        }
96                    }
97                }
98                for edge in edges.iter() {
99                    if let (Some(from), Some(to)) = (
100                        layouts.iter().find(|n| n.idx == edge.from_idx),
101                        layouts.iter().find(|n| n.idx == edge.to_idx),
102                    ) {
103                        path {
104                            key: "{edge.from_idx}-{edge.to_idx}-{edge.label}",
105                            class: if edge.strength >= 2 { "node-graph-edge node-graph-edge-strong" } else { "node-graph-edge" },
106                            d: "{cubic_path(from.cx, from.cy, to.cx, to.cy)}",
107                            fill: "none",
108                            stroke: "var(--qualia-accent, #f59e0b)",
109                            stroke_width: "{edge_visual(edge.strength).0}",
110                            opacity: "{edge_visual(edge.strength).1}",
111                            stroke_dasharray: if edge.strength >= 2 { "8 4" } else { "none" },
112                            style: "{edge_visual(edge.strength).2}",
113                            "marker-end": "url(#node-graph-arrow)",
114                        }
115                    }
116                }
117            }
118
119            for (idx, pane) in page.panes.iter().enumerate() {
120                div {
121                    key: "{idx}",
122                    style: "position: absolute; left: {pane.x as f64 * POINT_SCALE}px; top: {pane.y as f64 * POINT_SCALE}px; width: {(pane.w.max(20) as f64) * POINT_SCALE}px; min-height: {(pane.h.max(10) as f64) * POINT_SCALE}px; background: var(--qualia-surface, #181818); border: 1px solid var(--qualia-accent, #0ff); border-radius: 8px; padding: 0.5rem; display: flex; flex-direction: column; box-shadow: 0 8px 16px rgba(0,0,0,0.3); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); z-index: 1;",
123                    div {
124                        style: "font-size: 0.75rem; font-weight: bold; color: var(--qualia-text, #fff); margin-bottom: 0.4rem; border-bottom: 1px solid var(--qualia-border, #333); padding-bottom: 0.2rem;",
125                        "{pane.component_id}"
126                    }
127                    if !pane.data_bindings.is_empty() {
128                        div {
129                            style: "font-size: 0.65rem; color: var(--qualia-text-muted, #888); line-height: 1.4;",
130                            for binding in pane.data_bindings.iter() {
131                                div { "{binding}" }
132                            }
133                        }
134                    }
135                    if let Some(anchor) = pane.anchor.as_ref() {
136                        div {
137                            style: "font-size: 0.6rem; color: var(--qualia-accent); margin-top: 0.25rem;",
138                            "↳ {anchor}"
139                        }
140                    }
141                }
142            }
143        }
144    }
145}