1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

Round widget coordinates to even multiple of 1/32 (#5517)

* Closes https://github.com/emilk/egui/pull/5197
* Closes https://github.com/emilk/egui/issues/5163

This should help prevent rounding errors in layout code.

@lucasmerlin you may wanna test this with `egui_flex`
This commit is contained in:
Emil Ernerfeldt
2024-12-26 20:54:24 +01:00
committed by GitHub
parent f30f5e9578
commit dfcc679d5a
48 changed files with 377 additions and 136 deletions

View File

@@ -1,7 +1,7 @@
use std::ops::RangeInclusive;
use std::sync::Arc;
use emath::{pos2, vec2, Align, NumExt, Pos2, Rect, Vec2};
use emath::{pos2, vec2, Align, GuiRounding as _, NumExt, Pos2, Rect, Vec2};
use crate::{stroke::PathStroke, text::font::Font, Color32, Mesh, Stroke, Vertex};
@@ -630,7 +630,7 @@ fn galley_from_rows(
min_x = min_x.min(row.rect.min.x);
max_x = max_x.max(row.rect.max.x);
cursor_y += max_row_height;
cursor_y = point_scale.round_to_pixel(cursor_y);
cursor_y = point_scale.round_to_pixel(cursor_y); // TODO(emilk): it would be better to do the calculations in pixels instead.
}
let format_summary = format_summary(&job);
@@ -648,20 +648,25 @@ fn galley_from_rows(
let mut rect = Rect::from_min_max(pos2(min_x, 0.0), pos2(max_x, cursor_y));
if job.round_output_size_to_nearest_ui_point {
if job.round_output_to_gui {
for row in &mut rows {
row.rect = row.rect.round_ui();
}
let did_exceed_wrap_width_by_a_lot = rect.width() > job.wrap.max_width + 1.0;
// We round the size to whole ui points here (not pixels!) so that the egui layout code
// can have the advantage of working in integer units, avoiding rounding errors.
rect.min = rect.min.round();
rect.max = rect.max.round();
rect = rect.round_ui();
if did_exceed_wrap_width_by_a_lot {
// If the user picked a too aggressive wrap width (e.g. more narrow than any individual glyph),
// we should let the user know by reporting that our width is wider than the wrap width.
} else {
// Make sure we don't report being wider than the wrap width the user picked:
rect.max.x = rect.max.x.at_most(rect.min.x + job.wrap.max_width).floor();
rect.max.x = rect
.max
.x
.at_most(rect.min.x + job.wrap.max_width)
.floor_ui();
}
}
@@ -1133,6 +1138,7 @@ mod tests {
LayoutJob::single_section("# DNA\nMore text".into(), TextFormat::default());
layout_job.wrap.max_width = f32::INFINITY;
layout_job.wrap.max_rows = 1;
layout_job.round_output_to_gui = false;
let galley = layout(&mut fonts, layout_job.into());
assert!(galley.elided);
assert_eq!(