From 7db5256b720c3ecbe7c1cce7a1b47fd03151e03a Mon Sep 17 00:00:00 2001 From: KENZ Date: Fri, 10 Jan 2025 07:12:31 +0900 Subject: Draft `input_method` support --- widget/src/text_editor.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index f1ec589b..2931e7f6 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -33,6 +33,7 @@ //! ``` use crate::core::alignment; use crate::core::clipboard::{self, Clipboard}; +use crate::core::input_method; use crate::core::keyboard; use crate::core::keyboard::key; use crate::core::layout::{self, Layout}; @@ -46,8 +47,8 @@ use crate::core::widget::operation; use crate::core::widget::{self, Widget}; use crate::core::window; use crate::core::{ - Background, Border, Color, Element, Event, Length, Padding, Pixels, Point, - Rectangle, Shell, Size, SmolStr, Theme, Vector, + Background, Border, CaretInfo, Color, Element, Event, Length, Padding, + Pixels, Point, Rectangle, Shell, Size, SmolStr, Theme, Vector, }; use std::borrow::Cow; @@ -322,6 +323,46 @@ where self.class = class.into(); self } + + fn caret_rect( + &self, + tree: &widget::Tree, + renderer: &Renderer, + layout: Layout<'_>, + ) -> Option { + let bounds = layout.bounds(); + + let internal = self.content.0.borrow_mut(); + let state = tree.state.downcast_ref::>(); + + let text_bounds = bounds.shrink(self.padding); + let translation = text_bounds.position() - Point::ORIGIN; + + if let Some(_) = state.focus.as_ref() { + let position = match internal.editor.cursor() { + Cursor::Caret(position) => position, + Cursor::Selection(ranges) => ranges + .first() + .cloned() + .unwrap_or(Rectangle::default()) + .position(), + }; + Some(Rectangle::new( + position + translation, + Size::new( + 1.0, + self.line_height + .to_absolute( + self.text_size + .unwrap_or_else(|| renderer.default_size()), + ) + .into(), + ), + )) + } else { + None + } + } } /// The content of a [`TextEditor`]. @@ -605,7 +646,7 @@ where event: Event, layout: Layout<'_>, cursor: mouse::Cursor, - _renderer: &Renderer, + renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, _viewport: &Rectangle, @@ -701,6 +742,11 @@ where })); shell.capture_event(); } + Update::Commit(text) => { + shell.publish(on_edit(Action::Edit(Edit::Paste( + Arc::new(text), + )))); + } Update::Binding(binding) => { fn apply_binding< H: text::Highlighter, @@ -825,6 +871,19 @@ where } }; + shell.update_caret_info(if state.is_focused() { + let rect = self + .caret_rect(tree, renderer, layout) + .unwrap_or(Rectangle::default()); + let bottom_left = Point::new(rect.x, rect.y + rect.height); + Some(CaretInfo { + position: bottom_left, + input_method_allowed: true, + }) + } else { + None + }); + if is_redraw { self.last_status = Some(status); } else if self @@ -1129,6 +1188,7 @@ enum Update { Drag(Point), Release, Scroll(f32), + Commit(String), Binding(Binding), } @@ -1191,6 +1251,9 @@ impl Update { } _ => None, }, + Event::InputMethod(input_method::Event::Commit(text)) => { + Some(Update::Commit(text)) + } Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, -- cgit From 0c6d4eb23f07e0ab424dc22dd198924b8540192a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 2 Feb 2025 17:50:12 +0100 Subject: Run `cargo fmt` and fix lints --- widget/src/text_editor.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 2931e7f6..529c8b90 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -338,7 +338,7 @@ where let text_bounds = bounds.shrink(self.padding); let translation = text_bounds.position() - Point::ORIGIN; - if let Some(_) = state.focus.as_ref() { + if state.focus.is_some() { let position = match internal.editor.cursor() { Cursor::Caret(position) => position, Cursor::Selection(ranges) => ranges @@ -872,10 +872,11 @@ where }; shell.update_caret_info(if state.is_focused() { - let rect = self - .caret_rect(tree, renderer, layout) - .unwrap_or(Rectangle::default()); + let rect = + self.caret_rect(tree, renderer, layout).unwrap_or_default(); + let bottom_left = Point::new(rect.x, rect.y + rect.height); + Some(CaretInfo { position: bottom_left, input_method_allowed: true, -- cgit From ae10adda74320e8098bfeb401f12a278e1e7b3e2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 2 Feb 2025 20:45:29 +0100 Subject: Refactor and simplify `input_method` API --- widget/src/text_editor.rs | 133 ++++++++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 53 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 529c8b90..4f985f28 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -47,7 +47,7 @@ use crate::core::widget::operation; use crate::core::widget::{self, Widget}; use crate::core::window; use crate::core::{ - Background, Border, CaretInfo, Color, Element, Event, Length, Padding, + Background, Border, Color, Element, Event, InputMethod, Length, Padding, Pixels, Point, Rectangle, Shell, Size, SmolStr, Theme, Vector, }; @@ -324,43 +324,49 @@ where self } - fn caret_rect( + fn input_method<'b>( &self, - tree: &widget::Tree, + state: &'b State, renderer: &Renderer, layout: Layout<'_>, - ) -> Option { - let bounds = layout.bounds(); + ) -> 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 { + return InputMethod::Allowed; + }; + let bounds = layout.bounds(); let internal = self.content.0.borrow_mut(); - let state = tree.state.downcast_ref::>(); let text_bounds = bounds.shrink(self.padding); let translation = text_bounds.position() - Point::ORIGIN; - if state.focus.is_some() { - let position = match internal.editor.cursor() { - Cursor::Caret(position) => position, - Cursor::Selection(ranges) => ranges - .first() - .cloned() - .unwrap_or(Rectangle::default()) - .position(), - }; - Some(Rectangle::new( - position + translation, - Size::new( - 1.0, - self.line_height - .to_absolute( - self.text_size - .unwrap_or_else(|| renderer.default_size()), - ) - .into(), - ), - )) - } else { - None + let cursor = match internal.editor.cursor() { + Cursor::Caret(position) => position, + Cursor::Selection(ranges) => { + ranges.first().cloned().unwrap_or_default().position() + } + }; + + let line_height = self.line_height.to_absolute( + self.text_size.unwrap_or_else(|| renderer.default_size()), + ); + + let position = + cursor + translation + Vector::new(0.0, f32::from(line_height)); + + InputMethod::Open { + position, + purpose: input_method::Purpose::Normal, + preedit: Some(preedit), } } } @@ -499,11 +505,12 @@ pub struct State { highlighter_format_address: usize, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] struct Focus { updated_at: Instant, now: Instant, is_window_focused: bool, + is_ime_open: Option, } impl Focus { @@ -516,6 +523,7 @@ impl Focus { updated_at: now, now, is_window_focused: true, + is_ime_open: None, } } @@ -742,11 +750,23 @@ where })); shell.capture_event(); } - Update::Commit(text) => { - shell.publish(on_edit(Action::Edit(Edit::Paste( - Arc::new(text), - )))); - } + 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); + } + } + Ime::Preedit(text) => { + if let Some(focus) = &mut state.focus { + focus.is_ime_open = Some(text); + } + } + Ime::Commit(text) => { + shell.publish(on_edit(Action::Edit(Edit::Paste( + Arc::new(text), + )))); + } + }, Update::Binding(binding) => { fn apply_binding< H: text::Highlighter, @@ -871,22 +891,12 @@ where } }; - shell.update_caret_info(if state.is_focused() { - let rect = - self.caret_rect(tree, renderer, layout).unwrap_or_default(); - - let bottom_left = Point::new(rect.x, rect.y + rect.height); - - Some(CaretInfo { - position: bottom_left, - input_method_allowed: true, - }) - } else { - None - }); - if is_redraw { self.last_status = Some(status); + + shell.request_input_method( + &self.input_method(state, renderer, layout), + ); } else if self .last_status .is_some_and(|last_status| status != last_status) @@ -1189,10 +1199,16 @@ enum Update { Drag(Point), Release, Scroll(f32), - Commit(String), + InputMethod(Ime), Binding(Binding), } +enum Ime { + Toggle(bool), + Preedit(String), + Commit(String), +} + impl Update { fn from_event( event: Event, @@ -1252,9 +1268,20 @@ impl Update { } _ => None, }, - Event::InputMethod(input_method::Event::Commit(text)) => { - Some(Update::Commit(text)) - } + Event::InputMethod(event) => match event { + input_method::Event::Opened | input_method::Event::Closed => { + Some(Update::InputMethod(Ime::Toggle(matches!( + event, + input_method::Event::Opened + )))) + } + input_method::Event::Preedit(content, _range) => { + Some(Update::InputMethod(Ime::Preedit(content))) + } + input_method::Event::Commit(content) => { + Some(Update::InputMethod(Ime::Commit(content))) + } + }, Event::Keyboard(keyboard::Event::KeyPressed { key, modifiers, -- cgit 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 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'widget/src/text_editor.rs') 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) => { -- cgit From c83809adb907498ba2a573ec9fb50936601ac8fc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 3 Feb 2025 02:33:40 +0100 Subject: Implement basic IME selection in `Preedit` overlay --- widget/src/text_editor.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 72e15c28..26d05ccd 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -55,6 +55,7 @@ use std::borrow::Cow; use std::cell::RefCell; use std::fmt; use std::ops::DerefMut; +use std::ops::Range; use std::sync::Arc; pub use text::editor::{Action, Edit, Line, LineEnding, Motion}; @@ -365,7 +366,7 @@ where InputMethod::Open { position, purpose: input_method::Purpose::Normal, - preedit: Some(preedit), + preedit: Some(preedit.as_ref()), } } } @@ -496,7 +497,7 @@ where #[derive(Debug)] pub struct State { focus: Option, - preedit: Option, + preedit: Option, last_click: Option, drag_click: Option, partial_scroll: f32, @@ -751,11 +752,15 @@ where } Update::InputMethod(update) => match update { Ime::Toggle(is_open) => { - state.preedit = is_open.then(String::new); + state.preedit = + is_open.then(input_method::Preedit::new); } - Ime::Preedit(text) => { + Ime::Preedit { content, selection } => { if state.focus.is_some() { - state.preedit = Some(text); + state.preedit = Some(input_method::Preedit { + content, + selection, + }); } } Ime::Commit(text) => { @@ -1202,7 +1207,10 @@ enum Update { enum Ime { Toggle(bool), - Preedit(String), + Preedit { + content: String, + selection: Option>, + }, Commit(String), } @@ -1272,8 +1280,11 @@ impl Update { input_method::Event::Opened )))) } - input_method::Event::Preedit(content, _range) => { - Some(Update::InputMethod(Ime::Preedit(content))) + input_method::Event::Preedit(content, selection) => { + Some(Update::InputMethod(Ime::Preedit { + content, + selection, + })) } input_method::Event::Commit(content) => { Some(Update::InputMethod(Ime::Commit(content))) -- cgit From e8c680ce66b6b766a196e799b209e73e0bf416ab Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 3 Feb 2025 16:55:10 +0100 Subject: Request redraws on `InputMethod` events --- widget/src/text_editor.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 26d05ccd..486741c6 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -754,6 +754,8 @@ where Ime::Toggle(is_open) => { state.preedit = is_open.then(input_method::Preedit::new); + + shell.request_redraw(); } Ime::Preedit { content, selection } => { if state.focus.is_some() { @@ -761,6 +763,8 @@ where content, selection, }); + + shell.request_redraw(); } } Ime::Commit(text) => { -- cgit From bab18858cd60168b63ae442026f45a90eb6be731 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 3 Feb 2025 18:38:20 +0100 Subject: Handle pre-edits and commits only if `text_editor` is focused --- widget/src/text_editor.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 486741c6..cfdf6b5d 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -758,14 +758,10 @@ where shell.request_redraw(); } Ime::Preedit { content, selection } => { - if state.focus.is_some() { - state.preedit = Some(input_method::Preedit { - content, - selection, - }); + state.preedit = + Some(input_method::Preedit { content, selection }); - shell.request_redraw(); - } + shell.request_redraw(); } Ime::Commit(text) => { shell.publish(on_edit(Action::Edit(Edit::Paste( @@ -1284,15 +1280,20 @@ impl Update { input_method::Event::Opened )))) } - input_method::Event::Preedit(content, selection) => { + input_method::Event::Preedit(content, selection) + if state.focus.is_some() => + { Some(Update::InputMethod(Ime::Preedit { content, selection, })) } - input_method::Event::Commit(content) => { + input_method::Event::Commit(content) + if state.focus.is_some() => + { Some(Update::InputMethod(Ime::Commit(content))) } + _ => None, }, Event::Keyboard(keyboard::Event::KeyPressed { key, -- cgit