Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e57179742 | ||
|
|
877047c09d | ||
|
|
78b92fded8 | ||
|
|
1c20828e69 | ||
|
|
f2416fd056 |
@@ -0,0 +1,50 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
unix:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- { name: linux gcc, runner: ubuntu-24.04, cc: gcc }
|
||||
- { name: linux clang, runner: ubuntu-24.04, cc: clang }
|
||||
- { name: macos clang, runner: macos-15, cc: clang }
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Float coordinates
|
||||
run: |
|
||||
${{ matrix.cc }} -std=c99 -Wall -Wextra -Werror -DLAY_FLOAT=1 -o test_float test_onlay.c onlay.c
|
||||
./test_float
|
||||
- name: int16_t coordinates
|
||||
run: |
|
||||
${{ matrix.cc }} -std=c99 -Wall -Wextra -Werror -o test_int test_onlay.c onlay.c
|
||||
./test_int
|
||||
|
||||
windows:
|
||||
name: windows msvc
|
||||
runs-on: windows-2025
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Float and int16_t coordinates
|
||||
shell: cmd
|
||||
run: |
|
||||
for /f "usebackq delims=" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath`) do call "%%i\VC\Auxiliary\Build\vcvars64.bat"
|
||||
cl /nologo /W4 /WX /DLAY_FLOAT=1 /Fe:test_float.exe test_onlay.c onlay.c || exit /b 1
|
||||
test_float.exe || exit /b 1
|
||||
cl /nologo /W4 /WX /Fe:test_int.exe test_onlay.c onlay.c || exit /b 1
|
||||
test_int.exe
|
||||
@@ -1,5 +1,7 @@
|
||||
# Onlay
|
||||
|
||||

|
||||
|
||||
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.
|
||||
@@ -31,8 +33,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.
|
||||
`#define LAY_FLOAT 1` for float coordinates instead of `int16_t`.
|
||||
Single header. `#define LAY_IMPLEMENTATION` in exactly one translation unit of
|
||||
your program. `#define LAY_FLOAT 1` for float coordinates instead of `int16_t`.
|
||||
|
||||
## What is different from randrew/layout
|
||||
|
||||
@@ -53,16 +55,14 @@ 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 it is counted when a container is sized to its contents — otherwise a
|
||||
container came out exactly one gap per child too short.
|
||||
and counts when a container is sized to its contents — otherwise a container
|
||||
came out 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 when that item is sized to
|
||||
its contents.
|
||||
|
||||
Every change is marked `// ONLAY:` in the source with the reason next to it.
|
||||
rect an item lays its children into, and is added on both sides when that item
|
||||
is sized to its contents.
|
||||
|
||||
## Tests
|
||||
|
||||
@@ -72,10 +72,20 @@ 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 && ./onlaytest
|
||||
cc -DLAY_FLOAT=1 -o onlaytest test_onlay.c onlay.c && ./onlaytest
|
||||
```
|
||||
|
||||
## Licence
|
||||
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
|
||||
|
||||
MIT, as upstream. See `LICENSE` — the original copyright is Andrew Richards's
|
||||
and is unchanged.
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
+7
-7
@@ -114,9 +114,9 @@ test_weighted_columns(void)
|
||||
for (i = 0; i < n; i++) {
|
||||
lay_vec4 r = lay_get_rect(&ctx, kid[i]);
|
||||
char label[32];
|
||||
sprintf(label, "%.0f%% width", (double)(cols[i].w * 100.0f));
|
||||
snprintf(label, sizeof label, "%.0f%% width", (double)(cols[i].w * 100.0f));
|
||||
check(label, (int)r.v[2], cols[i].expect_w);
|
||||
sprintf(label, "%.0f%% x", (double)(cols[i].w * 100.0f));
|
||||
snprintf(label, sizeof label, "%.0f%% x", (double)(cols[i].w * 100.0f));
|
||||
check(label, (int)r.v[0], cols[i].expect_x);
|
||||
}
|
||||
lay_destroy_context(&ctx);
|
||||
@@ -183,16 +183,16 @@ test_template_rows(void)
|
||||
lay_reserve_items_capacity(&ctx, 32);
|
||||
|
||||
page = lay_item(&ctx);
|
||||
lay_set_size_xy(&ctx, page, W, 200);
|
||||
lay_set_size_xy(&ctx, page, (lay_scalar)W, 200);
|
||||
lay_set_contain(&ctx, page, LAY_COLUMN | LAY_START);
|
||||
lay_set_gap(&ctx, page, ROW_GAP2);
|
||||
lay_set_gap(&ctx, page, (lay_scalar)ROW_GAP2);
|
||||
|
||||
for (r = 0; r < ROWS; r++) {
|
||||
row = lay_item(&ctx);
|
||||
lay_set_behave(&ctx, row, LAY_HFILL);
|
||||
lay_set_size_xy(&ctx, row, 0, 30);
|
||||
lay_set_contain(&ctx, row, LAY_ROW | LAY_START);
|
||||
lay_set_gap(&ctx, row, COL_GAP);
|
||||
lay_set_gap(&ctx, row, (lay_scalar)COL_GAP);
|
||||
lay_insert(&ctx, page, row);
|
||||
|
||||
for (c = 0; c < 3; c++) {
|
||||
@@ -213,9 +213,9 @@ test_template_rows(void)
|
||||
for (c = 0; c < 3; c++) {
|
||||
lay_vec4 q = lay_get_rect(&ctx, cell[r][c]);
|
||||
char label[40];
|
||||
sprintf(label, "row %d col %d x", r, c);
|
||||
snprintf(label, sizeof label, "row %d col %d x", r, c);
|
||||
check(label, (int)q.v[0], (int)want_x[c]);
|
||||
sprintf(label, "row %d col %d w", r, c);
|
||||
snprintf(label, sizeof label, "row %d col %d w", r, c);
|
||||
check(label, (int)q.v[2], (int)want_w[c]);
|
||||
}
|
||||
lay_destroy_context(&ctx);
|
||||
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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')
|
||||
Reference in New Issue
Block a user