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

Conversation

kernelkind
Copy link

@kernelkind kernelkind commented Feb 11, 2025

This change allows layouter to use the TextBuffer instead of &str in the closure. It is necessary when layout decisions depend on more than just the raw string content, such as metadata stored in the concrete type implementing TextBuffer.

In our use case, we needed this to support mention highlighting when a user selects a mention. Since mentions can contain spaces, determining mention boundaries from the &str alone is impossible. Instead, we use the TextBuffer implementation to retrieve the correct bounds.

See the video below for a demonstration:

Screen.Recording.2025-02-11.at.4.30.13.PM.mov

Breaking change

This PR introduces a breaking change to the layouter function in TextEdit.

Previous API:

pub fn layouter(mut self, layouter: &'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>) -> Self

New API:

pub fn layouter(mut self, layouter: &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley>) -> Self

Impact on Existing Code

• Any existing usage of layouter will no longer compile.
• Callers must update their closures to use &dyn TextBuffer instead of &str.

Migration Guide

Before:

let mut layouter = |ui: &Ui, text: &str, wrap_width: f32| {
    let layout_job = my_highlighter(text);
    layout_job.wrap.max_width = wrap_width;
    ui.fonts(|f| f.layout_job(layout_job))
};

After:

let mut layouter = |ui: &Ui, text: &dyn TextBuffer, wrap_width: f32| {
    let layout_job = my_highlighter(text.as_str());
    layout_job.wrap.max_width = wrap_width;
    ui.fonts(|f| f.layout_job(layout_job))
};

  • There is not an issue for this change.
  • I have followed the instructions in the PR template

Signed-off-by: kernelkind <kernelkind@gmail.com>
@kernelkind kernelkind marked this pull request as ready for review February 11, 2025 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant