From 6b999e0f25f96e983006705009c14162fdf17bd0 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Mon, 3 Feb 2025 16:01:02 -0500 Subject: [PATCH] Use `TextBuffer` for `layouter` in `TextEdit` instead of `&str` Signed-off-by: kernelkind --- crates/egui/src/widgets/text_edit/builder.rs | 23 +++++++++++-------- .../egui/src/widgets/text_edit/text_buffer.rs | 9 ++++++++ crates/egui_demo_lib/src/demo/code_editor.rs | 4 ++-- .../src/easy_mark/easy_mark_editor.rs | 4 ++-- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 2f685bbc1233..361a5a527807 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -21,6 +21,8 @@ use crate::{ use super::{TextEditOutput, TextEditState}; +type LayouterFn<'t> = &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc; + /// A text region that the user can edit the contents of. /// /// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`]. @@ -73,7 +75,7 @@ pub struct TextEdit<'t> { id_salt: Option, font_selection: FontSelection, text_color: Option, - layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc>, + layouter: Option>, password: bool, frame: bool, margin: Margin, @@ -263,8 +265,8 @@ impl<'t> TextEdit<'t> { /// # egui::__run_test_ui(|ui| { /// # let mut my_code = String::new(); /// # fn my_memoized_highlighter(s: &str) -> egui::text::LayoutJob { Default::default() } - /// let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| { - /// let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(string); + /// let mut layouter = |ui: &egui::Ui, buf: &dyn egui::TextBuffer, wrap_width: f32| { + /// let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(buf.as_str()); /// layout_job.wrap.max_width = wrap_width; /// ui.fonts(|f| f.layout_job(layout_job)) /// }; @@ -272,7 +274,10 @@ impl<'t> TextEdit<'t> { /// # }); /// ``` #[inline] - pub fn layouter(mut self, layouter: &'t mut dyn FnMut(&Ui, &str, f32) -> Arc) -> Self { + pub fn layouter( + mut self, + layouter: &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc, + ) -> Self { self.layouter = Some(layouter); self @@ -518,8 +523,8 @@ impl TextEdit<'_> { }; let font_id_clone = font_id.clone(); - let mut default_layouter = move |ui: &Ui, text: &str, wrap_width: f32| { - let text = mask_if_password(password, text); + let mut default_layouter = move |ui: &Ui, text: &dyn TextBuffer, wrap_width: f32| { + let text = mask_if_password(password, text.as_str()); let layout_job = if multiline { LayoutJob::simple(text, font_id_clone.clone(), text_color, wrap_width) } else { @@ -530,7 +535,7 @@ impl TextEdit<'_> { let layouter = layouter.unwrap_or(&mut default_layouter); - let mut galley = layouter(ui, text.as_str(), wrap_width); + let mut galley = layouter(ui, text, wrap_width); let desired_inner_width = if clip_text { wrap_width // visual clipping with scroll in singleline input. @@ -883,7 +888,7 @@ fn events( state: &mut TextEditState, text: &mut dyn TextBuffer, galley: &mut Arc, - layouter: &mut dyn FnMut(&Ui, &str, f32) -> Arc, + layouter: &mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc, id: Id, wrap_width: f32, multiline: bool, @@ -1098,7 +1103,7 @@ fn events( any_change = true; // Layout again to avoid frame delay, and to keep `text` and `galley` in sync. - *galley = layouter(ui, text.as_str(), wrap_width); + *galley = layouter(ui, text, wrap_width); // Set cursor_range using new galley: cursor_range = CursorRange { diff --git a/crates/egui/src/widgets/text_edit/text_buffer.rs b/crates/egui/src/widgets/text_edit/text_buffer.rs index 31b746324ee0..1c687662eb33 100644 --- a/crates/egui/src/widgets/text_edit/text_buffer.rs +++ b/crates/egui/src/widgets/text_edit/text_buffer.rs @@ -183,6 +183,15 @@ pub trait TextBuffer { self.delete_selected(&CursorRange::two(min, max)) } } + + /// Returns a unique identifier for the implementing type. + /// + /// By default, this returns `0`, which represents an unknown type. + /// Implementers should override this method with a unique identifier + /// to enable safe downcasting. + fn type_id(&self) -> usize { + 0 + } } impl TextBuffer for String { diff --git a/crates/egui_demo_lib/src/demo/code_editor.rs b/crates/egui_demo_lib/src/demo/code_editor.rs index fe39e1be8f14..2d67f7d460a5 100644 --- a/crates/egui_demo_lib/src/demo/code_editor.rs +++ b/crates/egui_demo_lib/src/demo/code_editor.rs @@ -76,12 +76,12 @@ impl crate::View for CodeEditor { }); }); - let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| { + let mut layouter = |ui: &egui::Ui, buf: &dyn egui::TextBuffer, wrap_width: f32| { let mut layout_job = egui_extras::syntax_highlighting::highlight( ui.ctx(), ui.style(), &theme, - string, + buf.as_str(), language, ); layout_job.wrap.max_width = wrap_width; diff --git a/crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs b/crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs index 524beaf69de3..1abed6877c71 100644 --- a/crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs +++ b/crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs @@ -80,8 +80,8 @@ impl EasyMarkEditor { } = self; let response = if self.highlight_editor { - let mut layouter = |ui: &egui::Ui, easymark: &str, wrap_width: f32| { - let mut layout_job = highlighter.highlight(ui.style(), easymark); + let mut layouter = |ui: &egui::Ui, easymark: &dyn TextBuffer, wrap_width: f32| { + let mut layout_job = highlighter.highlight(ui.style(), easymark.as_str()); layout_job.wrap.max_width = wrap_width; ui.fonts(|f| f.layout_job(layout_job)) };