Skip to content

Commit 7513b17

Browse files
committed
Fix bug where swiching to the Fill Tool would trigger a fill operation
1 parent d6475cf commit 7513b17

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

editor/src/tool/tool_message_handler.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,41 +63,52 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
6363

6464
update_working_colors(document_data, responses);
6565
}
66-
SelectTool(tool) => {
66+
SelectTool(new_tool) => {
6767
let tool_data = &mut self.tool_state.tool_data;
6868
let document_data = &self.tool_state.document_tool_data;
6969
let old_tool = tool_data.active_tool_type;
7070

71-
// Prepare to reset the old and new tools by obtaining their FSM Abort state, which will be sent to the tool
72-
let reset = |tool| match tool {
73-
ToolType::Ellipse => EllipseMessage::Abort.into(),
74-
ToolType::Rectangle => RectangleMessage::Abort.into(),
75-
ToolType::Shape => ShapeMessage::Abort.into(),
76-
ToolType::Line => LineMessage::Abort.into(),
77-
ToolType::Pen => PenMessage::Abort.into(),
78-
ToolType::Select => SelectMessage::Abort.into(),
79-
_ => ToolMessage::NoOp,
71+
// Do nothing if switching to the same tool
72+
if new_tool == old_tool {
73+
return;
74+
}
75+
76+
// Get the Abort state of a tool's FSM
77+
let reset_message = |tool| match tool {
78+
ToolType::Ellipse => Some(EllipseMessage::Abort.into()),
79+
ToolType::Rectangle => Some(RectangleMessage::Abort.into()),
80+
ToolType::Shape => Some(ShapeMessage::Abort.into()),
81+
ToolType::Line => Some(LineMessage::Abort.into()),
82+
ToolType::Pen => Some(PenMessage::Abort.into()),
83+
ToolType::Select => Some(SelectMessage::Abort.into()),
84+
_ => None,
8085
};
81-
let new = reset(tool);
82-
let old = reset(old_tool);
8386

84-
// Send the old and new tools a transition to the FSM Abort state
85-
let mut send_to_tool = |tool_type, message: ToolMessage| {
87+
// Send the Abort state transition to the tool
88+
let mut send_message_to_tool = |tool_type, message: ToolMessage| {
8689
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
8790
tool.process_action(message, (document, document_data, input), responses);
8891
}
8992
};
90-
send_to_tool(tool, new);
91-
send_to_tool(old_tool, old);
93+
94+
// Send the old and new tools a transition to their FSM Abort states
95+
if let Some(tool_message) = reset_message(new_tool) {
96+
send_message_to_tool(new_tool, tool_message);
97+
}
98+
if let Some(tool_message) = reset_message(old_tool) {
99+
send_message_to_tool(old_tool, tool_message);
100+
}
92101

93102
// Special cases for specific tools
94-
if tool == ToolType::Select {
103+
if new_tool == ToolType::Select {
95104
responses.push_back(SelectMessage::UpdateSelectionBoundingBox.into());
96105
}
97-
self.tool_state.tool_data.active_tool_type = tool;
106+
107+
// Store the new active tool
108+
tool_data.active_tool_type = new_tool;
98109

99110
// Notify the frontend about the new active tool to be displayed
100-
responses.push_back(FrontendMessage::SetActiveTool { tool_name: tool.to_string() }.into());
111+
responses.push_back(FrontendMessage::SetActiveTool { tool_name: new_tool.to_string() }.into());
101112
}
102113
SwapColors => {
103114
let document_data = &mut self.tool_state.document_tool_data;

0 commit comments

Comments
 (0)