Repository: GraphiteEditor/Graphite
File: frontend/wasm/src/editor_api.rs (Rust)

Code before:
	pub fn connect_nodes_by_link(&self, output_node: u64, input_node: u64, input_node_connector_index: usize) {
		let message = NodeGraphMessage::ConnectNodesByLink {
			output_node,
			input_node,
			input_node_connector_index,
		};
		self.dispatch(message);
	}

	/// Notifies the backend that the user disconnected a node
	#[wasm_bindgen(js_name = disconnectNodes)]
	pub fn disconnect_nodes(&self, node_id: u64, input_index: usize) {
		let message = NodeGraphMessage::DisconnectNodes { node_id, input_index };
		self.dispatch(message);
	}

	/// Check for intersections between the curve and a rectangle defined by opposite corners
	#[wasm_bindgen(js_name = rectangleIntersects)]
	pub fn rectangle_intersects(&self, bezier_x: Vec<f64>, bezier_y: Vec<f64>, top: f64, left: f64, bottom: f64, right: f64) -> bool {
		let bezier = bezier_rs::Bezier::from_cubic_dvec2(
			(bezier_x[0], bezier_y[0]).into(),
			(bezier_x[1], bezier_y[1]).into(),
			(bezier_x[2], bezier_y[2]).into(),
			(bezier_x[3], bezier_y[3]).into(),
		);
		!bezier.rectangle_intersections((left, top).into(), (right, bottom).into()).is_empty()
	}

	/// Creates a new document node in the node graph
	#[wasm_bindgen(js_name = createNode)]
	pub fn create_node(&self, node_type: String, x: i32, y: i32) {
		let message = NodeGraphMessage::CreateNode { node_id: None, node_type, x, y };
		self.dispatch(message);
	}

	/// Notifies the backend that the user selected a node in the node graph
	#[wasm_bindgen(js_name = selectNodes)]
	pub fn select_nodes(&self, nodes: Vec<u64>) {
		let message = NodeGraphMessage::SelectNodes { nodes };
		self.dispatch(message);
	}

	/// Notifies the backend that the user double clicked a node
	#[wasm_bindgen(js_name = doubleClickNode)]
	pub fn double_click_node(&self, node: u64) {
		let message = NodeGraphMessage::DoubleClickNode { node };
		self.dispatch(message);
	}

	/// Notifies the backend that the selected nodes have been moved
	#[wasm_bindgen(js_name = moveSelectedNodes)]

Review comment (suggestion):
It's probably safe to also check if the connector curve is entirely within a node (which, once we change the design to make nodes bigger, or perhaps if you have a wide node with a long name and a lot of exposed inputs, this will matter more): `is_contained_within()`

Code after:
	pub fn connect_nodes_by_link(&self, output_node: u64, input_node: u64, input_node_connector_index: usize) {
		let message = NodeGraphMessage::ConnectNodesByLink {
			output_node,
			input_node,
			input_node_connector_index,
		};
		self.dispatch(message);
	}

	/// Shifts the node and its children to stop nodes going ontop of each other
	#[wasm_bindgen(js_name = shiftNode)]
	pub fn shift_node(&self, node_id: u64) {
		let message = NodeGraphMessage::ShiftNode { node_id };
		self.dispatch(message);
	}

	/// Notifies the backend that the user disconnected a node
	#[wasm_bindgen(js_name = disconnectNodes)]
	pub fn disconnect_nodes(&self, node_id: u64, input_index: usize) {
		let message = NodeGraphMessage::DisconnectNodes { node_id, input_index };
		self.dispatch(message);
	}

	/// Check for intersections between the curve and a rectangle defined by opposite corners
	#[wasm_bindgen(js_name = rectangleIntersects)]
	pub fn rectangle_intersects(&self, bezier_x: Vec<f64>, bezier_y: Vec<f64>, top: f64, left: f64, bottom: f64, right: f64) -> bool {
		let bezier = bezier_rs::Bezier::from_cubic_dvec2(
			(bezier_x[0], bezier_y[0]).into(),
			(bezier_x[1], bezier_y[1]).into(),
			(bezier_x[2], bezier_y[2]).into(),
			(bezier_x[3], bezier_y[3]).into(),
		);
		!bezier.rectangle_intersections((left, top).into(), (right, bottom).into()).is_empty() || bezier.is_contained_within((left, top).into(), (right, bottom).into())
	}

	/// Creates a new document node in the node graph
	#[wasm_bindgen(js_name = createNode)]
	pub fn create_node(&self, node_type: String, x: i32, y: i32) {
		let message = NodeGraphMessage::CreateNode { node_id: None, node_type, x, y };
		self.dispatch(message);
	}

	/// Notifies the backend that the user selected a node in the node graph
	#[wasm_bindgen(js_name = selectNodes)]
	pub fn select_nodes(&self, nodes: Vec<u64>) {
		let message = NodeGraphMessage::SelectNodes { nodes };
		self.dispatch(message);
	}

	/// Notifies the backend that the user double clicked a node
	#[wasm_bindgen(js_name = doubleClickNode)]