linux: Fix IME panel position while enumerating input methods (#12495)

Release Notes:

- N/A

This updates the IME position every time the selection changes, this is
probably only useful when you enumerate languages with your IME.

TODO:
- ~There is a rare chance that the ime panel is not updated because the
window input handler is None.~
- ~Update IME panel in vim mode.~
- ~Update IME panel when leaving Buffer search input.~

---------

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
Fernando Tagawa
2024-08-28 19:58:40 -07:00
committed by GitHub
co-authored by Mikayla Maki
parent e6d5f4406f
commit 8e8927db4b
21 changed files with 244 additions and 55 deletions
@@ -312,6 +312,23 @@ impl WaylandClientStatePtr {
}
}
pub fn update_ime_position(&self, bounds: Bounds<Pixels>) {
let client = self.get_client();
let mut state = client.borrow_mut();
if state.composing || state.text_input.is_none() {
return;
}
let text_input = state.text_input.as_ref().unwrap();
text_input.set_cursor_rectangle(
bounds.origin.x.0 as i32,
bounds.origin.y.0 as i32,
bounds.size.width.0 as i32,
bounds.size.height.0 as i32,
);
text_input.commit();
}
pub fn drop_window(&self, surface_id: &ObjectId) {
let mut client = self.get_client();
let mut state = client.borrow_mut();
@@ -1353,6 +1370,7 @@ impl Dispatch<zwp_text_input_v3::ZwpTextInputV3, ()> for WaylandClientStatePtr {
}
}
} else {
state.composing = false;
drop(state);
window.handle_ime(ImeInput::DeleteText);
}
@@ -622,8 +622,12 @@ impl WaylandWindowStatePtr {
let mut bounds: Option<Bounds<Pixels>> = None;
if let Some(mut input_handler) = state.input_handler.take() {
drop(state);
if let Some(range) = input_handler.selected_text_range() {
bounds = input_handler.bounds_for_range(range);
if let Some(selection) = input_handler.selected_text_range(true) {
bounds = input_handler.bounds_for_range(if selection.reversed {
selection.range.start..selection.range.start
} else {
selection.range.end..selection.range.end
});
}
self.state.borrow_mut().input_handler = Some(input_handler);
}
@@ -1006,6 +1010,13 @@ impl PlatformWindow for WaylandWindow {
}
}
fn update_ime_position(&self, bounds: Bounds<Pixels>) {
let state = self.borrow();
let client = state.client.clone();
drop(state);
client.update_ime_position(bounds);
}
fn gpu_specs(&self) -> Option<GPUSpecs> {
self.borrow().renderer.gpu_specs().into()
}
+43 -3
View File
@@ -148,8 +148,12 @@ pub struct X11ClientState {
pub struct X11ClientStatePtr(pub Weak<RefCell<X11ClientState>>);
impl X11ClientStatePtr {
fn get_client(&self) -> X11Client {
X11Client(self.0.upgrade().expect("client already dropped"))
}
pub fn drop_window(&self, x_window: u32) {
let client = X11Client(self.0.upgrade().expect("client already dropped"));
let client = self.get_client();
let mut state = client.0.borrow_mut();
if let Some(window_ref) = state.windows.remove(&x_window) {
@@ -167,6 +171,42 @@ impl X11ClientStatePtr {
state.common.signal.stop();
}
}
pub fn update_ime_position(&self, bounds: Bounds<Pixels>) {
let client = self.get_client();
let mut state = client.0.borrow_mut();
if state.composing || state.ximc.is_none() {
return;
}
let mut ximc = state.ximc.take().unwrap();
let xim_handler = state.xim_handler.take().unwrap();
let ic_attributes = ximc
.build_ic_attributes()
.push(
xim::AttributeName::InputStyle,
xim::InputStyle::PREEDIT_CALLBACKS
| xim::InputStyle::STATUS_NOTHING
| xim::InputStyle::PREEDIT_POSITION,
)
.push(xim::AttributeName::ClientWindow, xim_handler.window)
.push(xim::AttributeName::FocusWindow, xim_handler.window)
.nested_list(xim::AttributeName::PreeditAttributes, |b| {
b.push(
xim::AttributeName::SpotLocation,
xim::Point {
x: u32::from(bounds.origin.x + bounds.size.width) as i16,
y: u32::from(bounds.origin.y + bounds.size.height) as i16,
},
);
})
.build();
let _ = ximc
.set_ic_values(xim_handler.im_id, xim_handler.ic_id, ic_attributes)
.log_err();
state.ximc = Some(ximc);
state.xim_handler = Some(xim_handler);
}
}
#[derive(Clone)]
@@ -1029,13 +1069,13 @@ impl X11Client {
fn xim_handle_preedit(&self, window: xproto::Window, text: String) -> Option<()> {
let window = self.get_window(window).unwrap();
window.handle_ime_preedit(text);
let mut state = self.0.borrow_mut();
let mut ximc = state.ximc.take().unwrap();
let mut xim_handler = state.xim_handler.take().unwrap();
state.composing = true;
state.composing = !text.is_empty();
drop(state);
window.handle_ime_preedit(text);
if let Some(area) = window.get_ime_area() {
let ic_attributes = ximc
+9 -2
View File
@@ -873,8 +873,8 @@ impl X11WindowStatePtr {
let mut bounds: Option<Bounds<Pixels>> = None;
if let Some(mut input_handler) = state.input_handler.take() {
drop(state);
if let Some(range) = input_handler.selected_text_range() {
bounds = input_handler.bounds_for_range(range);
if let Some(selection) = input_handler.selected_text_range(true) {
bounds = input_handler.bounds_for_range(selection.range);
}
let mut state = self.state.borrow_mut();
state.input_handler = Some(input_handler);
@@ -1396,6 +1396,13 @@ impl PlatformWindow for X11Window {
}
}
fn update_ime_position(&self, bounds: Bounds<Pixels>) {
let mut state = self.0.state.borrow_mut();
let client = state.client.clone();
drop(state);
client.update_ime_position(bounds);
}
fn gpu_specs(&self) -> Option<GPUSpecs> {
self.0.state.borrow().renderer.gpu_specs().into()
}
+13 -4
View File
@@ -1120,6 +1120,13 @@ impl PlatformWindow for MacWindow {
None
}
fn update_ime_position(&self, _bounds: Bounds<Pixels>) {
unsafe {
let input_context: id = msg_send![class!(NSTextInputContext), currentInputContext];
let _: () = msg_send![input_context, invalidateCharacterCoordinates];
}
}
fn fps(&self) -> Option<f32> {
Some(self.0.lock().renderer.fps())
}
@@ -1311,7 +1318,7 @@ extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent:
// enter it will still swallow certain keys (e.g. 'f', 'j') and not others
// (e.g. 'n'). This is a problem for certain kinds of views, like the terminal.
with_input_handler(this, |input_handler| {
if input_handler.selected_text_range().is_none() {
if input_handler.selected_text_range(false).is_none() {
handled = true;
input_handler.replace_text_in_range(None, &text)
}
@@ -1683,10 +1690,12 @@ extern "C" fn marked_range(this: &Object, _: Sel) -> NSRange {
}
extern "C" fn selected_range(this: &Object, _: Sel) -> NSRange {
let selected_range_result =
with_input_handler(this, |input_handler| input_handler.selected_text_range()).flatten();
let selected_range_result = with_input_handler(this, |input_handler| {
input_handler.selected_text_range(false)
})
.flatten();
selected_range_result.map_or(NSRange::invalid(), |range| range.into())
selected_range_result.map_or(NSRange::invalid(), |selection| selection.range.into())
}
extern "C" fn first_rect_for_character_range(
+2
View File
@@ -279,6 +279,8 @@ impl PlatformWindow for TestWindow {
unimplemented!()
}
fn update_ime_position(&self, _bounds: Bounds<Pixels>) {}
fn gpu_specs(&self) -> Option<GPUSpecs> {
None
}
+3 -2
View File
@@ -579,8 +579,8 @@ fn handle_mouse_horizontal_wheel_msg(
fn retrieve_caret_position(state_ptr: &Rc<WindowsWindowStatePtr>) -> Option<POINT> {
with_input_handler_and_scale_factor(state_ptr, |input_handler, scale_factor| {
let caret_range = input_handler.selected_text_range()?;
let caret_position = input_handler.bounds_for_range(caret_range)?;
let caret_range = input_handler.selected_text_range(false)?;
let caret_position = input_handler.bounds_for_range(caret_range.range)?;
Some(POINT {
// logical to physical
x: (caret_position.origin.x.0 * scale_factor) as i32,
@@ -593,6 +593,7 @@ fn retrieve_caret_position(state_ptr: &Rc<WindowsWindowStatePtr>) -> Option<POIN
fn handle_ime_position(handle: HWND, state_ptr: Rc<WindowsWindowStatePtr>) -> Option<isize> {
unsafe {
let ctx = ImmGetContext(handle);
let Some(caret_position) = retrieve_caret_position(&state_ptr) else {
return Some(0);
};
@@ -676,6 +676,10 @@ impl PlatformWindow for WindowsWindow {
Some(self.0.state.borrow().renderer.gpu_specs())
}
fn update_ime_position(&self, _bounds: Bounds<Pixels>) {
// todo(windows)
}
fn fps(&self) -> Option<f32> {
None
}