Handle IME in a way which winit expects (#298)

Doing some investigation, the issues I'm seeing is that ibus is a bit/a
lot broken around compose keys

But this has correct behaviour for me. This matches the documentation of
the `winit`
[`Ime`](https://docs.rs/winit/latest/winit/event/enum.Ime.html) event.
This commit is contained in:
Daniel McNab 2024-05-11 16:01:06 +02:00 committed by GitHub
parent aaeeadbbd7
commit 9e697cc06f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 10 deletions

View File

@ -256,18 +256,13 @@ impl<T: EditableText> TextEditor<T> {
TextEvent::KeyboardKey(_, _) => Handled::No, TextEvent::KeyboardKey(_, _) => Handled::No,
TextEvent::Ime(ime) => match ime { TextEvent::Ime(ime) => match ime {
Ime::Commit(text) => { Ime::Commit(text) => {
if let Some(preedit) = self if let Some(selection_range) = self.selection.map(|x| x.range()) {
.preedit_range self.text_mut().edit(selection_range.clone(), text);
.clone()
.or_else(|| self.selection.map(|x| x.range()))
{
self.text_mut().edit(preedit.clone(), text);
self.selection = Some(Selection::caret( self.selection = Some(Selection::caret(
preedit.start + text.len(), selection_range.start + text.len(),
Affinity::Upstream, Affinity::Upstream,
)); ));
} }
self.preedit_range = None;
let contents = self.text().as_str().to_string(); let contents = self.text().as_str().to_string();
ctx.submit_action(Action::TextChanged(contents)); ctx.submit_action(Action::TextChanged(contents));
Handled::Yes Handled::Yes
@ -276,7 +271,11 @@ impl<T: EditableText> TextEditor<T> {
if let Some(preedit) = self.preedit_range.clone() { if let Some(preedit) = self.preedit_range.clone() {
self.text_mut().edit(preedit.clone(), preedit_string); self.text_mut().edit(preedit.clone(), preedit_string);
let np = preedit.start..(preedit.start + preedit_string.len()); let np = preedit.start..(preedit.start + preedit_string.len());
self.preedit_range = Some(np.clone()); self.preedit_range = if preedit_string.is_empty() {
None
} else {
Some(np.clone())
};
self.selection = if let Some(pec) = preedit_sel { self.selection = if let Some(pec) = preedit_sel {
Some(Selection::new( Some(Selection::new(
np.start + pec.0, np.start + pec.0,
@ -290,7 +289,11 @@ impl<T: EditableText> TextEditor<T> {
let sr = self.selection.map(|x| x.range()).unwrap_or(0..0); let sr = self.selection.map(|x| x.range()).unwrap_or(0..0);
self.text_mut().edit(sr.clone(), preedit_string); self.text_mut().edit(sr.clone(), preedit_string);
let np = sr.start..(sr.start + preedit_string.len()); let np = sr.start..(sr.start + preedit_string.len());
self.preedit_range = Some(np.clone()); self.preedit_range = if preedit_string.is_empty() {
None
} else {
Some(np.clone())
};
self.selection = if let Some(pec) = preedit_sel { self.selection = if let Some(pec) = preedit_sel {
Some(Selection::new( Some(Selection::new(
np.start + pec.0, np.start + pec.0,