From 5ec65c8df3bfd2175114f731a1168f0d1a55955c Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Wed, 2 Oct 2024 18:32:24 +0100 Subject: [PATCH] Newline fixes WIP --- examples/vello_editor/src/text.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/vello_editor/src/text.rs b/examples/vello_editor/src/text.rs index 8de7d05e..2bf44b14 100644 --- a/examples/vello_editor/src/text.rs +++ b/examples/vello_editor/src/text.rs @@ -7,6 +7,7 @@ use parley::layout::cursor::{Selection, VisualMode}; use parley::layout::Affinity; use parley::{layout::PositionedLayoutItem, FontContext}; use peniko::{kurbo::Affine, Color, Fill}; +use winit::keyboard::SmolStr; use std::time::Instant; use vello::Scene; use winit::{ @@ -248,15 +249,41 @@ impl Editor { } _ => { if let Some(text) = &event.text { + let text = if text == "\r" || text == "\r\n" { + &SmolStr::new_inline("\n") + } else { + text + }; + + let affinity = if code == KeyCode::Enter { + Affinity::Downstream + } else { + Affinity::Upstream + }; + + + dbg!(code); + dbg!(affinity); + dbg!(text); + dbg!(text.len()); + let start = self .delete_current_selection() .unwrap_or_else(|| self.selection.focus().text_range().start); self.buffer.insert_str(start, text); + + + let index = if code == KeyCode::Enter { + start + text.len() + } else { + start + text.len() + }; + self.update_layout(self.width, 1.0); self.selection = Selection::from_index( &self.layout, - start + text.len() - 1, - Affinity::Upstream, + index, + affinity, ); } }