Skip to content

Commit 868db92

Browse files
committed
Use TextBuffer for layouter in TextEdit instead of &str
Signed-off-by: kernelkind <kernelkind@gmail.com>
1 parent 390e0bf commit 868db92

4 files changed

Lines changed: 35 additions & 16 deletions

File tree

crates/egui/src/widgets/text_edit/builder.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::{
1919

2020
use super::{TextEditOutput, TextEditState};
2121

22+
type LayouterFn<'t> = &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>;
23+
2224
/// A text region that the user can edit the contents of.
2325
///
2426
/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
@@ -52,7 +54,7 @@ use super::{TextEditOutput, TextEditState};
5254
/// To do so, pass in a `&mut` reference to a `&str`, for instance:
5355
///
5456
/// ```
55-
/// fn selectable_text(ui: &mut egui::Ui, mut text: &str) {
57+
/// fn selectable_text(ui: &mut egui::Ui, mut text: &'static str) {
5658
/// ui.add(egui::TextEdit::multiline(&mut text));
5759
/// }
5860
/// ```
@@ -71,7 +73,7 @@ pub struct TextEdit<'t> {
7173
id_salt: Option<Id>,
7274
font_selection: FontSelection,
7375
text_color: Option<Color32>,
74-
layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>>,
76+
layouter: Option<LayouterFn<'t>>,
7577
password: bool,
7678
frame: bool,
7779
margin: Margin,
@@ -261,16 +263,19 @@ impl<'t> TextEdit<'t> {
261263
/// # egui::__run_test_ui(|ui| {
262264
/// # let mut my_code = String::new();
263265
/// # fn my_memoized_highlighter(s: &str) -> egui::text::LayoutJob { Default::default() }
264-
/// let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
265-
/// let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(string);
266+
/// let mut layouter = |ui: &egui::Ui, buf: &dyn egui::TextBuffer, wrap_width: f32| {
267+
/// let mut layout_job: egui::text::LayoutJob = my_memoized_highlighter(buf.as_str());
266268
/// layout_job.wrap.max_width = wrap_width;
267269
/// ui.fonts(|f| f.layout_job(layout_job))
268270
/// };
269271
/// ui.add(egui::TextEdit::multiline(&mut my_code).layouter(&mut layouter));
270272
/// # });
271273
/// ```
272274
#[inline]
273-
pub fn layouter(mut self, layouter: &'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>) -> Self {
275+
pub fn layouter(
276+
mut self,
277+
layouter: &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
278+
) -> Self {
274279
self.layouter = Some(layouter);
275280

276281
self
@@ -510,8 +515,8 @@ impl TextEdit<'_> {
510515
};
511516

512517
let font_id_clone = font_id.clone();
513-
let mut default_layouter = move |ui: &Ui, text: &str, wrap_width: f32| {
514-
let text = mask_if_password(password, text);
518+
let mut default_layouter = move |ui: &Ui, text: &dyn TextBuffer, wrap_width: f32| {
519+
let text = mask_if_password(password, text.as_str());
515520
let layout_job = if multiline {
516521
LayoutJob::simple(text, font_id_clone.clone(), text_color, wrap_width)
517522
} else {
@@ -522,7 +527,7 @@ impl TextEdit<'_> {
522527

523528
let layouter = layouter.unwrap_or(&mut default_layouter);
524529

525-
let mut galley = layouter(ui, text.as_str(), wrap_width);
530+
let mut galley = layouter(ui, text, wrap_width);
526531

527532
let desired_inner_width = if clip_text {
528533
wrap_width // visual clipping with scroll in singleline input.
@@ -879,7 +884,7 @@ fn events(
879884
state: &mut TextEditState,
880885
text: &mut dyn TextBuffer,
881886
galley: &mut Arc<Galley>,
882-
layouter: &mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>,
887+
layouter: &mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
883888
id: Id,
884889
wrap_width: f32,
885890
multiline: bool,
@@ -1094,7 +1099,7 @@ fn events(
10941099
any_change = true;
10951100

10961101
// Layout again to avoid frame delay, and to keep `text` and `galley` in sync.
1097-
*galley = layouter(ui, text.as_str(), wrap_width);
1102+
*galley = layouter(ui, text, wrap_width);
10981103

10991104
// Set cursor_range using new galley:
11001105
cursor_range = new_ccursor_range;

crates/egui/src/widgets/text_edit/text_buffer.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ pub trait TextBuffer {
172172
self.delete_selected(&CCursorRange::two(min, max))
173173
}
174174
}
175+
176+
fn as_any(&self) -> &dyn std::any::Any;
175177
}
176178

177179
impl TextBuffer for String {
@@ -215,9 +217,13 @@ impl TextBuffer for String {
215217
fn take(&mut self) -> String {
216218
std::mem::take(self)
217219
}
220+
221+
fn as_any(&self) -> &dyn std::any::Any {
222+
self
223+
}
218224
}
219225

220-
impl TextBuffer for Cow<'_, str> {
226+
impl TextBuffer for Cow<'static, str> {
221227
fn is_mutable(&self) -> bool {
222228
true
223229
}
@@ -245,10 +251,14 @@ impl TextBuffer for Cow<'_, str> {
245251
fn take(&mut self) -> String {
246252
std::mem::take(self).into_owned()
247253
}
254+
255+
fn as_any(&self) -> &dyn std::any::Any {
256+
self
257+
}
248258
}
249259

250260
/// Immutable view of a `&str`!
251-
impl TextBuffer for &str {
261+
impl TextBuffer for &'static str {
252262
fn is_mutable(&self) -> bool {
253263
false
254264
}
@@ -262,4 +272,8 @@ impl TextBuffer for &str {
262272
}
263273

264274
fn delete_char_range(&mut self, _ch_range: Range<usize>) {}
275+
276+
fn as_any(&self) -> &dyn std::any::Any {
277+
self
278+
}
265279
}

crates/egui_demo_lib/src/demo/code_editor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ impl crate::View for CodeEditor {
7676
});
7777
});
7878

79-
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
79+
let mut layouter = |ui: &egui::Ui, buf: &dyn egui::TextBuffer, wrap_width: f32| {
8080
let mut layout_job = egui_extras::syntax_highlighting::highlight(
8181
ui.ctx(),
8282
ui.style(),
8383
&theme,
84-
string,
84+
buf.as_str(),
8585
language,
8686
);
8787
layout_job.wrap.max_width = wrap_width;

crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl EasyMarkEditor {
8080
} = self;
8181

8282
let response = if self.highlight_editor {
83-
let mut layouter = |ui: &egui::Ui, easymark: &str, wrap_width: f32| {
84-
let mut layout_job = highlighter.highlight(ui.style(), easymark);
83+
let mut layouter = |ui: &egui::Ui, easymark: &dyn TextBuffer, wrap_width: f32| {
84+
let mut layout_job = highlighter.highlight(ui.style(), easymark.as_str());
8585
layout_job.wrap.max_width = wrap_width;
8686
ui.fonts(|f| f.layout_job(layout_job))
8787
};

0 commit comments

Comments
 (0)