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

Code before:
		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);
	}

	fn adjust_equidistant_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, vector_data: &VectorData) {
		if self.modifiers.break_handle {
			// Store original handle before equidistant adjustment
			self.store_handle(vector_data);
			self.alt_press = true;

			let new_position = self.next_point * 2. - self.next_handle_start;
			self.update_handle_position(new_position, anchor_pos, responses, layer);
		} else {
			self.restore_previous_handle(anchor_pos, responses, layer);
		}
	}

	fn adjust_locked_length_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if !self.modifiers.break_handle {
			let new_position = self.next_point * 2. - self.next_handle_start;
			self.update_handle_position(new_position, anchor_pos, responses, layer);
		}
	}

	fn store_handle(&mut self, vector_data: &VectorData) {
		if self.temp_handle_pos == DVec2::ZERO {

Review comment (suggestion):
You can do `anchor_point + (handle_end - anchor_point).project_onto_normalized(direction)` to avoid taking the square root in the length function.

Code after:
	}

	/// 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;
		self.update_handle_position(new_handle_position, anchor_point, responses, layer);
	}

	fn adjust_equidistant_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier, vector_data: &VectorData) {
		if self.modifiers.break_handle {
			self.store_handle(vector_data);
			self.alt_press = true;
			let new_position = self.next_point * 2. - self.next_handle_start;
			self.update_handle_position(new_position, anchor_pos, responses, layer);
		} else {
			self.restore_previous_handle(anchor_pos, responses, layer);
		}
	}

	fn adjust_locked_length_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if !self.modifiers.break_handle {
			let new_position = self.next_point * 2. - self.next_handle_start;
			self.update_handle_position(new_position, anchor_pos, responses, layer);
		}
	}

	/// Temporarily stores the opposite handle position to revert back when Alt is released in equidistant mode.