fix: fixed jittery drag

This commit is contained in:
Rendo 2026-08-10 20:39:16 +05:00
commit 6e070038a3
2 changed files with 15 additions and 9 deletions

View file

@ -10,7 +10,7 @@ pub struct Graph {
pub sockets: Vec<Socket>, pub sockets: Vec<Socket>,
pub connections: Vec<SocketConnection>, pub connections: Vec<SocketConnection>,
pub current_drag_socket: Option<usize>, pub current_drag_socket: Option<usize>,
pub current_drag_node: Option<usize>, pub current_drag_node: Option<(usize,Option<Point>)>,
} }
impl Graph { impl Graph {
@ -27,9 +27,13 @@ impl Graph {
self.current_drag_socket = None; self.current_drag_socket = None;
} }
}, },
GraphMessage::NodeStartDrag(index) => self.current_drag_node = Some(index), GraphMessage::NodeStartDrag(index) => self.current_drag_node = Some((index,None)),
GraphMessage::NodeDrag(point, idx) if let Some(node_index) = self.current_drag_node && idx == node_index => self.nodes[node_index].position = point, GraphMessage::NodeDrag(point, idx) if let Some((node_index,cached_position)) = self.current_drag_node && idx == node_index => {
GraphMessage::NodeStopDrag => self.current_drag_node = None, let previous_position = cached_position.unwrap_or(point);
self.nodes[idx].position += point - previous_position;
self.current_drag_node = Some((idx,Some(previous_position)));
},
GraphMessage::NodeStopDrag(idx) if let Some((node_index,_)) = self.current_drag_node && idx == node_index => self.current_drag_node = None,
GraphMessage::SpawnNode(node) => self.nodes.push(node), GraphMessage::SpawnNode(node) => self.nodes.push(node),
_ => {}, _ => {},
} }
@ -55,7 +59,7 @@ impl Graph {
pub enum GraphMessage { pub enum GraphMessage {
NodeStartDrag(usize), NodeStartDrag(usize),
NodeDrag(Point, usize), NodeDrag(Point, usize),
NodeStopDrag, NodeStopDrag(usize),
SocketDragStart(usize), SocketDragStart(usize),
SocketDragEnd(usize), SocketDragEnd(usize),

View file

@ -1,6 +1,6 @@
use iced::widget::container::{bordered_box, rounded_box}; use iced::widget::container::{bordered_box, rounded_box};
use iced::{Element, Length, Point}; use iced::{Element, Length, Point};
use iced::widget::{Column, column, container, mouse_area, row, space, text}; use iced::widget::{Column, column, container, mouse_area, pin, row, space, text};
use crate::socket::Socket; use crate::socket::Socket;
use crate::graph::GraphMessage; use crate::graph::GraphMessage;
@ -32,7 +32,8 @@ impl Node {
.style(rounded_box)) .style(rounded_box))
.on_press(Message::GraphMessage(GraphMessage::NodeStartDrag(index))) .on_press(Message::GraphMessage(GraphMessage::NodeStartDrag(index)))
.on_move(move |point| Message::GraphMessage(GraphMessage::NodeDrag(point, index))) .on_move(move |point| Message::GraphMessage(GraphMessage::NodeDrag(point, index)))
.on_release(Message::GraphMessage(GraphMessage::NodeStopDrag)); .on_release(Message::GraphMessage(GraphMessage::NodeStopDrag(index)))
.on_exit(Message::GraphMessage(GraphMessage::NodeStopDrag(index)));
let inputs = Column::with_children( let inputs = Column::with_children(
self.inputs.iter().map(|socket_id| self.inputs.iter().map(|socket_id|
@ -48,12 +49,13 @@ impl Node {
let body = row![inputs, space().width(Length::Fixed(40.)), outputs]; let body = row![inputs, space().width(Length::Fixed(40.)), outputs];
let content = column![header,body].spacing(8).padding(8); let content = column![header,body].spacing(8).padding(8);
container(content) pin(container(content)
.style(bordered_box) .style(bordered_box)
.width(Length::Fixed(100.)) .width(Length::Fixed(100.))
).position(self.position)
.into() .into()
} }
pub fn position(mut self, point: Point) -> Self { pub fn reposition(mut self, point: Point) -> Self {
self.position = point; self.position = point;
self self
} }