Skip to content

Commit

Permalink
Fix modifiers in sdl3 by adding optional 'modifiers' field in KeyChan…
Browse files Browse the repository at this point in the history
…ged.

Closes #200.
  • Loading branch information
LPGhatguy committed Jan 25, 2025
1 parent 693d7c2 commit ceb3dd9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions crates/yakui-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub enum Event {

/// Whether the key is now down.
down: bool,

/// Modifiers pressed for this event.
modifiers: Option<Modifiers>,
},

/// The state of the keyboard modifiers keys changed.
Expand Down
9 changes: 7 additions & 2 deletions crates/yakui-core/src/input/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ impl InputState {
response
}
Event::MouseScroll { delta } => self.send_mouse_scroll(dom, layout, *delta),
Event::KeyChanged { key, down } => self.keyboard_key_changed(dom, layout, *key, *down),
Event::KeyChanged {
key,
down,
modifiers,
} => self.keyboard_key_changed(dom, layout, *key, *down, *modifiers),
Event::ModifiersChanged(modifiers) => self.modifiers_changed(modifiers),
Event::TextInput(c) => self.text_input(dom, layout, *c),
_ => EventResponse::Bubble,
Expand Down Expand Up @@ -278,6 +282,7 @@ impl InputState {
layout: &LayoutDom,
key: KeyCode,
down: bool,
modifiers: Option<Modifiers>,
) -> EventResponse {
let selected = self.selection.get();
if let Some(id) = selected {
Expand All @@ -295,7 +300,7 @@ impl InputState {
let event = WidgetEvent::KeyChanged {
key,
down,
modifiers: self.modifiers.get(),
modifiers: modifiers.unwrap_or(self.modifiers.get()),
};
return self.fire_event(dom, layout, id, &mut node, &event);
}
Expand Down
24 changes: 18 additions & 6 deletions crates/yakui-sdl2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use sdl2::mouse::MouseButton as SdlMouseButton;
use sdl2::video::Window;
use yakui_core::event::Event;
use yakui_core::geometry::{Rect, UVec2, Vec2};
use yakui_core::input::MouseButton;
use yakui_core::input::{Modifiers, MouseButton};

use self::keys::from_sdl_scancode;
use self::keys::{from_sdl_modifiers, from_sdl_scancode};

pub struct YakuiSdl2 {
init: Option<InitState>,
Expand Down Expand Up @@ -110,17 +110,29 @@ impl YakuiSdl2 {
false
}

SdlEvent::KeyDown { scancode, .. } => {
SdlEvent::KeyDown {
scancode, keymod, ..
} => {
if let Some(key) = scancode.and_then(from_sdl_scancode) {
state.handle_event(Event::KeyChanged { key, down: true })
state.handle_event(Event::KeyChanged {
key,
down: true,
modifiers: Some(from_sdl_modifiers(*keymod)),
})
} else {
false
}
}

SdlEvent::KeyUp { scancode, .. } => {
SdlEvent::KeyUp {
scancode, keymod, ..
} => {
if let Some(key) = scancode.and_then(from_sdl_scancode) {
state.handle_event(Event::KeyChanged { key, down: false })
state.handle_event(Event::KeyChanged {
key,
down: false,
modifiers: Some(from_sdl_modifiers(*keymod)),
})
} else {
false
}
Expand Down
22 changes: 17 additions & 5 deletions crates/yakui-sdl3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use yakui_core::event::Event;
use yakui_core::geometry::{Rect, UVec2, Vec2};
use yakui_core::input::MouseButton;

use self::keys::from_sdl_scancode;
use self::keys::{from_sdl_modifiers, from_sdl_scancode};

pub struct YakuiSdl3 {
init: Option<InitState>,
Expand Down Expand Up @@ -122,17 +122,29 @@ impl YakuiSdl3 {
false
}

SdlEvent::KeyDown { scancode, .. } => {
SdlEvent::KeyDown {
scancode, keymod, ..
} => {
if let Some(key) = scancode.and_then(from_sdl_scancode) {
state.handle_event(Event::KeyChanged { key, down: true })
state.handle_event(Event::KeyChanged {
key,
down: true,
modifiers: Some(from_sdl_modifiers(*keymod)),
})
} else {
false
}
}

SdlEvent::KeyUp { scancode, .. } => {
SdlEvent::KeyUp {
scancode, keymod, ..
} => {
if let Some(key) = scancode.and_then(from_sdl_scancode) {
state.handle_event(Event::KeyChanged { key, down: false })
state.handle_event(Event::KeyChanged {
key,
down: false,
modifiers: Some(from_sdl_modifiers(*keymod)),
})
} else {
false
}
Expand Down
2 changes: 2 additions & 0 deletions crates/yakui-widgets/src/widgets/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ impl Widget for TextBoxWidget {
modifiers,
..
} => {
println!("Key: {key:?} {down:?} + {modifiers:?}");

let fonts = ctx.dom.get_global_or_init(Fonts::default);
fonts.with_system(|font_system| {
if let Some(editor) = self.cosmic_editor.get_mut() {
Expand Down
6 changes: 5 additions & 1 deletion crates/yakui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ impl YakuiWinit {
ElementState::Released => false,
};

state.handle_event(Event::KeyChanged { key, down: pressed })
state.handle_event(Event::KeyChanged {
key,
down: pressed,
modifiers: None,
})
} else {
false
}
Expand Down

0 comments on commit ceb3dd9

Please sign in to comment.