Skip to content

Commit d6475cf

Browse files
committed
Minor code readability improvements
1 parent 07cc2c2 commit d6475cf

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

editor/src/document/document_file.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,19 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
383383
self.backup();
384384
responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into());
385385
for path in self.selected_layers().cloned() {
386-
responses.push_front(DocumentOperation::DeleteLayer { path }.into())
386+
responses.push_front(DocumentOperation::DeleteLayer { path }.into());
387387
}
388388
}
389389
ClearOverlays => {
390390
responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into());
391391
for path in self.layer_data.keys().filter(|path| self.document.layer(path).unwrap().overlay).cloned() {
392-
responses.push_front(DocumentOperation::DeleteLayer { path }.into())
392+
responses.push_front(DocumentOperation::DeleteLayer { path }.into());
393393
}
394394
}
395395
DuplicateSelectedLayers => {
396396
self.backup();
397397
for path in self.selected_layers_sorted() {
398-
responses.push_back(DocumentOperation::DuplicateLayer { path }.into())
398+
responses.push_back(DocumentOperation::DuplicateLayer { path }.into());
399399
}
400400
}
401401
SetSelectedLayers(paths) => {
@@ -465,7 +465,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
465465
.flatten(),
466466
);
467467
if canvas_dirty {
468-
responses.push_back(RenderDocument.into())
468+
responses.push_back(RenderDocument.into());
469469
}
470470
}
471471
Err(e) => log::error!("DocumentError: {:?}", e),
@@ -539,7 +539,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
539539
let offset = if relative_position < 0 || non_selected.is_empty() { 0 } else { 1 };
540540
let fallback = offset * (non_selected.len());
541541
let insert_index = non_selected.iter().position(|x| *x == id).map(|x| x + offset).unwrap_or(fallback) as isize;
542-
responses.push_back(DocumentMessage::MoveSelectedLayersTo { path: path.to_vec(), insert_index }.into())
542+
responses.push_back(DocumentMessage::MoveSelectedLayersTo { path: path.to_vec(), insert_index }.into());
543543
}
544544
}
545545
}

editor/src/tool/tool_message_handler.rs

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,24 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
5151
let (document, input) = data;
5252
use ToolMessage::*;
5353
match message {
54-
SelectPrimaryColor(c) => {
55-
self.tool_state.document_tool_data.primary_color = c;
54+
SelectPrimaryColor(color) => {
55+
let document_data = &mut self.tool_state.document_tool_data;
56+
document_data.primary_color = color;
57+
5658
update_working_colors(&self.tool_state.document_tool_data, responses);
5759
}
58-
SelectSecondaryColor(c) => {
59-
self.tool_state.document_tool_data.secondary_color = c;
60-
update_working_colors(&self.tool_state.document_tool_data, responses);
60+
SelectSecondaryColor(color) => {
61+
let document_data = &mut self.tool_state.document_tool_data;
62+
document_data.secondary_color = color;
63+
64+
update_working_colors(document_data, responses);
6165
}
6266
SelectTool(tool) => {
63-
let old_tool = self.tool_state.tool_data.active_tool_type;
67+
let tool_data = &mut self.tool_state.tool_data;
68+
let document_data = &self.tool_state.document_tool_data;
69+
let old_tool = tool_data.active_tool_type;
70+
71+
// Prepare to reset the old and new tools by obtaining their FSM Abort state, which will be sent to the tool
6472
let reset = |tool| match tool {
6573
ToolType::Ellipse => EllipseMessage::Abort.into(),
6674
ToolType::Rectangle => RectangleMessage::Abort.into(),
@@ -70,40 +78,55 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
7078
ToolType::Select => SelectMessage::Abort.into(),
7179
_ => ToolMessage::NoOp,
7280
};
73-
let (new, old) = (reset(tool), reset(old_tool));
81+
let new = reset(tool);
82+
let old = reset(old_tool);
83+
84+
// Send the old and new tools a transition to the FSM Abort state
7485
let mut send_to_tool = |tool_type, message: ToolMessage| {
75-
if let Some(tool) = self.tool_state.tool_data.tools.get_mut(&tool_type) {
76-
tool.process_action(message, (document, &self.tool_state.document_tool_data, input), responses);
86+
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
87+
tool.process_action(message, (document, document_data, input), responses);
7788
}
7889
};
7990
send_to_tool(tool, new);
8091
send_to_tool(old_tool, old);
92+
93+
// Special cases for specific tools
8194
if tool == ToolType::Select {
8295
responses.push_back(SelectMessage::UpdateSelectionBoundingBox.into());
8396
}
8497
self.tool_state.tool_data.active_tool_type = tool;
8598

86-
responses.push_back(FrontendMessage::SetActiveTool { tool_name: tool.to_string() }.into())
99+
// Notify the frontend about the new active tool to be displayed
100+
responses.push_back(FrontendMessage::SetActiveTool { tool_name: tool.to_string() }.into());
87101
}
88102
SwapColors => {
89-
let doc_data = &mut self.tool_state.document_tool_data;
90-
std::mem::swap(&mut doc_data.primary_color, &mut doc_data.secondary_color);
91-
update_working_colors(doc_data, responses);
103+
let document_data = &mut self.tool_state.document_tool_data;
104+
105+
std::mem::swap(&mut document_data.primary_color, &mut document_data.secondary_color);
106+
107+
update_working_colors(document_data, responses);
92108
}
93109
ResetColors => {
94-
let doc_data = &mut self.tool_state.document_tool_data;
95-
doc_data.primary_color = Color::BLACK;
96-
doc_data.secondary_color = Color::WHITE;
97-
update_working_colors(doc_data, responses);
110+
let document_data = &mut self.tool_state.document_tool_data;
111+
112+
document_data.primary_color = Color::BLACK;
113+
document_data.secondary_color = Color::WHITE;
114+
115+
update_working_colors(document_data, responses);
98116
}
99117
SetToolOptions(tool_type, tool_options) => {
100-
self.tool_state.document_tool_data.tool_options.insert(tool_type, tool_options);
118+
let document_data = &mut self.tool_state.document_tool_data;
119+
120+
document_data.tool_options.insert(tool_type, tool_options);
101121
}
102122
message => {
103123
let tool_type = message_to_tool_type(&message);
104-
if let Some(tool) = self.tool_state.tool_data.tools.get_mut(&tool_type) {
105-
if tool_type == self.tool_state.tool_data.active_tool_type {
106-
tool.process_action(message, (document, &self.tool_state.document_tool_data, input), responses);
124+
let document_data = &self.tool_state.document_tool_data;
125+
let tool_data = &mut self.tool_state.tool_data;
126+
127+
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
128+
if tool_type == tool_data.active_tool_type {
129+
tool.process_action(message, (document, document_data, input), responses);
107130
}
108131
}
109132
}
@@ -112,13 +135,14 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
112135
fn actions(&self) -> ActionList {
113136
let mut list = actions!(ToolMessageDiscriminant; ResetColors, SwapColors, SelectTool, SetToolOptions);
114137
list.extend(self.tool_state.tool_data.active_tool().actions());
138+
115139
list
116140
}
117141
}
118142

119143
fn message_to_tool_type(message: &ToolMessage) -> ToolType {
120144
use ToolMessage::*;
121-
match message {
145+
let tool_type = match message {
122146
Fill(_) => ToolType::Fill,
123147
Rectangle(_) => ToolType::Rectangle,
124148
Ellipse(_) => ToolType::Ellipse,
@@ -131,14 +155,16 @@ fn message_to_tool_type(message: &ToolMessage) -> ToolType {
131155
Navigate(_) => ToolType::Navigate,
132156
Path(_) => ToolType::Path,
133157
_ => unreachable!(),
134-
}
158+
};
159+
160+
tool_type
135161
}
136162

137-
fn update_working_colors(doc_data: &DocumentToolData, responses: &mut VecDeque<Message>) {
163+
fn update_working_colors(document_data: &DocumentToolData, responses: &mut VecDeque<Message>) {
138164
responses.push_back(
139165
FrontendMessage::UpdateWorkingColors {
140-
primary: doc_data.primary_color,
141-
secondary: doc_data.secondary_color,
166+
primary: document_data.primary_color,
167+
secondary: document_data.secondary_color,
142168
}
143169
.into(),
144170
);

0 commit comments

Comments
 (0)