From 43ffdbf824d64edc12858aac90f3b010b466d7aa Mon Sep 17 00:00:00 2001 From: Muhammad Ragib Hasin Date: Tue, 6 Aug 2024 22:52:54 +0600 Subject: [PATCH] Fix non-standard textbox behavior in Windows (#489) --- masonry/src/text/edit.rs | 39 +++++++++++++++++++++-------------- masonry/src/text/selection.rs | 4 ++-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/masonry/src/text/edit.rs b/masonry/src/text/edit.rs index 8e738c1a1..000509944 100644 --- a/masonry/src/text/edit.rs +++ b/masonry/src/text/edit.rs @@ -200,23 +200,12 @@ 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(_) => 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 @@ -366,6 +355,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 ee2239ca3..36642abb3 100644 --- a/masonry/src/text/selection.rs +++ b/masonry/src/text/selection.rs @@ -184,8 +184,8 @@ impl TextWithSelection { } _ => Handled::No, }, - winit::keyboard::Key::Unidentified(_) => todo!(), - winit::keyboard::Key::Dead(_) => todo!(), + winit::keyboard::Key::Unidentified(_) => Handled::No, + winit::keyboard::Key::Dead(_) => Handled::No, } } TextEvent::KeyboardKey(_, _) => Handled::No,