mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-27 10:08:12 +08:00
Modify all message enum data to use named struct values, not tuples (#479)
* Massively reorganize and clean up the whole Rust codebase * Modify all message enum data to use named struct values, not tuples
This commit is contained in:
@@ -10,7 +10,9 @@ use serde::{Deserialize, Serialize};
|
||||
#[impl_message(Message, Tool)]
|
||||
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum ToolMessage {
|
||||
ActivateTool(ToolType),
|
||||
ActivateTool {
|
||||
tool_type: ToolType,
|
||||
},
|
||||
#[child]
|
||||
Crop(CropMessage),
|
||||
DocumentIsDirty,
|
||||
@@ -34,9 +36,16 @@ pub enum ToolMessage {
|
||||
ResetColors,
|
||||
#[child]
|
||||
Select(SelectMessage),
|
||||
SelectPrimaryColor(Color),
|
||||
SelectSecondaryColor(Color),
|
||||
SetToolOptions(ToolType, ToolOptions),
|
||||
SelectPrimaryColor {
|
||||
color: Color,
|
||||
},
|
||||
SelectSecondaryColor {
|
||||
color: Color,
|
||||
},
|
||||
SetToolOptions {
|
||||
tool_type: ToolType,
|
||||
tool_options: ToolOptions,
|
||||
},
|
||||
#[child]
|
||||
Shape(ShapeMessage),
|
||||
SwapColors,
|
||||
|
||||
@@ -20,13 +20,13 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessorMes
|
||||
let (document, input) = data;
|
||||
#[remain::sorted]
|
||||
match message {
|
||||
ActivateTool(new_tool) => {
|
||||
ActivateTool { tool_type } => {
|
||||
let tool_data = &mut self.tool_state.tool_data;
|
||||
let document_data = &self.tool_state.document_tool_data;
|
||||
let old_tool = tool_data.active_tool_type;
|
||||
|
||||
// Do nothing if switching to the same tool
|
||||
if new_tool == old_tool {
|
||||
if tool_type == old_tool {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -41,24 +41,24 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessorMes
|
||||
}
|
||||
};
|
||||
// Send the old and new tools a transition to their FSM Abort states
|
||||
if let Some(tool_message) = standard_tool_message(new_tool, StandardToolMessageType::Abort) {
|
||||
send_abort_to_tool(new_tool, tool_message, true);
|
||||
if let Some(tool_message) = standard_tool_message(tool_type, StandardToolMessageType::Abort) {
|
||||
send_abort_to_tool(tool_type, tool_message, true);
|
||||
}
|
||||
if let Some(tool_message) = standard_tool_message(old_tool, StandardToolMessageType::Abort) {
|
||||
send_abort_to_tool(old_tool, tool_message, false);
|
||||
}
|
||||
|
||||
// Send the DocumentIsDirty message to the active tool's sub-tool message handler
|
||||
if let Some(message) = standard_tool_message(new_tool, StandardToolMessageType::DocumentIsDirty) {
|
||||
if let Some(message) = standard_tool_message(tool_type, StandardToolMessageType::DocumentIsDirty) {
|
||||
responses.push_back(message.into());
|
||||
}
|
||||
|
||||
// Store the new active tool
|
||||
tool_data.active_tool_type = new_tool;
|
||||
tool_data.active_tool_type = tool_type;
|
||||
|
||||
// Notify the frontend about the new active tool to be displayed
|
||||
let tool_name = new_tool.to_string();
|
||||
let tool_options = self.tool_state.document_tool_data.tool_options.get(&new_tool).copied();
|
||||
let tool_name = tool_type.to_string();
|
||||
let tool_options = self.tool_state.document_tool_data.tool_options.get(&tool_type).copied();
|
||||
responses.push_back(FrontendMessage::UpdateActiveTool { tool_name, tool_options }.into());
|
||||
}
|
||||
DocumentIsDirty => {
|
||||
@@ -76,19 +76,19 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessorMes
|
||||
|
||||
update_working_colors(document_data, responses);
|
||||
}
|
||||
SelectPrimaryColor(color) => {
|
||||
SelectPrimaryColor { color } => {
|
||||
let document_data = &mut self.tool_state.document_tool_data;
|
||||
document_data.primary_color = color;
|
||||
|
||||
update_working_colors(&self.tool_state.document_tool_data, responses);
|
||||
}
|
||||
SelectSecondaryColor(color) => {
|
||||
SelectSecondaryColor { color } => {
|
||||
let document_data = &mut self.tool_state.document_tool_data;
|
||||
document_data.secondary_color = color;
|
||||
|
||||
update_working_colors(document_data, responses);
|
||||
}
|
||||
SetToolOptions(tool_type, tool_options) => {
|
||||
SetToolOptions { tool_type, tool_options } => {
|
||||
let document_data = &mut self.tool_state.document_tool_data;
|
||||
|
||||
document_data.tool_options.insert(tool_type, tool_options);
|
||||
|
||||
@@ -87,8 +87,8 @@ impl Fsm for EyedropperToolFsmState {
|
||||
if let Some(fill) = shape.style.fill() {
|
||||
if let Some(color) = fill.color() {
|
||||
match lmb_or_rmb {
|
||||
EyedropperMessage::LeftMouseDown => responses.push_back(ToolMessage::SelectPrimaryColor(color).into()),
|
||||
EyedropperMessage::RightMouseDown => responses.push_back(ToolMessage::SelectSecondaryColor(color).into()),
|
||||
EyedropperMessage::LeftMouseDown => responses.push_back(ToolMessage::SelectPrimaryColor { color }.into()),
|
||||
EyedropperMessage::RightMouseDown => responses.push_back(ToolMessage::SelectSecondaryColor { color }.into()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
use crate::document::DocumentMessageHandler;
|
||||
use crate::input::keyboard::Key;
|
||||
use crate::input::mouse::ViewportPosition;
|
||||
use crate::input::InputPreprocessorMessageHandler;
|
||||
use crate::message_prelude::*;
|
||||
use crate::viewport_tools::snapping::SnapHandler;
|
||||
|
||||
use graphene::Operation;
|
||||
|
||||
use glam::{DAffine2, DVec2, Vec2Swizzles};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Resize {
|
||||
pub drag_start: ViewportPosition,
|
||||
pub path: Option<Vec<LayerId>>,
|
||||
snap_handler: SnapHandler,
|
||||
}
|
||||
impl Resize {
|
||||
/// Starts a resize, assigning the snap targets and snapping the starting position.
|
||||
pub fn start(&mut self, document: &DocumentMessageHandler, mouse_position: DVec2) {
|
||||
let layers = document.all_layers_sorted();
|
||||
self.snap_handler.start_snap(document, layers, &[]);
|
||||
self.drag_start = self.snap_handler.snap_position(document, mouse_position);
|
||||
}
|
||||
|
||||
pub fn calculate_transform(&self, document: &DocumentMessageHandler, center: Key, lock_ratio: Key, ipp: &InputPreprocessorMessageHandler) -> Option<Message> {
|
||||
if let Some(path) = &self.path {
|
||||
let mut start = self.drag_start;
|
||||
|
||||
let stop = self.snap_handler.snap_position(document, ipp.mouse.position);
|
||||
|
||||
let mut size = stop - start;
|
||||
if ipp.keyboard.get(lock_ratio as usize) {
|
||||
size = size.abs().max(size.abs().yx()) * size.signum();
|
||||
}
|
||||
if ipp.keyboard.get(center as usize) {
|
||||
start -= size;
|
||||
size *= 2.;
|
||||
}
|
||||
|
||||
Some(
|
||||
Operation::SetLayerTransformInViewport {
|
||||
path: path.to_vec(),
|
||||
transform: DAffine2::from_scale_angle_translation(size, 0., start).to_cols_array(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cleanup(&mut self) {
|
||||
self.snap_handler.cleanup();
|
||||
self.path = None;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ pub enum SelectMessage {
|
||||
DragStop,
|
||||
MouseMove { snap_angle: Key },
|
||||
|
||||
Align(AlignAxis, AlignAggregate),
|
||||
Align { axis: AlignAxis, aggregate: AlignAggregate },
|
||||
FlipHorizontal,
|
||||
FlipVertical,
|
||||
}
|
||||
@@ -180,7 +180,7 @@ impl Fsm for SelectToolFsmState {
|
||||
|
||||
if let Some(intersection) = intersection.pop() {
|
||||
selected = vec![intersection];
|
||||
buffer.push(DocumentMessage::AddSelectedLayers(selected.clone()).into());
|
||||
buffer.push(DocumentMessage::AddSelectedLayers { additional_layers: selected.clone() }.into());
|
||||
buffer.push(DocumentMessage::StartTransaction.into());
|
||||
data.layers_dragging.append(&mut selected);
|
||||
Dragging
|
||||
@@ -258,7 +258,12 @@ impl Fsm for SelectToolFsmState {
|
||||
}
|
||||
(DrawingBox, DragStop) => {
|
||||
let quad = data.selection_quad();
|
||||
responses.push_front(DocumentMessage::AddSelectedLayers(document.graphene_document.intersects_quad_root(quad)).into());
|
||||
responses.push_front(
|
||||
DocumentMessage::AddSelectedLayers {
|
||||
additional_layers: document.graphene_document.intersects_quad_root(quad),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
responses.push_front(
|
||||
DocumentMessage::Overlays(
|
||||
Operation::DeleteLayer {
|
||||
@@ -276,18 +281,18 @@ impl Fsm for SelectToolFsmState {
|
||||
delete(&mut data.bounding_box_overlay_layer);
|
||||
Ready
|
||||
}
|
||||
(_, Align(axis, aggregate)) => {
|
||||
responses.push_back(DocumentMessage::AlignSelectedLayers(axis, aggregate).into());
|
||||
(_, Align { axis, aggregate }) => {
|
||||
responses.push_back(DocumentMessage::AlignSelectedLayers { axis, aggregate }.into());
|
||||
|
||||
self
|
||||
}
|
||||
(_, FlipHorizontal) => {
|
||||
responses.push_back(DocumentMessage::FlipSelectedLayers(FlipAxis::X).into());
|
||||
responses.push_back(DocumentMessage::FlipSelectedLayers { flip_axis: FlipAxis::X }.into());
|
||||
|
||||
self
|
||||
}
|
||||
(_, FlipVertical) => {
|
||||
responses.push_back(DocumentMessage::FlipSelectedLayers(FlipAxis::Y).into());
|
||||
responses.push_back(DocumentMessage::FlipSelectedLayers { flip_axis: FlipAxis::Y }.into());
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user