From 70f70b70fcbdaf7011feffdc19370f0e3633281a Mon Sep 17 00:00:00 2001 From: T0mstone <39707032+T0mstone@users.noreply.github.com> Date: Thu, 22 Apr 2021 17:05:25 +0200 Subject: [PATCH] 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` --- client/web/wasm/src/document.rs | 30 +++++++++++++++--------------- client/web/wasm/src/window.rs | 10 +++++----- core/document/src/shape_points.rs | 19 ++++++++++--------- core/editor/src/lib.rs | 3 +++ core/editor/src/tools/crop.rs | 2 +- core/editor/src/tools/mod.rs | 10 ++++++---- core/editor/src/tools/navigate.rs | 2 +- core/editor/src/tools/path.rs | 2 +- core/editor/src/tools/pen.rs | 2 +- core/editor/src/tools/sample.rs | 2 +- core/editor/src/workspace/mod.rs | 21 ++++++++++----------- 11 files changed, 54 insertions(+), 49 deletions(-) diff --git a/client/web/wasm/src/document.rs b/client/web/wasm/src/document.rs index db8219175..6de20fc1e 100644 --- a/client/web/wasm/src/document.rs +++ b/client/web/wasm/src/document.rs @@ -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) } diff --git a/client/web/wasm/src/window.rs b/client/web/wasm/src/window.rs index bc604d038..d7ff356e0 100644 --- a/client/web/wasm/src/window.rs +++ b/client/web/wasm/src/window.rs @@ -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 { - todo!(); + todo!("get_available_operations") // vec!["example1", "example2"].into_iter().map(JsValue::from).collect() } diff --git a/core/document/src/shape_points.rs b/core/document/src/shape_points.rs index 125c49a4d..f2c12d7e4 100644 --- a/core/document/src/shape_points.rs +++ b/core/document/src/shape_points.rs @@ -17,7 +17,7 @@ impl ShapePoints { ShapePoints { center: center.into(), extent: extent.into(), - sides: sides, + sides, } } @@ -36,7 +36,7 @@ impl ShapePoints { // Gets the length of one side #[inline] pub fn side_length(&self) -> f64 { - self.apothem_offset_angle().sin() * (self.sides as f64) * (2 as f64) + self.apothem_offset_angle().sin() * (self.sides as f64) * 2f64 } } @@ -45,7 +45,7 @@ impl std::fmt::Display for ShapePoints { fn rotate(v: &Vec2, theta: f64) -> Vec2 { let cosine = theta.cos(); let sine = theta.sin(); - return Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine); + Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine) } info!("sides{}", self.sides); for i in 0..self.sides { @@ -72,7 +72,7 @@ impl Iterator for ShapePathIter { fn rotate(v: &Vec2, theta: f64) -> Vec2 { let cosine = theta.cos(); let sine = theta.sin(); - return Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine); + Vec2::new(v.x * cosine - v.y * sine, v.x * sine + v.y * cosine) } self.ix += 1; match self.ix { @@ -102,9 +102,9 @@ impl Add for ShapePoints { impl kurbo::Shape for ShapePoints { type PathElementsIter = ShapePathIter; - #[inline] - fn perimeter(&self, _accuracy: f64) -> f64 { - self.side_length() * (self.sides as f64) + + fn path_elements(&self, _tolerance: f64) -> Self::PathElementsIter { + todo!() } #[inline] @@ -112,8 +112,9 @@ impl kurbo::Shape for ShapePoints { self.apothem() * self.perimeter(2.1) } - fn path_elements(&self, _tolerance: f64) -> Self::PathElementsIter { - todo!() + #[inline] + fn perimeter(&self, _accuracy: f64) -> f64 { + self.side_length() * (self.sides as f64) } fn winding(&self, _pt: Point) -> i32 { diff --git a/core/editor/src/lib.rs b/core/editor/src/lib.rs index 7e0c403af..a91a85827 100644 --- a/core/editor/src/lib.rs +++ b/core/editor/src/lib.rs @@ -1,3 +1,6 @@ +// since our policy is tabs, we want to stop clippy from warning about that +#![allow(clippy::tabs_in_doc_comments)] + #[macro_use] mod macros; diff --git a/core/editor/src/tools/crop.rs b/core/editor/src/tools/crop.rs index ef3f1c9fe..78d727958 100644 --- a/core/editor/src/tools/crop.rs +++ b/core/editor/src/tools/crop.rs @@ -10,6 +10,6 @@ pub struct Crop; impl Tool for Crop { fn handle_input(&mut self, event: &Event, document: &Document, tool_data: &DocumentToolData) -> (Vec, Vec) { - todo!(); + todo!("{}::handle_input {:?} {:?} {:?}", module_path!(), event, document, tool_data) } } diff --git a/core/editor/src/tools/mod.rs b/core/editor/src/tools/mod.rs index f3c1de13a..b587328f9 100644 --- a/core/editor/src/tools/mod.rs +++ b/core/editor/src/tools/mod.rs @@ -25,12 +25,14 @@ pub trait Fsm { fn transition(self, event: &Event, document: &Document, tool_data: &DocumentToolData, data: &mut Self::ToolData, responses: &mut Vec, operations: &mut Vec) -> Self; } +#[derive(Debug)] pub struct DocumentToolData { pub mouse_state: MouseState, pub mod_keys: ModKeys, pub primary_color: Color, pub secondary_color: Color, } + pub struct ToolData { pub active_tool_type: ToolType, pub tools: HashMap>, @@ -97,11 +99,11 @@ impl ToolFsmState { } fn default_tool_settings() -> HashMap { - let tool_init = |tool: &ToolType| (*tool, tool.default_settings()); + let tool_init = |tool: ToolType| (tool, tool.default_settings()); std::array::IntoIter::new([ - tool_init(&ToolType::Select), - tool_init(&ToolType::Ellipse), - tool_init(&ToolType::Shape), // TODO: Add more tool defaults + tool_init(ToolType::Select), + tool_init(ToolType::Ellipse), + tool_init(ToolType::Shape), // TODO: Add more tool defaults ]) .collect() } diff --git a/core/editor/src/tools/navigate.rs b/core/editor/src/tools/navigate.rs index 8c021a3fb..6293721d6 100644 --- a/core/editor/src/tools/navigate.rs +++ b/core/editor/src/tools/navigate.rs @@ -10,6 +10,6 @@ pub struct Navigate; impl Tool for Navigate { fn handle_input(&mut self, event: &Event, document: &Document, tool_data: &DocumentToolData) -> (Vec, Vec) { - todo!(); + todo!("{}::handle_input {:?} {:?} {:?}", module_path!(), event, document, tool_data) } } diff --git a/core/editor/src/tools/path.rs b/core/editor/src/tools/path.rs index 1d00e2b94..ef3fcaaf0 100644 --- a/core/editor/src/tools/path.rs +++ b/core/editor/src/tools/path.rs @@ -10,6 +10,6 @@ pub struct Path; impl Tool for Path { fn handle_input(&mut self, event: &Event, document: &Document, tool_data: &DocumentToolData) -> (Vec, Vec) { - todo!(); + todo!("{}::handle_input {:?} {:?} {:?}", module_path!(), event, document, tool_data) } } diff --git a/core/editor/src/tools/pen.rs b/core/editor/src/tools/pen.rs index 7d2f2a03d..812e881a7 100644 --- a/core/editor/src/tools/pen.rs +++ b/core/editor/src/tools/pen.rs @@ -10,6 +10,6 @@ pub struct Pen; impl Tool for Pen { fn handle_input(&mut self, event: &Event, document: &Document, tool_data: &DocumentToolData) -> (Vec, Vec) { - todo!(); + todo!("{}::handle_input {:?} {:?} {:?}", module_path!(), event, document, tool_data) } } diff --git a/core/editor/src/tools/sample.rs b/core/editor/src/tools/sample.rs index 8d6860477..1876e32ad 100644 --- a/core/editor/src/tools/sample.rs +++ b/core/editor/src/tools/sample.rs @@ -10,6 +10,6 @@ pub struct Sample; impl Tool for Sample { fn handle_input(&mut self, event: &Event, document: &Document, tool_data: &DocumentToolData) -> (Vec, Vec) { - todo!(); + todo!("{}::handle_input {:?} {:?} {:?}", module_path!(), event, document, tool_data) } } diff --git a/core/editor/src/workspace/mod.rs b/core/editor/src/workspace/mod.rs index 7e3cd1109..eedb8a832 100644 --- a/core/editor/src/workspace/mod.rs +++ b/core/editor/src/workspace/mod.rs @@ -1,9 +1,8 @@ -use crate::EditorError; pub type PanelId = usize; pub struct Workspace { - hovered_panel: PanelId, - root: PanelGroup, + pub hovered_panel: PanelId, + pub root: PanelGroup, } impl Workspace { @@ -19,9 +18,9 @@ impl Workspace { // get_serialized_layout() } -struct PanelGroup { - contents: Vec, - layout_direction: LayoutDirection, +pub struct PanelGroup { + pub contents: Vec, + pub layout_direction: LayoutDirection, } impl PanelGroup { @@ -33,17 +32,17 @@ impl PanelGroup { } } -enum Contents { +pub enum Contents { PanelArea(PanelArea), Group(PanelGroup), } -struct PanelArea { - panels: Vec, - active: PanelId, +pub struct PanelArea { + pub panels: Vec, + pub active: PanelId, } -enum LayoutDirection { +pub enum LayoutDirection { Horizontal, Vertical, }