@@ -19,6 +19,8 @@ use crate::{
1919
2020use 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;
0 commit comments