Repository: GraphiteEditor/Graphite
File: editor/src/messages/tool/tool_messages/pen_tool.rs (Rust)

Code before:
		let document = snap_data.document;
		self.next_handle_start = self.compute_snapped_angle(snap_data, transform, colinear, mouse, Some(self.next_point), false);
		let Some(layer) = layer else { return Some(PenToolFsmState::DraggingHandle(self.handle_mode)) };
		let vector_data = document.network_interface.compute_modified_vector(layer)?;

		match self.handle_mode {
			HandleMode::ColinearLocked | HandleMode::ColinearEquidistant => {
				self.g1_continuous = true;
				self.colinear(responses, layer, self.next_handle_start, self.next_point, &vector_data);
				self.adjust_handle_length(responses, layer, &vector_data);
			}
			HandleMode::Free => {
				self.g1_continuous = false;
			}
		}

		responses.add(OverlaysMessage::Draw);

		Some(PenToolFsmState::DraggingHandle(self.handle_mode))
	}

	fn adjust_handle_length(&mut self, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, vector_data: &VectorData) {
		let Some(latest) = self.latest_point() else { return };
		let anchor_pos = latest.pos;

		match self.handle_mode {
			HandleMode::ColinearEquidistant => self.adjust_equidistant_handle(anchor_pos, responses, layer, vector_data),
			HandleMode::ColinearLocked => self.adjust_locked_length_handle(anchor_pos, responses, layer),
			HandleMode::Free => {} // No adjustments needed in free mode
		}
	}

	fn colinear(&mut self, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, handle_start: DVec2, anchor_point: DVec2, vector_data: &VectorData) {
		let Some(direction) = (anchor_point - handle_start).try_normalize() else {
			log::warn!("Skipping colinear adjustment: handle_start and anchor_point are too close!");
			return;
		};

		let handle_offset = if let Some(handle_end) = self.handle_end {
			(handle_end - anchor_point).length()
		} else {
			let Some(segment) = self.segment_end_before_bent else { return };
			let end_handle = ManipulatorPointId::EndHandle(segment);
			let Some(end_handle) = end_handle.get_position(vector_data) else { return };
			(end_handle - anchor_point).length()
		};

		let new_handle_position = anchor_point + handle_offset * direction;
		self.update_handle_position(new_handle_position, anchor_point, responses, layer);
	}


Review comment (suggestion):
I'm a bit confused about what handle is being adjusted and why? Could you perhaps detail this in the function name / a doc comment.

Code after:

	fn drag_handle(&mut self, snap_data: SnapData, transform: DAffine2, mouse: DVec2, responses: &mut VecDeque<Message>, layer: Option<LayerNodeIdentifier>) -> Option<PenToolFsmState> {
		let colinear = (self.handle_mode == HandleMode::ColinearEquidistant && self.modifiers.break_handle) || (self.handle_mode == HandleMode::ColinearLocked && !self.modifiers.break_handle);
		let document = snap_data.document;
		self.next_handle_start = self.compute_snapped_angle(snap_data, transform, colinear, mouse, Some(self.next_point), false);
		let Some(layer) = layer else { return Some(PenToolFsmState::DraggingHandle(self.handle_mode)) };
		let vector_data = document.network_interface.compute_modified_vector(layer)?;

		match self.handle_mode {
			HandleMode::ColinearLocked | HandleMode::ColinearEquidistant => {
				self.g1_continuous = true;
				self.colinear(responses, layer, self.next_handle_start, self.next_point, &vector_data);
				self.adjust_handle_length(responses, layer, &vector_data);
			}
			HandleMode::Free => {
				self.g1_continuous = false;
			}
		}

		responses.add(OverlaysMessage::Draw);

		Some(PenToolFsmState::DraggingHandle(self.handle_mode))
	}

	/// Makes the opposite handle equidistant or locks its length.
	fn adjust_handle_length(&mut self, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, vector_data: &VectorData) {
		let Some(latest) = self.latest_point() else { return };
		let anchor_pos = latest.pos;

		match self.handle_mode {
			HandleMode::ColinearEquidistant => self.adjust_equidistant_handle(anchor_pos, responses, layer, vector_data),
			HandleMode::ColinearLocked => self.adjust_locked_length_handle(anchor_pos, responses, layer),
			HandleMode::Free => {} // No adjustments needed in free mode
		}
	}

	fn colinear(&mut self, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, handle_start: DVec2, anchor_point: DVec2, vector_data: &VectorData) {
		let Some(direction) = (anchor_point - handle_start).try_normalize() else {
			log::trace!("Skipping colinear adjustment: handle_start and anchor_point are too close!");
			return;
		};

		let handle_offset = if let Some(handle_end) = self.handle_end {
			(handle_end - anchor_point).length()
		} else {
			let Some(segment) = self.segment_end_before_bent else { return };
			let end_handle = ManipulatorPointId::EndHandle(segment);
			let Some(end_handle) = end_handle.get_position(vector_data) else { return };
			(end_handle - anchor_point).length()
		};
		let new_handle_position = anchor_point + handle_offset * direction;