diff --git a/src/gui/canvas/builder.rs b/src/gui/canvas/builder.rs index 66d8114..8ac0af2 100644 --- a/src/gui/canvas/builder.rs +++ b/src/gui/canvas/builder.rs @@ -52,7 +52,15 @@ impl CanvasBuilder { } // Creates a label with fg_color, font_size inherited from the canvas - pub fn label(&mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, text: Arc) -> &mut Control { + pub fn label( + &mut self, + x: f32, + y: f32, + w: f32, + h: f32, + radius: f32, + text: Arc, + ) -> &mut Control { let idx = self.canvas.controls.len(); self.canvas.controls.push(Control { rect: Rect { x, y, w, h }, @@ -116,7 +124,15 @@ impl CanvasBuilder { } // Creates a button with fg_color, bg_color, font_size inherited from the canvas - pub fn button(&mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, text: Arc) -> &mut Control { + pub fn button( + &mut self, + x: f32, + y: f32, + w: f32, + h: f32, + radius: f32, + text: Arc, + ) -> &mut Control { let idx = self.canvas.controls.len(); self.canvas.interactive_set_idx(x, y, w, h, idx); diff --git a/src/gui/canvas/mod.rs b/src/gui/canvas/mod.rs index 459917a..739ca75 100644 --- a/src/gui/canvas/mod.rs +++ b/src/gui/canvas/mod.rs @@ -220,6 +220,10 @@ impl Canvas { cmd_buffer.end_render_pass()?; cmd_buffer.build_and_execute_now() } + + pub fn data_mut(&mut self) -> &mut D { + &mut self.canvas.data + } } impl InteractionHandler for Canvas { diff --git a/src/overlays/keyboard.rs b/src/overlays/keyboard.rs index 18a1add..93bd299 100644 --- a/src/overlays/keyboard.rs +++ b/src/overlays/keyboard.rs @@ -6,12 +6,12 @@ use std::{ use crate::{ backend::{ - input::PointerMode, - overlay::{OverlayData, OverlayState}, + input::{InteractionHandler, PointerMode}, + overlay::{OverlayBackend, OverlayData, OverlayRenderer, OverlayState}, }, config::{self, ConfigType}, gui::{ - canvas::{builder::CanvasBuilder, control::Control}, + canvas::{builder::CanvasBuilder, control::Control, Canvas}, color_parse, KeyCapType, }, hid::{ @@ -180,7 +180,7 @@ where interaction_transform, ..Default::default() }, - backend: Box::new(canvas), + backend: Box::new(KeyboardBackend { canvas }), ..Default::default() }) } @@ -435,3 +435,68 @@ fn key_events_for_macro(macro_verbs: &Vec) -> Vec<(VirtualKey, bool)> { } key_events } + +struct KeyboardBackend { + canvas: Canvas, +} + +impl OverlayBackend for KeyboardBackend { + fn set_interaction(&mut self, interaction: Box) { + self.canvas.set_interaction(interaction) + } + fn set_renderer(&mut self, renderer: Box) { + self.canvas.set_renderer(renderer) + } +} + +impl InteractionHandler for KeyboardBackend { + fn on_pointer( + &mut self, + app: &mut AppState, + hit: &crate::backend::input::PointerHit, + pressed: bool, + ) { + self.canvas.on_pointer(app, hit, pressed) + } + fn on_scroll( + &mut self, + app: &mut AppState, + hit: &crate::backend::input::PointerHit, + delta: f32, + ) { + self.canvas.on_scroll(app, hit, delta) + } + fn on_left(&mut self, app: &mut AppState, pointer: usize) { + self.canvas.on_left(app, pointer) + } + fn on_hover( + &mut self, + app: &mut AppState, + hit: &crate::backend::input::PointerHit, + ) -> Option { + self.canvas.on_hover(app, hit) + } +} + +impl OverlayRenderer for KeyboardBackend { + fn init(&mut self, app: &mut AppState) -> anyhow::Result<()> { + self.canvas.init(app) + } + fn render(&mut self, app: &mut AppState) -> anyhow::Result<()> { + self.canvas.render(app) + } + fn extent(&mut self) -> Option<[u32; 3]> { + self.canvas.extent() + } + fn view(&mut self) -> Option> { + self.canvas.view() + } + fn pause(&mut self, app: &mut AppState) -> anyhow::Result<()> { + self.canvas.data_mut().modifiers = 0; + app.hid_provider.set_modifiers(0); + self.canvas.pause(app) + } + fn resume(&mut self, app: &mut AppState) -> anyhow::Result<()> { + self.canvas.resume(app) + } +}