gitmyhub

clay

C ★ 17k updated 1mo ago

High performance UI layout library in C.

Single-header C library that calculates UI layouts using a flexbox-like model and outputs a renderer-agnostic list of drawing commands, no dependencies, no malloc at runtime, compiles to 15 KB WebAssembly.

CWebAssemblyclangsetup: moderatecomplexity 3/5

Clay, short for C Layout, is a high-performance library for building two-dimensional user interface layouts in the C programming language. A UI layout library is the piece of code that figures out where on the screen each button, panel, image and piece of text should sit, and how those positions should change when the window resizes or content wraps. Clay handles that calculation and aims to do it in microseconds.

Under the hood, Clay uses a flex-box-like layout model, similar in spirit to how websites arrange elements, with support for text wrapping, scrolling containers, aspect-ratio scaling and a transition API for animating layouts. You describe a UI in C using nested macros that look a bit like a React component tree, and Clay produces a sorted list of rendering primitives such as rectangles and text. Clay does not draw anything itself; it is renderer-agnostic, so you take that list and hand it to whichever renderer you already have, whether that is a 2D engine, a 3D engine, or even HTML. The whole library is one header file of around 4,800 lines with no dependencies at all, not even the C standard library, and it manages memory through a static arena that you provide, with no calls to malloc or free at runtime. It can also be compiled with clang to a 15 KB WebAssembly file that runs in the browser.

You would actually use Clay if you are building a native desktop, game or embedded UI in C or C++ and want a layout engine that is fast, predictable, and free of hidden allocations. The full README is longer than what was provided.

Where it fits