egui/crates
kernelkind fe631ff9ea
Use `TextBuffer` for `layouter` in `TextEdit` instead of `&str` (#5712)
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](https://github.com/damus-io/notedeck/pull/723), 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:


https://github.com/user-attachments/assets/3cba2906-5546-4b52-b728-1da9c56a83e1


# Breaking change

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

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

New API:
```rust
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:
```rust
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:
```rust
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.
* [x]  I have followed the instructions in the PR template

Signed-off-by: kernelkind <kernelkind@gmail.com>
2025-04-06 18:45:20 +02:00
..
ecolor Add assert messages and print bad argument values in asserts (#5216) 2025-03-25 09:20:29 +01:00
eframe Use lychee link checker instead of linkinator (#5868) 2025-04-03 09:26:49 +02:00
egui Use `TextBuffer` for `layouter` in `TextEdit` instead of `&str` (#5712) 2025-04-06 18:45:20 +02:00
egui-wgpu Release 0.31.1 - text_edit and kittest fixes 2025-03-05 08:37:34 +01:00
egui-winit Fix text input on Android (#5759) 2025-03-21 14:35:46 +01:00
egui_demo_app Make text underline and strikethrough pixel perfect crisp (#5857) 2025-03-28 20:37:38 +01:00
egui_demo_lib Use `TextBuffer` for `layouter` in `TextEdit` instead of `&str` (#5712) 2025-04-06 18:45:20 +02:00
egui_extras Make text background rects pixel-sharp (#5864) 2025-03-30 16:21:00 +02:00
egui_glow Add assert messages and print bad argument values in asserts (#5216) 2025-03-25 09:20:29 +01:00
egui_kittest Improve text sharpness (#5838) 2025-03-21 12:56:47 +01:00
egui_plot Move `egui_plot` to its own repo (#4828) 2024-07-15 18:45:19 +02:00
egui_web Fix typos (#2866) 2023-04-18 15:52:45 +02:00
emath Optimize editing long text by caching each paragraph (#5411) 2025-04-01 18:55:39 +02:00
epaint Optimize editing long text by caching each paragraph (#5411) 2025-04-01 18:55:39 +02:00
epaint_default_fonts Release 0.31.1 - text_edit and kittest fixes 2025-03-05 08:37:34 +01:00