Skip to content

Commit

Permalink
element properties popup layout (no interactivity yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
justDeeevin committed Dec 20, 2024
1 parent 45c6f87 commit 2665eee
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 18 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/app/src/nuhxboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct NuhxBoard {
pub save_style_as_global: bool,
pub color_pickers: ColorPickers,
pub text_input: TextInput,
pub hovered_element: Option<usize>,
}

#[derive(Default)]
Expand Down Expand Up @@ -202,6 +203,7 @@ pub enum Message {
ToggleColorPicker(ColorPicker),
UpdateCanvas,
ChangeTextInput(TextInputType, String),
UpdateHoveredElement(Option<usize>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -343,6 +345,7 @@ impl NuhxBoard {
save_style_as_global: false,
color_pickers: ColorPickers::default(),
text_input: TextInput::default(),
hovered_element: None,
},
Task::batch([
Task::perform(async {}, move |_| {
Expand Down Expand Up @@ -710,6 +713,9 @@ impl NuhxBoard {
}
Message::ToggleColorPicker(picker) => self.color_pickers.toggle(picker),
Message::UpdateCanvas => {}
Message::UpdateHoveredElement(hovered_element) => {
self.hovered_element = hovered_element;
}
}
if clear_canvas {
self.canvas.clear();
Expand Down
7 changes: 6 additions & 1 deletion crates/app/src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ impl Window<NuhxBoard, Theme, Message> for Main {
.then_some(Message::Open(Box::new(KeyboardProperties))),
)
.into(),
context_menu_button("Element Properties").into(),
context_menu_button("Element Properties")
.on_press_maybe(
app.hovered_element
.map(|e| Message::Open(Box::new(ElementProperties { key: e }))),
)
.into(),
context_menu_button("Keyboard Style")
.on_press_maybe(
(!app.windows.any_of(&KeyboardStyle))
Expand Down
33 changes: 18 additions & 15 deletions crates/app/src/ui/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ fn captured_message() -> Option<Message> {

#[derive(Default)]
pub struct CanvasState {
hovered_element: Option<usize>,
held_element: Option<usize>,
selected_element: Option<usize>,
interaction: Interaction,
Expand All @@ -54,9 +53,8 @@ impl canvas::Program<Message> for NuhxBoard {
) -> (canvas::event::Status, Option<Message>) {
if !self.edit_mode {
let mut clear_canvas = false;
if state.hovered_element.is_some() {
state.hovered_element = None;
clear_canvas = true;
if self.hovered_element.is_some() {
return (Status::Captured, Some(Message::UpdateHoveredElement(None)));
}
if state.selected_element.is_some() {
state.selected_element = None;
Expand Down Expand Up @@ -94,8 +92,11 @@ impl canvas::Program<Message> for NuhxBoard {
Coord::from(def.location.clone()),
) < def.radius
{
if state.hovered_element != Some(index) {
state.hovered_element = Some(index);
if self.hovered_element != Some(index) {
return (
Status::Captured,
Some(Message::UpdateHoveredElement(Some(index))),
);
}

return (Status::Captured, captured_message());
Expand All @@ -116,18 +117,20 @@ impl canvas::Program<Message> for NuhxBoard {
let bounds = Polygon::new(LineString::from(vertices), vec![]);

if cursor_position.is_within(&bounds) {
if state.hovered_element != Some(index) {
state.hovered_element = Some(index);
if self.hovered_element != Some(index) {
return (
Status::Captured,
Some(Message::UpdateHoveredElement(Some(index))),
);
}
return (Status::Captured, captured_message());
}
}
}
}

if state.hovered_element.is_some() {
state.hovered_element = None;
return (Status::Captured, captured_message());
if self.hovered_element.is_some() {
return (Status::Captured, Some(Message::UpdateHoveredElement(None)));
}
}
Interaction::Dragging => {
Expand All @@ -152,7 +155,7 @@ impl canvas::Program<Message> for NuhxBoard {
state.interaction = Interaction::Dragging;

if state.selected_element.is_none() {
state.held_element = state.hovered_element;
state.held_element = self.hovered_element;
} else {
state.held_element = state.selected_element;
state.selected_element = None;
Expand All @@ -173,7 +176,7 @@ impl canvas::Program<Message> for NuhxBoard {
state.selected_element = state.held_element;
out
} else {
state.selected_element = state.hovered_element;
state.selected_element = self.hovered_element;
Some(Message::UpdateCanvas)
};

Expand Down Expand Up @@ -334,7 +337,7 @@ impl NuhxBoard {

// TODO: Still highlight when an element is selected. While the current code is
// closer to NohBoard's behavior, I'm not a fan of it.
if state.hovered_element == Some(index)
if self.hovered_element == Some(index)
&& state.held_element.is_none()
&& state.selected_element.is_none()
{
Expand Down Expand Up @@ -571,7 +574,7 @@ impl NuhxBoard {
);
}

if state.hovered_element == Some(index)
if self.hovered_element == Some(index)
&& state.held_element.is_none()
&& state.selected_element.is_none()
{
Expand Down
79 changes: 78 additions & 1 deletion crates/app/src/ui/popups/edit_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use iced::{
},
window, Alignment, Font, Length, Theme,
};
use iced_aw::number_input;
use iced_aw::{number_input, selection_list};
use iced_multi_window::Window;
use types::config::BoardElement;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyboardProperties;
Expand Down Expand Up @@ -395,3 +396,79 @@ impl Window<NuhxBoard, Theme, Message> for KeyboardStyle {
Theme::Light
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ElementProperties {
pub key: usize,
}

impl Window<NuhxBoard, Theme, Message> for ElementProperties {
fn id(&self) -> String {
format!("element_properties_{}", self.key)
}

fn settings(&self) -> window::Settings {
window::Settings {
// resizable: false,
size: iced::Size {
width: 400.0,
height: 100.0,
},
..Default::default()
}
}

fn view<'a>(&self, app: &'a NuhxBoard) -> iced::Element<'a, Message, Theme> {
let element = &app.layout.elements[self.key];
match element {
BoardElement::KeyboardKey(def) => {
let column_1 = column![
labeled_text_input("Text: ", text_input("", &def.text)),
labeled_text_input("Shift Text: ", text_input("", &def.shift_text)),
row![
text("Text Position: "),
number_input(def.text_position.x, 0.0.., |_| Message::none()),
number_input(def.text_position.y, 0.0.., |_| Message::none()),
button("Center"),
]
.align_y(Alignment::Center),
// TODO: two number inputs for x and y
labeled_text_input("Boundaries: ", text_input("", "")),
row![
column![
button("Add"),
button("Update"),
button("Remove"),
button("Up"),
button("Down"),
// Rectangle creation popup
button("Rectangle"),
],
selection_list(&def.boundaries, |_, _| Message::none())
]
];

let column_2 = column![
checkbox("Change capitalization on Caps Lock key", false),
// TODO: one number input for keycode
row![text("Key codes: "), text_input("", "")],
row![
column![button("Add"), button("Remove"), button("Detect")],
selection_list(&def.key_codes, |_, _| Message::none())
]
];

row![column_1, column_2].into()
}
_ => todo!(),
}
}

fn title(&self, _app: &NuhxBoard) -> String {
"Keyboard Key Properties".to_string()
}

fn theme(&self, _app: &NuhxBoard) -> Theme {
Theme::Light
}
}
1 change: 1 addition & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
display-info.workspace = true
geo.workspace = true
iced.workspace = true
ordered-float = "4.6.0"
schemars.workspace = true
serde = { version = "1.0.210", features = ["derive"] }
serde_json.workspace = true
18 changes: 17 additions & 1 deletion crates/types/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use iced::Point;
use ordered_float::OrderedFloat;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -114,7 +115,7 @@ pub struct MouseSpeedIndicatorDefinition {
pub radius: f32,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, PartialEq)]
#[serde(rename_all = "PascalCase")]
pub struct SerializablePoint {
pub x: f32,
Expand Down Expand Up @@ -142,3 +143,18 @@ impl std::ops::AddAssign<geo::Coord<f32>> for SerializablePoint {
self.y += rhs.y;
}
}

impl std::fmt::Display for SerializablePoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

impl Eq for SerializablePoint {}

impl std::hash::Hash for SerializablePoint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
OrderedFloat(self.x).hash(state);
OrderedFloat(self.y).hash(state);
}
}

0 comments on commit 2665eee

Please sign in to comment.