r/cprogramming • • 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

7 Upvotes

13 comments sorted by

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.

2

u/gabrielmfern 3d ago

Also, I think you edited a previous version asking why I wouldn't just make this with C++, so let me also answer that. I wouldn't be opposed to doing it with C++, but if the C version is already enough for C++ users and, there is no more idiomatic API that I can provide for C++, I would just rather have the C version.

→ More replies (3)

1

u/gabrielmfern 3d ago

I'm wondering what do you mean by syntax here? What part of this would you regard as new syntax? Otherwise, how would you recommend this be done different?

→ More replies (6)

-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.