4. Layout#
ui.text(TS_TITLE, "Hallo");
ui.box_begin(0, BOX_H | BOX_XCENTER, 8); // a row, children centred vertically, 8 DIP gap
ui.text(TS_BODY, "Name:");
ui.next_size(-1, 0); // the text box takes the remaining width
ui.textbox(ID_NAME, Some("Name eingeben"), &mut self.name, 64);
ui.next_size(120, 0); // 120 DIP wide, natural height
ui.button(ID_OK, "OK");
ui.box_end();
// a card with a 16 DIP padding
ui.box_begin(0, BOX_V | BOX_CARD | PAD_16, 4);
ui.text(TS_BODY_STRONG, "Karte");
ui.text(TS_CAPTION | TX_SECONDARY | TX_WRAP, "Karten gruppieren Inhalte.");
ui.box_end();
box_begin(id, flags, gap)/box_end(): children flow along the main axis (BOX_Hleft to right,BOX_Vtop to bottom). Padding:| PAD_8,| pad(24)(DIP).next_size(w, h)sizes the next widget or box in DIP:> 0fixed,0natural,< 0fill weight (-1= take the remaining space). The cross axis stretches by default;BOX_XCENTER/BOX_XSTART/BOX_XENDgive children their natural cross size.Main axis:
BOX_CENTER,BOX_END;spacer(dip)(-1= flexible).BOX_WRAP(horizontal boxes) for button rows, legends, tile palettes: a child that does not fit after the others of its row starts a new row (a row's first child stays, however wide). Each row is as tall as its tallest child; the cross alignment (BOX_XCENTER..., stretched children take the row's height) andBOX_CENTER/BOX_ENDapply per row, and the gap also separates the rows. The natural height is the rows plus their gaps, the natural width one row with every child: the box wraps where its parent gives it less (stretched across a column, a fill share, a fixed width, or at most the room left in a row). Fill children share the rest only while everything fits on one row, else they keep their natural size.ui.box_begin(0, BOX_H | BOX_XCENTER | BOX_WRAP, 8); for (i, tag) in self.tags.iter_mut().enumerate() { ui.toggle_button(ID_TAG + i as u32, &tag.name, &mut tag.on); // on: i32 } ui.box_end();Absolute placement:
next_rect(x, y, w, h)in DIP relative to the current box (overlays, floating panels over a canvas);next_rect_pxin window pixels.Decoration and behaviour:
BOX_CARD,BOX_LAYER(command bars),BOX_BG,BOX_CLIP,BOX_BLOCK(swallows clicks),BOX_DISABLED(everything inside disabled).BOX_FIXEDfor fill boxes whose size never depends on their content (fewer layout passes when deeply nested).Sizes are measured over passes: a new layout settles within 2-3 internal passes; you never see intermediate states.
last_rect()is the physical-pixel rectangle[x, y, w, h]of the last widget,box_rect()the content rectangle of the current box.Units: everything you pass to layout and widgets is DIP (96 dpi = 100 %); drawing calls and rectangles the library returns are physical pixels.
ui.dp(dip)converts,ui.hair()is a one-line width for the current dpi. Test with--scale 150.Grid box (
lib/ui/grid.rs) for forms and tables of controls: one entry per column, DIP> 0fixed,0its widest child,< 0a share of the width left; rows grow with their tallest child (gridbox_rows(&[..])sets them like columns). Children fill the cells row by row;next_span(cols, rows),next_cell(col, row)andnext_align(AL_START | AL_CENTER | AL_END | AL_STRETCH, ..)steer one child,GRID_H_*/GRID_V_*in the flags all of them (default: stretched across, centred vertically). The flags also take the box decorations. ATX_WRAPtext wraps at its cell's width and its row grows with the lines (labels and values:gridbox_begin(id, &[92, -1], 0, 6), thentext/text(TS_BODY | TX_WRAP, ..)pairs).ui.gridbox_begin(ID_FORM, &[0, -1, 120], BOX_CARD | pad(16), 8); ui.text(TS_BODY, "Name"); ui.textbox(ID_NAME, None, &mut self.name, 64); ui.button(ID_PICK, "Wählen …"); ui.text(TS_BODY, "Beschreibung"); ui.next_span(2, 1); // the field takes the last two columns ui.textbox(ID_DESC, None, &mut self.desc, 256); ui.gridbox_end();Animations:
ui.anim(id, target, ms)moves a value (0..256) towardstargetwith an ease-out curve;ui.anim_ex(id, target, ms, EASE_*)takes another curve (EASE_LINEAR,EASE_IN,EASE_IN_OUT,EASE_OUT_CUBIC,EASE_OUT_BACKovershoots a little), andui.anim_color(id, argb, ms, curve)fades a colour (hover fills, state colours).ease(curve, t)is the bare curve. With "reduce motion" (ui.anim_off()) every value jumps to its target.