Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-pass UI Layout : Why?

I've noticed that Android, WPF, and Silverlight all follow a two-pass layout pattern. There's a recursive Measure() method that gets called to size the element, possibly multiple times. Then a recursive Layout/Arrange() method is called which lays out the exact positions of children in their parent control, and will also set the final size of the control.

My question: why is this split into two passes, especially when, for some types of controls, Measure() can't compute the actual size of a control without actually laying out the positions of the children? Is there some type of layout minority case that is made possible by this?

I'm trying to create my own UI toolkit, and I'm currently leaning toward a one-pass Layout() pattern, but I'd like to be convinced whether this is wise or not.

Thanks for reading this :)

Sean

like image 250
Hanoixan Avatar asked Sep 07 '10 09:09

Hanoixan


1 Answers

The reason for the two passes is that any element in the structure can influence the remaining available space of the others.

Some element wish to take the largest possible space while others have fixed dimensions. You can also have elements with only a max width set. It creates an equation that can not be solved in one pass.

The different panels in the hierarchy ask the elements what size they need in the first pass, then distribute the space among them according to each panel's nature, and finally informs each element of its allocated space.

EDIT: Some more explanations

The main drawback of a single pass layout is that you are handling each element sequentially. A first element takes a certain amount of space and the others take the rest. Why is this element the first? Try your algorithm with different element order, and you will have different resulting layouts.

A two pass layout simulates a parallel behavior where each element influences the whole layout.

like image 189
Mart Avatar answered Oct 15 '22 17:10

Mart