Home /Examples/lib/examples/gui/LEARNING-NOTES.md

LEARNING-NOTES.md

lib/examples/gui/LEARNING-NOTES.md · 58 line(s) · markdown

GUI examples — what to learn here

These examples teach the arithmetic of layout — how a GUI decides the pixel rectangle of every widget before anything is drawn. They are calculation-only: no window is opened, so they run anywhere and print exact coordinates. This is the same math the standard UI library performs in lib/ui (SLStackLayout, SLGridLayout, SLLayoutBox).

Read them in order.

The three cases

LevelFileThe one new idea
BasicBASIC-clamp-to-parent.sleelaThe fundamental rule: a child is clamped to its parent's size.
ModerateMODERATE-stack-layout.sleelaA horizontal stack: fixed children keep their width, a flexible child absorbs the leftover space.
AdvancedADVANCED-grid-layout.sleelaA uniform grid: divide the area into equal cells separated by gutters, and compute each cell's rectangle.

The original CLASSIC-layout-bounds.sleela is the shortest starter.

What we want you to take away

  1. Layout is solved before it is painted. A layout engine assigns every widget an exact (x, y, width, height). If you can compute those numbers, you understand the layout — the drawing step just fills in the rectangles.
  2. Flexible space is "what's left over." The moderate example gives two children fixed widths and lets the middle one take the remainder after subtracting the fixed widths and the gaps. The built-in check — the last child's right edge equals the row width (400) — is how you prove the distribution is exact with no gap or overlap.
  3. A grid is repeated subtraction and multiplication. Cell size is (area − gutters) / count; each cell's origin is index * (cell + gutter). The advanced example walks rows and columns to emit a pixel-perfect lattice, exactly as SLGridLayout does.

Practical notes for SLeeLa 1.7

Run them

sleela run lib/examples/gui/MODERATE-stack-layout.sleela
sleela run lib/examples/gui/ADVANCED-grid-layout.sleela

Output is deterministic: the stack layout's right edge lands exactly at the row width (400), and the grid prints six 93×95 cells on a 3×2 lattice.