Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TextBuffer for layouter in TextEdit instead of &str #5712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use crate::{

use super::{TextEditOutput, TextEditState};

type LayouterFn<'t> = &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>;

/// A text region that the user can edit the contents of.
///
/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
Expand Down Expand Up @@ -73,7 +75,7 @@ pub struct TextEdit<'t> {
id_salt: Option<Id>,
font_selection: FontSelection,
text_color: Option<Color32>,
layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>>,
layouter: Option<LayouterFn<'t>>,
password: bool,
frame: bool,
margin: Margin,
Expand Down Expand Up @@ -263,16 +265,19 @@ 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))
/// };
/// ui.add(egui::TextEdit::multiline(&mut my_code).layouter(&mut layouter));
/// # });
/// ```
#[inline]
pub fn layouter(mut self, layouter: &'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>) -> Self {
pub fn layouter(
mut self,
layouter: &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
) -> Self {
self.layouter = Some(layouter);

self
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -883,7 +888,7 @@ fn events(
state: &mut TextEditState,
text: &mut dyn TextBuffer,
galley: &mut Arc<Galley>,
layouter: &mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>,
layouter: &mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>,
id: Id,
wrap_width: f32,
multiline: bool,
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions crates/egui/src/widgets/text_edit/text_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/src/demo/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/src/easy_mark/easy_mark_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
Expand Down