diff --git a/Cargo.lock b/Cargo.lock index 6d96e0f..6acfe1c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1936,6 +1936,7 @@ dependencies = [ "gilrs", "hidapi", "iced", + "iced_native", "rdkafka", "rusb", "serde", diff --git a/Cargo.toml b/Cargo.toml index 313f844..56dfbfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ rusb = "0.9.1" hidapi = "0.5.0" gilrs = "0.10.1" iced = "0.8.0" +iced_native = "0.9.1" diff --git a/src/gui.rs b/src/gui.rs index 0a2ff54..fb094a0 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,5 +1,6 @@ use iced::widget::{button, column, text}; -use iced::{Alignment, Element, Sandbox, Settings}; +use iced::executor; +use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command}; use iced_native::Event; pub fn main() -> iced::Result { @@ -8,10 +9,11 @@ pub fn main() -> iced::Result { struct Main { ntfy: String, + configuring: bool, } -#[derive(Debug, Clone, Copy)] -enum ControllerButton { +#[derive(Debug, Clone)] +enum Message { EventOccurred(iced_native::Event), Up, Down, @@ -25,44 +27,63 @@ enum ControllerButton { R, } -fn controller_button_to_string(btn: ControllerButton) -> String { - return format!("{:?}", btn); +fn controller_button_to_string(msg: Message) -> String { + return format!("{:?}", msg); } -impl Sandbox for Main { - type Message = ControllerButton; +impl Application for Main { + type Message = Message; + type Flags = (); + type Theme = Theme; + type Executor = executor::Default; - fn new() -> Self { - return Self { ntfy: String::from(":wave:") } + fn new(_flags: ()) -> (Self, Command) { + return (Self { + ntfy: String::from(":wave:"), + configuring: false, + }, Command::none()) } fn title(&self) -> String { return String::from("Rusty Pipe") } - fn update(&mut self, controllerButton: ControllerButton) { - match message { - ControllerButton::EventOccurred(Event) => { - Err(String::from("ohno")).unwrap(); + fn update(&mut self, msg: Message) -> Command { + match msg { + Message::EventOccurred(event) if self.configuring => { + match event { + iced_native::Event::Keyboard(kb) => { + self.ntfy = format!("would set {:?}", kb); + self.configuring = false; + }, + _ => {}, + } }, + Message::EventOccurred(event) if ! self.configuring => {}, _ => { - self.ntfy = format!("push a key to bind to {:?}", controllerButton); + self.configuring = true; + self.ntfy = format!("push a key to bind to {:?}", msg); }, } + return Command::none(); } - fn view(&self) -> Element { + fn subscription(&self) -> Subscription { + iced_native::subscription::events().map(Message::EventOccurred) + } + + fn view(&self) -> Element { return column![ - button(text(controller_button_to_string(ControllerButton::Up))).on_press(ControllerButton::Up), - button(text(controller_button_to_string(ControllerButton::Down))).on_press(ControllerButton::Down), - button(text(controller_button_to_string(ControllerButton::Left))).on_press(ControllerButton::Left), - button(text(controller_button_to_string(ControllerButton::Right))).on_press(ControllerButton::Right), - button(text(controller_button_to_string(ControllerButton::A))).on_press(ControllerButton::A), - button(text(controller_button_to_string(ControllerButton::B))).on_press(ControllerButton::B), - button(text(controller_button_to_string(ControllerButton::X))).on_press(ControllerButton::X), - button(text(controller_button_to_string(ControllerButton::Y))).on_press(ControllerButton::Y), - button(text(controller_button_to_string(ControllerButton::L))).on_press(ControllerButton::L), - button(text(controller_button_to_string(ControllerButton::R))).on_press(ControllerButton::R), + button(text(controller_button_to_string(Message::Up))).on_press(Message::Up), + button(text(controller_button_to_string(Message::Down))).on_press(Message::Down), + button(text(controller_button_to_string(Message::Left))).on_press(Message::Left), + button(text(controller_button_to_string(Message::Right))).on_press(Message::Right), + button(text(controller_button_to_string(Message::A))).on_press(Message::A), + button(text(controller_button_to_string(Message::B))).on_press(Message::B), + button(text(controller_button_to_string(Message::X))).on_press(Message::X), + button(text(controller_button_to_string(Message::Y))).on_press(Message::Y), + button(text(controller_button_to_string(Message::L))).on_press(Message::L), + button(text(controller_button_to_string(Message::R))).on_press(Message::R), text(self.ntfy.clone()).size(50), ] .padding(20)