Skip to content

Commit 150faa1

Browse files
committed
Dispatcher::handle_message does not need Result anymore (2)
1 parent 739a255 commit 150faa1

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

frontend/wasm/src/document.rs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ fn convert_error(err: editor::EditorError) -> JsValue {
1313
Error::new(&err.to_string()).into()
1414
}
1515

16-
fn dispatch<T: Into<Message>>(message: T) -> Result<(), JsValue> {
16+
fn dispatch<T: Into<Message>>(message: T) {
1717
let messages = crate::EDITOR_STATE.with(|state| state.borrow_mut().handle_message(message.into()));
1818
crate::handle_responses(messages);
19-
Ok(()) // TODO
2019
}
2120

2221
/// Modify the currently selected tool in the document state store
2322
#[wasm_bindgen]
2423
pub fn select_tool(tool: String) -> Result<(), JsValue> {
2524
match translate_tool(&tool) {
26-
Some(tool) => dispatch(ToolMessage::ActivateTool(tool)),
25+
Some(tool) => Ok(dispatch(ToolMessage::ActivateTool(tool))),
2726
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
2827
}
2928
}
@@ -33,7 +32,7 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
3332
pub fn set_tool_options(tool: String, options: &JsValue) -> Result<(), JsValue> {
3433
match options.into_serde::<ToolOptions>() {
3534
Ok(options) => match translate_tool(&tool) {
36-
Some(tool) => dispatch(ToolMessage::SetToolOptions(tool, options)),
35+
Some(tool) => Ok(dispatch(ToolMessage::SetToolOptions(tool, options))),
3736
None => Err(Error::new(&format!("Couldn't set options for {} because it was not recognized as a valid tool", tool)).into()),
3837
},
3938
Err(err) => Err(Error::new(&format!("Invalid JSON for ToolOptions: {}", err)).into()),
@@ -54,73 +53,73 @@ pub fn send_tool_message(tool: String, message: &JsValue) -> Result<(), JsValue>
5453
None => Err(Error::new(&format!("Couldn't send message for {} because it was not recognized as a valid tool", tool)).into()),
5554
};
5655
match tool_message {
57-
Ok(tool_message) => dispatch(tool_message),
56+
Ok(tool_message) => Ok(dispatch(tool_message)),
5857
Err(err) => Err(err),
5958
}
6059
}
6160

6261
#[wasm_bindgen]
63-
pub fn select_document(document: usize) -> Result<(), JsValue> {
62+
pub fn select_document(document: usize) {
6463
dispatch(DocumentsMessage::SelectDocument(document))
6564
}
6665

6766
#[wasm_bindgen]
68-
pub fn get_open_documents_list() -> Result<(), JsValue> {
67+
pub fn get_open_documents_list() {
6968
dispatch(DocumentsMessage::GetOpenDocumentsList)
7069
}
7170

7271
#[wasm_bindgen]
73-
pub fn new_document() -> Result<(), JsValue> {
72+
pub fn new_document() {
7473
dispatch(DocumentsMessage::NewDocument)
7574
}
7675

7776
#[wasm_bindgen]
78-
pub fn open_document() -> Result<(), JsValue> {
77+
pub fn open_document() {
7978
dispatch(DocumentsMessage::OpenDocument)
8079
}
8180

8281
#[wasm_bindgen]
83-
pub fn open_document_file(name: String, content: String) -> Result<(), JsValue> {
82+
pub fn open_document_file(name: String, content: String) {
8483
dispatch(DocumentsMessage::OpenDocumentFile(name, content))
8584
}
8685

8786
#[wasm_bindgen]
88-
pub fn save_document() -> Result<(), JsValue> {
87+
pub fn save_document() {
8988
dispatch(DocumentMessage::SaveDocument)
9089
}
9190

9291
#[wasm_bindgen]
93-
pub fn close_document(document: usize) -> Result<(), JsValue> {
92+
pub fn close_document(document: usize) {
9493
dispatch(DocumentsMessage::CloseDocument(document))
9594
}
9695

9796
#[wasm_bindgen]
98-
pub fn close_all_documents() -> Result<(), JsValue> {
97+
pub fn close_all_documents() {
9998
dispatch(DocumentsMessage::CloseAllDocuments)
10099
}
101100

102101
#[wasm_bindgen]
103-
pub fn close_active_document_with_confirmation() -> Result<(), JsValue> {
102+
pub fn close_active_document_with_confirmation() {
104103
dispatch(DocumentsMessage::CloseActiveDocumentWithConfirmation)
105104
}
106105

107106
#[wasm_bindgen]
108-
pub fn close_all_documents_with_confirmation() -> Result<(), JsValue> {
107+
pub fn close_all_documents_with_confirmation() {
109108
dispatch(DocumentsMessage::CloseAllDocumentsWithConfirmation)
110109
}
111110

112111
/// Send new bounds when document panel viewports get resized or moved within the editor
113112
/// [left, top, right, bottom]...
114113
#[wasm_bindgen]
115-
pub fn bounds_of_viewports(bounds_of_viewports: &[f64]) -> Result<(), JsValue> {
114+
pub fn bounds_of_viewports(bounds_of_viewports: &[f64]) {
116115
let chunked: Vec<_> = bounds_of_viewports.chunks(4).map(ViewportBounds::from_slice).collect();
117116
let ev = InputPreprocessorMessage::BoundsOfViewports(chunked);
118117
dispatch(ev)
119118
}
120119

121120
/// Mouse movement within the screenspace bounds of the viewport
122121
#[wasm_bindgen]
123-
pub fn on_mouse_move(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<(), JsValue> {
122+
pub fn on_mouse_move(x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
124123
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
125124

126125
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("invalid modifier keys");
@@ -131,7 +130,7 @@ pub fn on_mouse_move(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<()
131130

132131
/// Mouse scrolling within the screenspace bounds of the viewport
133132
#[wasm_bindgen]
134-
pub fn on_mouse_scroll(x: f64, y: f64, mouse_keys: u8, wheel_delta_x: i32, wheel_delta_y: i32, wheel_delta_z: i32, modifiers: u8) -> Result<(), JsValue> {
133+
pub fn on_mouse_scroll(x: f64, y: f64, mouse_keys: u8, wheel_delta_x: i32, wheel_delta_y: i32, wheel_delta_z: i32, modifiers: u8) {
135134
let mut editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
136135
editor_mouse_state.scroll_delta = ScrollDelta::new(wheel_delta_x, wheel_delta_y, wheel_delta_z);
137136

@@ -143,7 +142,7 @@ pub fn on_mouse_scroll(x: f64, y: f64, mouse_keys: u8, wheel_delta_x: i32, wheel
143142

144143
/// A mouse button depressed within screenspace the bounds of the viewport
145144
#[wasm_bindgen]
146-
pub fn on_mouse_down(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<(), JsValue> {
145+
pub fn on_mouse_down(x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
147146
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
148147

149148
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("invalid modifier keys");
@@ -154,7 +153,7 @@ pub fn on_mouse_down(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<()
154153

155154
/// A mouse button released
156155
#[wasm_bindgen]
157-
pub fn on_mouse_up(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<(), JsValue> {
156+
pub fn on_mouse_up(x: f64, y: f64, mouse_keys: u8, modifiers: u8) {
158157
let editor_mouse_state = EditorMouseState::from_keys_and_editor_position(mouse_keys, (x, y).into());
159158

160159
let modifier_keys = ModifierKeys::from_bits(modifiers).expect("invalid modifier keys");
@@ -165,7 +164,7 @@ pub fn on_mouse_up(x: f64, y: f64, mouse_keys: u8, modifiers: u8) -> Result<(),
165164

166165
/// A keyboard button depressed within screenspace the bounds of the viewport
167166
#[wasm_bindgen]
168-
pub fn on_key_down(name: String, modifiers: u8) -> Result<(), JsValue> {
167+
pub fn on_key_down(name: String, modifiers: u8) {
169168
let key = translate_key(&name);
170169
let mods = ModifierKeys::from_bits(modifiers).expect("invalid modifier keys");
171170
log::trace!("Key down {:?}, name: {}, modifiers: {:?}", key, name, mods);
@@ -175,7 +174,7 @@ pub fn on_key_down(name: String, modifiers: u8) -> Result<(), JsValue> {
175174

176175
/// A keyboard button released
177176
#[wasm_bindgen]
178-
pub fn on_key_up(name: String, modifiers: u8) -> Result<(), JsValue> {
177+
pub fn on_key_up(name: String, modifiers: u8) {
179178
let key = translate_key(&name);
180179
let mods = ModifierKeys::from_bits(modifiers).expect("invalid modifier keys");
181180
log::trace!("Key up {:?}, name: {}, modifiers: {:?}", key, name, mods);
@@ -185,55 +184,55 @@ pub fn on_key_up(name: String, modifiers: u8) -> Result<(), JsValue> {
185184

186185
/// Update primary color
187186
#[wasm_bindgen]
188-
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
187+
pub fn update_primary_color(primary_color: Color) {
189188
dispatch(ToolMessage::SelectPrimaryColor(primary_color.inner()))
190189
}
191190

192191
/// Update secondary color
193192
#[wasm_bindgen]
194-
pub fn update_secondary_color(secondary_color: Color) -> Result<(), JsValue> {
193+
pub fn update_secondary_color(secondary_color: Color) {
195194
dispatch(ToolMessage::SelectSecondaryColor(secondary_color.inner()))
196195
}
197196

198197
/// Swap primary and secondary color
199198
#[wasm_bindgen]
200-
pub fn swap_colors() -> Result<(), JsValue> {
199+
pub fn swap_colors() {
201200
dispatch(ToolMessage::SwapColors)
202201
}
203202

204203
/// Reset primary and secondary colors to their defaults
205204
#[wasm_bindgen]
206-
pub fn reset_colors() -> Result<(), JsValue> {
205+
pub fn reset_colors() {
207206
dispatch(ToolMessage::ResetColors)
208207
}
209208

210209
/// Undo history one step
211210
#[wasm_bindgen]
212-
pub fn undo() -> Result<(), JsValue> {
211+
pub fn undo() {
213212
dispatch(DocumentMessage::Undo)
214213
}
215214

216215
/// Redo history one step
217216
#[wasm_bindgen]
218-
pub fn redo() -> Result<(), JsValue> {
217+
pub fn redo() {
219218
dispatch(DocumentMessage::Redo)
220219
}
221220

222221
/// Select all layers
223222
#[wasm_bindgen]
224-
pub fn select_all_layers() -> Result<(), JsValue> {
223+
pub fn select_all_layers() {
225224
dispatch(DocumentMessage::SelectAllLayers)
226225
}
227226

228227
/// Deselect all layers
229228
#[wasm_bindgen]
230-
pub fn deselect_all_layers() -> Result<(), JsValue> {
229+
pub fn deselect_all_layers() {
231230
dispatch(DocumentMessage::DeselectAllLayers)
232231
}
233232

234233
/// Reorder selected layer
235234
#[wasm_bindgen]
236-
pub fn reorder_selected_layers(delta: i32) -> Result<(), JsValue> {
235+
pub fn reorder_selected_layers(delta: i32) {
237236
dispatch(DocumentMessage::ReorderSelectedLayers(delta))
238237
}
239238

@@ -260,96 +259,96 @@ pub fn set_blend_mode_for_selected_layers(blend_mode_svg_style_name: String) ->
260259
_ => return Err(convert_error(EditorError::Misc("UnknownBlendMode".to_string()))),
261260
};
262261

263-
dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode))
262+
Ok(dispatch(DocumentMessage::SetBlendModeForSelectedLayers(blend_mode)))
264263
}
265264

266265
/// Set the opacity for the selected layers
267266
#[wasm_bindgen]
268-
pub fn set_opacity_for_selected_layers(opacity_percent: f64) -> Result<(), JsValue> {
267+
pub fn set_opacity_for_selected_layers(opacity_percent: f64) {
269268
dispatch(DocumentMessage::SetOpacityForSelectedLayers(opacity_percent / 100.))
270269
}
271270

272271
/// Export the document
273272
#[wasm_bindgen]
274-
pub fn export_document() -> Result<(), JsValue> {
273+
pub fn export_document() {
275274
dispatch(DocumentMessage::ExportDocument)
276275
}
277276

278277
/// Sets the zoom to the value
279278
#[wasm_bindgen]
280-
pub fn set_canvas_zoom(new_zoom: f64) -> Result<(), JsValue> {
279+
pub fn set_canvas_zoom(new_zoom: f64) {
281280
let ev = MovementMessage::SetCanvasZoom(new_zoom);
282281
dispatch(ev)
283282
}
284283

285284
/// Zoom in to the next step
286285
#[wasm_bindgen]
287-
pub fn increase_canvas_zoom() -> Result<(), JsValue> {
286+
pub fn increase_canvas_zoom() {
288287
let ev = MovementMessage::IncreaseCanvasZoom;
289288
dispatch(ev)
290289
}
291290

292291
/// Zoom out to the next step
293292
#[wasm_bindgen]
294-
pub fn decrease_canvas_zoom() -> Result<(), JsValue> {
293+
pub fn decrease_canvas_zoom() {
295294
let ev = MovementMessage::DecreaseCanvasZoom;
296295
dispatch(ev)
297296
}
298297

299298
/// Sets the rotation to the new value (in radians)
300299
#[wasm_bindgen]
301-
pub fn set_rotation(new_radians: f64) -> Result<(), JsValue> {
300+
pub fn set_rotation(new_radians: f64) {
302301
let ev = MovementMessage::SetCanvasRotation(new_radians);
303302
dispatch(ev)
304303
}
305304

306305
/// Translates document (in viewport coords)
307306
#[wasm_bindgen]
308-
pub fn translate_canvas(delta_x: f64, delta_y: f64) -> Result<(), JsValue> {
307+
pub fn translate_canvas(delta_x: f64, delta_y: f64) {
309308
let ev = MovementMessage::TranslateCanvas((delta_x, delta_y).into());
310309
dispatch(ev)
311310
}
312311

313312
/// Translates document (in viewport coords)
314313
#[wasm_bindgen]
315-
pub fn translate_canvas_by_fraction(delta_x: f64, delta_y: f64) -> Result<(), JsValue> {
314+
pub fn translate_canvas_by_fraction(delta_x: f64, delta_y: f64) {
316315
let ev = MovementMessage::TranslateCanvasByViewportFraction((delta_x, delta_y).into());
317316
dispatch(ev)
318317
}
319318

320319
/// Update the list of selected layers. The layer paths have to be stored in one array and are separated by LayerId::MAX
321320
#[wasm_bindgen]
322-
pub fn select_layers(paths: Vec<LayerId>) -> Result<(), JsValue> {
321+
pub fn select_layers(paths: Vec<LayerId>) {
323322
let paths = paths.split(|id| *id == LayerId::MAX).map(|path| path.to_vec()).collect();
324323
dispatch(DocumentMessage::SetSelectedLayers(paths))
325324
}
326325

327326
/// Toggle visibility of a layer from the layer list
328327
#[wasm_bindgen]
329-
pub fn toggle_layer_visibility(path: Vec<LayerId>) -> Result<(), JsValue> {
328+
pub fn toggle_layer_visibility(path: Vec<LayerId>) {
330329
dispatch(DocumentMessage::ToggleLayerVisibility(path))
331330
}
332331

333332
/// Toggle expansions state of a layer from the layer list
334333
#[wasm_bindgen]
335-
pub fn toggle_layer_expansion(path: Vec<LayerId>) -> Result<(), JsValue> {
334+
pub fn toggle_layer_expansion(path: Vec<LayerId>) {
336335
dispatch(DocumentMessage::ToggleLayerExpansion(path))
337336
}
338337

339338
/// Renames a layer from the layer list
340339
#[wasm_bindgen]
341-
pub fn rename_layer(path: Vec<LayerId>, new_name: String) -> Result<(), JsValue> {
340+
pub fn rename_layer(path: Vec<LayerId>, new_name: String) {
342341
dispatch(DocumentMessage::RenameLayer(path, new_name))
343342
}
344343

345344
/// Deletes a layer from the layer list
346345
#[wasm_bindgen]
347-
pub fn delete_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
346+
pub fn delete_layer(path: Vec<LayerId>) {
348347
dispatch(DocumentMessage::DeleteLayer(path))
349348
}
350349

351350
/// Requests the backend to add a layer to the layer list
352351
#[wasm_bindgen]
353-
pub fn add_folder(path: Vec<LayerId>) -> Result<(), JsValue> {
352+
pub fn add_folder(path: Vec<LayerId>) {
354353
dispatch(DocumentMessage::CreateFolder(path))
355354
}

0 commit comments

Comments
 (0)