@@ -51,16 +51,24 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
51
51
let ( document, input) = data;
52
52
use ToolMessage :: * ;
53
53
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
+
56
58
update_working_colors ( & self . tool_state . document_tool_data , responses) ;
57
59
}
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) ;
61
65
}
62
66
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
64
72
let reset = |tool| match tool {
65
73
ToolType :: Ellipse => EllipseMessage :: Abort . into ( ) ,
66
74
ToolType :: Rectangle => RectangleMessage :: Abort . into ( ) ,
@@ -70,40 +78,55 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
70
78
ToolType :: Select => SelectMessage :: Abort . into ( ) ,
71
79
_ => ToolMessage :: NoOp ,
72
80
} ;
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
74
85
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) ;
77
88
}
78
89
} ;
79
90
send_to_tool ( tool, new) ;
80
91
send_to_tool ( old_tool, old) ;
92
+
93
+ // Special cases for specific tools
81
94
if tool == ToolType :: Select {
82
95
responses. push_back ( SelectMessage :: UpdateSelectionBoundingBox . into ( ) ) ;
83
96
}
84
97
self . tool_state . tool_data . active_tool_type = tool;
85
98
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 ( ) ) ;
87
101
}
88
102
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) ;
92
108
}
93
109
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) ;
98
116
}
99
117
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) ;
101
121
}
102
122
message => {
103
123
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) ;
107
130
}
108
131
}
109
132
}
@@ -112,13 +135,14 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
112
135
fn actions ( & self ) -> ActionList {
113
136
let mut list = actions ! ( ToolMessageDiscriminant ; ResetColors , SwapColors , SelectTool , SetToolOptions ) ;
114
137
list. extend ( self . tool_state . tool_data . active_tool ( ) . actions ( ) ) ;
138
+
115
139
list
116
140
}
117
141
}
118
142
119
143
fn message_to_tool_type ( message : & ToolMessage ) -> ToolType {
120
144
use ToolMessage :: * ;
121
- match message {
145
+ let tool_type = match message {
122
146
Fill ( _) => ToolType :: Fill ,
123
147
Rectangle ( _) => ToolType :: Rectangle ,
124
148
Ellipse ( _) => ToolType :: Ellipse ,
@@ -131,14 +155,16 @@ fn message_to_tool_type(message: &ToolMessage) -> ToolType {
131
155
Navigate ( _) => ToolType :: Navigate ,
132
156
Path ( _) => ToolType :: Path ,
133
157
_ => unreachable ! ( ) ,
134
- }
158
+ } ;
159
+
160
+ tool_type
135
161
}
136
162
137
- fn update_working_colors ( doc_data : & DocumentToolData , responses : & mut VecDeque < Message > ) {
163
+ fn update_working_colors ( document_data : & DocumentToolData , responses : & mut VecDeque < Message > ) {
138
164
responses. push_back (
139
165
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 ,
142
168
}
143
169
. into ( ) ,
144
170
) ;
0 commit comments