Improve IME support with new Event::Ime (#4358)

* Closes #4354 

Fix: can't repeat input chinese words

AND

For Windows :
ImeEnable
ImeDisable

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
rustbasic
2024-04-22 12:43:07 +02:00
committed by GitHub
co-authored by Emil Ernerfeldt
parent 587bc2034a
commit 436c671331
5 changed files with 99 additions and 62 deletions
+21 -8
View File
@@ -445,14 +445,8 @@ pub enum Event {
/// * `zoom > 1`: pinch spread
Zoom(f32),
/// IME composition start.
CompositionStart,
/// A new IME candidate is being suggested.
CompositionUpdate(String),
/// IME composition ended with this final result.
CompositionEnd(String),
/// IME Event
Ime(ImeEvent),
/// On touch screens, report this *in addition to*
/// [`Self::PointerMoved`], [`Self::PointerButton`], [`Self::PointerGone`]
@@ -507,6 +501,25 @@ pub enum Event {
},
}
/// IME event.
///
/// See <https://docs.rs/winit/latest/winit/event/enum.Ime.html>
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ImeEvent {
/// Notifies when the IME was enabled.
Enabled,
/// A new IME candidate is being suggested.
Preedit(String),
/// IME composition ended with this final result.
Commit(String),
/// Notifies when the IME was disabled.
Disabled,
}
/// Mouse button (or similar for touch input)
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
+38 -34
View File
@@ -950,47 +950,51 @@ fn events(
..
} => check_for_mutating_key_press(os, &mut cursor_range, text, galley, modifiers, *key),
Event::CompositionStart => {
state.has_ime = true;
None
}
Event::CompositionUpdate(text_mark) => {
// empty prediction can be produced when user press backspace
// or escape during ime. We should clear current text.
if text_mark != "\n" && text_mark != "\r" && state.has_ime {
let mut ccursor = text.delete_selected(&cursor_range);
let start_cursor = ccursor;
if !text_mark.is_empty() {
text.insert_text_at(&mut ccursor, text_mark, char_limit);
}
Event::Ime(ime_event) => match ime_event {
ImeEvent::Enabled => {
state.ime_enabled = true;
state.ime_cursor_range = cursor_range;
Some(CCursorRange::two(start_cursor, ccursor))
} else {
None
}
}
Event::CompositionEnd(prediction) => {
// CompositionEnd only characters may be typed into TextEdit without trigger CompositionStart first,
// so do not check `state.has_ime = true` in the following statement.
if prediction != "\n" && prediction != "\r" {
state.has_ime = false;
let mut ccursor;
if !prediction.is_empty()
&& cursor_range.secondary.ccursor.index
== state.ime_cursor_range.secondary.ccursor.index
{
ccursor = text.delete_selected(&cursor_range);
text.insert_text_at(&mut ccursor, prediction, char_limit);
ImeEvent::Preedit(text_mark) => {
if text_mark == "\n" || text_mark == "\r" {
None
} else {
ccursor = cursor_range.primary.ccursor;
// Empty prediction can be produced when user press backspace
// or escape during IME, so we clear current text.
let mut ccursor = text.delete_selected(&cursor_range);
let start_cursor = ccursor;
if !text_mark.is_empty() {
text.insert_text_at(&mut ccursor, text_mark, char_limit);
}
state.ime_cursor_range = cursor_range;
Some(CCursorRange::two(start_cursor, ccursor))
}
Some(CCursorRange::one(ccursor))
} else {
}
ImeEvent::Commit(prediction) => {
if prediction == "\n" || prediction == "\r" {
None
} else {
state.ime_enabled = false;
if !prediction.is_empty()
&& cursor_range.secondary.ccursor.index
== state.ime_cursor_range.secondary.ccursor.index
{
let mut ccursor = text.delete_selected(&cursor_range);
text.insert_text_at(&mut ccursor, prediction, char_limit);
Some(CCursorRange::one(ccursor))
} else {
let ccursor = cursor_range.primary.ccursor;
Some(CCursorRange::one(ccursor))
}
}
}
ImeEvent::Disabled => {
state.ime_enabled = false;
None
}
}
},
_ => None,
};
+1 -1
View File
@@ -42,7 +42,7 @@ pub struct TextEditState {
// If IME candidate window is shown on this text edit.
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) has_ime: bool,
pub(crate) ime_enabled: bool,
// cursor range for IME candidate.
#[cfg_attr(feature = "serde", serde(skip))]