Apply lints and cleanup to Rust code

This commit is contained in:
Keavon Chambers
2023-01-29 03:01:57 -08:00
parent 5388b59e97
commit d990110f63
27 changed files with 122 additions and 119 deletions
@@ -58,7 +58,7 @@ impl OverlayRenderer {
// Create, place, and style the manipulator overlays
for (manipulator_group_id, manipulator_group) in shape.manipulator_groups().enumerate() {
let manipulator_group_cache = self.manipulator_group_overlay_cache.entry((*layer_id, *manipulator_group_id)).or_insert(Default::default());
let manipulator_group_cache = self.manipulator_group_overlay_cache.entry((*layer_id, *manipulator_group_id)).or_default();
// Only view in and out handles if they are not on top of the anchor
let [in_handle, out_handle] = {
@@ -29,18 +29,22 @@ pub struct ShapeEditor {
selected_layers: Vec<Vec<LayerId>>,
}
pub struct SelectedPointsInfo<'a> {
pub points: Vec<ManipulatorPointInfo<'a>>,
pub offset: DVec2,
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct ManipulatorPointInfo<'a> {
pub shape_layer_path: &'a [LayerId],
pub manipulator_group_id: u64,
pub manipulator_type: ManipulatorType,
}
// TODO Consider keeping a list of selected manipulators to minimize traversals of the layers
impl ShapeEditor {
/// Select the first point within the selection threshold.
/// Returns a tuple of the points if found and the offset, or None otherwise.
pub fn select_point(
&self,
document: &Document,
mouse_position: DVec2,
select_threshold: f64,
add_to_selection: bool,
responses: &mut VecDeque<Message>,
) -> Option<(Vec<(&[LayerId], u64, ManipulatorType)>, DVec2)> {
/// Returns a tuple of the points if found and the offset, or `None` otherwise.
pub fn select_point(&self, document: &Document, mouse_position: DVec2, select_threshold: f64, add_to_selection: bool, responses: &mut VecDeque<Message>) -> Option<SelectedPointsInfo> {
if self.selected_layers.is_empty() {
return None;
}
@@ -73,7 +77,11 @@ impl ShapeEditor {
.enumerate()
.filter(|(_id, manipulator_group)| manipulator_group.is_anchor_selected())
.flat_map(|(id, manipulator_group)| manipulator_group.selected_points().map(move |point| (id, point.manipulator_type)))
.map(|(anchor, manipulator_point)| (path.as_slice(), *anchor, manipulator_point))
.map(|(anchor, manipulator_point)| ManipulatorPointInfo {
shape_layer_path: path.as_slice(),
manipulator_group_id: *anchor,
manipulator_type: manipulator_point,
})
})
.collect::<Vec<_>>();
@@ -82,32 +90,36 @@ impl ShapeEditor {
// This is selecting the manipulator only for now, next to generalize to points
if should_select {
// If we're replacing the selection, clear all points in other selected shapes
let add = add_to_selection || is_point_selected;
let point = (manipulator_group_id, ManipulatorType::from_index(manipulator_point_index));
// Clear all point in other selected shapes
if !add {
points.clear();
responses.push_back(DocumentMessage::DeselectAllManipulatorPoints.into());
points = vec![(shape_layer_path, point.0, point.1)];
} else {
points.push((shape_layer_path, point.0, point.1));
}
// Add to the selected points
let point_info = ManipulatorPointInfo {
shape_layer_path,
manipulator_group_id,
manipulator_type: ManipulatorType::from_index(manipulator_point_index),
};
points.push(point_info);
responses.push_back(
Operation::SelectManipulatorPoints {
layer_path: shape_layer_path.to_vec(),
point_ids: vec![point],
point_ids: vec![(point_info.manipulator_group_id, point_info.manipulator_type)],
add,
}
.into(),
);
// Offset to snap the selected point to the cursor
let offset = if let Ok(viewspace) = document.generate_transform_relative_to_viewport(shape_layer_path) {
mouse_position - viewspace.transform_point2(point_position)
} else {
DVec2::ZERO
};
let offset = document
.generate_transform_relative_to_viewport(shape_layer_path)
.map(|viewspace| mouse_position - viewspace.transform_point2(point_position))
.unwrap_or_default();
return Some((points, offset));
return Some(SelectedPointsInfo { points, offset });
} else {
responses.push_back(
Operation::DeselectManipulatorPoints {
@@ -116,7 +128,13 @@ impl ShapeEditor {
}
.into(),
);
points.retain(|x| *x != (shape_layer_path, manipulator_group_id, ManipulatorType::from_index(manipulator_point_index)));
points.retain(|x| {
*x != ManipulatorPointInfo {
shape_layer_path,
manipulator_group_id,
manipulator_type: ManipulatorType::from_index(manipulator_point_index),
}
});
return None;
}
@@ -1,3 +1,4 @@
use super::shape_editor::ManipulatorPointInfo;
use crate::application::generate_uuid;
use crate::consts::{
COLOR_ACCENT, SNAP_AXIS_OVERLAY_FADE_DISTANCE, SNAP_AXIS_TOLERANCE, SNAP_AXIS_UNSNAPPED_OPACITY, SNAP_POINT_OVERLAY_FADE_FAR, SNAP_POINT_OVERLAY_FADE_NEAR, SNAP_POINT_SIZE, SNAP_POINT_TOLERANCE,
@@ -257,7 +258,7 @@ impl SnapManager {
layer: &Layer,
path: &[LayerId],
include_handles: bool,
ignore_points: &[(&[LayerId], u64, ManipulatorType)],
ignore_points: &[ManipulatorPointInfo],
) {
if let LayerDataType::Shape(shape_layer) = &layer.data {
let transform = document_message_handler.document_legacy.multiply_transforms(path).unwrap();
@@ -277,7 +278,13 @@ impl SnapManager {
}
})
.filter_map(|(id, point)| point.as_ref().map(|val| (id, val)))
.filter(|(id, point)| !ignore_points.contains(&(path, *id, point.manipulator_type)))
.filter(|(id, point)| {
!ignore_points.contains(&ManipulatorPointInfo {
shape_layer_path: path,
manipulator_group_id: *id,
manipulator_type: point.manipulator_type,
})
})
.map(|(_id, point)| DVec2::new(point.position.x, point.position.y))
.map(|pos| transform.transform_point2(pos));
self.add_snap_points(document_message_handler, input, snap_points);
@@ -291,7 +298,7 @@ impl SnapManager {
input: &InputPreprocessorMessageHandler,
include_handles: &[&[LayerId]],
exclude: &[&[LayerId]],
ignore_points: &[(&[LayerId], u64, ManipulatorType)],
ignore_points: &[ManipulatorPointInfo],
) {
for path in document_message_handler.all_layers() {
if !exclude.contains(&path) {
@@ -4,7 +4,7 @@ use crate::messages::input_mapper::utility_types::input_keyboard::{Key, MouseMot
use crate::messages::layout::utility_types::layout_widget::PropertyHolder;
use crate::messages::prelude::*;
use crate::messages::tool::common_functionality::overlay_renderer::OverlayRenderer;
use crate::messages::tool::common_functionality::shape_editor::ShapeEditor;
use crate::messages::tool::common_functionality::shape_editor::{ManipulatorPointInfo, ShapeEditor};
use crate::messages::tool::common_functionality::snapping::SnapManager;
use crate::messages::tool::utility_types::{EventToMessageMap, Fsm, ToolActionHandlerData, ToolMetadata, ToolTransition, ToolType};
use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo};
@@ -157,10 +157,9 @@ impl Fsm for PathToolFsmState {
let toggle_add_to_selection = input.keyboard.get(add_to_selection as usize);
// Select the first point within the threshold (in pixels)
if let Some((mut new_selected, offset)) =
tool_data
.shape_editor
.select_point(&document.document_legacy, input.mouse.position, SELECTION_THRESHOLD, toggle_add_to_selection, responses)
if let Some(mut selected_points) = tool_data
.shape_editor
.select_point(&document.document_legacy, input.mouse.position, SELECTION_THRESHOLD, toggle_add_to_selection, responses)
{
responses.push_back(DocumentMessage::StartTransaction.into());
@@ -171,18 +170,24 @@ impl Fsm for PathToolFsmState {
// Do not snap against handles when anchor is selected
let mut extension = Vec::new();
for &(path, id, point_type) in new_selected.iter() {
if point_type == ManipulatorType::Anchor {
extension.push((path, id, ManipulatorType::InHandle));
extension.push((path, id, ManipulatorType::OutHandle));
for point in selected_points.points.iter() {
if point.manipulator_type == ManipulatorType::Anchor {
extension.push(ManipulatorPointInfo {
manipulator_type: ManipulatorType::InHandle,
..*point
});
extension.push(ManipulatorPointInfo {
manipulator_type: ManipulatorType::OutHandle,
..*point
});
}
}
new_selected.extend(extension);
selected_points.points.extend(extension);
let include_handles = tool_data.shape_editor.selected_layers_ref();
tool_data.snap_manager.add_all_document_handles(document, input, &include_handles, &[], &new_selected);
tool_data.snap_manager.add_all_document_handles(document, input, &include_handles, &[], &selected_points.points);
tool_data.drag_start_pos = input.mouse.position - offset;
tool_data.drag_start_pos = input.mouse.position - selected_points.offset;
PathToolFsmState::Dragging
}
// We didn't find a point nearby, so consider selecting the nearest shape instead
+1 -1
View File
@@ -287,7 +287,7 @@ pub struct ToolFsmState {
impl Default for ToolFsmState {
fn default() -> Self {
ToolFsmState {
Self {
tool_data: ToolData {
active_tool_type: ToolType::Select,
tools: list_tools_in_groups()