Files
Roman ChojnackiandClaude Opus 5.5 dffcce22d5 CSS: Reaktor's own engine replaces libcss, and a pass of simplifications
core/css/css.c reads the stylesheets once into typed values and cascades
them as CSS does: !important, then specificity, then source order. It takes
var(), color-mix(in srgb, ...), em and rem, calc() over pixels, the @media
features Reaktor answers, @import at the top of a sheet, :not(), :enabled,
attribute selectors read as classes, descendant and child combinators, and
escapes. Rules are bucketed by the last compound of their selector, so a
query checks only the rules that could match. cssflat.c, LCUI's libcss and
yutil, and the lcui submodule go.

Screenshots of every showcase tab, notepad and simple, light and dark, with
normal and more contrast, are byte-identical to the libcss build, with
Water.css and with Simple.css; --a11y-dump differs only in the a11y struct's
size. Three values change to what CSS says: an unset background is
transparent black, an unset border color is the text's, and Simple.css's
:enabled:hover no longer reaches a disabled button. Against libcss, loading
the sheets takes 0.35 ms instead of 1.24 and a first query 0.32 us instead
of 4.4. csstest covers the cascade, values, @media and hostile sheets: import
loops and fan-out, var() that grows or expands too often, deep calc(), a sheet
left open, a huge file. 200,000 mutated sheets ran clean under AddressSanitizer.

Simplifications: dead state (redraw_always, sw_noaa, the hover-gap pin, a
muted note, an overwritten slider range, the font-baking blocks of the SDL
renderer, the a11y change index); REAKTOR_LAY_FILL for FILL_X | FILL_Y; one
helper each for flag parsing, capture runs, timings, paths from the working
directory, array growth, focus moves, tooltips, button presses, a11y slot
probes, UIA fragments and test strings; launchtest's notef() and last_node();
the output folder set once in CMake and a write-if-changed function; one test
list, tools/ci/tests.txt, for every workflow, so basetest and imagetest run.

The showcase drops its relaunch button, an ask() button is as wide as its
label, and build.sh takes a bare target. The docs follow, and PERFORMANCE.md
and the chart carry a new pass of all seven benchmark arms on one machine.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-24 02:37:13 +02:00

368 lines
11 KiB
C

#include <stdio.h>
#include <string.h>
#include "layout.h"
#include "test.h"
#define ok test_eq
static struct nk_rect
R(float x, float y, float w, float h)
{
struct nk_rect r;
r.x = x; r.y = y; r.w = w; r.h = h;
return r;
}
static void
frame(reaktor_layout *l, float gap)
{
reaktor_box root = { 0 };
reaktor_box fixed = { 0 };
reaktor_box fill = { 0 };
root.dir = REAKTOR_LAY_ROW;
root.gap = gap;
root.flags = REAKTOR_LAY_FILL;
fixed.w = 100.0f;
fixed.h = 30.0f;
fill.h = 30.0f;
fill.flags = REAKTOR_LAY_FILL_X;
reaktor_layout_begin(l, R(10.0f, 20.0f, 400.0f, 200.0f));
reaktor_layout_open(l, 100u, &root);
reaktor_layout_leaf(l, 101u, &fixed);
fill.weight = 1.0f;
reaktor_layout_leaf(l, 102u, &fill);
fill.weight = 3.0f;
reaktor_layout_leaf(l, 103u, &fill);
reaktor_layout_close(l);
reaktor_layout_end(l);
}
static void
test_lag(reaktor_layout *l)
{
struct nk_rect r;
puts("a frame reads the frame before it");
ok("nothing is placed before the first layout runs",
reaktor_layout_rect(l, 101u, &r), 0);
frame(l, 0.0f);
ok("...and everything is, after it",
reaktor_layout_rect(l, 101u, &r), 1);
ok("a box nobody declared is still not placed",
reaktor_layout_rect(l, 999u, &r), 0);
}
static void
test_origin(reaktor_layout *l)
{
struct nk_rect root, first;
puts("");
puts("the tree's origin is added back");
ok("the root answers where it was put",
reaktor_layout_rect(l, 0u, &root) && root.x == 10.0f && root.y == 20.0f,
1);
ok("...at the size it was given",
root.w == 400.0f && root.h == 200.0f, 1);
reaktor_layout_rect(l, 101u, &first);
ok("the first child starts at the tree's left edge, not at zero",
first.x == 10.0f, 1);
ok("...and at its top, not floating in the middle", first.y == 20.0f, 1);
ok("a fixed box keeps the size it asked for",
first.w == 100.0f && first.h == 30.0f, 1);
}
static void
test_centring(reaktor_layout *l)
{
reaktor_box row = { 0 }, mid = { 0 }, top = { 0 };
struct nk_rect a, b;
puts("");
puts("centering is asked for, never assumed");
row.dir = REAKTOR_LAY_ROW;
row.flags = REAKTOR_LAY_FILL;
top.w = mid.w = 40.0f;
top.h = mid.h = 20.0f;
mid.flags = REAKTOR_LAY_CENTER_Y;
reaktor_layout_begin(l, R(0.0f, 0.0f, 200.0f, 100.0f));
reaktor_layout_open(l, 300u, &row);
reaktor_layout_leaf(l, 301u, &top);
reaktor_layout_leaf(l, 302u, &mid);
reaktor_layout_close(l);
reaktor_layout_end(l);
reaktor_layout_rect(l, 301u, &a);
reaktor_layout_rect(l, 302u, &b);
ok("a box that says nothing sits at the top", a.y == 0.0f, 1);
ok("one that asks to be centered is centered",
b.y == (100.0f - 20.0f) / 2.0f, 1);
}
static void
test_weight(reaktor_layout *l)
{
struct nk_rect a, b, c;
puts("");
puts("what is left over is split by weight");
reaktor_layout_rect(l, 101u, &a);
reaktor_layout_rect(l, 102u, &b);
reaktor_layout_rect(l, 103u, &c);
ok("the fixed box took none of it", a.w == 100.0f, 1);
ok("three parts to one, not one to one", c.w == 3.0f * b.w, 1);
ok("and between them they took the rest",
a.w + b.w + c.w == 400.0f, 1);
ok("laid end to end, in the order declared",
b.x == a.x + a.w && c.x == b.x + b.w, 1);
}
static void
test_gap(reaktor_layout *l)
{
struct nk_rect a, b, c;
puts("");
puts("a gap separates children and costs them the room");
frame(l, 12.0f);
reaktor_layout_rect(l, 101u, &a);
reaktor_layout_rect(l, 102u, &b);
reaktor_layout_rect(l, 103u, &c);
ok("the second box starts a gap after the first",
b.x == a.x + a.w + 12.0f, 1);
ok("and the third after the second",
c.x == b.x + b.w + 12.0f, 1);
ok("two gaps came out of the fillers, not out of the row",
a.w + b.w + c.w == 400.0f - 24.0f, 1);
ok("the fixed box is still fixed", a.w == 100.0f, 1);
}
static void
test_wrap(reaktor_layout *l)
{
reaktor_box row = { 0 }, cell = { 0 };
struct nk_rect r[7], box;
int i;
puts("");
puts("a wrapping row starts a second line, a gap below the first");
row.dir = REAKTOR_LAY_ROW;
row.w = 300.0f;
row.gap = 10.0f;
row.flags = REAKTOR_LAY_WRAP;
cell.w = 100.0f;
cell.h = 20.0f;
reaktor_layout_begin(l, R(0.0f, 0.0f, 300.0f, 300.0f));
reaktor_layout_open(l, 300u, &row);
for (i = 0; i < 7; i++) reaktor_layout_leaf(l, 301u + (unsigned)i, &cell);
reaktor_layout_close(l);
reaktor_layout_end(l);
for (i = 0; i < 7; i++) reaktor_layout_rect(l, 301u + (unsigned)i, &r[i]);
ok("two boxes to a line, a gap apart",
r[0].x == 0.0f && r[1].x == 110.0f, 1);
ok("the third went to the next line, back at the left",
r[2].x == 0.0f, 1);
ok("which sits a gap below the first, not against it",
r[2].y == r[0].y + 20.0f + 10.0f, 1);
ok("and the line after that is the same distance again",
r[4].y == r[2].y + 30.0f, 1);
ok("a short last line is still a line",
r[6].x == 0.0f && r[6].y == r[4].y + 30.0f, 1);
ok("the row is as tall as four lines and the three gaps between them",
reaktor_layout_rect(l, 300u, &box) && box.h == 4.0f * 20.0f + 30.0f, 1);
}
static void
test_nesting(reaktor_layout *l)
{
reaktor_box col = { 0 }, row = { 0 }, leaf = { 0 };
struct nk_rect outer, inner;
puts("");
puts("a box inside a box");
col.dir = REAKTOR_LAY_COLUMN;
col.flags = REAKTOR_LAY_FILL;
row.dir = REAKTOR_LAY_ROW;
row.h = 40.0f;
row.flags = REAKTOR_LAY_FILL_X;
row.ml = 8.0f;
leaf.w = 50.0f;
leaf.h = 20.0f;
reaktor_layout_begin(l, R(0.0f, 0.0f, 300.0f, 300.0f));
reaktor_layout_open(l, 200u, &col);
reaktor_layout_open(l, 201u, &row);
reaktor_layout_leaf(l, 202u, &leaf);
reaktor_layout_close(l);
reaktor_layout_close(l);
reaktor_layout_end(l);
ok("the inner row is inside the outer column",
reaktor_layout_rect(l, 201u, &outer) && outer.h == 40.0f, 1);
ok("its left margin moved it, and only it",
outer.x == 8.0f, 1);
ok("the leaf is inside the row",
reaktor_layout_rect(l, 202u, &inner) &&
inner.x >= outer.x && inner.y >= outer.y, 1);
ok("last frame's boxes are gone, not merged with these",
reaktor_layout_rect(l, 101u, &inner), 0);
}
static void
test_limits(reaktor_layout *l)
{
reaktor_box b = { 0 };
int i;
puts("");
puts("two trees at absolute positions, each minding its own business");
{
reaktor_box a = { 0 }, b2 = { 0 }, kid = { 0 };
struct nk_rect ra, rb;
a.dir = REAKTOR_LAY_COLUMN; a.w = 100.0f; a.ml = 30.0f; a.mt = 40.0f;
b2.dir = REAKTOR_LAY_COLUMN; b2.w = 100.0f; b2.ml = 30.0f; b2.mt = 200.0f;
kid.h = 25.0f; kid.flags = REAKTOR_LAY_FILL_X;
reaktor_layout_begin(l, R(0.0f, 0.0f, 400.0f, 400.0f));
reaktor_layout_open(l, 600u, &a);
reaktor_layout_leaf(l, 601u, &kid);
reaktor_layout_close(l);
reaktor_layout_open(l, 610u, &b2);
reaktor_layout_leaf(l, 611u, &kid);
reaktor_layout_close(l);
reaktor_layout_end(l);
reaktor_layout_rect(l, 600u, &ra);
reaktor_layout_rect(l, 610u, &rb);
ok("the first is where its margins put it",
(int)ra.x == 30 && (int)ra.y == 40, 1);
ok("the second is where ITS margins put it, not after the first",
(int)rb.x == 30 && (int)rb.y == 200, 1);
ok("and each is as tall as its own child",
(int)ra.h == 25 && (int)rb.h == 25, 1);
}
puts("");
puts("a container with no height of its own");
{
reaktor_box col = { 0 }, a = { 0 }, c = { 0 };
struct nk_rect r;
col.dir = REAKTOR_LAY_COLUMN;
col.w = 200.0f;
col.gap = 7.0f;
a.h = 30.0f; a.flags = REAKTOR_LAY_FILL_X;
c.h = 19.0f; c.flags = REAKTOR_LAY_FILL_X;
reaktor_layout_begin(l, R(0.0f, 0.0f, 400.0f, 400.0f));
reaktor_layout_open(l, 500u, &col);
reaktor_layout_leaf(l, 501u, &a);
reaktor_layout_leaf(l, 502u, &c);
reaktor_layout_close(l);
reaktor_layout_end(l);
ok("it is as tall as what is in it, gap included",
reaktor_layout_rect(l, 500u, &r) ? (int)r.h : -1, 30 + 7 + 19);
ok("...and as wide as it asked to be",
reaktor_layout_rect(l, 500u, &r) ? (int)r.w : -1, 200);
}
puts("");
puts("more boxes than there is room for");
b.w = 4.0f;
b.h = 4.0f;
reaktor_layout_begin(l, R(0.0f, 0.0f, 100.0f, 100.0f));
for (i = 0; i < REAKTOR_LAY_MAX + 40; i++)
reaktor_layout_leaf(l, (unsigned)(1000 + i), &b);
reaktor_layout_end(l);
{
struct nk_rect r;
ok("the overflow is counted", l->overflow_boxes, 41);
ok("the boxes that fitted still came out",
reaktor_layout_rect(l, 1000u, &r), 1);
ok("the ones past the end did not",
reaktor_layout_rect(l, (unsigned)(1000 + REAKTOR_LAY_MAX + 10), &r), 0);
}
puts("");
puts("nested deeper than the stack");
{
struct nk_rect r;
int i;
b.w = 10.0f; b.h = 10.0f;
reaktor_layout_begin(l, R(0.0f, 0.0f, 100.0f, 100.0f));
for (i = 0; i < REAKTOR_LAY_DEPTH + 5; i++)
reaktor_layout_open(l, (unsigned)(3000 + i), &b);
ok("an open past the stack still counts",
l->depth, REAKTOR_LAY_DEPTH + 6);
for (i = 0; i < REAKTOR_LAY_DEPTH + 5; i++)
reaktor_layout_close(l);
reaktor_layout_leaf(l, 3999u, &b);
reaktor_layout_end(l);
ok("every open was balanced by its close", l->depth, 1);
ok("...so what came after is still placed",
reaktor_layout_rect(l, 3999u, &r), 1);
}
puts("");
puts("a container that overflowed the arena");
{
struct nk_rect r;
int i;
reaktor_layout_begin(l, R(0.0f, 0.0f, 100.0f, 100.0f));
for (i = 0; i < REAKTOR_LAY_MAX; i++)
reaktor_layout_leaf(l, (unsigned)(4000 + i), &b);
reaktor_layout_open(l, 4900u, &b);
reaktor_layout_leaf(l, 4901u, &b);
reaktor_layout_close(l);
reaktor_layout_end(l);
ok("it survived being asked", l->depth, 1);
ok("the lost container is not placed",
reaktor_layout_rect(l, 4900u, &r), 0);
}
puts("");
puts("closing more than was opened");
reaktor_layout_begin(l, R(0.0f, 0.0f, 100.0f, 100.0f));
reaktor_layout_close(l);
reaktor_layout_close(l);
reaktor_layout_leaf(l, 2000u, &b);
reaktor_layout_end(l);
{
struct nk_rect r;
ok("the root survived it", reaktor_layout_rect(l, 2000u, &r), 1);
}
}
int
main(void)
{
static reaktor_layout l;
memset(&l, 0, sizeof(l));
test_lag(&l);
test_origin(&l);
test_weight(&l);
test_gap(&l);
test_wrap(&l);
test_nesting(&l);
test_centring(&l);
test_limits(&l);
reaktor_layout_free(&l);
return test_done("layout");
}