Repository: GraphiteEditor/Graphite
File: editor/src/messages/portfolio/document/overlays/utility_types_web.rs (Rust)

Code before:
	pub fn manipulator_handle(&mut self, position: DVec2, selected: bool, color: Option<&str>) {
		self.start_dpi_aware_transform();

		let position = position.round() - DVec2::splat(0.5);

		self.render_context.begin_path();
		self.render_context
			.arc(position.x, position.y, MANIPULATOR_GROUP_MARKER_SIZE / 2., 0., TAU)
			.expect("Failed to draw the circle");

		let fill = if selected { COLOR_OVERLAY_BLUE } else { COLOR_OVERLAY_WHITE };
		self.render_context.set_fill_style_str(fill);
		self.render_context.set_stroke_style_str(color.unwrap_or(COLOR_OVERLAY_BLUE));
		self.render_context.fill();
		self.render_context.stroke();

		self.end_dpi_aware_transform();
	}

	pub fn manipulator_anchor(&mut self, position: DVec2, selected: bool, color: Option<&str>) {
		let color_stroke = color.unwrap_or(COLOR_OVERLAY_BLUE);
		let color_fill = if selected { color_stroke } else { COLOR_OVERLAY_WHITE };
		self.square(position, None, Some(color_fill), Some(color_stroke));
	}

	pub fn gradient_handle(&mut self, position: DVec2, selected: bool, color: Option<&str>) {
		self.start_dpi_aware_transform();

		let position = position.round() - DVec2::splat(0.5);

		let (radius_offset, stroke_width) = if selected { (1.0, 3.0) } else { (0.0, 1.0) };
		let radius = MANIPULATOR_GROUP_MARKER_SIZE / 1.5 + 1. + radius_offset;

		{
			let stroke_circle = |radius: f64, width: f64, color: &str| {
				self.render_context.begin_path();
				self.render_context.arc(position.x, position.y, radius, 0., TAU).expect("Failed to draw the circle");
				self.render_context.set_line_width(width);
				self.render_context.set_stroke_style_str(color);
				self.render_context.stroke();
			};

			self.render_context.begin_path();
			self.render_context.arc(position.x, position.y, radius, 0., TAU).expect("Failed to draw the circle");
			let fill = color.unwrap_or(if selected { COLOR_OVERLAY_WHITE } else { COLOR_OVERLAY_BLUE });
			self.render_context.set_fill_style_str(fill);
			self.render_context.fill();

			stroke_circle(radius + stroke_width / 2., 1.0, "#000000");

			stroke_circle(radius, stroke_width, COLOR_OVERLAY_WHITE);

Review comment (bug):
Is there ever a reason for `color` to be `None`? If not, it shouldn't take an `Option`, and the unwrapping (hopefully in a non-crashing manner, or `.expect()` at least being preferable to `.unwrap()`) happening in the caller.

Code after:
	pub fn manipulator_handle(&mut self, position: DVec2, selected: bool, color: Option<&str>) {
		self.start_dpi_aware_transform();

		let position = position.round() - DVec2::splat(0.5);

		self.render_context.begin_path();
		self.render_context
			.arc(position.x, position.y, MANIPULATOR_GROUP_MARKER_SIZE / 2., 0., TAU)
			.expect("Failed to draw the circle");

		let fill = if selected { COLOR_OVERLAY_BLUE } else { COLOR_OVERLAY_WHITE };
		self.render_context.set_fill_style_str(fill);
		self.render_context.set_stroke_style_str(color.unwrap_or(COLOR_OVERLAY_BLUE));
		self.render_context.fill();
		self.render_context.stroke();

		self.end_dpi_aware_transform();
	}

	pub fn manipulator_anchor(&mut self, position: DVec2, selected: bool, color: Option<&str>) {
		let color_stroke = color.unwrap_or(COLOR_OVERLAY_BLUE);
		let color_fill = if selected { color_stroke } else { COLOR_OVERLAY_WHITE };
		self.square(position, None, Some(color_fill), Some(color_stroke));
	}

	pub fn gradient_color_stop(&mut self, position: DVec2, selected: bool, color: &str) {
		self.start_dpi_aware_transform();

		let position = position.round() - DVec2::splat(0.5);

		let (radius_offset, stroke_width) = if selected { (1., 3.) } else { (0., 1.) };
		let radius = MANIPULATOR_GROUP_MARKER_SIZE / 1.5 + 1. + radius_offset;

		let draw_circle = |radius: f64, width: Option<f64>, color: &str| {
			self.render_context.begin_path();
			self.render_context.arc(position.x, position.y, radius, 0., TAU).expect("Failed to draw the circle");

			if let Some(width) = width {
				self.render_context.set_line_width(width);
				self.render_context.set_stroke_style_str(color);
				self.render_context.stroke();
			} else {
				self.render_context.set_fill_style_str(color);
				self.render_context.fill();
			}
		};
		// Fill
		draw_circle(radius, None, color);
		// Stroke (inner)
		draw_circle(radius + stroke_width / 2., Some(1.), COLOR_OVERLAY_BLACK);
		// Stroke (outer)