1 Commits
Author SHA1 Message Date
Roman ChojnackiandClaude Opus 5.5 b9c932473f CI: the tests in both coordinate types, on GCC, Clang and MSVC
Every job builds test_onlay.c with warnings as errors, once with float
coordinates and once with int16_t, and runs it. The test now uses snprintf
and casts what it passes as lay_scalar, which MSVC's /W4 asked for.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-23 10:01:09 +02:00
4 changed files with 10 additions and 188 deletions
+10 -20
View File
@@ -1,7 +1,5 @@
# Onlay
![An app layout reflowing as its window and sidebar change width](docs/demo.gif)
A 2D layout engine in C99. It computes rectangles and does nothing else — it
draws nothing, owns no widgets, handles no input, and knows nothing about the
program using it.
@@ -33,8 +31,8 @@ lay_vec4 r = lay_get_rect(&ctx, row); /* r.v[0..3] is x, y, w, h */
lay_destroy_context(&ctx);
```
Single header. `#define LAY_IMPLEMENTATION` in exactly one translation unit of
your program. `#define LAY_FLOAT 1` for float coordinates instead of `int16_t`.
Single header. `#define LAY_IMPLEMENTATION` in exactly one translation unit.
`#define LAY_FLOAT 1` for float coordinates instead of `int16_t`.
## What is different from randrew/layout
@@ -55,14 +53,16 @@ fraction.
**Gaps.** `lay_set_gap(ctx, item, n)` puts space between an item's children
without putting it outside them, so a row of three has two gaps and no leading
or trailing one. It applies down a wrapped row's lines as well as along them,
and counts when a container is sized to its contents — otherwise a container
came out one gap per child too short.
and it is counted when a container is sized to its contents — otherwise a
container came out exactly one gap per child too short.
**Padding**, from [randrew/layout#23](https://github.com/randrew/layout/pull/23)
by [codecat](https://github.com/codecat), ported onto the struct rect. A margin
is space outside an item and a padding is space inside it: a padding insets the
rect an item lays its children into, and is added on both sides when that item
is sized to its contents.
rect an item lays its children into, and is added when that item is sized to
its contents.
Every change is marked `// ONLAY:` in the source with the reason next to it.
## Tests
@@ -72,20 +72,10 @@ weighted columns, floors that grow, template rows, and padding against margin.
Upstream's own `test_layout.c` is not carried: it assumes the vector rect.
```
cc -DLAY_FLOAT=1 -o onlaytest test_onlay.c onlay.c && ./onlaytest
cc -DLAY_FLOAT=1 -o onlaytest test_onlay.c && ./onlaytest
```
CI runs it with float and `int16_t` coordinates on GCC, Clang and MSVC, warnings
as errors.
The animation above is Onlay's own output: `tools/demo.c` lays the same app out
at every frame and `tools/demo.py` draws the rectangles it prints (Pillow).
```
cc -DLAY_FLOAT=1 -o demo tools/demo.c onlay.c -lm && ./demo | python tools/demo.py
```
## License
## Licence
MIT, as upstream. See `LICENSE` — the original copyright is Andrew Richards's
and is unchanged.
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

-107
View File
@@ -1,107 +0,0 @@
#include <math.h>
#include <stdio.h>
#include "../onlay.h"
#define FRAMES 90
#define CANVAS_W 800
#define CANVAS_H 450
#define PI 3.14159265f
enum { WINDOW, BAR, LOGO, TITLE, PILL, SIDEBAR, ENTRY, CONTENT, PANEL, CHIP, DOCK, KEY };
static lay_context ctx;
static int kind[512], tint[512];
static lay_id add(lay_id parent, int k, int t, lay_scalar w, lay_scalar h,
uint32_t contain, uint32_t behave)
{
lay_id it = lay_item(&ctx);
kind[it] = k;
tint[it] = t;
lay_set_size_xy(&ctx, it, w, h);
lay_set_contain(&ctx, it, contain);
lay_set_behave(&ctx, it, behave);
if (parent != LAY_INVALID_ID) lay_insert(&ctx, parent, it);
return it;
}
static float wave(float t, float cycles)
{
return 0.5f - 0.5f * cosf(2.0f * PI * cycles * t);
}
static void frame(int f)
{
static const lay_scalar chip_w[] = { 64, 92, 48, 118, 76, 56, 104, 70,
88, 52, 124, 60, 96, 72, 58, 110 };
static const lay_scalar entry_h[] = { 26, 26, 36, 26, 26, 36, 26, 26 };
float t = (float)f / FRAMES;
lay_scalar win_w = 600.0f + 160.0f * wave(t, 1.0f);
lay_scalar side_w = 120.0f + 80.0f * wave(t + 0.25f, 2.0f);
lay_id root, bar, body, side, content, head, grid, dock;
lay_id i;
int k;
lay_reset_context(&ctx);
root = add(LAY_INVALID_ID, WINDOW, 0, win_w, 410, LAY_COLUMN, 0);
lay_set_paddings_ltrb(&ctx, root, 12, 12, 12, 12);
lay_set_gap(&ctx, root, 10);
bar = add(root, BAR, 0, 0, 40, LAY_ROW, LAY_HFILL);
lay_set_paddings_ltrb(&ctx, bar, 8, 8, 8, 8);
lay_set_gap(&ctx, bar, 8);
add(bar, LOGO, 0, 24, 24, 0, 0);
add(bar, TITLE, 0, 0, 24, 0, LAY_HFILL);
for (k = 0; k < 3; k++) add(bar, PILL, k, 52, 24, 0, 0);
body = add(root, 0, 0, 0, 0, LAY_ROW, LAY_FILL);
lay_set_gap(&ctx, body, 10);
side = add(body, SIDEBAR, 0, side_w, 0, LAY_COLUMN | LAY_START, LAY_VFILL);
lay_set_paddings_ltrb(&ctx, side, 8, 8, 8, 8);
lay_set_gap(&ctx, side, 6);
for (k = 0; k < 8; k++) add(side, ENTRY, k == 2, 0, entry_h[k], 0, LAY_HFILL);
content = add(body, CONTENT, 0, 0, 0, LAY_COLUMN | LAY_START, LAY_FILL);
lay_set_paddings_ltrb(&ctx, content, 10, 10, 10, 10);
lay_set_gap(&ctx, content, 10);
head = add(content, 0, 0, 0, 64, LAY_ROW, LAY_HFILL);
lay_set_gap(&ctx, head, 10);
i = add(head, PANEL, 0, 0, 0, 0, LAY_FILL);
lay_set_weight(&ctx, i, 2.0f);
add(head, PANEL, 1, 0, 0, 0, LAY_FILL);
grid = add(content, 0, 0, 0, 0, LAY_ROW | LAY_WRAP | LAY_START, LAY_HFILL);
lay_set_gap(&ctx, grid, 8);
for (k = 0; k < (int)(sizeof chip_w / sizeof chip_w[0]); k++)
add(grid, CHIP, k % 5, chip_w[k], 26, 0, 0);
dock = add(root, DOCK, 0, 0, 36, LAY_ROW, LAY_HFILL);
lay_set_paddings_ltrb(&ctx, dock, 6, 6, 6, 6);
lay_set_gap(&ctx, dock, 6);
for (k = 0; k < 7; k++) add(dock, KEY, k == 3, 32, 24, 0, 0);
lay_run_context(&ctx);
for (i = 0; i < lay_items_count(&ctx); i++) {
lay_vec4 r = lay_get_rect(&ctx, i);
if (i && !kind[i]) continue;
printf("%d %d %d %.2f %.2f %.2f %.2f\n", f, kind[i], tint[i],
r.v[0] + (CANVAS_W - win_w) / 2.0f, r.v[1] + (CANVAS_H - 410) / 2.0f,
r.v[2], r.v[3]);
}
}
int main(void)
{
int f;
lay_init_context(&ctx);
lay_reserve_items_capacity(&ctx, 128);
for (f = 0; f < FRAMES; f++) frame(f);
lay_destroy_context(&ctx);
return 0;
}
-61
View File
@@ -1,61 +0,0 @@
import sys
from collections import defaultdict
from PIL import Image, ImageDraw
W, H, SCALE = 800, 450, 2
BACK = (11, 16, 32)
ACCENTS = [(59, 130, 246), (139, 92, 246), (16, 185, 129), (245, 158, 11), (244, 63, 94)]
def mix(a, b, k):
return tuple(round(x + (y - x) * k) for x, y in zip(a, b))
SURFACE = (21, 31, 51)
RAISED = (31, 43, 69)
INDIGO = (99, 102, 241)
STYLE = {
0: lambda t: ((18, 26, 43), 14, (35, 48, 74)),
1: lambda t: (SURFACE, 10, None),
2: lambda t: (INDIGO, 7, None),
3: lambda t: (RAISED, 6, None),
4: lambda t: (mix(RAISED, ACCENTS[t], 0.35), 12, None),
5: lambda t: (SURFACE, 10, None),
6: lambda t: (mix(RAISED, INDIGO, 0.8) if t else RAISED, 6, None),
7: lambda t: (SURFACE, 10, None),
8: lambda t: (mix(RAISED, INDIGO, 0.25) if t == 0 else RAISED, 8, None),
9: lambda t: (mix(RAISED, ACCENTS[t], 0.45), 13, None),
10: lambda t: (SURFACE, 10, None),
11: lambda t: (mix(RAISED, INDIGO, 0.8) if t else RAISED, 7, None),
}
def draw(boxes):
img = Image.new('RGB', (W * SCALE, H * SCALE), BACK)
pen = ImageDraw.Draw(img)
for kind, tint, x, y, w, h in boxes:
fill, radius, outline = STYLE[kind](tint)
r = [x * SCALE, y * SCALE, (x + w) * SCALE - 1, (y + h) * SCALE - 1]
pen.rounded_rectangle(r, radius * SCALE, fill=fill,
outline=outline, width=SCALE if outline else 0)
return img.resize((W, H), Image.LANCZOS)
def main(out):
frames = defaultdict(list)
for line in sys.stdin:
f, kind, tint, x, y, w, h = line.split()
frames[int(f)].append((int(kind), int(tint), float(x), float(y), float(w), float(h)))
images = [draw(frames[f]) for f in sorted(frames)]
sample = Image.new('RGB', (W, H * 4))
for k in range(4):
sample.paste(images[k * len(images) // 4], (0, H * k))
palette = sample.quantize(128, method=Image.Quantize.MEDIANCUT)
gif = [im.quantize(palette=palette, dither=Image.Dither.NONE) for im in images]
gif[0].save(out, save_all=True, append_images=gif[1:], duration=50, loop=0,
optimize=True, disposal=1)
if __name__ == '__main__':
main(sys.argv[1] if len(sys.argv) > 1 else 'docs/demo.gif')