chore: modifiers event on keyboard enter and leave

This commit is contained in:
Ashley Wulber 2026-05-27 11:30:30 -04:00 committed by Michael Murphy
parent 6105d8bdef
commit a7d71d9ace
3 changed files with 61 additions and 12 deletions

View file

@ -30,7 +30,6 @@ use std::{
convert::Infallible, convert::Infallible,
fmt::Debug, fmt::Debug,
sync::{Arc, Mutex, atomic::AtomicU32}, sync::{Arc, Mutex, atomic::AtomicU32},
thread::panicking,
time::Duration, time::Duration,
}; };
use wayland_backend::client::ObjectId; use wayland_backend::client::ObjectId;
@ -71,7 +70,7 @@ use cctk::{
registry::RegistryState, registry::RegistryState,
seat::{ seat::{
SeatState, SeatState,
keyboard::KeyEvent, keyboard::{KeyEvent, Modifiers},
pointer::{CursorIcon, PointerData, ThemedPointer}, pointer::{CursorIcon, PointerData, ThemedPointer},
touch::TouchData, touch::TouchData,
}, },
@ -97,7 +96,6 @@ use cctk::{
}; };
use iced_runtime::{ use iced_runtime::{
core::{self, Point, touch}, core::{self, Point, touch},
keyboard::Modifiers,
platform_specific::{ platform_specific::{
self, self,
wayland::{ wayland::{

View file

@ -4,10 +4,46 @@ use crate::platform_specific::wayland::{
}; };
use cctk::sctk::{ use cctk::sctk::{
delegate_keyboard, delegate_keyboard,
seat::keyboard::{KeyboardHandler, Keysym}, seat::keyboard::{KeyboardHandler, Keysym, Modifiers},
}; };
use cctk::sctk::{reexports::client::Proxy, seat::keyboard::RawModifiers}; use cctk::sctk::{reexports::client::Proxy, seat::keyboard::RawModifiers};
fn modifiers_from_keysyms(
keysyms: &[Keysym],
previous: Modifiers,
) -> Modifiers {
let mut modifiers = Modifiers {
caps_lock: previous.caps_lock,
num_lock: previous.num_lock,
..Modifiers::default()
};
for keysym in keysyms {
match keysym.raw() {
xkeysym::key::Shift_L | xkeysym::key::Shift_R => {
modifiers.shift = true
}
xkeysym::key::Control_L | xkeysym::key::Control_R => {
modifiers.ctrl = true
}
xkeysym::key::Alt_L | xkeysym::key::Alt_R => modifiers.alt = true,
xkeysym::key::Super_L
| xkeysym::key::Super_R
| xkeysym::key::Hyper_L
| xkeysym::key::Hyper_R
| xkeysym::key::Meta_L
| xkeysym::key::Meta_R => modifiers.logo = true,
xkeysym::key::Caps_Lock | xkeysym::key::Shift_Lock => {
modifiers.caps_lock = true
}
xkeysym::key::Num_Lock => modifiers.num_lock = true,
_ => {}
}
}
modifiers
}
impl KeyboardHandler for SctkState { impl KeyboardHandler for SctkState {
fn enter( fn enter(
&mut self, &mut self,
@ -17,9 +53,9 @@ impl KeyboardHandler for SctkState {
surface: &cctk::sctk::reexports::client::protocol::wl_surface::WlSurface, surface: &cctk::sctk::reexports::client::protocol::wl_surface::WlSurface,
_serial: u32, _serial: u32,
_raw: &[u32], _raw: &[u32],
_keysyms: &[Keysym], keysyms: &[Keysym],
) { ) {
let (i, mut is_active, seat) = { let (i, mut is_active, seat, modifiers) = {
let (i, is_active, my_seat) = let (i, is_active, my_seat) =
match self.seats.iter_mut().enumerate().find_map(|(i, s)| { match self.seats.iter_mut().enumerate().find_map(|(i, s)| {
if s.kbd.as_ref() == Some(keyboard) { if s.kbd.as_ref() == Some(keyboard) {
@ -41,9 +77,11 @@ impl KeyboardHandler for SctkState {
surface surface
}; };
_ = my_seat.kbd_focus.replace(surface.clone()); _ = my_seat.kbd_focus.replace(surface.clone());
let modifiers = modifiers_from_keysyms(keysyms, my_seat._modifiers);
my_seat._modifiers = modifiers;
let seat = my_seat.seat.clone(); let seat = my_seat.seat.clone();
(i, is_active, seat) (i, is_active, seat, modifiers)
}; };
if !is_active && self.seats[0].kbd_focus.is_none() { if !is_active && self.seats[0].kbd_focus.is_none() {
@ -73,6 +111,12 @@ impl KeyboardHandler for SctkState {
seat_id: seat.clone(), seat_id: seat.clone(),
surface: surface.clone(), surface: surface.clone(),
}); });
self.sctk_events.push(SctkEvent::KeyboardEvent {
variant: KeyboardEventVariant::Modifiers(modifiers),
kbd_id: keyboard.clone(),
seat_id: seat.clone(),
surface: surface.clone(),
});
} }
} }
} }
@ -126,6 +170,7 @@ impl KeyboardHandler for SctkState {
if self.windows.iter().any(|w| w.window.id() == id) { if self.windows.iter().any(|w| w.window.id() == id) {
continue; continue;
} }
let modifiers = s._modifiers;
self.sctk_events.push(SctkEvent::Winit( self.sctk_events.push(SctkEvent::Winit(
id, id,
winit::event::WindowEvent::Focused(true), winit::event::WindowEvent::Focused(true),
@ -137,6 +182,12 @@ impl KeyboardHandler for SctkState {
kbd_id: s.kbd.clone().unwrap(), kbd_id: s.kbd.clone().unwrap(),
seat_id: s.seat.clone(), seat_id: s.seat.clone(),
surface: surface.clone(), surface: surface.clone(),
});
self.sctk_events.push(SctkEvent::KeyboardEvent {
variant: KeyboardEventVariant::Modifiers(modifiers),
kbd_id: s.kbd.clone().unwrap(),
seat_id: s.seat.clone(),
surface: surface.clone(),
}) })
} }
} }
@ -248,6 +299,7 @@ impl KeyboardHandler for SctkState {
}; };
let seat_id = my_seat.seat.clone(); let seat_id = my_seat.seat.clone();
let kbd_id = keyboard.clone(); let kbd_id = keyboard.clone();
my_seat._modifiers = modifiers;
if is_active { if is_active {
if let Some(surface) = my_seat.kbd_focus.clone() { if let Some(surface) = my_seat.kbd_focus.clone() {

View file

@ -4,10 +4,9 @@ use crate::platform_specific::wayland::{
}; };
use cctk::sctk::{ use cctk::sctk::{
delegate_seat, delegate_seat,
reexports::client::{protocol::wl_keyboard::WlKeyboard, Proxy}, reexports::client::{Proxy, protocol::wl_keyboard::WlKeyboard},
seat::{pointer::ThemeSpec, SeatHandler}, seat::{SeatHandler, keyboard::Modifiers, pointer::ThemeSpec},
}; };
use iced_runtime::keyboard::Modifiers;
use std::sync::Arc; use std::sync::Arc;
impl SeatHandler for SctkState { impl SeatHandler for SctkState {