From ba98b175cbbb4d43541bfa0413e0169c88710bbd Mon Sep 17 00:00:00 2001 From: Devin Droddy Date: Fri, 20 Dec 2024 13:04:12 -0500 Subject: [PATCH] More versatile update logic for `CenterTextPosition` and `SwapBoundaries` Leverages `CommonDefinition` --- crates/app/src/nuhxboard.rs | 44 ++++++++++++++++++------------------- crates/types/src/config.rs | 12 ++++++++++ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/crates/app/src/nuhxboard.rs b/crates/app/src/nuhxboard.rs index 4b39cf2..f09987c 100644 --- a/crates/app/src/nuhxboard.rs +++ b/crates/app/src/nuhxboard.rs @@ -793,23 +793,22 @@ impl NuhxBoard { } Message::CenterTextPosition(i) => { let element = &mut self.layout.elements[i]; - match element { - BoardElement::KeyboardKey(def) => { - let bounds = Polygon::new( - LineString::new( - def.boundaries - .iter() - .map(|p| Coord::from(p.clone())) - .collect::>(), - ), - vec![], - ); - let centroid = bounds.centroid().unwrap(); - - def.text_position.x = centroid.x().trunc(); - def.text_position.y = centroid.y().trunc(); - } - _ => todo!(), + if let Ok(mut def) = CommonDefinition::try_from(element.clone()) { + let bounds = Polygon::new( + LineString::new( + def.boundaries + .iter() + .map(|p| Coord::from(p.clone())) + .collect::>(), + ), + vec![], + ); + let centroid = bounds.centroid().unwrap(); + + def.text_position.x = centroid.x().trunc(); + def.text_position.y = centroid.y().trunc(); + } else { + panic!("Cannot center text position of mouse speed indicator"); } } Message::ChangeNumberInput(input_type) => match input_type { @@ -833,12 +832,11 @@ impl NuhxBoard { }, Message::SwapBoundaries(element_i, left, right) => { let element = &mut self.layout.elements[element_i]; - match element { - BoardElement::KeyboardKey(def) => { - def.boundaries.swap(left, right); - self.selections.boundary.insert(element_i, right); - } - _ => todo!(), + if let Ok(mut def) = CommonDefinition::try_from(element.clone()) { + def.boundaries.swap(left, right); + self.selections.boundary.insert(element_i, right); + } else { + panic!("Cannot swap boundaries of mouse speed indicator"); } } } diff --git a/crates/types/src/config.rs b/crates/types/src/config.rs index d6b54e2..1256ca5 100644 --- a/crates/types/src/config.rs +++ b/crates/types/src/config.rs @@ -85,6 +85,18 @@ pub struct CommonDefinition { pub keycodes: Vec, } +impl TryFrom for CommonDefinition { + type Error = (); + fn try_from(value: BoardElement) -> Result { + match value { + BoardElement::KeyboardKey(def) => Ok(def.into()), + BoardElement::MouseKey(def) => Ok(def.into()), + BoardElement::MouseScroll(def) => Ok(def.into()), + BoardElement::MouseSpeedIndicator(_) => Err(()), + } + } +} + impl From for CommonDefinition { fn from(val: KeyboardKeyDefinition) -> Self { CommonDefinition {