Skip to content

Commit

Permalink
More versatile update logic for CenterTextPosition and `SwapBoundar…
Browse files Browse the repository at this point in the history
…ies`

Leverages `CommonDefinition`
  • Loading branch information
justDeeevin committed Dec 20, 2024
1 parent da4c8e5 commit ba98b17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
44 changes: 21 additions & 23 deletions crates/app/src/nuhxboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<_>>(),
),
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<_>>(),
),
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 {
Expand All @@ -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");
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/types/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ pub struct CommonDefinition {
pub keycodes: Vec<u32>,
}

impl TryFrom<BoardElement> for CommonDefinition {
type Error = ();
fn try_from(value: BoardElement) -> Result<Self, Self::Error> {
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<KeyboardKeyDefinition> for CommonDefinition {
fn from(val: KeyboardKeyDefinition) -> Self {
CommonDefinition {
Expand Down

0 comments on commit ba98b17

Please sign in to comment.