From 4108dd0926ca8f250826d66c3f24784cb4604d58 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 6 Feb 2025 19:03:09 +0900 Subject: [PATCH] Fix candidate window position for the first time --- core/src/input_method.rs | 17 +++++++++++------ widget/src/text_editor.rs | 8 ++++---- widget/src/text_input.rs | 11 ++++++----- winit/src/program/window_manager.rs | 12 ++++++------ 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/core/src/input_method.rs b/core/src/input_method.rs index 4e8c383b7d..56663f7129 100644 --- a/core/src/input_method.rs +++ b/core/src/input_method.rs @@ -11,7 +11,10 @@ pub enum InputMethod { /// No input method is allowed. Disabled, /// Input methods are allowed, but not open yet. - Allowed, + Allowed { + /// The position at which the input method dialog should be placed. + position: Point, + }, /// Input method is open. Open { /// The position at which the input method dialog should be placed. @@ -101,11 +104,11 @@ impl InputMethod { /// /// let mut ime = InputMethod::Disabled; /// - /// ime.merge(&InputMethod::::Allowed); - /// assert_eq!(ime, InputMethod::Allowed); + /// ime.merge(&InputMethod::::Allowed { position: Point::ORIGIN }); + /// assert_eq!(ime, InputMethod::Allowed{ position: Point::ORIGIN }); /// /// ime.merge(&InputMethod::::Disabled); - /// assert_eq!(ime, InputMethod::Allowed); + /// assert_eq!(ime, InputMethod::Allowed{ position: Point::ORIGIN }); /// /// ime.merge(&open); /// assert_eq!(ime, open); @@ -117,7 +120,7 @@ impl InputMethod { match (&self, other) { (InputMethod::Open { .. }, _) | ( - InputMethod::Allowed, + InputMethod::Allowed { .. }, InputMethod::None | InputMethod::Disabled, ) | (InputMethod::Disabled, InputMethod::None) => {} @@ -142,7 +145,9 @@ impl InputMethod { match self { Self::None => InputMethod::None, Self::Disabled => InputMethod::Disabled, - Self::Allowed => InputMethod::Allowed, + Self::Allowed { position } => InputMethod::Allowed { + position: *position, + }, Self::Open { position, purpose, diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index e685256bff..178aeb51e5 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,6 +359,10 @@ where let position = cursor + translation + Vector::new(0.0, f32::from(line_height)); + let Some(preedit) = &state.preedit else { + return InputMethod::Allowed { position }; + }; + InputMethod::Open { position, purpose: input_method::Purpose::Normal, diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 7be5bbd918..ba5fe3a647 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); @@ -432,9 +428,14 @@ where let x = (text_bounds.x + cursor_x).floor() - scroll_offset + alignment_offset; + let position = Point::new(x, text_bounds.y + text_bounds.height); + + let Some(preedit) = &state.is_ime_open else { + return InputMethod::Allowed { position }; + }; InputMethod::Open { - position: Point::new(x, text_bounds.y + text_bounds.height), + position, purpose: if self.is_secure { input_method::Purpose::Secure } else { diff --git a/winit/src/program/window_manager.rs b/winit/src/program/window_manager.rs index ae214e7cfd..bed1f60220 100644 --- a/winit/src/program/window_manager.rs +++ b/winit/src/program/window_manager.rs @@ -210,8 +210,13 @@ where InputMethod::Disabled => { self.raw.set_ime_allowed(false); } - InputMethod::Allowed | InputMethod::Open { .. } => { + InputMethod::Allowed { position } + | InputMethod::Open { position, .. } => { self.raw.set_ime_allowed(true); + self.raw.set_ime_cursor_area( + LogicalPosition::new(position.x, position.y), + LogicalSize::new(10, 10), // TODO? + ); } } @@ -221,11 +226,6 @@ where preedit, } = input_method { - self.raw.set_ime_cursor_area( - LogicalPosition::new(position.x, position.y), - LogicalSize::new(10, 10), // TODO? - ); - self.raw.set_ime_purpose(conversion::ime_purpose(purpose)); if let Some(preedit) = preedit {