Polish a few things (#81)

* Implement/suppress various compiler/clippy lints

* Change `tool_init` to take `ToolType` by value

* Factor out error conversion into a function

* Consume parameters with `todo`

* Make `workspace` stuff public

Making them public also removes the warnings
without having to suppress them.

Also, this commit removes the unused import of
`EditorError`

* Remove allow(unused_variables), use vars in `todo`

Also implements `Debug` on `DocumentToolData`
This commit is contained in:
T0mstone
2021-04-22 17:05:25 +02:00
committed by Keavon Chambers
parent 87ed3a1bbd
commit b556dd6bfd
11 changed files with 54 additions and 49 deletions

View File

@@ -1,14 +1,18 @@
use crate::shims::Error;
use crate::wrappers::{translate_key, translate_tool, Color};
use crate::EDITOR_STATE;
use crate::{shims::Error, utils};
use editor_core::events;
use wasm_bindgen::prelude::*;
fn convert_error(err: editor_core::EditorError) -> JsValue {
Error::new(&err.to_string()).into()
}
/// Modify the currently selected tool in the document state store
#[wasm_bindgen]
pub fn select_tool(tool: String) -> Result<(), JsValue> {
EDITOR_STATE.with(|editor| match translate_tool(&tool) {
Some(tool) => editor.borrow_mut().handle_event(events::Event::SelectTool(tool)).map_err(|err| Error::new(&err.to_string()).into()),
Some(tool) => editor.borrow_mut().handle_event(events::Event::SelectTool(tool)).map_err(convert_error),
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
})
}
@@ -19,7 +23,7 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
pub fn on_mouse_move(x: u32, y: u32) -> Result<(), JsValue> {
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
let ev = events::Event::MouseMove(events::ViewportPosition { x, y });
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
}
/// A mouse button depressed within screenspace the bounds of the viewport
@@ -31,7 +35,7 @@ pub fn on_mouse_down(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
position: events::ViewportPosition { x, y },
mouse_keys,
});
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
}
/// A mouse button released
@@ -43,7 +47,7 @@ pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
position: events::ViewportPosition { x, y },
mouse_keys,
});
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
}
/// A keyboard button depressed within screenspace the bounds of the viewport
@@ -52,7 +56,7 @@ pub fn on_key_down(name: String) -> Result<(), JsValue> {
let key = translate_key(&name);
log::trace!("key down {:?}, name: {}", key, name);
let ev = events::Event::KeyDown(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
}
/// A keyboard button released
@@ -61,7 +65,7 @@ pub fn on_key_up(name: String) -> Result<(), JsValue> {
let key = translate_key(&name);
log::trace!("key up {:?}, name: {}", key, name);
let ev = events::Event::KeyUp(key);
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
}
/// Update primary color
@@ -69,7 +73,7 @@ pub fn on_key_up(name: String) -> Result<(), JsValue> {
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectPrimaryColor(primary_color.inner())))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
.map_err(convert_error)
}
/// Update secondary color
@@ -77,21 +81,17 @@ pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
pub fn update_secondary_color(secondary_color: Color) -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectSecondaryColor(secondary_color.inner())))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
.map_err(convert_error)
}
/// Swap primary and secondary color
#[wasm_bindgen]
pub fn swap_colors() -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::SwapColors))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::SwapColors)).map_err(convert_error)
}
/// Reset primary and secondary colors to their defaults
#[wasm_bindgen]
pub fn reset_colors() -> Result<(), JsValue> {
EDITOR_STATE
.with(|editor| editor.borrow_mut().handle_event(events::Event::ResetColors))
.map_err(|err: editor_core::EditorError| Error::new(&err.to_string()).into())
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::ResetColors)).map_err(convert_error)
}

View File

@@ -5,32 +5,32 @@ type DocumentId = u32;
/// Modify the active Document in the editor state store
#[wasm_bindgen]
pub fn set_active_document(document_id: DocumentId) {
todo!()
todo!("set_active_document {}", document_id)
}
/// Query the name of a specific document
#[wasm_bindgen]
pub fn get_document_name(document_id: DocumentId) -> String {
todo!()
todo!("get_document_name {}", document_id)
}
/// Query the id of the most recently interacted with document
#[wasm_bindgen]
pub fn get_active_document() -> DocumentId {
todo!()
todo!("get_active_document")
}
use editor_core::workspace::PanelId;
/// Notify the editor that the mouse hovers above a panel
#[wasm_bindgen]
pub fn panel_hover_enter(panel_id: PanelId) {
todo!()
todo!("panel_hover_enter {}", panel_id)
}
/// Query a list of currently available operations
#[wasm_bindgen]
pub fn get_available_operations() -> Vec<JsValue> {
todo!();
todo!("get_available_operations")
// vec!["example1", "example2"].into_iter().map(JsValue::from).collect()
}