1
0
Fork 0
forked from wry/wry

text-input: add input method abstraction

This commit is contained in:
Julian Orth 2025-10-15 22:01:24 +02:00
parent 26988f5ce5
commit 881fb24878
8 changed files with 122 additions and 45 deletions

View file

@ -2,7 +2,10 @@ use {
crate::{
backend::KeyState,
client::{Client, ClientError},
ifs::wl_seat::{text_input::zwp_input_method_v2::ZwpInputMethodV2, wl_keyboard},
ifs::wl_seat::{
text_input::{InputMethodKeyboardGrab, zwp_input_method_v2::ZwpInputMethodV2},
wl_keyboard,
},
keyboard::{KeyboardState, KeyboardStateId},
leaks::Tracker,
object::{Object, Version},
@ -49,7 +52,7 @@ impl ZwpInputMethodKeyboardGrabV2 {
self.kb_state_id.set(kb_state.id);
}
pub fn on_key(&self, time_usec: u64, key: u32, state: KeyState, kb_state: &KeyboardState) {
fn on_key(&self, time_usec: u64, key: u32, state: KeyState, kb_state: &KeyboardState) {
let serial = self.client.next_serial();
if self.kb_state_id.get() != kb_state.id {
self.update_state(serial, kb_state);
@ -70,7 +73,7 @@ impl ZwpInputMethodKeyboardGrabV2 {
})
}
pub fn on_modifiers(&self, kb_state: &KeyboardState) {
fn on_modifiers(&self, kb_state: &KeyboardState) {
let serial = self.client.next_serial();
if self.kb_state_id.get() != kb_state.id {
self.update_state(serial, kb_state);
@ -99,6 +102,22 @@ impl ZwpInputMethodKeyboardGrabV2 {
}
}
impl InputMethodKeyboardGrab for ZwpInputMethodKeyboardGrabV2 {
fn on_key(&self, time_usec: u64, key: u32, state: KeyState, kb_state: &KeyboardState) -> bool {
self.on_key(time_usec, key, state, kb_state);
false
}
fn on_modifiers(&self, kb_state: &KeyboardState) -> bool {
self.on_modifiers(kb_state);
false
}
fn on_repeat_info(&self) {
self.send_repeat_info();
}
}
impl ZwpInputMethodKeyboardGrabV2RequestHandler for ZwpInputMethodKeyboardGrabV2 {
type Error = ZwpInputMethodKeyboardGrabV2Error;

View file

@ -5,7 +5,7 @@ use {
wl_seat::{
WlSeatGlobal,
text_input::{
MAX_TEXT_SIZE, TextDisconnectReason, TextInputConnection,
InputMethod, MAX_TEXT_SIZE, TextDisconnectReason, TextInputConnection,
zwp_input_method_keyboard_grab_v2::ZwpInputMethodKeyboardGrabV2,
},
},
@ -104,6 +104,40 @@ impl ZwpInputMethodV2 {
}
}
impl InputMethod for ZwpInputMethodV2 {
fn set_connection(&self, con: Option<&Rc<TextInputConnection>>) {
self.connection.set(con.cloned());
}
fn popups(&self) -> &SmallMap<ZwpInputPopupSurfaceV2Id, Rc<ZwpInputPopupSurfaceV2>, 1> {
&self.popups
}
fn activate(&self) {
self.activate();
}
fn deactivate(&self) {
self.send_deactivate();
}
fn content_type(&self, hint: u32, purpose: u32) {
self.send_content_type(hint, purpose);
}
fn text_change_cause(&self, cause: u32) {
self.send_text_change_cause(cause);
}
fn surrounding_text(&self, text: &str, cursor: u32, anchor: u32) {
self.send_surrounding_text(text, cursor, anchor);
}
fn done(self: Rc<Self>, _seat: &WlSeatGlobal) {
(*self).send_done();
}
}
impl ZwpInputMethodV2RequestHandler for ZwpInputMethodV2 {
type Error = ZwpInputMethodV2Error;

View file

@ -5,8 +5,8 @@ use {
wl_seat::{
WlSeatGlobal,
text_input::{
MAX_TEXT_SIZE, TextConnectReason, TextDisconnectReason, TextInputConnection,
zwp_input_method_v2::ZwpInputMethodV2,
InputMethod, MAX_TEXT_SIZE, TextConnectReason, TextDisconnectReason,
TextInputConnection,
},
},
wl_surface::WlSurface,
@ -72,13 +72,13 @@ impl ZwpTextInputV3 {
}
}
pub fn send_all_to(&self, im: &ZwpInputMethodV2) {
pub fn send_all_to(&self, im: &dyn InputMethod) {
let state = &*self.state.borrow();
{
let (a, b, c) = &state.surrounding_text;
im.send_surrounding_text(a, *b, *c);
im.surrounding_text(a, *b, *c);
}
im.send_content_type(state.content_type.0, state.content_type.1);
im.content_type(state.content_type.0, state.content_type.1);
}
pub fn send_enter(&self, surface: &WlSurface) {
@ -255,7 +255,7 @@ impl ZwpTextInputV3RequestHandler for ZwpTextInputV3 {
if state.cursor_rectangle != val
&& let Some(con) = &con
{
for (_, popup) in &con.input_method.popups {
for (_, popup) in con.input_method.popups() {
popup.schedule_positioning();
}
}
@ -264,26 +264,26 @@ impl ZwpTextInputV3RequestHandler for ZwpTextInputV3 {
if let Some(val) = pending.content_type {
if let Some(con) = &con {
sent_any = true;
con.input_method.send_content_type(val.0, val.1);
con.input_method.content_type(val.0, val.1);
}
state.content_type = val;
}
if let Some(val) = pending.text_change_cause {
if let Some(con) = &con {
sent_any = true;
con.input_method.send_text_change_cause(val);
con.input_method.text_change_cause(val);
}
state.text_change_cause = val;
}
if let Some(val) = pending.surrounding_text {
if let Some(con) = &con {
sent_any = true;
con.input_method.send_surrounding_text(&val.0, val.1, val.2);
con.input_method.surrounding_text(&val.0, val.1, val.2);
}
state.surrounding_text = val;
}
if sent_any && let Some(con) = &con {
con.input_method.send_done();
con.input_method.clone().done(&self.seat);
}
Ok(())
}