From 3d259b18d965e16f6b40a0ec4cb2ff18fd419419 Mon Sep 17 00:00:00 2001 From: Muhammad Ragib Hasin Date: Tue, 6 Aug 2024 16:25:10 +0600 Subject: [PATCH 1/3] Fix non-standard textbox behavior in Windows --- masonry/src/text/edit.rs | 45 ++++++++++++++++++++++------------- masonry/src/text/selection.rs | 12 ++++++++-- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/masonry/src/text/edit.rs b/masonry/src/text/edit.rs index 4fd0db213..d1fb8e51d 100644 --- a/masonry/src/text/edit.rs +++ b/masonry/src/text/edit.rs @@ -188,23 +188,18 @@ impl TextEditor { } Key::Named(_) => Handled::No, Key::Character(c) => { - let selection = self.inner.selection.unwrap_or(Selection { - anchor: 0, - active: 0, - active_affinity: Affinity::Downstream, - h_pos: None, - }); - self.text_mut().edit(selection.range(), &**c); - self.inner.selection = Some(Selection::caret( - selection.min() + c.len(), - // We have just added this character, so we are "affined" with it - Affinity::Downstream, - )); - let contents = self.text().as_str().to_string(); - ctx.submit_action(Action::TextChanged(contents)); - Handled::Yes + self.insert_text(event.text.as_ref().unwrap_or(c), ctx) + } + Key::Unidentified(_) => { + if cfg!(windows) { + event + .text + .as_ref() + .map_or(Handled::No, |c| self.insert_text(c, ctx)) + } else { + Handled::No + } } - Key::Unidentified(_) => Handled::No, Key::Dead(d) => { eprintln!("Got dead key {d:?}. Will handle"); Handled::No @@ -354,6 +349,24 @@ impl TextEditor { TextEvent::FocusChange(_) => Handled::No, } } + + fn insert_text(&mut self, c: &winit::keyboard::SmolStr, ctx: &mut EventCtx) -> Handled { + let selection = self.inner.selection.unwrap_or(Selection { + anchor: 0, + active: 0, + active_affinity: Affinity::Downstream, + h_pos: None, + }); + self.text_mut().edit(selection.range(), &**c); + self.inner.selection = Some(Selection::caret( + selection.min() + c.len(), + // We have just added this character, so we are "affined" with it + Affinity::Downstream, + )); + let contents = self.text().as_str().to_string(); + ctx.submit_action(Action::TextChanged(contents)); + Handled::Yes + } } impl Deref for TextEditor { diff --git a/masonry/src/text/selection.rs b/masonry/src/text/selection.rs index 20b889e3b..15111e5c9 100644 --- a/masonry/src/text/selection.rs +++ b/masonry/src/text/selection.rs @@ -184,8 +184,16 @@ impl TextWithSelection { } _ => Handled::No, }, - winit::keyboard::Key::Unidentified(_) => todo!(), - winit::keyboard::Key::Dead(_) => todo!(), + winit::keyboard::Key::Unidentified(key) => { + // TODO: figure out correct behavior + tracing::warn!(?key, "got unidentified key, did not handle"); + Handled::No + } + winit::keyboard::Key::Dead(key) => { + // TODO: figure out correct behavior + tracing::warn!(?key, "got dead key, did not handle"); + Handled::No + } } } TextEvent::KeyboardKey(_, _) => Handled::No, From 5d4506d49661e304e35a02c5daa9800217b46de9 Mon Sep 17 00:00:00 2001 From: Muhammad Ragib Hasin Date: Tue, 6 Aug 2024 18:19:11 +0600 Subject: [PATCH 2/3] Response to review comments --- masonry/src/text/edit.rs | 2 ++ masonry/src/text/selection.rs | 12 ++---------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/masonry/src/text/edit.rs b/masonry/src/text/edit.rs index d1fb8e51d..bf4b28dd4 100644 --- a/masonry/src/text/edit.rs +++ b/masonry/src/text/edit.rs @@ -191,6 +191,8 @@ impl TextEditor { self.insert_text(event.text.as_ref().unwrap_or(c), ctx) } Key::Unidentified(_) => { + // Ensures conformance with native Windows behavior in case some + // key event hook sets unidentifed key code and some text for pickup if cfg!(windows) { event .text diff --git a/masonry/src/text/selection.rs b/masonry/src/text/selection.rs index 15111e5c9..4be235351 100644 --- a/masonry/src/text/selection.rs +++ b/masonry/src/text/selection.rs @@ -184,16 +184,8 @@ impl TextWithSelection { } _ => Handled::No, }, - winit::keyboard::Key::Unidentified(key) => { - // TODO: figure out correct behavior - tracing::warn!(?key, "got unidentified key, did not handle"); - Handled::No - } - winit::keyboard::Key::Dead(key) => { - // TODO: figure out correct behavior - tracing::warn!(?key, "got dead key, did not handle"); - Handled::No - } + winit::keyboard::Key::Unidentified(_) => Handled::No, + winit::keyboard::Key::Dead(_) => Handled::No, } } TextEvent::KeyboardKey(_, _) => Handled::No, From 6613a254e69cbd72f651ed110a07117a4787f7d5 Mon Sep 17 00:00:00 2001 From: Muhammad Ragib Hasin Date: Tue, 6 Aug 2024 22:42:43 +0600 Subject: [PATCH 3/3] Remove the conditional for windows --- masonry/src/text/edit.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/masonry/src/text/edit.rs b/masonry/src/text/edit.rs index bf4b28dd4..50e41a11e 100644 --- a/masonry/src/text/edit.rs +++ b/masonry/src/text/edit.rs @@ -190,18 +190,10 @@ impl TextEditor { Key::Character(c) => { self.insert_text(event.text.as_ref().unwrap_or(c), ctx) } - Key::Unidentified(_) => { - // Ensures conformance with native Windows behavior in case some - // key event hook sets unidentifed key code and some text for pickup - if cfg!(windows) { - event - .text - .as_ref() - .map_or(Handled::No, |c| self.insert_text(c, ctx)) - } else { - Handled::No - } - } + Key::Unidentified(_) => match event.text.as_ref() { + Some(text) => self.insert_text(text, ctx), + None => Handled::No, + }, Key::Dead(d) => { eprintln!("Got dead key {d:?}. Will handle"); Handled::No