-
I'm currently working on implementing the dragging of graph nodes. The nodes are positioned using the In JavaScript, I would typically check if the event target is my parent element, so that the As far as I understand, both of these approaches are not currently possible in Dioxus. Does anyone have an idea on how to approach this? The question is specifically about desktop. Here is the code: #[derive(Clone)]
pub struct NodeData {
pub id: usize,
pub name: String,
pub x: f64,
pub y: f64,
}
impl PartialEq for NodeData {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
#[derive(PartialEq, Clone, Props)]
pub struct GraphProps {
nodes: Vec<NodeData>,
}
#[allow(non_snake_case)]
#[component]
pub fn Graph(props: GraphProps) -> Element {
let mut nodes = use_signal(|| props.nodes);
let mut dragging_node_idx = use_signal(|| None);
let on_dragging = move |node_id| {
println!("Dragging node: {}", node_id);
let idx = nodes().iter().position(|node| node.id == node_id);
dragging_node_idx.set(idx);
};
let stop_dragging = move |_| {
println!("Stop dragging node: {:?}", dragging_node_idx());
dragging_node_idx.set(None);
};
let on_mouse_move = move |event: MouseEvent| {
if let Some(idx) = dragging_node_idx() {
let coords = event.element_coordinates();
let x = coords.x;
let y = coords.y;
println!("x = {}, y = {}", x, y);
let old_node = nodes()[idx].clone();
let new_node = NodeData { x, y, ..old_node };
nodes.write()[idx] = new_node;
}
};
rsx!(
div {
class: "graph",
onmouseleave: stop_dragging,
onmouseup: stop_dragging,
onmousemove: on_mouse_move,
for node in nodes() {
Node {
key: "{node.id}",
id: node.id,
name: node.name,
x: node.x,
y: node.y,
dragging: on_dragging,
}
}
}
)
}
#[derive(PartialEq, Clone, Props)]
struct NodeProps {
id: usize,
name: String,
x: f64,
y: f64,
dragging: EventHandler<usize>,
}
#[allow(non_snake_case)]
#[component]
fn Node(props: NodeProps) -> Element {
let position_style = format!("left: {}px; top: {}px;", props.x, props.y);
rsx!(
div {
class: "node",
style: position_style,
onmousedown: move |_| props.dragging.call(props.id),
Text { ramp: TextRamp::Body, { props.name } }
}
)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can get the rendered position of an element with the onmounted event. Here is an example I am working on a project with graph UI that might be useful as a reference here |
Beta Was this translation helpful? Give feedback.
You can get the rendered position of an element with the onmounted event. Here is an example
I am working on a project with graph UI that might be useful as a reference here