From d28af5739bfaafa141dc8071a0c910e8693f3b3c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 3 Feb 2025 00:51:57 +0100 Subject: Track pre-edits separately from focus in text inputs --- widget/src/text_editor.rs | 15 ++++++--------- widget/src/text_input.rs | 19 +++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) (limited to 'widget') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 4f985f28..72e15c28 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -332,14 +332,13 @@ where ) -> InputMethod<&'b str> { let Some(Focus { is_window_focused: true, - is_ime_open, .. }) = &state.focus else { return InputMethod::Disabled; }; - let Some(preedit) = &is_ime_open else { + let Some(preedit) = &state.preedit else { return InputMethod::Allowed; }; @@ -497,6 +496,7 @@ where #[derive(Debug)] pub struct State { focus: Option, + preedit: Option, last_click: Option, drag_click: Option, partial_scroll: f32, @@ -510,7 +510,6 @@ struct Focus { updated_at: Instant, now: Instant, is_window_focused: bool, - is_ime_open: Option, } impl Focus { @@ -523,7 +522,6 @@ impl Focus { updated_at: now, now, is_window_focused: true, - is_ime_open: None, } } @@ -573,6 +571,7 @@ where fn state(&self) -> widget::tree::State { widget::tree::State::new(State { focus: None, + preedit: None, last_click: None, drag_click: None, partial_scroll: 0.0, @@ -752,13 +751,11 @@ where } Update::InputMethod(update) => match update { Ime::Toggle(is_open) => { - if let Some(focus) = &mut state.focus { - focus.is_ime_open = is_open.then(String::new); - } + state.preedit = is_open.then(String::new); } Ime::Preedit(text) => { - if let Some(focus) = &mut state.focus { - focus.is_ime_open = Some(text); + if state.focus.is_some() { + state.preedit = Some(text); } } Ime::Commit(text) => { diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index d0e93927..58bbc0d6 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -400,14 +400,13 @@ where ) -> InputMethod<&'b str> { let Some(Focus { is_window_focused: true, - is_ime_open, .. }) = &state.is_focused else { return InputMethod::Disabled; }; - let Some(preedit) = is_ime_open else { + let Some(preedit) = &state.is_ime_open else { return InputMethod::Allowed; }; @@ -729,7 +728,6 @@ where updated_at: now, now, is_window_focused: true, - is_ime_open: None, }) } else { None @@ -1257,17 +1255,15 @@ where input_method::Event::Opened | input_method::Event::Closed => { let state = state::(tree); - if let Some(focus) = &mut state.is_focused { - focus.is_ime_open = - matches!(event, input_method::Event::Opened) - .then(String::new); - } + state.is_ime_open = + matches!(event, input_method::Event::Opened) + .then(String::new); } input_method::Event::Preedit(content, _range) => { let state = state::(tree); - if let Some(focus) = &mut state.is_focused { - focus.is_ime_open = Some(content.to_owned()); + if state.is_focused.is_some() { + state.is_ime_open = Some(content.to_owned()); } } input_method::Event::Commit(text) => { @@ -1519,6 +1515,7 @@ pub struct State { placeholder: paragraph::Plain

, icon: paragraph::Plain

, is_focused: Option, + is_ime_open: Option, is_dragging: bool, is_pasting: Option, last_click: Option, @@ -1538,7 +1535,6 @@ struct Focus { updated_at: Instant, now: Instant, is_window_focused: bool, - is_ime_open: Option, } impl State

{ @@ -1565,7 +1561,6 @@ impl State

{ updated_at: now, now, is_window_focused: true, - is_ime_open: None, }); self.move_cursor_to_end(); -- cgit