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

Prevent text shrinking in tooltips; round wrap-width to integer #5161

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion crates/epaint/src/text/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,31 @@ struct GalleyCache {
}

impl GalleyCache {
fn layout(&mut self, fonts: &mut FontsImpl, job: LayoutJob) -> Arc<Galley> {
fn layout(&mut self, fonts: &mut FontsImpl, mut job: LayoutJob) -> Arc<Galley> {
if job.wrap.max_width.is_finite() {
// Protect against rounding errors in egui layout code.

// Say the user asks to wrap at width 200.0.
// The text layout wraps, and reports that the final width was 196.0 points.
// This than trickles up the `Ui` chain and gets stored as the width for a tooltip (say).
// On the next frame, this is then set as the max width for the tooltip,
// and we end up calling the text layout code again, this time with a wrap width of 196.0.
// Except, somewhere in the `Ui` chain with added margins etc, a rounding error was introduced,
// so that we actually set a wrap-width of 195.9997 instead.
// Now the text that fit perfrectly at 196.0 needs to wrap one word earlier,
// and so the text re-wraps and reports a new width of 185.0 points.
// And then the cycle continues.

// So we limit max_width to integers.

// Related issues:
// * https://github.com/emilk/egui/issues/4927
// * https://github.com/emilk/egui/issues/4928
// * https://github.com/emilk/egui/issues/5084
emilk marked this conversation as resolved.
Show resolved Hide resolved

job.wrap.max_width = job.wrap.max_width.round();
}

let hash = crate::util::hash(&job); // TODO(emilk): even faster hasher?

match self.cache.entry(hash) {
Expand Down
Loading