From fcdf53afdee9cd12bf2e01c5f6e572859b9a3f96 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 6 Feb 2025 01:50:25 +0900 Subject: [PATCH 1/2] Set correct text size for text in preedit window --- core/src/input_method.rs | 17 ++++++++++++----- widget/src/text_editor.rs | 12 ++++++++---- widget/src/text_input.rs | 3 ++- winit/src/program/window_manager.rs | 4 +++- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/core/src/input_method.rs b/core/src/input_method.rs index 4e8c383b7d..ab00b5af21 100644 --- a/core/src/input_method.rs +++ b/core/src/input_method.rs @@ -1,5 +1,5 @@ //! Listen to input method events. -use crate::Point; +use crate::{Pixels, Point}; use std::ops::Range; @@ -34,15 +34,20 @@ pub struct Preedit { pub content: T, /// The selected range of the content. pub selection: Option>, + /// The text size of the content. + pub text_size: Option, } impl Preedit { /// Creates a new empty [`Preedit`]. - pub fn new() -> Self + pub fn new(text_size: Option>) -> Self where T: Default, { - Self::default() + Self { + text_size: text_size.map(Into::into), + ..Default::default() + } } /// Turns a [`Preedit`] into its owned version. @@ -53,6 +58,7 @@ impl Preedit { Preedit { content: self.content.as_ref().to_owned(), selection: self.selection.clone(), + text_size: self.text_size, } } } @@ -63,6 +69,7 @@ impl Preedit { Preedit { content: &self.content, selection: self.selection.clone(), + text_size: self.text_size, } } } @@ -90,13 +97,13 @@ impl InputMethod { /// let open = InputMethod::Open { /// position: Point::ORIGIN, /// purpose: Purpose::Normal, - /// preedit: Some(Preedit { content: "1".to_owned(), selection: None }), + /// preedit: Some(Preedit { content: "1".to_owned(), selection: None, text_size: None }), /// }; /// /// let open_2 = InputMethod::Open { /// position: Point::ORIGIN, /// purpose: Purpose::Secure, - /// preedit: Some(Preedit { content: "2".to_owned(), selection: None }), + /// preedit: Some(Preedit { content: "2".to_owned(), selection: None, text_size: None }), /// }; /// /// let mut ime = InputMethod::Disabled; diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index e685256bff..df42d601a7 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -753,14 +753,18 @@ where } Update::InputMethod(update) => match update { Ime::Toggle(is_open) => { - state.preedit = - is_open.then(input_method::Preedit::new); + state.preedit = is_open.then(|| { + input_method::Preedit::new(self.text_size) + }); shell.request_redraw(); } Ime::Preedit { content, selection } => { - state.preedit = - Some(input_method::Preedit { content, selection }); + state.preedit = Some(input_method::Preedit { + content, + selection, + text_size: self.text_size, + }); shell.request_redraw(); } diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 7be5bbd918..de957e1462 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -1262,7 +1262,7 @@ where state.is_ime_open = matches!(event, input_method::Event::Opened) - .then(input_method::Preedit::new); + .then(|| input_method::Preedit::new(self.size)); shell.request_redraw(); } @@ -1273,6 +1273,7 @@ where state.is_ime_open = Some(input_method::Preedit { content: content.to_owned(), selection: selection.clone(), + text_size: self.size, }); shell.request_redraw(); diff --git a/winit/src/program/window_manager.rs b/winit/src/program/window_manager.rs index ae214e7cfd..d5b334df1d 100644 --- a/winit/src/program/window_manager.rs +++ b/winit/src/program/window_manager.rs @@ -322,7 +322,9 @@ where self.content = Renderer::Paragraph::with_spans(Text { content: &spans, bounds: Size::INFINITY, - size: renderer.default_size(), + size: preedit + .text_size + .unwrap_or_else(|| renderer.default_size()), line_height: text::LineHeight::default(), font: renderer.default_font(), horizontal_alignment: alignment::Horizontal::Left, From cf851e133ad6aaedaf07b58c68e7c41d41ee151a Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 6 Feb 2025 09:57:01 +0900 Subject: [PATCH 2/2] Do not pass text size to `Preedit::new` --- core/src/input_method.rs | 7 ++----- widget/src/text_editor.rs | 4 +++- widget/src/text_input.rs | 9 +++++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/src/input_method.rs b/core/src/input_method.rs index ab00b5af21..9c83b083ca 100644 --- a/core/src/input_method.rs +++ b/core/src/input_method.rs @@ -40,14 +40,11 @@ pub struct Preedit { impl Preedit { /// Creates a new empty [`Preedit`]. - pub fn new(text_size: Option>) -> Self + pub fn new() -> Self where T: Default, { - Self { - text_size: text_size.map(Into::into), - ..Default::default() - } + Self::default() } /// Turns a [`Preedit`] into its owned version. diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index df42d601a7..ac458951b5 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -754,7 +754,9 @@ where Update::InputMethod(update) => match update { Ime::Toggle(is_open) => { state.preedit = is_open.then(|| { - input_method::Preedit::new(self.text_size) + let mut preedit = input_method::Preedit::new(); + preedit.text_size = self.text_size; + preedit }); shell.request_redraw(); diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index de957e1462..6d31749057 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -1261,8 +1261,13 @@ where let state = state::(tree); state.is_ime_open = - matches!(event, input_method::Event::Opened) - .then(|| input_method::Preedit::new(self.size)); + matches!(event, input_method::Event::Opened).then( + || { + let mut preedit = input_method::Preedit::new(); + preedit.text_size = self.size; + preedit + }, + ); shell.request_redraw(); }