From d5660fef940dde02ae22d99767023ff7d201d394 Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Sat, 18 May 2024 21:28:32 +0100 Subject: [PATCH] ui: Fix some layout rounding issues --- engine/sources/ui/layout/box_layout.cc | 29 ++++++++++++++------------ engine/sources/ui/painter.cc | 4 +--- engine/sources/ui/widget/time_graph.cc | 4 +++- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/engine/sources/ui/layout/box_layout.cc b/engine/sources/ui/layout/box_layout.cc index e62f214d..a5771784 100644 --- a/engine/sources/ui/layout/box_layout.cc +++ b/engine/sources/ui/layout/box_layout.cc @@ -64,6 +64,10 @@ void BoxLayout::pre_layout(LayoutSize available_space) { LayoutUnit main_axis = 0; LayoutUnit cross_axis = 0; for (const auto &child : children()) { + if (child->is_pane()) { + static_cast(*child).pre_layout({}); + } + Length child_main_axis = child->minimum_size().main_axis_length(m_orientation); main_axis += child_main_axis.resolve(tree(), available_main_axis_length); main_axis += spacing; @@ -71,10 +75,6 @@ void BoxLayout::pre_layout(LayoutSize available_space) { Length child_cross_axis = child->minimum_size().cross_axis_length(m_orientation); cross_axis = vull::max(cross_axis, child_cross_axis.resolve(tree(), available_space.cross_axis_length(m_orientation))); - - if (child->is_pane()) { - static_cast(*child).pre_layout({}); - } } main_axis += margins().main_axis_total(tree(), m_orientation); @@ -90,14 +90,14 @@ void BoxLayout::pre_layout(LayoutSize available_space) { } void BoxLayout::layout(LayoutSize available_space) { - // 1. Get total available main axis length and resolve the spacing property against it. + // Get total available main axis length and resolve the spacing property against it. const LayoutUnit available_main_axis_length = available_space.main_axis_length(m_orientation); const LayoutUnit spacing = m_spacing.resolve(tree(), available_main_axis_length); - // 2. Set computed cross axis length to the total available length. + // Set computed cross axis length to the total available length. set_computed_cross_axis(available_space.cross_axis_length(m_orientation)); - // 3. Create LayoutItems from child elements for processing. + // Create LayoutItems from child elements for processing. // TODO(small-vector) Vector items; items.ensure_capacity(children().size()); @@ -109,12 +109,12 @@ void BoxLayout::layout(LayoutSize available_space) { return; } - // 4. Calculate the maximum child cross axis length as the total available cross axis length minus the margins. + // Calculate the maximum child cross axis length as the total available cross axis length minus the margins. // TODO: Percentage margins. LayoutUnit maximum_cross_axis_length = computed_cross_axis(); maximum_cross_axis_length -= margins().cross_axis_total(tree(), m_orientation); - // 5. Calculate the cross axis length and offset for each item. + // Calculate the cross axis length and offset for each item. for (auto &item : items) { // Resolve element maximum length against the box maximum length. item.cross_axis_length = @@ -127,7 +127,7 @@ void BoxLayout::layout(LayoutSize available_space) { } } - // 6. Size all items to their minimum, keeping track of how much main axis space is leftover. + // Size all items to their minimum, keeping track of how much main axis space is leftover. LayoutUnit uncommitted_length = available_main_axis_length; uncommitted_length -= margins().main_axis_total(tree(), m_orientation); uncommitted_length -= spacing * int32_t(items.size() - 1); @@ -150,7 +150,7 @@ void BoxLayout::layout(LayoutSize available_space) { } } - // 7. Share out remaining length. + // Share out the remaining length. while (uncommitted_length > 0 && unfinalised_item_count > 0) { LayoutUnit slice = uncommitted_length / int32_t(unfinalised_item_count); uncommitted_length = 0; @@ -171,7 +171,7 @@ void BoxLayout::layout(LayoutSize available_space) { } } - // 8. Place the items. + // Place the items. LayoutUnit main_axis = margins().main_axis_start(tree(), m_orientation); LayoutUnit cross_axis = margins().cross_axis_start(tree(), m_orientation); for (const auto &item : items) { @@ -187,9 +187,12 @@ void BoxLayout::layout(LayoutSize available_space) { static_cast(element).layout(element.computed_size()); } main_axis += item.main_axis_length + spacing; + + // Keep main axis offset rounded. + main_axis = LayoutUnit::from_int_pixels(main_axis.round()); } - // 8. Set computed main axis length. + // Set computed main axis length. set_computed_main_axis(main_axis); } diff --git a/engine/sources/ui/painter.cc b/engine/sources/ui/painter.cc index 57125b0f..c6c73458 100644 --- a/engine/sources/ui/painter.cc +++ b/engine/sources/ui/painter.cc @@ -78,9 +78,7 @@ void Painter::paint_text(Font &font, LayoutPoint position, const Colour &colour, .texture_index = get_texture_index(m_atlas->sampled_image()), }}, }); - - // Round the advance to the nearest pixel to avoid jittering issues. - position += LayoutDelta::from_int_pixels(advance.round()); + position += advance; } } diff --git a/engine/sources/ui/widget/time_graph.cc b/engine/sources/ui/widget/time_graph.cc index fbad579e..708114e6 100644 --- a/engine/sources/ui/widget/time_graph.cc +++ b/engine/sources/ui/widget/time_graph.cc @@ -89,7 +89,9 @@ void TimeGraph::pre_layout(LayoutSize available_space) { m_legend_vbox->clear_children(); const auto &latest_bar = m_bars[m_bars.size() - 1]; for (const auto §ion : vull::reverse_view(latest_bar.sections)) { - const auto text = vull::format("{}: {} ms", section.name, section.duration * 1000.0f); + // TODO: vull::format should have padding builtin. + const auto *pad_string = section.duration < 0.01f ? " " : ""; + const auto text = vull::format("{}: {}{} ms", section.name, pad_string, section.duration * 1000.0f); auto &label = m_legend_vbox->add_child