From da5b5193401151e19e5bba8f4cd5c210a7efeb6b Mon Sep 17 00:00:00 2001 From: Timmy Xiao <34635512+tzx@users.noreply.github.com> Date: Fri, 26 Jan 2024 13:35:39 -0800 Subject: [PATCH] Make IME support optional since not all compositors support it --- window/src/os/wayland/inputhandler.rs | 8 ++++++-- window/src/os/wayland/keyboard.rs | 18 +++++++++++------- window/src/os/wayland/seat.rs | 4 +++- window/src/os/wayland/state.rs | 6 ++---- window/src/os/wayland/window.rs | 18 ++++++++++-------- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/window/src/os/wayland/inputhandler.rs b/window/src/os/wayland/inputhandler.rs index be74fa8f6..357d941f1 100644 --- a/window/src/os/wayland/inputhandler.rs +++ b/window/src/os/wayland/inputhandler.rs @@ -158,7 +158,7 @@ impl Dispatch for TextInputState { ) { log::trace!("ZwpTextInputEvent: {event:?}"); let mut pending_state = { - let text_input = &mut state.text_input; + let text_input = state.text_input.as_mut().unwrap(); let mut inner = text_input.inner.lock().unwrap(); inner.pending_state.entry(input.id()).or_default().clone() }; @@ -201,6 +201,8 @@ impl Dispatch for TextInputState { state .text_input + .as_ref() + .unwrap() .inner .lock() .unwrap() @@ -222,6 +224,8 @@ impl WaylandState { impl Drop for WaylandState { fn drop(&mut self) { - self.text_input.shutdown(); + if let Some(text_input) = self.text_input.as_mut() { + text_input.shutdown(); + } } } diff --git a/window/src/os/wayland/keyboard.rs b/window/src/os/wayland/keyboard.rs index a831d29c3..e9c03ddaf 100644 --- a/window/src/os/wayland/keyboard.rs +++ b/window/src/os/wayland/keyboard.rs @@ -31,20 +31,24 @@ impl Dispatch for WaylandState { if let Some(sud) = SurfaceUserData::try_from_wl(&surface) { let window_id = sud.window_id; state.keyboard_window_id.borrow_mut().replace(window_id); - if let Some(input) = state.text_input.get_text_input_for_keyboard(keyboard) { - input.enable(); - input.commit(); + if let Some(text_input) = &state.text_input { + if let Some(input) = text_input.get_text_input_for_keyboard(keyboard) { + input.enable(); + input.commit(); + } + text_input.advise_surface(surface, keyboard); } - state.text_input.advise_surface(surface, keyboard); } else { log::warn!("{:?}, no known surface", event); } } WlKeyboardEvent::Leave { serial, .. } => { *state.last_serial.borrow_mut() = *serial; - if let Some(input) = state.text_input.get_text_input_for_keyboard(keyboard) { - input.disable(); - input.commit(); + if let Some(text_input) = &state.text_input { + if let Some(input) = text_input.get_text_input_for_keyboard(keyboard) { + input.disable(); + input.commit(); + } } } WlKeyboardEvent::Key { serial, .. } | WlKeyboardEvent::Modifiers { serial, .. } => { diff --git a/window/src/os/wayland/seat.rs b/window/src/os/wayland/seat.rs index d9e6b1bba..3798f4259 100644 --- a/window/src/os/wayland/seat.rs +++ b/window/src/os/wayland/seat.rs @@ -30,7 +30,9 @@ impl SeatHandler for WaylandState { let keyboard = seat.get_keyboard(qh, KeyboardData {}); self.keyboard = Some(keyboard.clone()); - self.text_input.advise_seat(&seat, &keyboard, qh); + if let Some(text_input) = &self.text_input { + text_input.advise_seat(&seat, &keyboard, qh); + } } if capability == Capability::Pointer && self.pointer.is_none() { diff --git a/window/src/os/wayland/state.rs b/window/src/os/wayland/state.rs index 44b85ed36..3e1ce4783 100644 --- a/window/src/os/wayland/state.rs +++ b/window/src/os/wayland/state.rs @@ -50,7 +50,7 @@ pub(super) struct WaylandState { registry: RegistryState, pub(super) output: OutputState, pub(super) compositor: CompositorState, - pub(super) text_input: TextInputState, + pub(super) text_input: Option, pub(super) output_manager: Option, pub(super) seat: SeatState, pub(super) xdg: XdgShell, @@ -85,7 +85,7 @@ impl WaylandState { registry: RegistryState::new(globals), output: OutputState::new(globals, qh), compositor: CompositorState::bind(globals, qh)?, - text_input: TextInputState::bind(globals, qh)?, + text_input: TextInputState::bind(globals, qh).ok(), output_manager: if config::configuration().enable_zwlr_output_manager { Some(OutputManagerState::bind(globals, qh)?) } else { @@ -141,12 +141,10 @@ impl OutputHandler for WaylandState { fn update_output(&mut self, _conn: &Connection, _qh: &QueueHandle, _output: WlOutput) { log::trace!("update output: OutputHandler"); - todo!() } fn output_destroyed(&mut self, _conn: &Connection, _qh: &QueueHandle, _output: WlOutput) { log::trace!("output destroyed: OutputHandler"); - todo!() } } // Undocumented in sctk 0.17: This is required to use have user data with a surface diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index e4578e97d..4817480ba 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -873,14 +873,16 @@ impl WaylandWindowInner { let surface_udata = SurfaceUserData::from_wl(&surface); let factor = surface_udata.surface_data().scale_factor(); - if let Some(input) = state.text_input.get_text_input_for_surface(&surface) { - input.set_cursor_rectangle( - rect.min_x() as i32 / factor, - rect.min_y() as i32 / factor, - rect.width() as i32 / factor, - rect.height() as i32 / factor, - ); - input.commit(); + if let Some(text_input) = &state.text_input { + if let Some(input) = text_input.get_text_input_for_surface(&surface) { + input.set_cursor_rectangle( + rect.min_x() as i32 / factor, + rect.min_y() as i32 / factor, + rect.width() as i32 / factor, + rect.height() as i32 / factor, + ); + input.commit(); + } } } }