Skip to main content

leptos_node_graph/
theme.rs

1/// Controls how a node's input and output anchors are arranged.
2#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
3pub enum AnchorLayout {
4    /// Inputs in a left column, outputs in a right column (default).
5    #[default]
6    Columns,
7    /// A single vertical stack: all inputs, then all outputs below them.
8    /// Each row spans the full node width so consumers can lay anchor
9    /// children out as a `[name | value]` grid. Input dots stay on the left
10    /// edge, output dots on the right edge, so connection routing is unchanged.
11    Stacked,
12}
13
14/// Shape of an anchor's socket dot. Per-anchor override; defaults to `Circle`.
15///
16/// Sockets are drawn as SVG paths, so every shape supports both flavors the
17/// anchor uses: stroked-and-hollow (idle) and solid (connected / compatible).
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
19pub enum DotShape {
20    /// Round socket (default).
21    #[default]
22    Circle,
23    /// Rotated square — e.g. for vector/diamond sockets.
24    Diamond,
25    /// Square socket.
26    Square,
27    /// Six-sided socket — reads as "structured / opaque" next to the primitives.
28    Hexagon,
29    /// Right-pointing triangle — reads as "flow / trigger".
30    Triangle,
31    /// Four-pointed star.
32    Star,
33}
34
35impl DotShape {
36    /// The socket outline as a single SVG path, in a `0 0 24 24` viewBox.
37    ///
38    /// Geometry stays ~2 units inside the box so a stroke (up to ~4 viewBox
39    /// units, i.e. a 1.5px border on an 8px dot) never clips.
40    pub fn path(self) -> &'static str {
41        match self {
42            // Two half-circle arcs — a `<path>` keeps every shape one element.
43            DotShape::Circle => "M12 2.5A9.5 9.5 0 1 0 12 21.5A9.5 9.5 0 1 0 12 2.5Z",
44            DotShape::Diamond => "M12 2.5L21.5 12L12 21.5L2.5 12Z",
45            DotShape::Square => "M3 3H21V21H3Z",
46            DotShape::Hexagon => "M12 2.5L21 7.75V18.25L12 21.5L3 18.25V7.75Z",
47            DotShape::Triangle => "M4.5 3L21 12L4.5 21Z",
48            DotShape::Star => "M12 2L14.6 9.4L22 12L14.6 14.6L12 22L9.4 14.6L2 12L9.4 9.4Z",
49        }
50    }
51}
52
53/// Style configuration for node cards.
54#[derive(Clone, Debug)]
55pub struct NodeStyle {
56    pub background: String,
57    pub border: String,
58    /// Selection outline (uses CSS outline, not border — no layout shift).
59    pub outline_selected: String,
60    pub border_radius: String,
61    pub shadow: String,
62    pub shadow_selected: String,
63    pub min_width: String,
64    /// Fixed node width (CSS length, e.g. "256px"). When non-empty the node
65    /// renders at exactly this width; combined with the node's `overflow:
66    /// hidden`, body field content that doesn't fit elides (the field value
67    /// spans are `text-overflow: ellipsis`). Empty = size to content above
68    /// `min_width` (the prior behavior).
69    pub width: String,
70    pub opacity_dragging: f64,
71    /// Horizontal padding applied to all sections (header, body, ports).
72    pub padding_x: f64,
73    /// Vertical padding for the header section.
74    pub header_padding_y: f64,
75    /// Vertical padding for the body section (0 if no body).
76    pub body_padding_y: f64,
77    /// Vertical padding for the ports section.
78    pub ports_padding_y: f64,
79    /// Border between header and body/ports.
80    pub header_border_bottom: String,
81    /// Background for the header section.
82    pub header_background: String,
83    /// Header text color.
84    pub header_color: String,
85    /// Header font size.
86    pub header_font_size: String,
87    /// Accent line height in pixels (used when Node has accent_color prop).
88    pub header_accent_height: f64,
89    /// Body field label color.
90    pub field_label_color: String,
91    /// Body field label font size.
92    pub field_label_font_size: String,
93    /// Body field gap between label and content.
94    pub field_gap: String,
95    /// Body field label min width.
96    pub field_label_min_width: String,
97    /// Body border bottom (between body and ports).
98    pub body_border_bottom: String,
99    /// How input and output anchors are arranged within the node.
100    pub anchor_layout: AnchorLayout,
101    /// Cursor over the parts of the node that start a drag — i.e. everything
102    /// except anchor dots (crosshair) and form controls, which set their own.
103    pub cursor: String,
104    /// Cursor while a node drag is in progress.
105    pub cursor_dragging: String,
106    /// Cursor over the resize handle, and node-wide while a resize is running.
107    pub cursor_resize: String,
108    /// Whether nodes can be width-resized by dragging their right edge.
109    pub resizable: bool,
110    /// Hit width of the right-edge resize handle, in pixels. Keep it below
111    /// `AnchorStyle.dot_inset` so the handle doesn't sit under the output dots
112    /// (where they do overlap, the dots win).
113    pub resize_handle_width: f64,
114    /// Handle color while that node is being resized (it's invisible otherwise —
115    /// the cursor is the idle affordance).
116    pub resize_handle_color: String,
117    /// Floor for a dragged width. The effective floor is the larger of this and
118    /// `min_width` when that is a plain px value, so a dragged width can't drift
119    /// below what CSS will actually render.
120    pub resize_min_width: f64,
121    /// Ceiling for a dragged width. `None` = unbounded.
122    pub resize_max_width: Option<f64>,
123}
124
125impl Default for NodeStyle {
126    fn default() -> Self {
127        Self {
128            background: "#1e1e22".into(),
129            border: "1px solid #3f3f46".into(),
130            outline_selected: "1.5px solid #ef4444".into(),
131            border_radius: "8px".into(),
132            shadow: "0 4px 12px rgba(0,0,0,0.4)".into(),
133            shadow_selected: "0 0 0 1px #ef4444, 0 4px 16px rgba(239,68,68,0.25)".into(),
134            min_width: "160px".into(),
135            width: String::new(),
136            opacity_dragging: 0.92,
137            padding_x: 10.0,
138            header_padding_y: 6.0,
139            body_padding_y: 6.0,
140            ports_padding_y: 4.0,
141            header_border_bottom: "1px solid #27272a".into(),
142            header_background: "#232327".into(),
143            header_color: "#a1a1aa".into(),
144            header_font_size: "12px".into(),
145            header_accent_height: 2.0,
146            field_label_color: "#71717a".into(),
147            field_label_font_size: "10px".into(),
148            field_gap: "6px".into(),
149            field_label_min_width: "38px".into(),
150            body_border_bottom: "1px solid #27272a".into(),
151            anchor_layout: AnchorLayout::Columns,
152            cursor: "grab".into(),
153            cursor_dragging: "grabbing".into(),
154            cursor_resize: "ew-resize".into(),
155            resizable: true,
156            resize_handle_width: 6.0,
157            resize_handle_color: "#71717a".into(),
158            resize_min_width: 120.0,
159            resize_max_width: None,
160        }
161    }
162}
163
164/// Style configuration for the node creation menu.
165#[derive(Clone, Debug)]
166pub struct NodeMenuStyle {
167    pub background: String,
168    pub border: String,
169    pub shadow: String,
170    pub input_background: String,
171    pub input_border: String,
172    pub input_color: String,
173    pub placeholder_color: String,
174    pub category_color: String,
175    pub item_color: String,
176    pub description_color: String,
177    pub hover_background: String,
178    pub port_color: String,
179    pub empty_color: String,
180    pub divider: String,
181}
182
183impl Default for NodeMenuStyle {
184    fn default() -> Self {
185        Self {
186            background: "#1e1e22".into(),
187            border: "1px solid #3f3f46".into(),
188            shadow: "0 8px 24px rgba(0,0,0,0.5)".into(),
189            input_background: "#27272a".into(),
190            input_border: "1px solid #3f3f46".into(),
191            input_color: "#d4d4d8".into(),
192            placeholder_color: "#71717a".into(),
193            category_color: "#52525b".into(),
194            item_color: "#d4d4d8".into(),
195            description_color: "#71717a".into(),
196            hover_background: "rgba(99, 102, 241, 0.15)".into(),
197            port_color: "#a1a1aa".into(),
198            empty_color: "#71717a".into(),
199            divider: "1px solid #27272a".into(),
200        }
201    }
202}
203
204/// Style configuration for group box overlays.
205#[derive(Clone, Debug)]
206pub struct GroupStyle {
207    pub default_color: String,
208    pub border_radius: String,
209    pub error_border: String,
210    pub error_background: String,
211    pub error_label_color: String,
212    pub label_font_size: String,
213    pub label_font_weight: String,
214    pub input_background: String,
215}
216
217impl Default for GroupStyle {
218    fn default() -> Self {
219        Self {
220            default_color: "#8b5cf6".into(),
221            border_radius: "8px".into(),
222            error_border: "rgba(239, 68, 68, 0.5)".into(),
223            error_background: "rgba(239, 68, 68, 0.08)".into(),
224            error_label_color: "rgba(239, 68, 68, 0.8)".into(),
225            label_font_size: "10px".into(),
226            label_font_weight: "600".into(),
227            input_background: "transparent".into(),
228        }
229    }
230}
231
232/// Style configuration for anchor (port) rendering.
233/// The library renders the full anchor row: dot + label/children.
234#[derive(Clone, Debug)]
235pub struct AnchorStyle {
236    /// Dot size in pixels.
237    pub dot_size: f64,
238    /// Dot border width.
239    pub dot_border_width: f64,
240    /// Dot default border color (unconnected, no draft).
241    pub dot_color: String,
242    /// Dot color when connected.
243    pub dot_connected_color: String,
244    /// Dot color when compatible with draft.
245    pub dot_compatible_color: String,
246    /// Dot glow when compatible, as a CSS `filter` value. A filter (not a
247    /// box-shadow) so the glow follows the socket's SHAPE — a box-shadow would
248    /// halo a square around a triangle or hexagon socket.
249    pub dot_compatible_glow: String,
250    /// Label font size.
251    pub label_font_size: String,
252    /// Label default color.
253    pub label_color: String,
254    /// Label color when compatible.
255    pub label_compatible_color: String,
256    /// Fixed row height in pixels. Children cannot exceed this.
257    pub row_height: f64,
258    /// Y offset from node top to the center of the first port.
259    /// Typically header_height + padding + row_height/2.
260    pub first_port_y: f64,
261    /// X inset from node edge for the dot center (input ports from left, output from right).
262    pub dot_inset: f64,
263    /// Row padding (CSS).
264    pub row_padding: String,
265    /// Row gap between dot and label.
266    pub row_gap: String,
267    /// Opacity when incompatible during draft.
268    pub incompatible_opacity: f64,
269    /// Tooltip background.
270    pub tooltip_background: String,
271    /// Tooltip border.
272    pub tooltip_border: String,
273    /// Tooltip text color.
274    pub tooltip_color: String,
275}
276
277impl Default for AnchorStyle {
278    fn default() -> Self {
279        Self {
280            dot_size: 8.0,
281            dot_border_width: 1.5,
282            dot_color: "#71717a".into(),
283            dot_connected_color: "#71717a".into(),
284            dot_compatible_color: "#22d3ee".into(),
285            dot_compatible_glow:
286                "drop-shadow(0 0 2px #22d3ee) drop-shadow(0 0 5px rgba(34,211,238,0.45))".into(),
287            label_font_size: "11px".into(),
288            label_color: "#a1a1aa".into(),
289            label_compatible_color: "#22d3ee".into(),
290            row_height: 24.0,
291            first_port_y: 0.0, // computed from header+body measurements
292            dot_inset: 14.0,
293            row_padding: "0 10px".into(),
294            row_gap: "6px".into(),
295            incompatible_opacity: 0.25,
296            tooltip_background: "#27272a".into(),
297            tooltip_border: "1px solid #3f3f46".into(),
298            tooltip_color: "#a1a1aa".into(),
299        }
300    }
301}