Send empty Ime::Preedit before the Ime::Commit

This should help downstream to automatically clear it.
This commit is contained in:
Kirill Chibisov 2022-09-11 00:48:24 +03:00 committed by GitHub
parent ba49db2cb9
commit 5d2aca90bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 22 deletions

View file

@ -88,25 +88,28 @@ pub(super) fn handle_text_input(
_ => return,
};
// Clear preedit at the start of `Done`.
event_sink.push_window_event(
WindowEvent::Ime(Ime::Preedit(String::new(), None)),
window_id,
);
// Send `Commit`.
if let Some(text) = inner.pending_commit.take() {
event_sink.push_window_event(WindowEvent::Ime(Ime::Commit(text)), window_id);
}
// Always send preedit on `Done` events.
let (text, range) = inner
.pending_preedit
.take()
.map(|preedit| {
let cursor_range = preedit
.cursor_begin
.map(|b| (b, preedit.cursor_end.unwrap_or(b)));
// Send preedit.
if let Some(preedit) = inner.pending_preedit.take() {
let cursor_range = preedit
.cursor_begin
.map(|b| (b, preedit.cursor_end.unwrap_or(b)));
(preedit.text, cursor_range)
})
.unwrap_or_default();
let event = Ime::Preedit(text, range);
event_sink.push_window_event(WindowEvent::Ime(event), window_id);
event_sink.push_window_event(
WindowEvent::Ime(Ime::Preedit(preedit.text, cursor_range)),
window_id,
);
}
}
_ => (),
}

View file

@ -621,6 +621,12 @@ impl<T: 'static> EventProcessor<T> {
// If we're composing right now, send the string we've got from X11 via
// Ime::Commit.
if self.is_composing && keycode == 0 && !written.is_empty() {
let event = Event::WindowEvent {
window_id,
event: WindowEvent::Ime(Ime::Preedit(String::new(), None)),
};
callback(event);
let event = Event::WindowEvent {
window_id,
event: WindowEvent::Ime(Ime::Commit(written)),

View file

@ -431,6 +431,10 @@ declare_class!(
let is_control = string.chars().next().map_or(false, |c| c.is_control());
if self.is_ime_enabled() && !is_control {
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: self.window_id(),
event: WindowEvent::Ime(Ime::Preedit(String::new(), None)),
}));
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: self.window_id(),
event: WindowEvent::Ime(Ime::Commit(string)),

View file

@ -1262,6 +1262,10 @@ unsafe fn public_window_callback_inner<T: 'static>(
if let Some(text) = ime_context.get_composed_text() {
userdata.window_state_lock().ime_state = ImeState::Enabled;
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Ime(Ime::Preedit(String::new(), None)),
});
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Ime(Ime::Commit(text)),
@ -1298,6 +1302,10 @@ unsafe fn public_window_callback_inner<T: 'static>(
// trying receiving composing result and commit if exists.
let ime_context = ImeContext::current(window);
if let Some(text) = ime_context.get_composed_text() {
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Ime(Ime::Preedit(String::new(), None)),
});
userdata.send_event(Event::WindowEvent {
window_id: RootWindowId(WindowId(window)),
event: WindowEvent::Ime(Ime::Commit(text)),