Skip to content

Commit

Permalink
Add with_grab() and downcasting to KeyboardHandle
Browse files Browse the repository at this point in the history
Same deal, lets the compositor inspect the grab should it need it.
  • Loading branch information
YaLTeR authored and Drakulix committed Oct 27, 2024
1 parent ade68a8 commit e337dea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::backend::input::KeyState;
use crate::utils::{IsAlive, Serial, SERIAL_COUNTER};
use downcast_rs::{impl_downcast, Downcast};
use std::collections::HashSet;
#[cfg(feature = "wayland_frontend")]
use std::sync::RwLock;
Expand Down Expand Up @@ -590,7 +591,7 @@ impl<D: SeatHandler + 'static> Clone for GrabStartData<D> {
/// When your grab ends (either as you requested it or if it was forcefully cancelled by the server),
/// the struct implementing this trait will be dropped. As such you should put clean-up logic in the destructor,
/// rather than trying to guess when the grab will end.
pub trait KeyboardGrab<D: SeatHandler> {
pub trait KeyboardGrab<D: SeatHandler>: Downcast {
/// An input was reported.
///
/// `modifiers` are only passed when their state actually changes. The modifier must be
Expand Down Expand Up @@ -623,6 +624,8 @@ pub trait KeyboardGrab<D: SeatHandler> {
fn unset(&mut self, data: &mut D);
}

impl_downcast!(KeyboardGrab<D> where D: SeatHandler);

/// An handle to a keyboard handler
///
/// It can be cloned and all clones manipulate the same internal state.
Expand Down Expand Up @@ -918,6 +921,16 @@ impl<D: SeatHandler + 'static> KeyboardHandle<D> {
}
}

/// Calls `f` with the active grab, if any.
pub fn with_grab<T>(&self, f: impl FnOnce(Serial, &dyn KeyboardGrab<D>) -> T) -> Option<T> {
let guard = self.arc.internal.lock().unwrap();
if let GrabStatus::Active(s, g) = &guard.grab {
Some(f(*s, &**g))
} else {
None
}
}

/// Handle a keystroke
///
/// All keystrokes from the input backend should be fed _in order_ to this method of the
Expand Down
2 changes: 1 addition & 1 deletion src/wayland/input_method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use crate::{
};

pub use input_method_handle::{InputMethodHandle, InputMethodUserData};
pub use input_method_keyboard_grab::InputMethodKeyboardUserData;
pub use input_method_keyboard_grab::{InputMethodKeyboardGrab, InputMethodKeyboardUserData};
pub use input_method_popup_surface::InputMethodPopupSurfaceUserData;

use super::text_input::TextInputHandle;
Expand Down

0 comments on commit e337dea

Please sign in to comment.