1// Layout: constraints down, sizes up. An element keeps the limits it was2// last laid out within and its size, and is laid out again only when3// marked or given other limits; rows, columns and stacks.45import std::string;6import std::vector;7import std::option;8import std::math;9import pkg::alloy::window;10import pkg::alloy::gpu;11import pkg::alloy::draw;1213import ui;14import tree;15import events;16import languages;1718// Lays out what changed, the root at the viewport's size.19pub fn lay_out_all(self ui: &var Ui) {20 if (!ui.layout_pending) {21 return;22 }23 ui.layout_pending = false;24 const window = Limits::tight(ui.viewport.width, ui.viewport.height);25 if (ui.root is ::Some |root|) {26 ui.lay_out_node(root, window);27 }28 for ([..ui.layers.length()]) |k| {29 ui.lay_out_node(ui.layers.get(k).host.index, window);30 }31}3233pub fn lay_out_node(self ui: &var Ui, index: u32, limits: Limits) -> Extent {34 const direction = ui.resolve_direction(index);35 const node = &ui.node(index);36 if (!node.needs_layout && node.limits == limits) {37 return node.size;38 }39 const sized = within(&ui.node_sizing(index), &limits);40 var arrange = Arrange {41 .ui = &var ui,42 .children = ui.flattened(index),43 .direction = direction44 };45 var size = Extent::zero();46 var hovers = false;47 if (ui.node_var(index).widget is ::Some |&var widget|) {48 arrange.mirrored = direction is ::RightToLeft && widget.mirrors_children();49 size = widget.layout(&var arrange, sized);50 hovers = widget.wants_hover();51 }52 if (hovers) {53 ui.hovers = true;54 }55 size = sized.constrain(size);56 // right to left, each child's x from the start becomes one from the57 // left58 for (arrange.placed.view()) |&p| {59 const width = ui.node(p.node).size.width;60 ui.place_node(p.node, Point { .x = size.width - p.at.x - width, .y = p.at.y });61 }62 const laid = &var ui.node_var(index);63 const resized = laid.size != size;64 laid.needs_paint = laid.needs_paint || resized;65 laid.size = size;66 laid.limits = limits;67 laid.needs_layout = false;68 if (resized) {69 ui.mark_sync(index);70 }71 return size;72}7374// The limits the sizing leaves of the parent's.75fn within(sizing: &Sizing, limits: &Limits) -> Limits {76 var l = copy limits;77 l.min_width = clamp(max(l.min_width, sizing.min_width), l.min_width, l.max_width);78 l.max_width = max(min(l.max_width, sizing.max_width), l.min_width);79 l.min_height = clamp(max(l.min_height, sizing.min_height), l.min_height, l.max_height);80 l.max_height = max(min(l.max_height, sizing.max_height), l.min_height);81 if (sizing.width is ::Some |width|) {82 l.min_width = clamp(width, l.min_width, l.max_width);83 l.max_width = l.min_width;84 }85 if (sizing.height is ::Some |height|) {86 l.min_height = clamp(height, l.min_height, l.max_height);87 l.max_height = l.min_height;88 }89 return l;90}9192pub fn place_node(self ui: &var Ui, index: u32, at: Point) {93 const node = &var ui.node_var(index);94 if (node.position.x != at.x || node.position.y != at.y) {95 node.position = at;96 node.moved = true;97 ui.mark_sync(index);98 }99}100101// The node's children as its layout sees them: those of show and each in102// place of them, which sit at their parent's top left.103pub fn flattened(self ui: &var Ui, index: u32) -> Vector<u32> {104 var out = Vector<u32>::empty();105 ui.flatten_into(index, &var out);106 return move out;107}108109fn flatten_into(self ui: &var Ui, index: u32, out: &var Vector<u32>) {110 const count = ui.node(index).children.length();111 for ([..count]) |k| {112 const child = ui.node(index).children.get(k);113 if (ui.node(child).hidden) {114 continue;115 }116 if (ui.node(child).transparent) {117 ui.resolve_direction(child);118 const group = &var ui.node_var(child);119 group.needs_layout = false;120 group.needs_paint = false;121 ui.place_node(child, ORIGIN);122 ui.flatten_into(child, &var out);123 } else {124 out.push(child);125 }126 }127}128129/* --- Axes --- */130131// The direction a row (Horizontal) or a column (Vertical) lays its132// children along, its main axis; the other is its cross axis.133exp type Axis = enum {134 Horizontal,135 Vertical,136};137138// Of a horizontal pair and a vertical one (an x and a y, a width and a139// height), the one along the axis.140pub fn main(self axis: &Axis, horizontal: f32, vertical: f32) -> f32 => match (axis) {141 ::Horizontal horizontal;142 ::Vertical vertical;143};144145// The one across the axis.146pub fn cross(self axis: &Axis, horizontal: f32, vertical: f32) -> f32 => match (axis) {147 ::Horizontal vertical;148 ::Vertical horizontal;149};150151pub fn point(self axis: &Axis, main: f32, cross: f32) -> Point => match (axis) {152 ::Horizontal Point { .x = main, .y = cross };153 ::Vertical Point { .x = cross, .y = main };154};155156pub fn extent(self axis: &Axis, main: f32, cross: f32) -> Extent => match (axis) {157 ::Horizontal Extent { .width = main, .height = cross };158 ::Vertical Extent { .width = cross, .height = main };159};160161pub fn limits(162 self axis: &Axis,163 main_min: f32,164 main_max: f32,165 cross_min: f32,166 cross_max: f32167) -> Limits {168 return match (axis) {169 ::Horizontal Limits {170 .min_width = main_min,171 .max_width = main_max,172 .min_height = cross_min,173 .max_height = cross_max174 };175 ::Vertical Limits {176 .min_width = cross_min,177 .max_width = cross_max,178 .min_height = main_min,179 .max_height = main_max180 };181 };182}183184/* --- Rows, columns and stacks --- */185186pub fn layout(self c: &var Column, arrange: &var Arrange, limits: Limits) -> Extent {187 return line(&var arrange, &limits, &c.padding, c.gap, c.align, c.justify, Axis::Vertical);188}189190pub fn layout(self r: &var Row, arrange: &var Arrange, limits: Limits) -> Extent {191 return line(&var arrange, &limits, &r.padding, r.gap, r.align, r.justify, Axis::Horizontal);192}193194// Children along the axis: those that do not grow take what they need195// first, the growing ones share what is left. Hot, so it tests a bool:196// the Axis's methods, taking it by reference, measured slower here.197fn line(198 arrange: &var Arrange,199 limits: &Limits,200 padding: &Padding,201 gap: f32,202 align: Alignment,203 justify: Justify,204 axis: Axis205) -> Extent {206 const vertical = axis is ::Vertical;207 const inner = limits.inside(&padding);208 const main_max = if (vertical) inner.max_height else inner.max_width;209 const main_min = if (vertical) inner.min_height else inner.min_width;210 const cross_max = if (vertical) inner.max_width else inner.max_height;211 const cross_min = if (vertical) inner.min_width else inner.min_height;212 const stretch = align is ::Stretch && cross_max < UNBOUNDED;213 const count = arrange.count();214 var mains = Vector<f32>::filled(0.0, count);215 var crosses = Vector<f32>::filled(0.0, count);216 var used: f32 = if (count > 0) gap * (count - 1) to f32 else 0.0;217 var grow: f32 = 0.0;218 for ([..count]) |k| {219 const g = arrange.sizing(k).grow;220 if (g > 0.0) {221 grow += g;222 continue;223 }224 const size = arrange.lay_out(225 k,226 along(vertical, 0.0, UNBOUNDED, cross(stretch, cross_max), cross_max)227 );228 mains.set(k, if (vertical) size.height else size.width);229 crosses.set(k, if (vertical) size.width else size.height);230 used += mains.get(k);231 }232 if (grow > 0.0) {233 const left = if (main_max < UNBOUNDED) max(main_max - used, 0.0) else 0.0;234 for ([..count]) |k| {235 const g = arrange.sizing(k).grow;236 if (g <= 0.0) {237 continue;238 }239 const share = left * g / grow;240 const size = arrange.lay_out(241 k,242 along(vertical, share, share, cross(stretch, cross_max), cross_max)243 );244 mains.set(k, if (vertical) size.height else size.width);245 crosses.set(k, if (vertical) size.width else size.height);246 used += mains.get(k);247 }248 }249 var cross_size = cross_min;250 for (crosses.view()) |&c| {251 cross_size = max(cross_size, c);252 }253 if (stretch) {254 cross_size = cross_max;255 }256 cross_size = min(cross_size, cross_max);257 var main_size = max(used, main_min);258 if (!(justify is ::Start) && main_max < UNBOUNDED) {259 main_size = main_max;260 }261 main_size = min(max(main_size, used), max(main_max, used));262 const free = max(main_size - used, 0.0);263 var at: f32 = 0.0;264 var spacing = gap;265 match (justify) {266 ::Start {}267 ::Center {268 at = free / 2.0;269 }270 ::End {271 at = free;272 }273 ::Between {274 if (count > 1) {275 spacing = gap + free / (count - 1) to f32;276 }277 }278 }279 for ([..count]) |k| {280 const room = cross_size - crosses.get(k);281 const offset = align.offset(room);282 const point = if (vertical) Point {283 .x = padding.start + offset,284 .y = padding.top + at285 } else Point {286 .x = padding.start + at,287 .y = padding.top + offset288 };289 arrange.place(k, point);290 at += mains.get(k) + spacing;291 }292 const across = padding.start + padding.end;293 const down = padding.top + padding.bottom;294 return if (vertical) Extent {295 .width = cross_size + across,296 .height = main_size + down297 } else Extent {298 .width = main_size + across,299 .height = cross_size + down300 };301}302303fn cross(stretch: bool, cross_max: f32) -> f32 => if (stretch) cross_max else 0.0;304305// Limits for a line's child, from its main and cross axes.306fn along(vertical: bool, main_min: f32, main_max: f32, cross_min: f32, cross_max: f32) -> Limits {307 return if (vertical) Limits {308 .min_width = cross_min,309 .max_width = cross_max,310 .min_height = main_min,311 .max_height = main_max312 } else Limits {313 .min_width = main_min,314 .max_width = main_max,315 .min_height = cross_min,316 .max_height = cross_max317 };318}319320pub fn layout(self s: &var Stack, arrange: &var Arrange, limits: Limits) -> Extent {321 const inner = limits.inside(&s.padding);322 const stretch = s.align is ::Stretch323 && inner.max_width < UNBOUNDED324 && inner.max_height < UNBOUNDED;325 const child_limits = if (stretch) Limits::tight(326 inner.max_width,327 inner.max_height328 ) else inner.loosened();329 var sizes = Vector<Extent>::empty();330 var size = Extent { .width = inner.min_width, .height = inner.min_height };331 for ([..arrange.count()]) |k| {332 const child = arrange.lay_out(k, child_limits);333 size.width = max(size.width, child.width);334 size.height = max(size.height, child.height);335 sizes.push(child);336 }337 for (sizes.view(), [..sizes.length()]) |&child, k| {338 arrange.place(k, Point {339 .x = s.padding.start + s.align.offset(size.width - child.width),340 .y = s.padding.top + s.align.offset(size.height - child.height)341 });342 }343 return Extent {344 .width = size.width + s.padding.start + s.padding.end,345 .height = size.height + s.padding.top + s.padding.bottom346 };347}348349pub fn children(self c: &Column) -> &[Element] => &c.children;350pub fn children(self r: &Row) -> &[Element] => &r.children;351pub fn children(self s: &Stack) -> &[Element] => &s.children;352pub fn sizing(self c: &Column) -> Sizing => c.sizing;353pub fn sizing(self r: &Row) -> Sizing => r.sizing;354pub fn sizing(self s: &Stack) -> Sizing => s.sizing;355pub fn clips(self c: &Column) -> bool => c.clip_radii is ::Some;356pub fn clips(self r: &Row) -> bool => r.clip_radii is ::Some;357pub fn clips(self s: &Stack) -> bool => s.clip_radii is ::Some;358pub fn clip_radii(self c: &Column) -> Radii => radii_of(&c.clip_radii);359pub fn clip_radii(self r: &Row) -> Radii => radii_of(&r.clip_radii);360pub fn clip_radii(self s: &Stack) -> Radii => radii_of(&s.clip_radii);361362fn radii_of(clip: &Option<Radii>) -> Radii => if (clip is ::Some |r|) r else Radii::none();363364exp fn renew(self c: &var Column, next: Column) -> Change {365 c = move next;366 return Change::Layout;367}368369exp fn renew(self r: &var Row, next: Row) -> Change {370 r = move next;371 return Change::Layout;372}373374exp fn renew(self s: &var Stack, next: Stack) -> Change {375 s = move next;376 return Change::Layout;377}378379exp fn paint(self c: &Column, look: &Look, paint: &var Paint) {380 paint_box(&look, &var paint, c.background, c.border, c.radius);381}382383exp fn paint(self r: &Row, look: &Look, paint: &var Paint) {384 paint_box(&look, &var paint, r.background, r.border, r.radius);385}386387exp fn paint(self s: &Stack, look: &Look, paint: &var Paint) {388 paint_box(&look, &var paint, s.background, s.border, s.radius);389}390391exp fn paint_box(392 look: &Look,393 paint: &var Paint,394 background: Ink,395 border: Ink,396 radius: f32397) {398 const area = Rect::sized(look.size.width, look.size.height);399 if (background.color(&look.theme) is ::Some |color|) {400 paint.rounded_rect(area, Radii::all(radius), Fill::Color(color));401 }402 if (border.color(&look.theme) is ::Some |color|) {403 paint.border(area, Radii::all(radius), 1.0, color);404 }405}406407exp fn paint(self g: &Group, look: &Look, paint: &var Paint) {}408409exp fn paint(self s: &Spacer, look: &Look, paint: &var Paint) {}410411exp fn renew(self s: &var Spacer, next: Spacer) -> Change {412 const relayout = s.sizing != next.sizing;413 s.sizing = next.sizing;414 return if (relayout) Change::Layout else Change::Nothing;415}416417pub fn sizing(self s: &Spacer) -> Sizing => s.sizing;418419exp fn renew(self g: &var Group, next: Group) -> Change => Change::Nothing;420421/* --- Wrapping rows and grids --- */422423pub fn layout(self w: &var Wrap, arrange: &var Arrange, limits: Limits) -> Extent {424 const inner = limits.inside(&w.padding);425 const width = inner.max_width;426 var sizes = Vector<Extent>::empty();427 for ([..arrange.count()]) |k| {428 sizes.push(arrange.lay_out(429 k,430 Limits { .max_width = width, .max_height = inner.max_height }431 ));432 }433 var line = WrapLine { .first = 0, .top = 0.0, .height = 0.0 };434 var x: f32 = 0.0; // the line's width so far435 var widest: f32 = 0.0;436 for (sizes.view(), [..sizes.length()]) |&size, k| {437 if (k > line.first && x + w.gap + size.width > width) {438 w.place_line(&var arrange, &sizes.view(), &line, k);439 widest = max(widest, x);440 line = WrapLine {441 .first = k,442 .top = line.top + (line.height + w.line_gap),443 .height = 0.0444 };445 x = 0.0;446 }447 x += (if (k > line.first) w.gap else 0.0) + size.width;448 line.height = max(line.height, size.height);449 }450 var y: f32 = 0.0;451 if (sizes.length() > 0) {452 w.place_line(&var arrange, &sizes.view(), &line, sizes.length());453 widest = max(widest, x);454 y = line.top + line.height;455 }456 return Extent {457 .width = max(widest, inner.min_width) + w.padding.start + w.padding.end,458 .height = max(y, inner.min_height) + w.padding.top + w.padding.bottom459 };460}461462// A line of a wrap: its first child, its top and its height.463type WrapLine = struct {464 first: u64,465 top: f32,466 height: f32,467};468469// Places the line's children, up to `end`, aligned within its height.470fn place_line(self w: &Wrap, arrange: &var Arrange, sizes: &[Extent], line: &WrapLine, end: u64) {471 var x = w.padding.start;472 for (sizes[line.first..end], [line.first..end]) |&size, k| {473 const y = w.align.offset(line.height - size.height);474 arrange.place(k, Point { .x = x, .y = w.padding.top + line.top + y });475 x += size.width + w.gap;476 }477}478479// The column's track: a share of the width when the grid names none.480fn track(self g: &Grid, column: u64) -> Track {481 return if (g.columns.length() == 0) Track::Fraction(1.0) else g.columns.get(column);482}483484pub fn layout(self g: &var Grid, arrange: &var Arrange, limits: Limits) -> Extent {485 const inner = limits.inside(&g.padding);486 const columns = max(g.columns.length(), 1);487 const count = arrange.count();488 // the columns' widths: fixed, then the widest content, then shares489 var widths = Vector<f32>::filled(0.0, columns);490 var used: f32 = g.column_gap * (columns - 1) to f32;491 var fractions: f32 = 0.0;492 for ([..columns]) |c| {493 match (g.track(c)) {494 ::Fixed |w| {495 widths.set(c, w);496 used += w;497 }498 ::Content {499 var widest: f32 = 0.0;500 var k = c;501 while (k < count) {502 widest = max(503 widest,504 arrange.lay_out(k, Limits { .max_width = inner.max_width }).width505 );506 k += columns;507 }508 widths.set(c, widest);509 used += widest;510 }511 ::Fraction |f| {512 fractions += f;513 }514 }515 }516 const left = if (inner.max_width < UNBOUNDED) max(inner.max_width - used, 0.0) else 0.0;517 for ([..columns]) |c| {518 if (g.track(c) is ::Fraction |f|) {519 widths.set(c, if (fractions > 0.0) left * f / fractions else 0.0);520 }521 }522 // rows as tall as their tallest child523 var heights = Vector<f32>::filled(0.0, columns);524 var y = g.padding.top;525 var row: u64 = 0;526 while (row * columns < count) {527 var tallest: f32 = 0.0;528 for ([..columns]) |c| {529 const k = row * columns + c;530 if (k < count) {531 const w = widths.get(c);532 const size = arrange.lay_out(k, Limits { .min_width = w, .max_width = w });533 heights.set(c, size.height);534 tallest = max(tallest, size.height);535 }536 }537 var x = g.padding.start;538 for (widths.view(), heights.view(), [..columns]) |w, height, c| {539 const k = row * columns + c;540 if (k < count) {541 arrange.place(k, Point { .x = x, .y = y + g.align.offset(tallest - height) });542 }543 x += w + g.column_gap;544 }545 y += tallest + g.row_gap;546 row += 1;547 }548 if (row > 0) {549 y -= g.row_gap;550 }551 var total = g.column_gap * (columns - 1) to f32;552 for (widths.view()) |w| {553 total += w;554 }555 return Extent {556 .width = max(total, inner.min_width) + g.padding.start + g.padding.end,557 .height = max(y - g.padding.top, inner.min_height) + g.padding.top + g.padding.bottom558 };559}560561pub fn children(self w: &Wrap) -> &[Element] => &w.children;562pub fn children(self g: &Grid) -> &[Element] => &g.children;563pub fn sizing(self w: &Wrap) -> Sizing => w.sizing;564pub fn sizing(self g: &Grid) -> Sizing => g.sizing;565pub fn clips(self w: &Wrap) -> bool => w.clip_radii is ::Some;566pub fn clips(self g: &Grid) -> bool => g.clip_radii is ::Some;567pub fn clip_radii(self w: &Wrap) -> Radii => radii_of(&w.clip_radii);568pub fn clip_radii(self g: &Grid) -> Radii => radii_of(&g.clip_radii);569570exp fn renew(self w: &var Wrap, next: Wrap) -> Change {571 w = move next;572 return Change::Layout;573}574575exp fn renew(self g: &var Grid, next: Grid) -> Change {576 g = move next;577 return Change::Layout;578}579580exp fn paint(self w: &Wrap, look: &Look, paint: &var Paint) {581 paint_box(&look, &var paint, w.background, w.border, w.radius);582}583584exp fn paint(self g: &Grid, look: &Look, paint: &var Paint) {585 paint_box(&look, &var paint, g.background, g.border, g.radius);586}