Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui_linux: Fix unbounded memory growth when composing text with Fcitx5 under KWin/Wayland (#61079)

ᴀᴍᴛᴏᴀᴇʀ5079b33d6575
diff --git a/crates/gpui_linux/src/linux/wayland/client.rs b/crates/gpui_linux/src/linux/wayland/client.rs
index a4cc9fde9f1..fdc40a3de87 100644
--- a/crates/gpui_linux/src/linux/wayland/client.rs
+++ b/crates/gpui_linux/src/linux/wayland/client.rs
@@ -112,6 +112,80 @@ const MIN_KEYCODE: u32 = 8;
const UNKNOWN_KEYBOARD_LAYOUT_NAME: SharedString = SharedString::new_static("unknown");
const XDG_ACTIVATION_TOKEN_ENV_VAR: &str = "XDG_ACTIVATION_TOKEN";
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+struct ImeCursorRectangle {
+ x: i32,
+ y: i32,
+ width: i32,
+ height: i32,
+}
+
+impl From<Bounds<Pixels>> for ImeCursorRectangle {
+ fn from(bounds: Bounds<Pixels>) -> Self {
+ Self {
+ x: bounds.origin.x.as_f32() as i32,
+ y: bounds.origin.y.as_f32() as i32,
+ width: bounds.size.width.as_f32() as i32,
+ height: bounds.size.height.as_f32() as i32,
+ }
+ }
+}
+
+trait ImeCursorRectangleSink {
+ fn set_ime_cursor_rectangle(&self, x: i32, y: i32, width: i32, height: i32);
+ fn commit_ime_state(&self);
+}
+
+impl ImeCursorRectangleSink for zwp_text_input_v3::ZwpTextInputV3 {
+ fn set_ime_cursor_rectangle(&self, x: i32, y: i32, width: i32, height: i32) {
+ self.set_cursor_rectangle(x, y, width, height);
+ }
+
+ fn commit_ime_state(&self) {
+ self.commit();
+ }
+}
+
+fn set_ime_cursor_rectangle(
+ text_input: &impl ImeCursorRectangleSink,
+ cursor_rectangle: ImeCursorRectangle,
+) {
+ text_input.set_ime_cursor_rectangle(
+ cursor_rectangle.x,
+ cursor_rectangle.y,
+ cursor_rectangle.width,
+ cursor_rectangle.height,
+ );
+}
+
+fn update_ime_cursor_rectangle(
+ text_input: &impl ImeCursorRectangleSink,
+ last_ime_cursor_rectangle: &mut Option<ImeCursorRectangle>,
+ bounds: Bounds<Pixels>,
+) {
+ let cursor_rectangle = ImeCursorRectangle::from(bounds);
+ if *last_ime_cursor_rectangle == Some(cursor_rectangle) {
+ return;
+ }
+
+ *last_ime_cursor_rectangle = Some(cursor_rectangle);
+ set_ime_cursor_rectangle(text_input, cursor_rectangle);
+ text_input.commit_ime_state();
+}
+
+fn set_ime_cursor_rectangle_after_done(
+ text_input: &impl ImeCursorRectangleSink,
+ last_ime_cursor_rectangle: &mut Option<ImeCursorRectangle>,
+ bounds: Bounds<Pixels>,
+ should_commit: bool,
+) {
+ if should_commit {
+ update_ime_cursor_rectangle(text_input, last_ime_cursor_rectangle, bounds);
+ } else {
+ set_ime_cursor_rectangle(text_input, ImeCursorRectangle::from(bounds));
+ }
+}
+
fn take_startup_activation_token_from_environment() -> Option<String> {
let startup_activation_token = std::env::var(XDG_ACTIVATION_TOKEN_ENV_VAR)
.ok()
@@ -244,6 +318,7 @@ pub(crate) struct WaylandClientState {
pre_edit_text: Option<String>,
ime_pre_edit: Option<String>,
composing: bool,
+ last_ime_cursor_rectangle: Option<ImeCursorRectangle>,
// Surface to Window mapping
windows: HashMap<ObjectId, WaylandWindowStatePtr>,
// Output to scale mapping
@@ -349,25 +424,25 @@ impl WaylandClientStatePtr {
let client = self.get_client();
let mut state = client.borrow_mut();
state.ime_enabled = Some(true);
+ state.last_ime_cursor_rectangle = None;
let Some(text_input) = state.text_input.take() else {
return;
};
text_input.enable();
text_input.set_content_type(ContentHint::None, ContentPurpose::Normal);
+ let mut cursor_rectangle = None;
if let Some(window) = state.keyboard_focused_window.clone() {
drop(state);
if let Some(area) = window.get_ime_area() {
- text_input.set_cursor_rectangle(
- f32::from(area.origin.x) as i32,
- f32::from(area.origin.y) as i32,
- f32::from(area.size.width) as i32,
- f32::from(area.size.height) as i32,
- );
+ let area = ImeCursorRectangle::from(area);
+ set_ime_cursor_rectangle(&text_input, area);
+ cursor_rectangle = Some(area);
}
state = client.borrow_mut();
}
text_input.commit();
+ state.last_ime_cursor_rectangle = cursor_rectangle;
state.text_input = Some(text_input);
}
@@ -389,19 +464,14 @@ impl WaylandClientStatePtr {
pub fn update_ime_position(&self, bounds: Bounds<Pixels>) {
let client = self.get_client();
- let state = client.borrow_mut();
- if state.text_input.is_none() || state.pre_edit_text.is_some() {
+ let mut state = client.borrow_mut();
+ if state.pre_edit_text.is_some() {
return;
}
-
- let text_input = state.text_input.as_ref().unwrap();
- text_input.set_cursor_rectangle(
- bounds.origin.x.as_f32() as i32,
- bounds.origin.y.as_f32() as i32,
- bounds.size.width.as_f32() as i32,
- bounds.size.height.as_f32() as i32,
- );
- text_input.commit();
+ let Some(text_input) = state.text_input.clone() else {
+ return;
+ };
+ update_ime_cursor_rectangle(&text_input, &mut state.last_ime_cursor_rectangle, bounds);
}
pub fn handle_keyboard_layout_change(&self) {
@@ -718,6 +788,7 @@ impl WaylandClient {
pre_edit_text: None,
ime_pre_edit: None,
composing: false,
+ last_ime_cursor_rectangle: None,
outputs: HashMap::default(),
in_progress_outputs,
wl_outputs,
@@ -1823,15 +1894,13 @@ impl Dispatch<zwp_text_input_v3::ZwpTextInputV3, ()> for WaylandClientStatePtr {
drop(state);
window.handle_ime(ImeInput::SetMarkedText(text));
if let Some(area) = window.get_ime_area() {
- text_input.set_cursor_rectangle(
- f32::from(area.origin.x) as i32,
- f32::from(area.origin.y) as i32,
- f32::from(area.size.width) as i32,
- f32::from(area.size.height) as i32,
+ let mut state = client.borrow_mut();
+ set_ime_cursor_rectangle_after_done(
+ text_input,
+ &mut state.last_ime_cursor_rectangle,
+ area,
+ last_serial == serial,
);
- if last_serial == serial {
- text_input.commit();
- }
}
} else {
state.composing = false;
@@ -2664,3 +2733,93 @@ impl Dispatch<XdgDialogV1, ()> for WaylandClientStatePtr {
) {
}
}
+
+#[cfg(test)]
+mod tests {
+ use std::cell::Cell;
+
+ use super::*;
+
+ #[derive(Default)]
+ struct FakeImeCursorRectangleSink {
+ cursor_rectangles: RefCell<Vec<(i32, i32, i32, i32)>>,
+ commit_count: Cell<usize>,
+ }
+
+ impl ImeCursorRectangleSink for FakeImeCursorRectangleSink {
+ fn set_ime_cursor_rectangle(&self, x: i32, y: i32, width: i32, height: i32) {
+ self.cursor_rectangles
+ .borrow_mut()
+ .push((x, y, width, height));
+ }
+
+ fn commit_ime_state(&self) {
+ self.commit_count.set(self.commit_count.get() + 1);
+ }
+ }
+
+ fn ime_cursor_bounds(x: f32) -> Bounds<Pixels> {
+ Bounds::new(point(px(x), px(20.25)), size(px(1.0), px(18.75)))
+ }
+
+ #[test]
+ fn caches_cursor_rectangle_committed_after_done() {
+ let text_input = FakeImeCursorRectangleSink::default();
+ let mut last_ime_cursor_rectangle = None;
+ let initial_bounds = ime_cursor_bounds(10.0);
+ let updated_bounds = ime_cursor_bounds(20.0);
+
+ update_ime_cursor_rectangle(&text_input, &mut last_ime_cursor_rectangle, initial_bounds);
+ set_ime_cursor_rectangle_after_done(
+ &text_input,
+ &mut last_ime_cursor_rectangle,
+ updated_bounds,
+ true,
+ );
+ update_ime_cursor_rectangle(&text_input, &mut last_ime_cursor_rectangle, updated_bounds);
+
+ assert_eq!(text_input.commit_count.get(), 2);
+ assert_eq!(text_input.cursor_rectangles.borrow().len(), 2);
+ }
+
+ #[test]
+ fn skips_unchanged_cursor_rectangle_after_done() {
+ let text_input = FakeImeCursorRectangleSink::default();
+ let mut last_ime_cursor_rectangle = None;
+ let bounds = ime_cursor_bounds(10.0);
+
+ update_ime_cursor_rectangle(&text_input, &mut last_ime_cursor_rectangle, bounds);
+ set_ime_cursor_rectangle_after_done(
+ &text_input,
+ &mut last_ime_cursor_rectangle,
+ bounds,
+ true,
+ );
+
+ assert_eq!(text_input.commit_count.get(), 1);
+ assert_eq!(text_input.cursor_rectangles.borrow().len(), 1);
+ }
+
+ #[test]
+ fn skips_cursor_rectangles_with_unchanged_protocol_coordinates() {
+ let text_input = FakeImeCursorRectangleSink::default();
+ let mut last_ime_cursor_rectangle = None;
+
+ update_ime_cursor_rectangle(
+ &text_input,
+ &mut last_ime_cursor_rectangle,
+ ime_cursor_bounds(10.25),
+ );
+ update_ime_cursor_rectangle(
+ &text_input,
+ &mut last_ime_cursor_rectangle,
+ ime_cursor_bounds(10.75),
+ );
+
+ assert_eq!(text_input.commit_count.get(), 1);
+ assert_eq!(
+ text_input.cursor_rectangles.borrow().as_slice(),
+ &[(10, 20, 1, 18)]
+ );
+ }
+}