From 7979125ed793918dd4a0e5a1ddec8d17bffbd5bf Mon Sep 17 00:00:00 2001
From: Héctor Ramón Jiménez <hector@hecrj.dev>
Date: Wed, 12 Feb 2025 08:46:35 +0100
Subject: Simplify `InputMethod` API with only two states

Co-authored-by: rhysd <lin90162@yahoo.co.jp>
Co-authored-by: KENZ <KENZ.gelsoft@gmail.com>
---
 widget/src/scrollable.rs  |  4 ++--
 widget/src/text_editor.rs |  8 ++------
 widget/src/text_input.rs  | 28 ++++++++++++----------------
 3 files changed, 16 insertions(+), 24 deletions(-)

(limited to 'widget')

diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs
index fe71fd6b..0cf75c04 100644
--- a/widget/src/scrollable.rs
+++ b/widget/src/scrollable.rs
@@ -729,7 +729,7 @@ where
                     _ => mouse::Cursor::Unavailable,
                 };
 
-                let had_input_method = shell.input_method().is_open();
+                let had_input_method = shell.input_method().is_enabled();
 
                 let translation =
                     state.translation(self.direction, bounds, content_bounds);
@@ -750,7 +750,7 @@ where
                 );
 
                 if !had_input_method {
-                    if let InputMethod::Open { position, .. } =
+                    if let InputMethod::Enabled { position, .. } =
                         shell.input_method_mut()
                     {
                         *position = *position - translation;
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index ce5da9ef..7e40a56a 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -339,10 +339,6 @@ where
             return InputMethod::Disabled;
         };
 
-        let Some(preedit) = &state.preedit else {
-            return InputMethod::Allowed;
-        };
-
         let bounds = layout.bounds();
         let internal = self.content.0.borrow_mut();
 
@@ -363,10 +359,10 @@ where
         let position =
             cursor + translation + Vector::new(0.0, f32::from(line_height));
 
-        InputMethod::Open {
+        InputMethod::Enabled {
             position,
             purpose: input_method::Purpose::Normal,
-            preedit: Some(preedit.as_ref()),
+            preedit: state.preedit.as_ref().map(input_method::Preedit::as_ref),
         }
     }
 }
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs
index bb2685bd..2cad13cd 100644
--- a/widget/src/text_input.rs
+++ b/widget/src/text_input.rs
@@ -406,10 +406,6 @@ where
             return InputMethod::Disabled;
         };
 
-        let Some(preedit) = &state.is_ime_open else {
-            return InputMethod::Allowed;
-        };
-
         let secure_value = self.is_secure.then(|| value.secure());
         let value = secure_value.as_ref().unwrap_or(value);
 
@@ -433,14 +429,14 @@ where
         let x = (text_bounds.x + cursor_x).floor() - scroll_offset
             + alignment_offset;
 
-        InputMethod::Open {
+        InputMethod::Enabled {
             position: Point::new(x, text_bounds.y + text_bounds.height),
             purpose: if self.is_secure {
                 input_method::Purpose::Secure
             } else {
                 input_method::Purpose::Normal
             },
-            preedit: Some(preedit.as_ref()),
+            preedit: state.preedit.as_ref().map(input_method::Preedit::as_ref),
         }
     }
 
@@ -584,7 +580,7 @@ where
         let draw = |renderer: &mut Renderer, viewport| {
             let paragraph = if text.is_empty()
                 && state
-                    .is_ime_open
+                    .preedit
                     .as_ref()
                     .map(|preedit| preedit.content.is_empty())
                     .unwrap_or(true)
@@ -1260,7 +1256,7 @@ where
                 input_method::Event::Opened | input_method::Event::Closed => {
                     let state = state::<Renderer>(tree);
 
-                    state.is_ime_open =
+                    state.preedit =
                         matches!(event, input_method::Event::Opened)
                             .then(input_method::Preedit::new);
 
@@ -1270,7 +1266,7 @@ where
                     let state = state::<Renderer>(tree);
 
                     if state.is_focused.is_some() {
-                        state.is_ime_open = Some(input_method::Preedit {
+                        state.preedit = Some(input_method::Preedit {
                             content: content.to_owned(),
                             selection: selection.clone(),
                             text_size: self.size,
@@ -1340,6 +1336,12 @@ where
                                 millis_until_redraw as u64,
                             ),
                         );
+
+                        shell.request_input_method(&self.input_method(
+                            state,
+                            layout,
+                            &self.value,
+                        ));
                     }
                 }
             }
@@ -1363,12 +1365,6 @@ where
 
         if let Event::Window(window::Event::RedrawRequested(_now)) = event {
             self.last_status = Some(status);
-
-            shell.request_input_method(&self.input_method(
-                state,
-                layout,
-                &self.value,
-            ));
         } else if self
             .last_status
             .is_some_and(|last_status| status != last_status)
@@ -1528,9 +1524,9 @@ pub struct State<P: text::Paragraph> {
     placeholder: paragraph::Plain<P>,
     icon: paragraph::Plain<P>,
     is_focused: Option<Focus>,
-    is_ime_open: Option<input_method::Preedit>,
     is_dragging: bool,
     is_pasting: Option<Value>,
+    preedit: Option<input_method::Preedit>,
     last_click: Option<mouse::Click>,
     cursor: Cursor,
     keyboard_modifiers: keyboard::Modifiers,
-- 
cgit