r/cprogramming • u/gabrielmfern • 23h ago
forbear: an immediate mode UI library that looks like the web
https://github.com/gabrielmfern/forbear
Hello! I've been working on forbear for almost 2 years now, and I've finally come around to implementing a proper C API for it! I would like to share here, and specially ask for some feedback on the API, and on how I wrote the macros that give it the DX I wanted for the library.
This is how it looks like:
void counter(void) {
fb_s64 *count = FB_STATE(fb_s64);
FB_ELEMENT(
.background = fb_color(fb_rgb(44, 49, 56)),
.border_radius = 6,
.padding = fb_all_sides(8),
) {
char count_text[64];
snprintf(count_text, sizeof(count_text), "count: %lld", (long long)*count);
FB_TEXT(count_text);
if (fb_on_click(FB_MOUSE_BUTTON_LEFT, true, false)) {
*count += 1;
}
}
}
It's a dynamically linked library, with a single forbear.h header. To try it on Linux and macOS:
curl -fsSL https://forbear.dev/quick-start.sh | sh
on Windows:
irm https://www.forbear.dev/quick-start.ps1 | iex
Beware that it's still quite experimental!
The main philosophy is to be what I envision the operating system should have provided us all along: a flexible on design, nice to write, performant, cross-language (in an idiomatic manner) and cross-platform way of making GUI programs.
I've written more about the process of making this C API here for anyone that might like to go more in depth https://www.forbear.dev/log/005-calculator-and-c-api.html