Skip to content

Latest commit

 

History

History
376 lines (302 loc) · 23.4 KB

layout.md

File metadata and controls

376 lines (302 loc) · 23.4 KB

Layout

Status: this is the library. A page arranges a retained tree of widgets, and this file is what that tree is. It arrived in master with v0.9.0, and the flat list it replaced is kept on the branch layout-legacy -- the last state of the old model, for reading an older program rather than for building one. Everything below is a decision unless it is under Open questions at the end.

Why the page is no longer rebuilt

The library began with a page that rebuilds itself: Layout() calls ClearWidgets() and creates the controls again from the window's own fields, and a control's callback writes those fields. For a dialog with six controls that is small, honest and enough. It stops being enough at four places:

  • The geometry is the page's arithmetic. Every page writes the same shapes again: a stack with gaps, a section heading, a settings card 64 DIP high with a title, a line of detail and a slot for the control on its right, a row of buttons as wide as they asked to be, and the content extent the scroll bar wants. examples/gallery, examples/settings, examples/nav and the applications built on the library each have their own copy, with their own numbers.
  • Nothing survives a rebuild, so scrolling needs a second model. A wheel notch does not rebuild, so the page keeps a scroll offset, a drawn offset, a glide it writes itself, a ScrollBar it creates and wires up, ClipRect(), ContentTransform(), scrolls on every control inside, and the rule that Layout() must never run while scrolling. That is the library handing its own job to the page.
  • A rebuild during a callback is a trap. Layout() is routinely called from inside a control's callback. Everything that is holding something at that moment has to be written to survive it, and the library is full of the workarounds: persistent, retired, the layer that outlives one layout, the promise that a control removed mid-message is destroyed after it returns. It works, but it is a cost paid by every control, for a page that could just... not rebuild.
  • A flat list has nothing to report. UI Automation sees the window and then every control as its child, in paint order. A card is not a group, a heading is not a heading, and the rows of an open drop-down are not item elements, because nothing in the window knows what a card is. That is not a gap in the UIA code; it is a gap in the model.

The tree

Widget becomes a node.

Member
Widget *parent Who owns it. Null at the root.
std::vector<std::unique_ptr<Widget>> children What it owns, in order. Order is paint order, hit-test order reversed, tab order, and the order a screen reader reads.
bool visible Drawn, hit-tested and read only when true. A layout that switches between pages switches this and nothing else.
T *Add(T *w) Takes ownership, appends, returns w. The same shape as Window::Add today.
void Remove(Widget *w) Takes it out. Destruction is still deferred to after the message being handled, which is machinery that already exists.

A control is no longer a rectangle a page placed. It is a node whose parent's layout chose its position, and whose size it asked for or was given.

A rectangle is in its parent's space. The painter walks the tree with a stack of transforms and clips -- the window's own, a scrolling container's offset, a card's padding, a position on its way somewhere -- and hit testing unwinds the same stack. Scrolling is already a transform today, one level up; this is the same idea with the level count freed. It is also what makes motion cheap: a subtree that moves is a transform, and nothing has to be told.

The other half of that convention, and the one that keeps every control in this library working: a widget draws at its own rect, in the space it was arranged in. The painter goes down to a widget's top left before calling it, so its paint code goes on reading rect.left exactly as it did when a page placed it by hand -- and a widget that is gliding is drawn where it is while its children are drawn from where it is, so a subtree on its way somewhere is one translation and nothing inside it knows.

A widget may opt out with space = Space::Page: its rectangle is then in the window's own space, no ancestor transform touches it, and what the transforms were doing becomes its own job -- clip itself to the room it has, keep out of the way of anything that scrolls around it, and be hit-tested exactly where it draws. That is the honest answer for a widget that hosts a child window or draws a canvas of its own, and taking it on should be a decision somebody makes on purpose, which is why it is a field a widget sets rather than a mode the window is put into.

The rectangle a layout arranges is the target; what is drawn is a rectangle gliding toward it. See Motion.

What a control says about itself

Widget gains one question:

virtual Want Measure(const Room &room) const;

Want is what it would like to be, per axis, and there are three answers rather than a number:

Content As big as its content measures. size carries the measurement, and it is a preference: a parent may give more, or less.
Fill As big as there is. In a column the Fill children share what the others left over.
Fixed Exactly size, and a parent honours it: a square ring, a control that is one switch wide.

Room is what the question is asked in: the fonts (with the measured-text cache behind them), the Spec in force, the width on offer and the height when it is known. A button answers Content with its preferred width and Fixed at the control height; a wrapping label answers Content with the height it needs at the width on offer, which is why the width is part of the question; a widget with no opinion answers Fill. A widget with children answers with what its own layout measured.

This is the one part of the protocol that cannot be changed cheaply later, because every control implements it. Decided: three states per axis.

The Layout

Layout is a class of its own, owned by the container it arranges:

struct Widget {
    std::unique_ptr<Layout> layout;      // null: children keep the rectangles they were given
    void SetLayout(Layout *l);
};
struct Layout {
    virtual Want Measure(const Room &room) const;
    virtual void Arrange(const Room &room, const D2D1_RECT_F &box) = 0;
    virtual void Tick(float dt);          // the animations this layout owns
    void Invalidate();                    // mark the subtree dirty
    bool Glide(float dt);                 // move the drawn rectangles, and say whether anything moved
    Spec spec;                            // the numbers, edit in place
};

Both passes take the room, because arranging is also where a child is measured against the width it is really being given -- which is not the one it was offered, once a line wraps.

Nothing arranges inside a callback. A change marks the subtree dirty, and the window arranges once, at one defined point in the frame, before it paints. That is what makes a retained tree cheap (ten changes in one message are one arrange) and it is also the answer to re-entrancy: a callback may add, remove, hide or restyle anything it likes, and the tree it is running in is never rearranged under it. The library's habit of calling back into page code with lambdas gets safer, not riskier.

Built-in layouts, in the order they are needed:

StackLayout The default, and the one most pages want: children one after another, a gap between them, padding, headings that take their own band.
RowLayout Children side by side, each as wide as it asked to be, or sharing the room. A row with a width of its own is that width, whatever is in it.
CardLayout The settings card, and the layout of the Card widget below.
GridLayout Columns and rows, with a cell able to span. The one that needs a real measure pass, and the one to build last.
CustomLayout A callback: CustomLayout([](Widget *host, const D2D1_RECT_F &box) { ... }). The escape hatch for a page that wants to do its own arithmetic, which is what today's Layout() is.

The spec is a thing

Grouped card surfaces, expandable detail groups and optional centred width limits are described in Grouped and expandable cards, with a complete example and window-free verification modes.

The numbers in today's pages are not arbitrary: a page margin of 24, cards 64 DIP high and 4 apart, a control slot 16 DIP from the card's right edge, a page title 44 high with 8 above it, headings 30 high with 24 above them, a control height of 32. They are the platform's own settings-page numbers, the same in every Windows app, which is exactly why they should not be typed into every page.

They become one struct the layouts read -- Spec, defaulting to those numbers, with the theme.h metrics folding in and a way for a page to override a field or hand in its own. A layout places; the spec says what "placed" looks like. A theme change (DPI, accent, density) reloads it and marks the tree dirty, so a page never sees it.

The user says the order, not the coordinates

auto *page = nav->AddPage();
page->SetLayout(new StackLayout());
page->Add(new PageTitle(L"General"));
page->Add(new Heading(L"Choices"));
page->Add(new CheckBox(L"Show a notification when done", notify, onChange));

auto *card = page->Add(new Card(glyph::kView, L"Quality", L"Segmented - a few words, side by side"));
card->Set(new Segmented({ L"Auto", L"High", L"Low" }, quality, onChange));
card->value = [&] { return kQuality[quality]; };   // what the control reads back as

The icon goes in the constructor when there is one, and nowhere else: an icon is a Glyph, which no string converts to, so the two-string Card can never be the one with an icon.

A PageTitle, a Heading, a Label and a Card are widgets, so what a page used to draw in PaintPage() -- its headings, its card backgrounds, its lines of detail -- is placed, measured, ordered and reported like anything else, and PaintPage() has nothing left to do.

A Card is the platform's control template rather than a shape: an icon if it has one, a title line, a description line, one control in its slot (a Button, a ToggleSwitch, a TextBox, a Segmented) and the line that reads that control's value back. Windows Settings is built out of that one template, so a library that looks like Windows Settings is built out of it too -- which is why it belongs in the library as a widget instead of being a shape every page assembles, and why it is the widget a page reaches for most.

The template with none of its words is the one other shape it has, and then it is not a settings row at all: Card() holds controls and no text, and they are placed as the contents of a box -- from the card's left edge to its right, with a child that asks to fill taking the whole card. It is what a row of two controls in a card is, and what a settings row cannot be: a row with no words has no column to put its control in.

Scrolling belongs to the container

A container whose arranged children do not fit scrolls them: the children are clipped to it, it keeps the offset, it takes the wheel, it holds a ScrollBar of its own and glides the offset on the frame loop. That is what CSS calls overflow: auto and what a WinUI ScrollViewer is.

What disappears with it: scrolls, ClipRect(), ContentTransform(), the ScrollBar a page creates and wires up, the maxScroll arithmetic, and the rule that a page must not call Layout() while scrolling -- a rule that only exists because a rebuild would fight the scroll.

Written, and smaller than the paragraph above promised. ScrollView is a View with a stack inside it, and scrolling turned out to be one idea rather than a mechanism: the children are arranged in a column as tall as they come out and moved up by however far the view is scrolled, so a wheel notch changes a number and everything downstream follows. The glide animates it because a child whose rectangle has moved is a child whose drawn rectangle is on its way there; the hit test follows it because it reaches a widget where it looks; the clip is the view, and it is the only part of this that is about the container rather than about the children. There is no scroll offset for anything to remember and no second animation. VisibleArea() answers with the nearest container that clips, which is the one thing that did have to be taught about it. The box a container clips to is asked of it (Widget::ClipBox()), and its own rectangle is only the default: a container that is showing a part of itself -- a panel being uncovered as it arrives, with the rows inside it standing still -- clips its children to that part, and the hit test asks the same question, so what is not shown is not answered for either.

The bar came after, and it is the reason the view is two children rather than one: the column a page adds to, and the bar, in that order -- the bar is painted over the column, so it has to be last, and it cannot be last if the thing a page keeps adding to is the view itself. A page still calls Add on the view; the view forwards it to the column, which is also where the page's margin lives. The bar is a child rather than something the view draws, so the tree paints it, hit-tests it and ticks it, and the view's layout is what keeps it told: how much of the page there is, how much of it is visible, where the page has been scrolled to, and where it is drawn -- which trails the target through a glide, so the thumb goes with the page instead of arriving before it.

Motion

A layout arranges a child into a target rectangle; the child is drawn at a rectangle that glides toward it. The layout holds both, because it is the thing that knows what changed, and Tick advances them, so a card steps down when something above it opens, a replaced page slides, and a scrolled list glides -- by the same mechanism, in one place, off the tree's transforms.

With animations off the drawn rectangle is the target in the same frame. That is the rule the theme already keeps for everything else: to the destination, not frozen halfway.

For v1: the glide, the page switch, and a re-arrange that tweens. Enter and exit transitions after that.

Debugging a layout

A page is mostly its layout, and a layout is arithmetic, so the fault is usually a box that is not where somebody thought it was. There are two ways to look at it and they answer different questions.

Print the rectangles. Arrange the tree and write out what each widget came out as -- a test, or a --dump switch on an example. This is what a fault in the arithmetic wants: no window, no GPU, no screenshot, and a diffable answer. The layouts in this file were built against it.

Draw the rectangles. Built with -DMICULA_DEBUG_LAYOUT=1, the window draws over the page:

red the rectangle the layout arranged the widget into (rect)
blue the clip the widget is painted under
orange a container's content box: its own rectangle less its padding (Layout::ContentBox)
magenta rect while the widget is drawn somewhere else -- a position in flight

The whole pass is compiled out without the flag: no code, no colours. A build that has it can still turn it off at run time with micula::debug::layout = false. It draws unclipped on purpose, because a box that overflows its clip is the one thing worth seeing.

Input, focus, and the tree

  • Hit testing descends from the root, skipping invisible subtrees and anything outside its parent's clip, last child first. Capture and focus stay where they are, on the window.
  • Tab order is document order, so a page no longer has to keep its control order in step with its creation order.
  • A pointer move reaches the control under the pointer, and a control that needs to hear about moves that leave its own subtree -- today's ExternalRegion -- keeps a way to say so.
  • A modal child is the old Layer: it takes the input under it, modal, lightDismiss, smoke and onDismiss keep their meaning, and it is the last child of the root, so it paints over everything and needs nothing like z. Where it goes is the layer's own answer -- Widget::Cover, asked by the tree of a child that answered AsLayer -- and the default is the whole of the widget it was added to, which is what a dialog is: add it to the page and it covers the page. A layer that is not a cover at all -- a tip, a box the size of its own words -- overrides it. A flyout does not: it covers the page, because it takes the wheel and a click outside it closes it, and the list is a child of the flyout rather than the flyout itself. A layout never arranges one: a column that did would have given it a slot and pushed the page down by the height of it.

Accessibility

The tree is the automation tree. That is the point of having one: a Card is a group named by its title, a Heading is a heading with a level, a Page in a SideNav is a page, and the rows of an open drop-down can be item elements of the drop-down, because a control can have children now.

Most of the list under "Not there yet" in Window is a list of things a flat list cannot express. The five questions a control answers about itself (AccessibleName, AccessibleType, AccessibleToggle, AccessibleValue, AccessibleRange) and the two it answers about the set it draws (AccessibleItems, AccessibleItem) do not change.

What the window becomes

Window becomes the root of the tree -- it is a Widget -- and it holds the root widget the page builds into: the client area, which is WinUI's Window.Content and the thing a page adds to. Its own furniture is its other children: the title bar, its buttons, the pane. kCaptionH stops being a constant and becomes the height of a widget, and the minimise, maximise and close buttons join the automation tree, which they are missing from today.

The frame becomes:

  1. messages, which change the tree and mark it dirty
  2. arrange, if anything is dirty
  3. tick the animations
  4. paint

What survives

  • The control API: rect, visible, enabled, hover, pressed, the callbacks, and the lambda style. A control is now told its size instead of working it out from the page's arithmetic.
  • The painter, the fonts, the measured-text cache, the theme, the glyphs, MinSize.
  • The interaction rules that were paid for: capture, pressed, Dismiss(), the deferred destruction, focus and the focus ring, the keyboard behaviour of every control.

What goes

Gone Replaced by
Window::Layout() and the rebuild the tree; a change marks it dirty and the frame arranges
ClearWidgets(), persistent the tree, and hiding
rect in page coordinates rect in the parent's space
scrolls, ClipRect(), ContentTransform(), VisibleArea(), the page's ScrollBar a container that overflows
PaintPage() widgets: PageTitle, Heading, Label, Card
z tree order
A control laying itself out by calling the page's Layout() its own children, and its own layout

Migration

The migration happened in this order, and each phase was something a person could look at and disagree with:

  1. This file. The design, agreed before code. done
  2. The core. Widget as a node, hit test and focus over the tree, Layout with Measure/Arrange/Invalidate/Tick, StackLayout and RowLayout, Spec, the debug overlay, Card with its own layout, and the widgets a page needs to say anything at all: Label, Heading. done -- all of it compiles clean and examples/gallery is a page written against it, with a --dump that prints the rectangles it came out as.
  3. Scrolling and the rest of the controls. ScrollView is written, with the clip, the wheel, the glide and its bar; so is Dialog, which is what made a layer's placement a rule of the tree rather than a thing a dialog did for itself; so is DropDown, on a Flyout that a menu and a suggestion box will want too; and so is SideNav, the last control that was written against the flat list. done -- every control in the library is written against the tree, and so are the three examples: examples/gallery is the controls, examples/nav is the pane and the shell that switches pages in it, and examples/settings is the window in the README screenshot.
  4. The grid. GridLayout: columns as a template -- a Sizing each, so a column is fixed, as wide as what is in it, or a share of what is left -- with the spans a child can ask for. The adaptive case belongs beside it rather than inside it: a card grid whose column count comes from a minimum card width and the room it has, which is what a gallery of cards wants, and what a page of two-part rows wants once the two parts stop fitting side by side. Page is not part of this: a page is a Widget with a layout of its own, and ScrollView's is what pages are built with.
  5. Accessibility and polish. The automation tree was the first half of this and is done: a widget says what it is made of, so a heading is a heading and the rows of a list are elements a client visits one at a time. What is left of the phase: arrivals and departures -- the glide is the only motion the tree has, and WinUI has a set of others, a card growing into its page, a row leaving a list, a layer fading through a scale, all worth trying here; a held press as the way a touch opens a context menu, which is the one gesture left unwritten; and the docs rewritten (window.md, controls.md, widget.md, README.md) with this file turned into the user-facing document.
  6. The applications. AlasViewer is the first, and it is the real test: a Widget of its own with a Paint that draws a game screen, a page built by hand, and a source dependency on this checkout rather than an installed package -- so it is the thing that says whether the model survives a program that is not an example. in progress, and still the one finding what the model is missing.

The branch that held all of this is gone: what it built is master now, and nothing has to be kept in step with anything. What it replaced is on layout-legacy, kept as a record rather than as a version that ships.

Merging back was a breaking release, and the note is short because the new model is the smaller one: a page stops computing rectangles and starts adding children, and a control that used to read rect reads the rectangle its parent's layout gave it. This file is the note.

Open questions

  1. How much of the window is a widget in v1. The title bar, the pane and the window buttons are children of the root in this design; whether they become widgets in the first pass or the second is a question about how much moves at once, not about the model.
  2. What the v1 animation set is. The glide, the page switch -- with its entrance -- and a re-arrange that tweens are in this file; transitions for anything else are not.

Answered on 2026-09-29, and the answers are in the sections above: Window is the root and holds the root widget; a rectangle is in the parent's space, with an opt-out for a widget that would rather do all of it itself; Want has three states per axis; a container that overflows scrolls without being asked. CustomLayout stays, and is written: it is the escape hatch for a page with one odd row, and the shape Window::Layout() becomes for a page that keeps its own arithmetic.