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:
parent
74c972fa37
commit
6276281e8a
4 changed files with 185 additions and 30 deletions
|
@ -71,7 +71,8 @@ use crate::platform::linux::xdg_desktop_portal::{Event as XDPEvent, XDPEventSour
|
||||||
use crate::platform::linux::LinuxClient;
|
use crate::platform::linux::LinuxClient;
|
||||||
use crate::platform::PlatformWindow;
|
use crate::platform::PlatformWindow;
|
||||||
use crate::{
|
use crate::{
|
||||||
point, px, FileDropEvent, ForegroundExecutor, MouseExitEvent, WindowAppearance, SCROLL_LINES,
|
point, px, Bounds, FileDropEvent, ForegroundExecutor, MouseExitEvent, WindowAppearance,
|
||||||
|
SCROLL_LINES,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
AnyWindowHandle, CursorStyle, DisplayId, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
|
AnyWindowHandle, CursorStyle, DisplayId, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
|
||||||
|
@ -151,6 +152,7 @@ pub(crate) struct WaylandClientState {
|
||||||
data_device: Option<wl_data_device::WlDataDevice>,
|
data_device: Option<wl_data_device::WlDataDevice>,
|
||||||
text_input: Option<zwp_text_input_v3::ZwpTextInputV3>,
|
text_input: Option<zwp_text_input_v3::ZwpTextInputV3>,
|
||||||
pre_edit_text: Option<String>,
|
pre_edit_text: Option<String>,
|
||||||
|
composing: bool,
|
||||||
// Surface to Window mapping
|
// Surface to Window mapping
|
||||||
windows: HashMap<ObjectId, WaylandWindowStatePtr>,
|
windows: HashMap<ObjectId, WaylandWindowStatePtr>,
|
||||||
// Output to scale mapping
|
// Output to scale mapping
|
||||||
|
@ -218,6 +220,41 @@ impl WaylandClientStatePtr {
|
||||||
self.0.upgrade().unwrap().borrow().serial_tracker.get(kind)
|
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) {
|
pub fn drop_window(&self, surface_id: &ObjectId) {
|
||||||
let mut client = self.get_client();
|
let mut client = self.get_client();
|
||||||
let mut state = client.borrow_mut();
|
let mut state = client.borrow_mut();
|
||||||
|
@ -372,6 +409,7 @@ impl WaylandClient {
|
||||||
data_device,
|
data_device,
|
||||||
text_input: None,
|
text_input: None,
|
||||||
pre_edit_text: None,
|
pre_edit_text: None,
|
||||||
|
composing: false,
|
||||||
output_scales: outputs,
|
output_scales: outputs,
|
||||||
windows: HashMap::default(),
|
windows: HashMap::default(),
|
||||||
common,
|
common,
|
||||||
|
@ -1050,41 +1088,43 @@ impl Dispatch<zwp_text_input_v3::ZwpTextInputV3, ()> for WaylandClientStatePtr {
|
||||||
let mut state = client.borrow_mut();
|
let mut state = client.borrow_mut();
|
||||||
match event {
|
match event {
|
||||||
zwp_text_input_v3::Event::Enter { surface } => {
|
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);
|
drop(state);
|
||||||
if let Some(area) = window.get_ime_area() {
|
this.enable_ime();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
zwp_text_input_v3::Event::Leave { surface } => {
|
zwp_text_input_v3::Event::Leave { surface } => {
|
||||||
text_input.disable();
|
drop(state);
|
||||||
text_input.commit();
|
this.disable_ime();
|
||||||
}
|
}
|
||||||
zwp_text_input_v3::Event::CommitString { text } => {
|
zwp_text_input_v3::Event::CommitString { text } => {
|
||||||
|
state.composing = false;
|
||||||
let Some(window) = state.keyboard_focused_window.clone() else {
|
let Some(window) = state.keyboard_focused_window.clone() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(commit_text) = text {
|
if let Some(commit_text) = text {
|
||||||
drop(state);
|
drop(state);
|
||||||
|
// 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));
|
window.handle_ime(ImeInput::InsertText(commit_text));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
zwp_text_input_v3::Event::PreeditString {
|
zwp_text_input_v3::Event::PreeditString {
|
||||||
text,
|
text,
|
||||||
cursor_begin,
|
cursor_begin,
|
||||||
cursor_end,
|
cursor_end,
|
||||||
} => {
|
} => {
|
||||||
|
state.composing = true;
|
||||||
state.pre_edit_text = text;
|
state.pre_edit_text = text;
|
||||||
}
|
}
|
||||||
zwp_text_input_v3::Event::Done { serial } => {
|
zwp_text_input_v3::Event::Done { serial } => {
|
||||||
|
@ -1238,16 +1278,24 @@ impl Dispatch<wl_pointer::WlPointer, ()> for WaylandClientStatePtr {
|
||||||
}
|
}
|
||||||
match button_state {
|
match button_state {
|
||||||
wl_pointer::ButtonState::Pressed => {
|
wl_pointer::ButtonState::Pressed => {
|
||||||
if let (Some(window), Some(text), Some(compose_state)) = (
|
if let Some(window) = state.keyboard_focused_window.clone() {
|
||||||
state.keyboard_focused_window.clone(),
|
if state.composing && state.text_input.is_some() {
|
||||||
state.pre_edit_text.take(),
|
let text_input = state.text_input.as_ref().unwrap();
|
||||||
state.compose_state.as_mut(),
|
drop(state);
|
||||||
) {
|
// text_input_v3 don't have something like a reset function
|
||||||
compose_state.reset();
|
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);
|
drop(state);
|
||||||
window.handle_ime(ImeInput::InsertText(text));
|
window.handle_ime(ImeInput::InsertText(text));
|
||||||
state = client.borrow_mut();
|
state = client.borrow_mut();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
let click_elapsed = state.click.last_click.elapsed();
|
let click_elapsed = state.click.last_click.elapsed();
|
||||||
|
|
||||||
if click_elapsed < DOUBLE_CLICK_INTERVAL
|
if click_elapsed < DOUBLE_CLICK_INTERVAL
|
||||||
|
|
|
@ -170,6 +170,7 @@ pub(crate) struct WaylandWindow(pub WaylandWindowStatePtr);
|
||||||
pub enum ImeInput {
|
pub enum ImeInput {
|
||||||
InsertText(String),
|
InsertText(String),
|
||||||
SetMarkedText(String),
|
SetMarkedText(String),
|
||||||
|
UnmarkText,
|
||||||
DeleteText,
|
DeleteText,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,6 +449,9 @@ impl WaylandWindowStatePtr {
|
||||||
ImeInput::SetMarkedText(text) => {
|
ImeInput::SetMarkedText(text) => {
|
||||||
input_handler.replace_and_mark_text_in_range(None, &text, None);
|
input_handler.replace_and_mark_text_in_range(None, &text, None);
|
||||||
}
|
}
|
||||||
|
ImeInput::UnmarkText => {
|
||||||
|
input_handler.unmark_text();
|
||||||
|
}
|
||||||
ImeInput::DeleteText => {
|
ImeInput::DeleteText => {
|
||||||
if let Some(marked) = input_handler.marked_text_range() {
|
if let Some(marked) = input_handler.marked_text_range() {
|
||||||
input_handler.replace_text_in_range(Some(marked), "");
|
input_handler.replace_text_in_range(Some(marked), "");
|
||||||
|
|
|
@ -25,6 +25,7 @@ use x11rb::protocol::{randr, render, xinput, xkb, xproto, Event};
|
||||||
use x11rb::resource_manager::Database;
|
use x11rb::resource_manager::Database;
|
||||||
use x11rb::xcb_ffi::XCBConnection;
|
use x11rb::xcb_ffi::XCBConnection;
|
||||||
use xim::{x11rb::X11rbClient, Client};
|
use xim::{x11rb::X11rbClient, Client};
|
||||||
|
use xim::{AHashMap, AttributeName, ClientHandler, InputStyle};
|
||||||
use xkbc::x11::ffi::{XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION};
|
use xkbc::x11::ffi::{XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION};
|
||||||
use xkbcommon::xkb as xkbc;
|
use xkbcommon::xkb as xkbc;
|
||||||
|
|
||||||
|
@ -113,6 +114,7 @@ pub struct X11ClientState {
|
||||||
|
|
||||||
pub(crate) compose_state: xkbc::compose::State,
|
pub(crate) compose_state: xkbc::compose::State,
|
||||||
pub(crate) pre_edit_text: Option<String>,
|
pub(crate) pre_edit_text: Option<String>,
|
||||||
|
pub(crate) composing: bool,
|
||||||
pub(crate) cursor_handle: cursor::Handle,
|
pub(crate) cursor_handle: cursor::Handle,
|
||||||
pub(crate) cursor_styles: HashMap<xproto::Window, CursorStyle>,
|
pub(crate) cursor_styles: HashMap<xproto::Window, CursorStyle>,
|
||||||
pub(crate) cursor_cache: HashMap<CursorStyle, xproto::Cursor>,
|
pub(crate) cursor_cache: HashMap<CursorStyle, xproto::Cursor>,
|
||||||
|
@ -393,6 +395,7 @@ impl X11Client {
|
||||||
|
|
||||||
compose_state: compose_state,
|
compose_state: compose_state,
|
||||||
pre_edit_text: None,
|
pre_edit_text: None,
|
||||||
|
composing: false,
|
||||||
|
|
||||||
cursor_handle,
|
cursor_handle,
|
||||||
cursor_styles: HashMap::default(),
|
cursor_styles: HashMap::default(),
|
||||||
|
@ -407,6 +410,58 @@ impl X11Client {
|
||||||
})))
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn enable_ime(&self) {
|
||||||
|
let mut state = self.0.borrow_mut();
|
||||||
|
if state.ximc.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut ximc = state.ximc.take().unwrap();
|
||||||
|
let mut xim_handler = state.xim_handler.take().unwrap();
|
||||||
|
let mut ic_attributes = ximc
|
||||||
|
.build_ic_attributes()
|
||||||
|
.push(
|
||||||
|
AttributeName::InputStyle,
|
||||||
|
InputStyle::PREEDIT_CALLBACKS
|
||||||
|
| InputStyle::STATUS_NOTHING
|
||||||
|
| InputStyle::PREEDIT_NONE,
|
||||||
|
)
|
||||||
|
.push(AttributeName::ClientWindow, xim_handler.window)
|
||||||
|
.push(AttributeName::FocusWindow, xim_handler.window);
|
||||||
|
|
||||||
|
let window_id = state.focused_window;
|
||||||
|
drop(state);
|
||||||
|
if let Some(window_id) = window_id {
|
||||||
|
let window = self.get_window(window_id).unwrap();
|
||||||
|
if let Some(area) = window.get_ime_area() {
|
||||||
|
ic_attributes =
|
||||||
|
ic_attributes.nested_list(xim::AttributeName::PreeditAttributes, |b| {
|
||||||
|
b.push(
|
||||||
|
xim::AttributeName::SpotLocation,
|
||||||
|
xim::Point {
|
||||||
|
x: u32::from(area.origin.x + area.size.width) as i16,
|
||||||
|
y: u32::from(area.origin.y + area.size.height) as i16,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ximc.create_ic(xim_handler.im_id, ic_attributes.build());
|
||||||
|
state = self.0.borrow_mut();
|
||||||
|
state.xim_handler = Some(xim_handler);
|
||||||
|
state.ximc = Some(ximc);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn disable_ime(&self) {
|
||||||
|
let mut state = self.0.borrow_mut();
|
||||||
|
state.composing = false;
|
||||||
|
if let Some(mut ximc) = state.ximc.take() {
|
||||||
|
let xim_handler = state.xim_handler.as_ref().unwrap();
|
||||||
|
ximc.destroy_ic(xim_handler.im_id, xim_handler.ic_id);
|
||||||
|
state.ximc = Some(ximc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_window(&self, win: xproto::Window) -> Option<X11WindowStatePtr> {
|
fn get_window(&self, win: xproto::Window) -> Option<X11WindowStatePtr> {
|
||||||
let state = self.0.borrow();
|
let state = self.0.borrow();
|
||||||
state
|
state
|
||||||
|
@ -452,12 +507,21 @@ impl X11Client {
|
||||||
Event::FocusIn(event) => {
|
Event::FocusIn(event) => {
|
||||||
let window = self.get_window(event.event)?;
|
let window = self.get_window(event.event)?;
|
||||||
window.set_focused(true);
|
window.set_focused(true);
|
||||||
self.0.borrow_mut().focused_window = Some(event.event);
|
let mut state = self.0.borrow_mut();
|
||||||
|
state.focused_window = Some(event.event);
|
||||||
|
drop(state);
|
||||||
|
self.enable_ime();
|
||||||
}
|
}
|
||||||
Event::FocusOut(event) => {
|
Event::FocusOut(event) => {
|
||||||
let window = self.get_window(event.event)?;
|
let window = self.get_window(event.event)?;
|
||||||
window.set_focused(false);
|
window.set_focused(false);
|
||||||
self.0.borrow_mut().focused_window = None;
|
let mut state = self.0.borrow_mut();
|
||||||
|
state.focused_window = None;
|
||||||
|
state.compose_state.reset();
|
||||||
|
state.pre_edit_text.take();
|
||||||
|
drop(state);
|
||||||
|
self.disable_ime();
|
||||||
|
window.handle_ime_delete();
|
||||||
}
|
}
|
||||||
Event::XkbStateNotify(event) => {
|
Event::XkbStateNotify(event) => {
|
||||||
let mut state = self.0.borrow_mut();
|
let mut state = self.0.borrow_mut();
|
||||||
|
@ -558,6 +622,19 @@ impl X11Client {
|
||||||
px(event.event_x as f32 / u16::MAX as f32 / state.scale_factor),
|
px(event.event_x as f32 / u16::MAX as f32 / state.scale_factor),
|
||||||
px(event.event_y as f32 / u16::MAX as f32 / state.scale_factor),
|
px(event.event_y as f32 / u16::MAX as f32 / state.scale_factor),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if state.composing && state.ximc.is_some() {
|
||||||
|
drop(state);
|
||||||
|
self.disable_ime();
|
||||||
|
self.enable_ime();
|
||||||
|
window.handle_ime_unmark();
|
||||||
|
state = self.0.borrow_mut();
|
||||||
|
} else if let Some(text) = state.pre_edit_text.take() {
|
||||||
|
state.compose_state.reset();
|
||||||
|
drop(state);
|
||||||
|
window.handle_ime_commit(text);
|
||||||
|
state = self.0.borrow_mut();
|
||||||
|
}
|
||||||
if let Some(button) = button_of_key(event.detail.try_into().unwrap()) {
|
if let Some(button) = button_of_key(event.detail.try_into().unwrap()) {
|
||||||
let click_elapsed = state.last_click.elapsed();
|
let click_elapsed = state.last_click.elapsed();
|
||||||
|
|
||||||
|
@ -739,6 +816,9 @@ impl X11Client {
|
||||||
|
|
||||||
fn xim_handle_commit(&self, window: xproto::Window, text: String) -> Option<()> {
|
fn xim_handle_commit(&self, window: xproto::Window, text: String) -> Option<()> {
|
||||||
let window = self.get_window(window).unwrap();
|
let window = self.get_window(window).unwrap();
|
||||||
|
let mut state = self.0.borrow_mut();
|
||||||
|
state.composing = false;
|
||||||
|
drop(state);
|
||||||
|
|
||||||
window.handle_ime_commit(text);
|
window.handle_ime_commit(text);
|
||||||
Some(())
|
Some(())
|
||||||
|
@ -751,6 +831,7 @@ impl X11Client {
|
||||||
let mut state = self.0.borrow_mut();
|
let mut state = self.0.borrow_mut();
|
||||||
let mut ximc = state.ximc.take().unwrap();
|
let mut ximc = state.ximc.take().unwrap();
|
||||||
let mut xim_handler = state.xim_handler.take().unwrap();
|
let mut xim_handler = state.xim_handler.take().unwrap();
|
||||||
|
state.composing = true;
|
||||||
drop(state);
|
drop(state);
|
||||||
|
|
||||||
if let Some(area) = window.get_ime_area() {
|
if let Some(area) = window.get_ime_area() {
|
||||||
|
|
|
@ -579,6 +579,28 @@ impl X11WindowStatePtr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_ime_unmark(&self) {
|
||||||
|
let mut state = self.state.borrow_mut();
|
||||||
|
if let Some(mut input_handler) = state.input_handler.take() {
|
||||||
|
drop(state);
|
||||||
|
input_handler.unmark_text();
|
||||||
|
let mut state = self.state.borrow_mut();
|
||||||
|
state.input_handler = Some(input_handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_ime_delete(&self) {
|
||||||
|
let mut state = self.state.borrow_mut();
|
||||||
|
if let Some(mut input_handler) = state.input_handler.take() {
|
||||||
|
drop(state);
|
||||||
|
if let Some(marked) = input_handler.marked_text_range() {
|
||||||
|
input_handler.replace_text_in_range(Some(marked), "");
|
||||||
|
}
|
||||||
|
let mut state = self.state.borrow_mut();
|
||||||
|
state.input_handler = Some(input_handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_ime_area(&self) -> Option<Bounds<Pixels>> {
|
pub fn get_ime_area(&self) -> Option<Bounds<Pixels>> {
|
||||||
let mut state = self.state.borrow_mut();
|
let mut state = self.state.borrow_mut();
|
||||||
let mut bounds: Option<Bounds<Pixels>> = None;
|
let mut bounds: Option<Bounds<Pixels>> = None;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue