Skip to content

Commit

Permalink
ui: Fix some layout rounding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed May 18, 2024
1 parent a09fbb7 commit d5660fe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
29 changes: 16 additions & 13 deletions engine/sources/ui/layout/box_layout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ 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<Pane &>(*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;

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<Pane &>(*child).pre_layout({});
}
}

main_axis += margins().main_axis_total(tree(), m_orientation);
Expand All @@ -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<LayoutItem> items;
items.ensure_capacity(children().size());
Expand All @@ -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 =
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -187,9 +187,12 @@ void BoxLayout::layout(LayoutSize available_space) {
static_cast<Pane &>(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);
}

Expand Down
4 changes: 1 addition & 3 deletions engine/sources/ui/painter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 3 additions & 1 deletion engine/sources/ui/widget/time_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 &section : 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<Label>(vull::move(text));
label.set_align(Align::Right);
label.set_colour(colour_for_section(section.name));
Expand Down

0 comments on commit d5660fe

Please sign in to comment.