linux: Fix IBus in vim mode and some compose state fixes (#12299)

Release Notes:

- N/A

Fixes #12198 and some minor fixes:
* IBus was intercepting normal keys like `a`, `k` which caused some
problems in vim mode.
* Wayland: Trying to commit the pre_edit on click wasn't working
properly, should be fixed now.
* X11: The pre_edit was supposed to be cleared when losing keyboard
focus.
* X11: We should commit the pre_edit on click.

---------

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Fernando Tagawa 2024-05-26 21:17:38 -03:00 committed by GitHub
parent 74c972fa37
commit 6276281e8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 185 additions and 30 deletions

View file

@ -71,7 +71,8 @@ use crate::platform::linux::xdg_desktop_portal::{Event as XDPEvent, XDPEventSour
use crate::platform::linux::LinuxClient;
use crate::platform::PlatformWindow;
use crate::{
point, px, FileDropEvent, ForegroundExecutor, MouseExitEvent, WindowAppearance, SCROLL_LINES,
point, px, Bounds, FileDropEvent, ForegroundExecutor, MouseExitEvent, WindowAppearance,
SCROLL_LINES,
};
use crate::{
AnyWindowHandle, CursorStyle, DisplayId, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
@ -151,6 +152,7 @@ pub(crate) struct WaylandClientState {
data_device: Option<wl_data_device::WlDataDevice>,
text_input: Option<zwp_text_input_v3::ZwpTextInputV3>,
pre_edit_text: Option<String>,
composing: bool,
// Surface to Window mapping
windows: HashMap<ObjectId, WaylandWindowStatePtr>,
// Output to scale mapping
@ -218,6 +220,41 @@ impl WaylandClientStatePtr {
self.0.upgrade().unwrap().borrow().serial_tracker.get(kind)
}
pub fn enable_ime(&self) {
let client = self.get_client();
let mut state = client.borrow_mut();
let Some(mut text_input) = state.text_input.take() else {
return;
};
text_input.enable();
text_input.set_content_type(ContentHint::None, ContentPurpose::Normal);
if let Some(window) = state.keyboard_focused_window.clone() {
drop(state);
if let Some(area) = window.get_ime_area() {
text_input.set_cursor_rectangle(
area.origin.x.0 as i32,
area.origin.y.0 as i32,
area.size.width.0 as i32,
area.size.height.0 as i32,
);
}
state = client.borrow_mut();
}
text_input.commit();
state.text_input = Some(text_input);
}
pub fn disable_ime(&self) {
let client = self.get_client();
let mut state = client.borrow_mut();
state.composing = false;
if let Some(text_input) = &state.text_input {
text_input.disable();
text_input.commit();
}
}
pub fn drop_window(&self, surface_id: &ObjectId) {
let mut client = self.get_client();
let mut state = client.borrow_mut();
@ -372,6 +409,7 @@ impl WaylandClient {
data_device,
text_input: None,
pre_edit_text: None,
composing: false,
output_scales: outputs,
windows: HashMap::default(),
common,
@ -1050,34 +1088,35 @@ impl Dispatch<zwp_text_input_v3::ZwpTextInputV3, ()> for WaylandClientStatePtr {
let mut state = client.borrow_mut();
match event {
zwp_text_input_v3::Event::Enter { surface } => {
text_input.enable();
text_input.set_content_type(ContentHint::None, ContentPurpose::Normal);
if let Some(window) = state.keyboard_focused_window.clone() {
drop(state);
if let Some(area) = window.get_ime_area() {
text_input.set_cursor_rectangle(
area.origin.x.0 as i32,
area.origin.y.0 as i32,
area.size.width.0 as i32,
area.size.height.0 as i32,
);
}
}
text_input.commit();
drop(state);
this.enable_ime();
}
zwp_text_input_v3::Event::Leave { surface } => {
text_input.disable();
text_input.commit();
drop(state);
this.disable_ime();
}
zwp_text_input_v3::Event::CommitString { text } => {
state.composing = false;
let Some(window) = state.keyboard_focused_window.clone() else {
return;
};
if let Some(commit_text) = text {
drop(state);
window.handle_ime(ImeInput::InsertText(commit_text));
// IBus Intercepts keys like `a`, `b`, but those keys are needed for vim mode.
// We should only send ASCII characters to Zed, otherwise a user could remap a letter like `か` or `相`.
if commit_text.len() == 1 {
window.handle_input(PlatformInput::KeyDown(KeyDownEvent {
keystroke: Keystroke {
modifiers: Modifiers::default(),
key: commit_text.clone(),
ime_key: Some(commit_text),
},
is_held: false,
}));
} else {
window.handle_ime(ImeInput::InsertText(commit_text));
}
}
}
zwp_text_input_v3::Event::PreeditString {
@ -1085,6 +1124,7 @@ impl Dispatch<zwp_text_input_v3::ZwpTextInputV3, ()> for WaylandClientStatePtr {
cursor_begin,
cursor_end,
} => {
state.composing = true;
state.pre_edit_text = text;
}
zwp_text_input_v3::Event::Done { serial } => {
@ -1238,15 +1278,23 @@ impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClientStatePtr {
}
match button_state {
wl_pointer::ButtonState::Pressed => {
if let (Some(window), Some(text), Some(compose_state)) = (
state.keyboard_focused_window.clone(),
state.pre_edit_text.take(),
state.compose_state.as_mut(),
) {
compose_state.reset();
drop(state);
window.handle_ime(ImeInput::InsertText(text));
state = client.borrow_mut();
if let Some(window) = state.keyboard_focused_window.clone() {
if state.composing && state.text_input.is_some() {
let text_input = state.text_input.as_ref().unwrap();
drop(state);
// text_input_v3 don't have something like a reset function
this.disable_ime();
this.enable_ime();
window.handle_ime(ImeInput::UnmarkText);
state = client.borrow_mut();
} else if let (Some(text), Some(compose)) =
(state.pre_edit_text.take(), state.compose_state.as_mut())
{
compose.reset();
drop(state);
window.handle_ime(ImeInput::InsertText(text));
state = client.borrow_mut();
}
}
let click_elapsed = state.click.last_click.elapsed();