
Libui: a portable GUI library for C - axiomdata316
https://github.com/andlabs/libui
======
saagarjha
A lot of the comments have mentioned the lack of documentation this project
has, and then they've received replies of the form "you should submit a pull
request, the author doesn't owe you anything". Here's my take on it: I don't
owe the author anything either. But if I'm considering which project to use,
I'd probably choose the one with documentation, since it signals better goals
and higher code quality. It's absurd to think that I should just pick up a
random project and be obligated to make it better, and that I should be called
out for giving advice on how I think a project can be improved. This isn't to
say that I've never submitted an unsolicited pull request to a project to get
it improved–on the contrary, this is something I do very often–but I do this
because I feel that the project has something of value and can be improved. I
shouldn't be forced to make a contribution to a project if I just don't think
it's doing everything right. (This comment isn't mean to be aimed at this
project in particular, by the way, it's just a general response to all the
people in this thread.)

~~~
FraKtus
I work for many years with FFMpeg that has no documentation and no code
sample, and it's okay for me. I prefer that the developers keep their time
optimizing the code rather than writing documentation that needs to be
maintained and become a problem when not in sync. My state of mind is that if
it's open source then just read the code.

~~~
akvadrako
FFMpeg is not some random project - it's irreplaceable and very popular.

------
discussedbefore
Another option (C++ LGPL w/ Lua,JS bindings):
[https://github.com/yue/yue](https://github.com/yue/yue)

Other projects building on andlabs/ui and [https://github.com/parro-it/libui-
node](https://github.com/parro-it/libui-node):

[https://github.com/mimecorg/vuido](https://github.com/mimecorg/vuido) \- vue

[https://github.com/kusti8/proton-native](https://github.com/kusti8/proton-
native) \- react

~~~
erlend_sh
Another one:

[https://github.com/zserge/webview](https://github.com/zserge/webview)

~~~
diego_moita
This is very, very interesting.

It allows to build the whole UI using HTML, CSS and JS and leverage it with
all the tooling and libraries already built for the web.

------
fernly
I might be interested, esp. since it claims Python bindings, but documentation
appears to be nil. Seriously, nonexistent. A GUI lib surely has an API complex
enough to require _some_ kind of reference docs, at least. But the contents of
the doc folder[1] are just a joke, most of the files contain single lines, and
the .md files are fragments with many "TODO" notes.

So OK maybe I can get some insight reading the code. The "common" folder seems
a good place to start[2]. Um, no. Short, generic uninformative filenames
("matrix.c", "control.c") and very few introductory comments. The opposite of
"literate programming".

[1]
[https://github.com/andlabs/libui/blob/master/doc/main](https://github.com/andlabs/libui/blob/master/doc/main)

[2]
[https://github.com/andlabs/libui/tree/master/common](https://github.com/andlabs/libui/tree/master/common)

~~~
vortico
The entire API is 1100 lines.
[https://github.com/andlabs/libui/blob/master/ui.h](https://github.com/andlabs/libui/blob/master/ui.h)
You don't need a separate documentation for that. In fact, most functions have
sufficient docstrings if their names don't fully describe their function.

Your hostility toward an MIT licensed project is what is ruining the open-
contribution model of developing software. In 2005, it was "Hey, this looks
great, want me to write up a documentation?" Now it's "Not up to my standards,
it's a joke." Absolutely disgusting behavior that is becoming gradually more
common.

~~~
geofft
I got to the first function in the API:

    
    
        _UI_EXTERN const char *uiInit(uiInitOptions *options);
    

uiInitOptions is a struct that contains a single size_t Size. What's it the
size of? Can I pass a NULL pointer? Does this function take ownership of the
pointer, and if so, does it need to be allocated with malloc? If not, when can
I free it - can I use a pointer onto my stack? Why does it take a non-const
pointer - does the size (of whatever) change? What's the string that's
returned? What's the lifetime of the string that's returned? Can another
thread call uiInit, or does that break everything horribly? Or does it only
break horribly if you don't copy the returned string first?

You don't _need_ documentation, sure. But it's a good way to get users of the
library that make wild assumptions so that you can no longer make reasonable
upgrades to the library without breaking them, and that makes nobody (neither
the library author, who wants people to use it, nor the users themselves, nor
end users) happy.

~~~
vortico
Great! If you can answer those questions, send a pull request to add it to the
docstring. You've spent some time thinking of those questions, so you'd write
a very complete docstring. Complaining will not solve the issue. Sending a PR
will solve it. This is what I was saying above. I am attempting to orient
people into the "open-contribution" community, where we share the opinion that
free software is NOT free to produce. Someone must do the things you are
suggesting at some point if they are to exist, and who better than yourself?

With regards to your last paragraph, a common misconception is that a
"payment" of open-source software is number of users. That would be true for
proprietary software where payment = number of users * price, but for open-
source software the developers couldn't care less (except maybe superficially,
but this mental reward is insignificant for the long-term motivation for a
project). They want to make a library that solves their own problems and is
stable. They allow people to help improve the library in exchange for
releasing it for their unrestricted use. Otherwise, there is little motivation
to release it. When I find a new library that solves a problem in an
interesting way, I learn using whatever is available (existing documentation,
headers, source, and examples) and write down the difficult parts. The price I
pay for using the software is to send my notes back to the developers in a
polished, organized form that is consistent with their existing documentation.
Then, no one needs to repeat what I've done in the future.

~~~
rendaw
There are times when people report an issue to an open source project and the
response is "You should fix it yourself".

Open source contribution happens when the contributor has invested themself in
the project and wants to maintain the relationship. Perhaps the requester is
evaluating multiple packages and wants to inform the project why they're going
with another one instead. Maybe they got to the point in their evaluation that
they're ready to commit only to discover a critical issue hidden in the (lack
of) documentation.

It also comes off as hostile since implies that the requester's time is less
valuable than the project maintainer's. Perhaps the requester is busy
contributing to other open source projects. Perhaps the requester only has
time right now to use the software, but has the goal of contributing in the
future.

The project's maintainer's responsibility is maintaining the software,
presumably because of some personal interest. Reporting and even expecting
them to fix issues is absolutely normal.

~~~
jancsika
I find if you maintain an open source project _as if_ your users were
customers, things tend to go pretty smooth.

~~~
edflsafoiewq
Smooth for whom? Not the one beholden to a bunch of non-paying "customers" I'd
wager.

~~~
jancsika
What is the bad thing you posit would happen when a bunch of non-paying
"customers" are treated in a professional manner?

------
wruza
For those interested, it seems to use classical grids to align elements, and
it is the only way to do it, since there is no mention on coordinates (if I
read it thoroughly enough).

    
    
      void uiGridInsertAt(uiGrid *g, uiControl *c, uiControl *existing, uiAt at, int xspan, int yspan, int hexpand, uiAlign halign, int vexpand, uiAlign valign);
    

The overall feeling is that it’s a [small] subset of GTK. I think they try to
fill an empty cross-platform niche between lightweight and native-looking Tk
and heavies like GTK and Qt. Doc could explain how is that better than Tk
itself, but it has to be written. The other possibility is that they do it for
fun and distinctive use cases are shallow – this is often seen in second order
ui kits.

~~~
jsd1982
I think the point of this library is more to offer a native wrapping UI
toolkit, including windows and Darwin, not necessarily a GTK subset, though
you probably meant that in terms of a subset of components rather than
implementation. This library has no renderer of its own and instead delegates
that away.

~~~
andlabs
Also the design of uiGrid was directly based on that of GtkGrid.

------
yani
I have arrived at this library yesterday while trying to do a cross platform
drag and drop app. I decided to go with x/screen package instead. The
documentation and lack of examples threw me off. If I have to read the source
in details to understand the app, I am better off writing the code x 3 with
native tools. The library is supposed to save time not steal from it. It is
wonderdul idea and I really like the name. I wish the author will make it more
dev and contributor friendly so we can take it off the ground.

~~~
andlabs
Not only does libui not yet do drag-and-drop, but drag-and-drop is
sufficiently complex enough that simple libraries are probably not going to
have this out of the box. That being said, I do plan on adding drag-and-drop
later.

------
dang
Discussed in 2016:
[https://news.ycombinator.com/item?id=11735393](https://news.ycombinator.com/item?id=11735393)

~~~
akkartik
Thanks! The first thing I did was to click on the 'past' link. But that was no
help here because the project has changed its title in between.

------
andlabs
Judging from the comments here, it appears I have completely failed at
properly getting across just how early this thing is in development. I've
since updated my README with a status report.

As for documentation, that is an active WIP. I have recently pinned down a
documentation style I like, and applied it to the newest APIs; all that's left
to do really is to spread it to the older APIs as well, and write a tool to
turn that documentation into HTML, because I have not found a documentation
tool that operates how I want it to.

~~~
andlabs
Oh, and one other thing I forgot to mention about documentation: the nature of
this project requires me to balance what I can get behaving identically across
platforms, what I can guarantee about behavior across platforms that must
differ, and what I am forced to leave as "implementation-defined" with no
further specification. If this was a non-native project like GTK+ and Qt, I
would be the one making these decisions, but in this case I am not, so I must
take a lot of factors into consideration when writing documentation. If you
read through the docs I did have on package ui, you'll find a lot of TODOs
about behavior.

------
ComodoHacker
The title should be "GUI library written in C", not "GUI library for C". There
are bindings for many languages. Also "libui was originally written as part of
my package ui for Go".

------
waynecochran
No Documentation? Documentation as an afterthought is not a good sign that
this was well thought out. How about a white paper? A tutorial? Just code?

~~~
vortico
Why would you need an entire whitepaper for wrapping OS widgets in a window?
The API is the documentation, and the example code gives you everything you
need to get going. Why would you need anything else?

~~~
pacaro
I’m mixed on whether api docs are needed, if you have them, they have to be
kept in sync, if you don’t then examples have to cover as much of the api
surface area as possible

Higher level docs would be nice though, goals and non-goals, high level
feature roadmap etc.

For example a cursory reading of the code suggests that there is no a11y
support, which is a deal breaker for some projects and not so important for
others, it would be nice to know where that sits on the roadmap

------
valera_rozuvan
I am surprised that ocornut/imgui [1] is not mentioned anywhere here. I used
it for an admin interface in a past project. The author makes it a point to do
work on the library based on donations (such as from Patreon, or PayPal). I
suppose that's the main reason the project is alive and kicking today.
OpenSource projects that are somehow financed - are in a much better situation
than projects done on a spare time budget.

\---

[1] [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui)

------
c-smile
Interesting, never saw such way of std::vector/std::map allocations:

    
    
        g->children = new std::vector<struct gridChild *>;
        g->indexof = new std::map<uiControl *, size_t>;
    

At least needless level of indirection ...

[https://github.com/andlabs/libui/blob/master/windows/grid.cp...](https://github.com/andlabs/libui/blob/master/windows/grid.cpp#L654)

~~~
pubby
There's a glaring memory leak if the second new throws.

------
hawski
Lately I was thinking about the Right Thing [0] of Native Crosspostform UI -
write a spare frontend for every platform and write the backend in something
portable. It could be a nice idea to come up with a document that would
describe every platform APIs for:

\- accessibility

\- localization

\- text rendering

\- input methods

\- widget toolkit

\- event loop

\- HIG

The document should also contain example code for something basic for every
platform. All this information could be then used to create the Right Thing
Toolkit.

Probably it should aim for the future. So only include APIs that most probably
would not be deprecated in 5 years. The platforms that I'm thinking about:
Android, Freedesktop/Unix-like, iOS, macOS and Windows.

I was wondering what is current situation with Windows. Is UWP the future or
will it be soon replaced by yet another API? Is side-loading currently as
effortless as running of Win32 apps? Or maybe Win32 is good enough, because
they will always support it?

[0] as opposed to the Worse is Better, because that's current situation and
it's not all that hard, use SDL or whatever and draw everything yourself.

~~~
chris_wot
I would love to see a boost library that does all of this :-)

------
nineteen999
One thing I have noticed about programmers in general, and this thread tended
to remind me of it, is that everyone has high expectations of other's
documentation, but when put on the spot, rarely meet those expectations
themselves.

At my most recent stint in a software company once of the most frequently
heard retorts about absent documentation was "the code _IS_ the
documentation".

Yet often these same people were the first to complain when another developer
or operations staff hadn't documented something and they'd got themselves into
a pickle trying to guess what was going on _rather than read the code
themselves._

Often the people most vocal about documentation are the worst at preparing it
themselves in my experience, a form of projection onto others. YMMV.

------
ktpsns
How does it compare to the good old (and mature)
[https://www.wxwidgets.org/](https://www.wxwidgets.org/) ? It is one of the
C++ toolkits which was famous for using native widgets.

------
rohan1024
The same library for Go
[https://github.com/andlabs/ui](https://github.com/andlabs/ui)

~~~
secure
Note that this is a Go wrapper around the C libui library, not a Go
implementation of it.

------
sametmax
I wish more projects lile this existed. Most of the tools don't need a huge UI
but something simple to install and well integrated is very much needed.

I like the toga project in python :
[https://pybee.org/project/projects/libraries/toga/](https://pybee.org/project/projects/libraries/toga/)

Tkinter is terribly integrated and QT hard to install so toga hits a sweet
spot. But it's very immature.

My favorite toolkit is still wx, however it just reached V3 compat so it's
considered unstable. You can pip install it (well, linux support is so so yet)
and all the UI looks native, including the file dialogs.

~~~
terminalcommand
I think GTK is an excellent library for C. If you use Glade and GTKBuilder,
you don't have to write the boilerplate either. You design you UI in Glade
visually, add your signals, set your id's etc. Then you save your design as an
XML file. In your C code you load that design with a simple function, then
assign the function names for the signals you've created in the visual
designer. Then you're all set to go.

The library (GTK) feels the same in every language, whether it's C, Python,
Go, etc.

I admit, it can be a hassle to get the library up and running, but once you do
that, it is surprisingly simple to code in.

~~~
rapsey
The UI's look like garbage on anything non linux though.

~~~
terminalcommand
Well, GIMP uses GTK and it looks really sharp on all platforms. It all comes
down to theming.

I wouldn't say it looks like garbage, but if you want a more native look and
feel you could certainly set it up in the settings.

(For example: you could look at
[https://stackoverflow.com/questions/37035936/how-to-get-
nati...](https://stackoverflow.com/questions/37035936/how-to-get-native-
windows-decorations-on-gtk3-on-windows-7-and-msys2) )

As a library purely coded in C, I still believe that GTK and GNOME lay a solid
foundation for easy development in C.

Before downvoting you might want to give GTK another chance. Even this libui
library uses GTK under the hood for linux.

------
mark_l_watson
Nice project and I like seeing the many language bindings other people have
contributed. I am definitely going to try this.

I did something a little similar about 20 years ago in my two McGraw Hill
books Portable GUI in C++. But Libui looks very much better.

------
oblio
Github badly needs a prototype/alpha/beta/rc/production tag, like Sourceforge
and Pypi have.

It should be set to prototype by default.

~~~
andlabs
It does this for "Releases", but not for entire projects. And if someone
starts browsing files, then you'd have to worry about what that status is at
the commit for the file you are reading; this will vary based on how the
projects organize their git branches, tags, and releases.

------
jxub
One of the funniest commit messages I've ever seen:
[https://github.com/andlabs/libui/commit/422ec83162de7b9689b7...](https://github.com/andlabs/libui/commit/422ec83162de7b9689b7fb066404119a020f1b0e)

------
rajeevk
How does it compare to wxwidgets? I have used neither of these. I need a
platform independent(mac/Linux/windows) GUI for a upcoming project and I was
comparing Qt and wxwidgets and decided to use wxwidgets. Now wonder whether I
can use libui instead of wxwidgets?

~~~
jhasse
wxWidgets is a lot more mature. If you are fine with a C++ API instead of
plain C, I'd go with wxWidgets.

------
criddell
I was just reading ui.h. It's been quite a while since I wrote anything in C
so this might be a dumb question, but does C not have namespaces? How can
major projects use third party libraries and not have to deal with naming
collisions?

~~~
detaro
No, C doesn't have namespaces as a language feature. Guess why all the
function names start with _ui_.

~~~
criddell
But ui is a super common prefix. I've seen it used for user-interface variable
and function names and also in Microsoft-world in projects that abuse
Hungarian notation (unsigned int).

Clearly it must not be a problem, I just don't understand how people work
around it.

~~~
yawaramin
C compilation units have a static section which isn't exposed to the outside
world (i.e. other compilation units):
[https://www.codingfriends.com/index.php/2010/07/13/static-
in...](https://www.codingfriends.com/index.php/2010/07/13/static-in-c-hide-
that-function/)

------
iveqy
So what I can understand this is "just" a wrapper for existing GUI frameworks?
Why is this better than just using GTK that already is portable?

~~~
guessmyname
Writing a GTK UI for macOS, for example, will make the interface look very
different to the look and feel that the operation system is offering. If you
use a library like libui, the ABI is the same for every major operating
system, but the result matches what the OS is using. If you don't care about
the look and feel of the interface then I guess this library is not for you.
As someone who is responsible for maintaining the UI of a big application for
three major systems (macOS, Windows and Linux) a library like this would have
been a blessing years ago before the project started.

~~~
wruza
Apart from being curious why your comment is downvoted, did you consider using
e.g. wx or tk? They both had their disadvantages last time we checked (~10
years ago), but it would be nice to hear someone’s opinion on them in this
thread.

------
unwind
On Unix this seems to map to GTK+, so I guess it's a bit like wxWidgets then?

Interesting but since GTK+ itself is cross-platforms it feels a bit heavy to
wrap it.

~~~
Vogtinator
> GTK+ itself is cross-platforms

Only in theory. It does not look and behave even close to native anywhere
except on GNOME 3 with default settings.

~~~
unwind
Uh, no, it's not aiming to be a "native-looking" interface, it's GTK+ on all
supported platforms.

------
signa11
nuklear
([https://github.com/vurtun/nuklear](https://github.com/vurtun/nuklear)) is
also pretty cool. and it seems to have bindings for various languages (besides
C) f.e. go, rust, lua etc.

~~~
CyberDildonics
Have you ever actually used nuklear? It is awkward and confusing to even
change an example and have it work, let alone creating what you really want.
There is no documentation that will help you either.

------
chris_wot
Is there a portable C++ GUI library though?

~~~
jcelerier
Qt is the most portable GUI library. It can run on bare metal.

~~~
chris_wot
Except it doesn't run native controls, at least so far as I'm aware.

~~~
Vogtinator
> Except it doesn't run native controls, at least so far as I'm aware.

Can you tell the difference?
[https://3.bp.blogspot.com/-O1Naap_IGVQ/VGJsRBEfQjI/AAAAAAAAA...](https://3.bp.blogspot.com/-O1Naap_IGVQ/VGJsRBEfQjI/AAAAAAAAAiM/3_eoqupIMlQ/s1600/VLC_MediaPlayer-215.png)

~~~
criddell
Well, it's not the prettiest Mac application, but it works great!

------
LeoNatan25
What a surprise, so there are alternatives to Electron garbage! Who knew!

~~~
__sr__
Qt? Although it has its own problems, the end product is often way better than
Electron.

~~~
kitsunesoba
It may have changed since I last gave it a shot previously one annoyance I had
with Qt was binary distribution. Getting it all packaged up correctly for each
platform is much less than straightforward and if your app isn’t FOSS it’s
exacerbated by LGPL linking requirements. It should really be a simple, single
button process.

~~~
jcelerier
> Getting it all packaged up correctly for each platform is much less than
> straightforward and if your app isn’t FOSS it’s exacerbated by LGPL linking
> requirements.

Electron also has LGPL dependencies (Blink, FFMPEG) and libui depends on GTK
which is also under LGPL so...

