Skip to main content

leptos_node_graph/
selection.rs

1use std::marker::PhantomData;
2
3use leptos::prelude::*;
4
5use crate::registry::EditorRegistry;
6use crate::types::*;
7
8/// Style configuration for the selection box.
9#[derive(Clone, Debug)]
10pub struct SelectionBoxStyle {
11    pub border: String,
12    pub background: String,
13}
14
15impl Default for SelectionBoxStyle {
16    fn default() -> Self {
17        Self {
18            border: "1px solid rgba(99, 102, 241, 0.6)".into(),
19            background: "rgba(99, 102, 241, 0.1)".into(),
20        }
21    }
22}
23
24#[component]
25pub fn SelectionBox<N, P, C, T>(
26    #[prop(optional)] _marker: PhantomData<(N, P, C, T)>,
27) -> impl IntoView
28where
29    N: NodeId,
30    P: PortId,
31    C: ConnectionId,
32    T: PortType,
33{
34    let registry = expect_context::<EditorRegistry<N, P, C, T>>();
35    let style_config = use_context::<SelectionBoxStyle>().unwrap_or_default();
36
37    let box_style = move || {
38        let bs = registry.box_select.get();
39        let vp = registry.viewport.get();
40
41        bs.map(|bs| {
42            let rect = bs.to_rect();
43            let screen_pos = vp.canvas_to_screen(rect.position);
44            let width = rect.size.width * vp.zoom;
45            let height = rect.size.height * vp.zoom;
46
47            format!(
48                "position: absolute; left: {}px; top: {}px; width: {}px; height: {}px; \
49                 border: {}; background: {}; pointer-events: none; z-index: 1000;",
50                screen_pos.x,
51                screen_pos.y,
52                width,
53                height,
54                style_config.border,
55                style_config.background
56            )
57        })
58    };
59
60    move || {
61        box_style().map(|style| {
62            view! { <div style=style /> }
63        })
64    }
65}