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

Code before:
		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 {
			self.temp_handle_pos = self.handle_end.unwrap_or_else(|| {
				let Some(segment) = self.segment_end_before_bent else { return DVec2::ZERO };
				ManipulatorPointId::EndHandle(segment).get_position(vector_data).unwrap_or(DVec2::ZERO)
			});
		}
	}

	fn restore_previous_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if self.alt_press {
			self.alt_press = false;
			self.update_handle_position(self.temp_handle_pos, anchor_pos, responses, layer);
			self.temp_handle_pos = DVec2::ZERO; // Reset storage to avoid reuse
		}
	}

	fn update_handle_position(&mut self, new_position: DVec2, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if let Some(handle) = self.handle_end.as_mut() {
			*handle = new_position;
		} else {
			let Some(segment) = self.segment_end_before_bent else { return };

			let relative_position = new_position - anchor_pos;
			let modification_type = VectorModificationType::SetEndHandle { segment, relative_position };
			responses.add(GraphOperationMessage::Vector { layer, modification_type });
		}
	}

Review comment (suggestion):
It isn't really clear which handle is stored and why. Perhaps this could be added to the name / a doc comment.

Code after:
			(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.
	fn store_handle(&mut self, vector_data: &VectorData) {
		if !self.alt_press {
			self.previous_handle_end_pos = self.handle_end.or_else(|| {
				let segment = self.segment_end_before_bent?;
				ManipulatorPointId::EndHandle(segment).get_position(vector_data)
			});
		}
	}

	fn restore_previous_handle(&mut self, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if self.alt_press {
			self.alt_press = false;
			if let Some(previous_handle) = self.previous_handle_end_pos {
				self.update_handle_position(previous_handle, anchor_pos, responses, layer);
			}
			self.previous_handle_end_pos = None; // Reset storage
		}
	}

	fn update_handle_position(&mut self, new_position: DVec2, anchor_pos: DVec2, responses: &mut VecDeque<Message>, layer: LayerNodeIdentifier) {
		if let Some(handle) = self.handle_end.as_mut() {
			*handle = new_position;
		} else {
			let Some(segment) = self.segment_end_before_bent else { return };
			let relative_position = new_position - anchor_pos;
			let modification_type = VectorModificationType::SetEndHandle { segment, relative_position };