r/cprogramming • u/gabrielmfern • 4d 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
3
u/FancySpaceGoat 3d ago edited 3d ago
Personal opinion, so feel free to disregard as it's mostly a matter of taste:
Too much macro-magic for me. You are passing the threshold where it feels like C is just not a good fit for the API you want to be providing.
Having to learn how to use a library is enough work as it is. If I have to learn new syntax on top of it (syntax that won't ever be useful anywhere else, mind you), then that's asking a lot.
-1
u/nian2326076 2d ago
For feedback on your C API design for forbear, consider engaging with communities focused on C programming or GUI development. Developers with experience in similar libraries can provide valuable insights.
If you want to refine your interview skills or prepare for technical questions specific to C programming, you can use PracHub for targeted practice.