W27: Wayland: IME surrounding text and delete-surrounding

Input methods that correct the text around the cursor, such as
Vietnamese engines and predictive keyboards, need the field's text
(text-input-v3 `set_surrounding_text`) and a way to delete part of it
(`delete_surrounding_text`). winit master added both with new core types
and an `Ime::DeleteSurrounding` event; winit 0.30's `Ime` enum is
exhaustive, so they come here as a Wayland extension instead:

- `WindowExtWayland::set_ime_surrounding_text(text, cursor, anchor)`
  clamps the byte offsets to character boundaries and cuts text over the
  protocol's 4000 bytes around the cursor and selection. Enabling a text
  input sends it again with the purpose and cursor area, which enabling
  resets; disallowing IME forgets it.
- A deletion goes out with the `Ime::Commit` of the same input method
  update, an empty one if the update has no commit.
  `WindowExtWayland::take_ime_delete_surrounding` returns it while that
  commit is handled, so the application deletes after removing the
  preedit and before inserting the text, in text-input-v3's order.

Co-authored-by: DorotaC <43449960+dcz-self@users.noreply.github.com>
This commit is contained in:
aionggandDorotaC committed 2026-10-21 18:07:40 +00:00
1 parent 2b9cd1cf47
commit f40c76a238
8 files changed
+264 -13

No files matched your search

+47
View File
@@ -140,15 +140,62 @@ impl<T> EventLoopBuilderExtWayland for EventLoopBuilder<T> {
}
}
/// Text an input method asks to delete around the cursor, in bytes of UTF-8 text.
///
/// The counts exclude the selection and the preedit: `before_bytes` ends where the preedit or
/// selection starts and `after_bytes` starts where it ends.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ImeDeleteSurrounding {
/// Bytes to delete before the cursor.
pub before_bytes: usize,
/// Bytes to delete after the cursor.
pub after_bytes: usize,
}
/// Additional methods on [`Window`] that are specific to Wayland.
///
/// [`Window`]: crate::window::Window
pub trait WindowExtWayland {
/// Returns `xdg_toplevel` of the window or [`None`] if the window is X11 window.
fn xdg_toplevel(&self) -> Option<NonNull<c_void>>;
/// Tells the input method the text around the cursor of the focused text field, so it can
/// base suggestions and corrections on it (text-input-v3 surrounding text).
///
/// `cursor` and `anchor` are byte offsets into `text`, the other end of the selection being
/// `anchor`; without a selection they are equal. Leave the preedit out of `text`. Offsets
/// are clamped to the text and moved back to a character boundary, and text longer than
/// 4000 bytes is cut around the cursor and selection. Call it again whenever the text, cursor
/// or selection changes. Disallowing IME forgets the text. X11 ignores it.
fn set_ime_surrounding_text(&self, text: &str, cursor: usize, anchor: usize);
/// The surrounding text to delete before inserting the [`Ime::Commit`] being handled.
///
/// Call this when handling each `Ime::Commit`, after removing the preedit and before
/// inserting the committed text, which may be empty when an input method only deletes.
/// Returns `None` when the commit deletes nothing, and always on X11.
///
/// [`Ime::Commit`]: crate::event::Ime::Commit
fn take_ime_delete_surrounding(&self) -> Option<ImeDeleteSurrounding>;
}
impl WindowExtWayland for Window {
fn set_ime_surrounding_text(&self, text: &str, cursor: usize, anchor: usize) {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => {},
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => {
window.set_ime_surrounding_text(text, cursor, anchor)
},
}
}
fn take_ime_delete_surrounding(&self) -> Option<ImeDeleteSurrounding> {
match &self.window {
#[cfg(x11_platform)]
crate::platform_impl::Window::X(_) => None,
#[cfg(wayland_platform)]
crate::platform_impl::Window::Wayland(window) => window.take_ime_delete_surrounding(),
}
}
#[inline]
fn xdg_toplevel(&self) -> Option<NonNull<c_void>> {
#[allow(clippy::single_match)]
@@ -23,7 +23,7 @@ use tracing::warn;
use crate::cursor::OnlyCursorImage;
use crate::dpi::LogicalSize;
use crate::error::{EventLoopError, OsError as RootOsError};
use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent};
use crate::event::{Event, Ime, InnerSizeWriter, StartCause, WindowEvent};
use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents};
use crate::platform::pump_events::PumpStatus;
use crate::platform_impl::platform::min_timeout;
@@ -464,6 +464,18 @@ impl<T: 'static> EventLoop<T> {
});
for event in buffer_sink.drain() {
let event = event.map_nonuser_event().unwrap();
// Hand the window the deletion that goes with this commit, for
// `WindowExtWayland::take_ime_delete_surrounding`.
if let Event::WindowEvent { window_id, event: WindowEvent::Ime(Ime::Commit(_)) } =
&event
{
self.with_state(|state| {
let delete = state.ime_deletions.pop_front().flatten();
if let Some(window) = state.windows.get_mut().get(&window_id.0) {
window.lock().unwrap().ime_delete_surrounding = delete;
}
});
}
callback(event, &self.window_target);
}
+1 -1
View File
@@ -28,7 +28,7 @@ mod touch;
pub use pointer::relative_pointer::RelativePointerState;
pub use pointer::{PointerConstraintsState, WinitPointerData, WinitPointerDataExt};
pub use text_input::{TextInputState, ZwpTextInputV3Ext};
pub use text_input::{surrounding_text_excerpt, TextInputState, ZwpTextInputV3Ext};
use keyboard::{KeyboardData, KeyboardState};
use text_input::TextInputData;
@@ -12,6 +12,7 @@ use sctk::reexports::protocols::wp::text_input::zv3::client::zwp_text_input_v3::
};
use crate::event::{Ime, WindowEvent};
use crate::platform::wayland::ImeDeleteSurrounding;
use crate::platform_impl::wayland;
use crate::platform_impl::wayland::state::WinitState;
use crate::window::ImePurpose;
@@ -73,7 +74,7 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {
if window.ime_allowed() {
text_input.enable();
text_input.set_content_type_by_purpose(window.ime_purpose());
window.apply_ime_state(text_input);
text_input.commit();
state.events_sink.push_window_event(WindowEvent::Ime(Ime::Enabled), window_id);
}
@@ -121,6 +122,14 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {
None => return,
};
// The deletion goes out with a commit, so the application applies it after
// removing the preedit and before inserting the text, as text-input-v3 orders
// them.
let delete = text_input_data.pending_delete.take();
if delete.is_some() && text_input_data.pending_commit.is_none() {
text_input_data.pending_commit = Some(String::new());
}
// Clear preedit, unless all we'll be doing next is sending a new preedit.
if text_input_data.pending_commit.is_some()
|| text_input_data.pending_preedit.is_none()
@@ -134,6 +143,7 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {
// Send `Commit`.
if let Some(text) = text_input_data.pending_commit.take() {
state.ime_deletions.push_back(delete);
state
.events_sink
.push_window_event(WindowEvent::Ime(Ime::Commit(text)), window_id);
@@ -151,8 +161,11 @@ impl Dispatch<ZwpTextInputV3, TextInputData, WinitState> for TextInputState {
);
}
},
TextInputEvent::DeleteSurroundingText { .. } => {
// Not handled.
TextInputEvent::DeleteSurroundingText { before_length, after_length } => {
text_input_data.pending_delete = Some(ImeDeleteSurrounding {
before_bytes: before_length as usize,
after_bytes: after_length as usize,
});
},
_ => {},
}
@@ -203,6 +216,9 @@ pub struct TextInputDataInner {
/// Whether the window was last sent a preedit rather than its end.
preedit_shown: bool,
/// The surrounding text to delete on `done`.
pending_delete: Option<ImeDeleteSurrounding>,
}
/// The state of the preedit.
@@ -214,3 +230,100 @@ struct Preedit {
delegate_dispatch!(WinitState: [ZwpTextInputManagerV3: GlobalData] => TextInputState);
delegate_dispatch!(WinitState: [ZwpTextInputV3: TextInputData] => TextInputState);
/// The longest surrounding text text-input-v3 can carry.
const MAX_SURROUNDING_TEXT_BYTES: usize = 4000;
/// Clamps `cursor` and `anchor` to `text` and moves them back to character boundaries, then cuts
/// text longer than [`MAX_SURROUNDING_TEXT_BYTES`] to a window around the cursor and the
/// selection, or around the cursor alone when the selection doesn't fit. Returns the text and the
/// cursor and anchor offsets in it.
pub fn surrounding_text_excerpt(text: &str, cursor: usize, anchor: usize) -> (&str, usize, usize) {
let cursor = floor_char_boundary(text, cursor);
let anchor = floor_char_boundary(text, anchor);
if text.len() <= MAX_SURROUNDING_TEXT_BYTES {
return (text, cursor, anchor);
}
let (low, high) = (cursor.min(anchor), cursor.max(anchor));
let (low, high) =
if high - low <= MAX_SURROUNDING_TEXT_BYTES { (low, high) } else { (cursor, cursor) };
// Split the spare bytes evenly on both sides, giving one side's unused share to the other.
let spare = MAX_SURROUNDING_TEXT_BYTES - (high - low);
let end = (low.saturating_sub(spare / 2) + MAX_SURROUNDING_TEXT_BYTES).min(text.len());
let start = ceil_char_boundary(text, end - MAX_SURROUNDING_TEXT_BYTES);
let end = floor_char_boundary(text, end);
let anchor = anchor.clamp(start, end);
(&text[start..end], cursor - start, anchor - start)
}
fn floor_char_boundary(text: &str, index: usize) -> usize {
let mut index = index.min(text.len());
while !text.is_char_boundary(index) {
index -= 1;
}
index
}
fn ceil_char_boundary(text: &str, index: usize) -> usize {
let mut index = index.min(text.len());
while !text.is_char_boundary(index) {
index += 1;
}
index
}
#[cfg(test)]
mod tests {
use super::{surrounding_text_excerpt, MAX_SURROUNDING_TEXT_BYTES as MAX};
#[test]
fn short_text_is_kept_and_offsets_are_clamped() {
assert_eq!(surrounding_text_excerpt("foobar", 3, 3), ("foobar", 3, 3));
assert_eq!(surrounding_text_excerpt("foobar", 1, 4), ("foobar", 1, 4));
assert_eq!(surrounding_text_excerpt("foo", 10, 99), ("foo", 3, 3));
// `и` is two bytes; an offset inside it moves back to its start.
assert_eq!(surrounding_text_excerpt("граница", 1, 5), ("граница", 0, 4));
}
#[test]
fn long_text_is_cut_around_the_cursor() {
let text = "a".repeat(10_000);
let (excerpt, cursor, anchor) = surrounding_text_excerpt(&text, 5_000, 5_000);
assert_eq!(excerpt.len(), MAX);
assert_eq!((cursor, anchor), (MAX / 2, MAX / 2));
// Near either end, the unused share goes to the other side.
assert_eq!(surrounding_text_excerpt(&text, 100, 100), (&text[..MAX], 100, 100));
let (excerpt, cursor, _) = surrounding_text_excerpt(&text, 9_900, 9_900);
assert_eq!((excerpt.len(), cursor), (MAX, MAX - 100));
}
#[test]
fn long_text_keeps_the_selection() {
let text = "a".repeat(10_000);
let (excerpt, cursor, anchor) = surrounding_text_excerpt(&text, 6_000, 3_000);
assert_eq!(excerpt.len(), MAX);
assert_eq!(cursor - anchor, 3_000);
assert_eq!(anchor, 500);
// A selection longer than the limit keeps the cursor; the anchor stops at the cut.
let (excerpt, cursor, anchor) = surrounding_text_excerpt(&text, 9_000, 1_000);
assert_eq!(excerpt.len(), MAX);
assert_eq!((cursor, anchor), (3_000, 0));
}
#[test]
fn long_text_is_cut_on_char_boundaries() {
// Three-byte characters: 4000 isn't a multiple of 3, so the cut must move inward.
let text = "ấ".repeat(3_000);
for cursor in [0, 4_500, 4_501, 9_000] {
let (excerpt, cursor_in, anchor_in) = surrounding_text_excerpt(&text, cursor, cursor);
assert!(excerpt.len() <= MAX && excerpt.len() > MAX - 6, "{cursor}");
assert!(excerpt.chars().all(|c| c == 'ấ'), "{cursor}: cut inside a character");
assert_eq!(cursor_in % 3, 0, "{cursor}");
assert_eq!(cursor_in, anchor_in);
}
}
}
+6
View File
@@ -2,6 +2,8 @@ use std::cell::RefCell;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
use crate::platform::wayland::ImeDeleteSurrounding;
use ahash::AHashMap;
use sctk::reexports::calloop::LoopHandle;
@@ -100,6 +102,9 @@ pub struct WinitState {
/// event loop run.
pub events_sink: EventSink,
/// The surrounding text deletion that goes with each `Ime::Commit` in `events_sink`, in order.
pub ime_deletions: std::collections::VecDeque<Option<ImeDeleteSurrounding>>,
/// Xdg activation.
pub xdg_activation: Option<XdgActivationState>,
@@ -224,6 +229,7 @@ impl WinitState {
monitors: Arc::new(Mutex::new(monitors)),
events_sink: EventSink::new(),
ime_deletions: Default::default(),
loop_handle,
// Make it true by default.
dispatched_events: true,
@@ -625,6 +625,18 @@ impl Window {
self.window_state.lock().unwrap().set_ime_purpose(purpose);
}
#[inline]
pub fn set_ime_surrounding_text(&self, text: &str, cursor: usize, anchor: usize) {
self.window_state.lock().unwrap().set_ime_surrounding_text(text, cursor, anchor);
}
#[inline]
pub fn take_ime_delete_surrounding(
&self,
) -> Option<crate::platform::wayland::ImeDeleteSurrounding> {
self.window_state.lock().unwrap().ime_delete_surrounding.take()
}
#[inline]
pub fn reset_ime(&self) {
let window_state = self.window_state.lock().unwrap();
@@ -39,8 +39,10 @@ use crate::platform_impl::wayland::types::kwin_blur::KWinBlurManager;
use crate::platform_impl::{PlatformCustomCursor, WindowId};
use crate::window::{CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme};
use crate::platform::wayland::ImeDeleteSurrounding;
use crate::platform_impl::wayland::seat::{
PointerConstraintsState, WinitPointerData, WinitPointerDataExt, ZwpTextInputV3Ext,
surrounding_text_excerpt, PointerConstraintsState, WinitPointerData, WinitPointerDataExt,
ZwpTextInputV3Ext,
};
use crate::platform_impl::wayland::state::{WindowCompositorUpdate, WinitState};
@@ -115,6 +117,12 @@ pub struct WindowState {
/// The last IME cursor area, as `x, y, width, height`.
ime_cursor_area: Option<(i32, i32, i32, i32)>,
/// The last surrounding text, trimmed, with the cursor and anchor offsets in it.
ime_surrounding_text: Option<(String, i32, i32)>,
/// The deletion that goes with the `Ime::Commit` being delivered.
pub ime_delete_surrounding: Option<ImeDeleteSurrounding>,
/// The text inputs observed on the window.
text_inputs: Vec<ZwpTextInputV3>,
@@ -204,6 +212,8 @@ impl WindowState {
ime_allowed: false,
ime_purpose: ImePurpose::Normal,
ime_cursor_area: None,
ime_surrounding_text: None,
ime_delete_surrounding: None,
last_configure: None,
max_inner_size: None,
min_inner_size: MIN_WINDOW_SIZE,
@@ -1022,13 +1032,16 @@ impl WindowState {
/// Returns `true` if the requested state was applied.
pub fn set_ime_allowed(&mut self, allowed: bool) -> bool {
self.ime_allowed = allowed;
if !allowed {
self.ime_surrounding_text = None;
}
let mut applied = false;
for text_input in &self.text_inputs {
applied = true;
if allowed {
text_input.enable();
text_input.set_content_type_by_purpose(self.ime_purpose);
self.apply_ime_state(text_input);
} else {
text_input.disable();
}
@@ -1038,6 +1051,30 @@ impl WindowState {
applied
}
/// Sends the state that enabling a text input resets, without committing it.
pub fn apply_ime_state(&self, text_input: &ZwpTextInputV3) {
text_input.set_content_type_by_purpose(self.ime_purpose);
if let Some((x, y, width, height)) = self.ime_cursor_area {
text_input.set_cursor_rectangle(x, y, width, height);
}
if let Some((text, cursor, anchor)) = &self.ime_surrounding_text {
text_input.set_surrounding_text(text.clone(), *cursor, *anchor);
}
}
/// Set the text around the IME cursor.
pub fn set_ime_surrounding_text(&mut self, text: &str, cursor: usize, anchor: usize) {
let (text, cursor, anchor) = surrounding_text_excerpt(text, cursor, anchor);
let (cursor, anchor) = (cursor as i32, anchor as i32);
if self.ime_allowed {
for text_input in &self.text_inputs {
text_input.set_surrounding_text(text.to_owned(), cursor, anchor);
text_input.commit();
}
}
self.ime_surrounding_text = Some((text.to_owned(), cursor, anchor));
}
/// Set the IME position.
pub fn set_ime_cursor_area(&mut self, position: LogicalPosition<u32>, size: LogicalSize<u32>) {
// FIXME: This won't fly unless user will have a way to request IME window per seat, since
@@ -1062,10 +1099,7 @@ impl WindowState {
text_input.disable();
text_input.commit();
text_input.enable();
text_input.set_content_type_by_purpose(self.ime_purpose);
if let Some((x, y, width, height)) = self.ime_cursor_area {
text_input.set_cursor_rectangle(x, y, width, height);
}
self.apply_ime_state(text_input);
text_input.commit();
preedit_shown |= text_input.take_preedit();
}
+29 -2
View File
@@ -138,11 +138,38 @@ fn linux_reset_ime_discards_the_composition() {
reset[disable..enable].contains("text_input.commit();"),
"Wayland reset doesn't commit between disabling and enabling"
);
for request in ["set_content_type_by_purpose", "set_cursor_rectangle"] {
assert!(reset[enable..].contains(request), "Wayland reset doesn't restore `{request}`");
assert!(
reset[enable..].contains("self.apply_ime_state(text_input);"),
"Wayland reset doesn't restore the IME state"
);
let apply = section(&state, "pub fn apply_ime_state(", "\n }\n");
for request in ["set_content_type_by_purpose", "set_cursor_rectangle", "set_surrounding_text"] {
assert!(apply.contains(request), "enabling text input doesn't restore `{request}`");
}
}
#[test]
fn wayland_surrounding_text_deletion_goes_with_a_commit() {
// text-input-v3 applies a deletion after removing the preedit and before inserting the
// commit. winit 0.30's `Ime` has no deletion event, so the deletion is handed to the window
// as its commit is delivered, and a deletion without a commit gets an empty one.
let text_input = source("linux/wayland/seat/text_input/mod.rs");
let done = section(&text_input, "TextInputEvent::Done { .. } => {", "\n },\n");
assert!(
done.contains("text_input_data.pending_commit = Some(String::new());"),
"a deletion without a commit isn't delivered"
);
assert!(
done.contains("state.ime_deletions.push_back(delete);"),
"commits don't queue their deletion"
);
let event_loop = source("linux/wayland/event_loop/mod.rs");
assert!(
event_loop.contains("window.lock().unwrap().ime_delete_surrounding = delete;"),
"delivering a commit doesn't hand its deletion to the window"
);
}
#[test]
fn windows_reset_ime_cancels_the_composition() {
// `CPS_CANCEL` discards the composition string; `CPS_COMPLETE` would commit it instead. The