Skip to content

Commit

Permalink
release modifiers on keyboard hide
Browse files Browse the repository at this point in the history
  • Loading branch information
galister committed Sep 27, 2024
1 parent 83ae798 commit 7f72abc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/gui/canvas/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ impl<D, S> CanvasBuilder<D, S> {
}

// 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<str>) -> &mut Control<D, S> {
pub fn label(
&mut self,
x: f32,
y: f32,
w: f32,
h: f32,
radius: f32,
text: Arc<str>,
) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h },
Expand Down Expand Up @@ -116,7 +124,15 @@ impl<D, S> CanvasBuilder<D, S> {
}

// 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<str>) -> &mut Control<D, S> {
pub fn button(
&mut self,
x: f32,
y: f32,
w: f32,
h: f32,
radius: f32,
text: Arc<str>,
) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();

self.canvas.interactive_set_idx(x, y, w, h, idx);
Expand Down
4 changes: 4 additions & 0 deletions src/gui/canvas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ impl<D, S> Canvas<D, S> {
cmd_buffer.end_render_pass()?;
cmd_buffer.build_and_execute_now()
}

pub fn data_mut(&mut self) -> &mut D {
&mut self.canvas.data
}
}

impl<D, S> InteractionHandler for Canvas<D, S> {
Expand Down
73 changes: 69 additions & 4 deletions src/overlays/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -180,7 +180,7 @@ where
interaction_transform,
..Default::default()
},
backend: Box::new(canvas),
backend: Box::new(KeyboardBackend { canvas }),
..Default::default()
})
}
Expand Down Expand Up @@ -435,3 +435,68 @@ fn key_events_for_macro(macro_verbs: &Vec<String>) -> Vec<(VirtualKey, bool)> {
}
key_events
}

struct KeyboardBackend {
canvas: Canvas<KeyboardData, KeyButtonData>,
}

impl OverlayBackend for KeyboardBackend {
fn set_interaction(&mut self, interaction: Box<dyn crate::backend::input::InteractionHandler>) {
self.canvas.set_interaction(interaction)
}
fn set_renderer(&mut self, renderer: Box<dyn crate::backend::overlay::OverlayRenderer>) {
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<crate::backend::input::Haptics> {
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<std::sync::Arc<vulkano::image::view::ImageView>> {
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)
}
}

0 comments on commit 7f72abc

Please sign in to comment.