Skip to content

Commit

Permalink
Fix IME bug where currently typed characters got copied
Browse files Browse the repository at this point in the history
* Closes #3730 

Fix IME: Currently typed characters get copied when switching TextEdit
fields
  • Loading branch information
rustbasic authored Mar 8, 2024
1 parent 76411b5 commit 385daeb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,19 +940,24 @@ fn events(
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))
} 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.
// 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 = text.delete_selected(&cursor_range);
if !prediction.is_empty() {
let mut ccursor;
if !prediction.is_empty() && cursor_range == state.ime_cursor_range {
ccursor = text.delete_selected(&cursor_range);
text.insert_text_at(&mut ccursor, prediction, char_limit);
} else {
ccursor = cursor_range.primary.ccursor;
}
Some(CCursorRange::one(ccursor))
} else {
Expand Down
4 changes: 4 additions & 0 deletions crates/egui/src/widgets/text_edit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub struct TextEditState {
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) has_ime: bool,

// cursor range for IME candidate.
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) ime_cursor_range: CursorRange,

// Visual offset when editing singleline text bigger than the width.
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) singleline_offset: f32,
Expand Down

0 comments on commit 385daeb

Please sign in to comment.