@@ -63,41 +63,52 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
63
63
64
64
update_working_colors ( document_data, responses) ;
65
65
}
66
- SelectTool ( tool ) => {
66
+ SelectTool ( new_tool ) => {
67
67
let tool_data = & mut self . tool_state . tool_data ;
68
68
let document_data = & self . tool_state . document_tool_data ;
69
69
let old_tool = tool_data. active_tool_type ;
70
70
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 ,
80
85
} ;
81
- let new = reset ( tool) ;
82
- let old = reset ( old_tool) ;
83
86
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 | {
86
89
if let Some ( tool) = tool_data. tools . get_mut ( & tool_type) {
87
90
tool. process_action ( message, ( document, document_data, input) , responses) ;
88
91
}
89
92
} ;
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
+ }
92
101
93
102
// Special cases for specific tools
94
- if tool == ToolType :: Select {
103
+ if new_tool == ToolType :: Select {
95
104
responses. push_back ( SelectMessage :: UpdateSelectionBoundingBox . into ( ) ) ;
96
105
}
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;
98
109
99
110
// 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 ( ) ) ;
101
112
}
102
113
SwapColors => {
103
114
let document_data = & mut self . tool_state . document_tool_data ;
0 commit comments