IME: Handle composition events to show suggestion on web (#278)

* Handle composition message to show suggestion.

* CI check

* Apply suggestions from code review

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* Some minor changes

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Lin Han
2021-04-12 20:57:14 +02:00
committed by GitHub
co-authored by Emil Ernerfeldt
parent 0353f40dd5
commit 20bf09560e
3 changed files with 59 additions and 8 deletions
+7
View File
@@ -116,6 +116,13 @@ pub enum Event {
///
/// On touch-up first send `PointerButton{pressed: false, …}` followed by `PointerLeft`.
PointerGone,
/// IME composition start.
CompositionStart,
/// A new IME candidate is being suggested.
CompositionUpdate(String),
/// IME composition ended with this final result.
CompositionEnd(String),
}
/// Mouse button (or similar for touch input)
+39
View File
@@ -9,6 +9,10 @@ pub(crate) struct State {
#[cfg_attr(feature = "persistence", serde(skip))]
undoer: Undoer<(CCursorPair, String)>,
// If IME candidate window is shown on this text edit.
#[cfg_attr(feature = "persistence", serde(skip))]
has_ime: bool,
}
#[derive(Clone, Copy, Debug, Default)]
@@ -503,6 +507,41 @@ impl<'t> TextEdit<'t> {
modifiers,
} => on_key_press(&mut cursorp, text, &galley, *key, modifiers),
Event::CompositionStart => {
state.has_ime = true;
None
}
Event::CompositionUpdate(text_mark) => {
if !text_mark.is_empty()
&& text_mark != "\n"
&& text_mark != "\r"
&& state.has_ime
{
let mut ccursor = delete_selected(text, &cursorp);
let start_cursor = ccursor;
insert_text(&mut ccursor, text, text_mark);
Some(CCursorPair::two(start_cursor, ccursor))
} else {
None
}
}
Event::CompositionEnd(prediction) => {
if !prediction.is_empty()
&& prediction != "\n"
&& prediction != "\r"
&& state.has_ime
{
state.has_ime = false;
let mut ccursor = delete_selected(text, &cursorp);
insert_text(&mut ccursor, text, prediction);
Some(CCursorPair::one(ccursor))
} else {
None
}
}
_ => None,
};