How to use capy-ui

Background

I wanna make a little application that has a gui, honestly more of a script for people who are scared of typing.

Attempt Nr. 1

I tried to use capy-ui, but it says I have to use a nominated mach version, which I dont want to, but am willing to if nothing else works. It’s build script is also really weird, which is why I would like to avoid it.

Attempt Nr. 2

The only other alternative I could find was imgui, the issue is, I want a static binary and I dont know how to link a cpp lib statically.

Current attempt

Back to capy.

ocornut/imgui has a bindings page that includes Zig. In theory those bindings for Zig should provide you with working examples. I haven’t personally tried any of those but I recognized michal-z and SpexGuy in there and they probably have good quality code in their respective repos.

That said…

Importing the build script of a dependency and using funcionality exposed by it is 100% legitimate and by design (the ability to import build scripts was added to the build system specifically to enable this use case).

You can go read the implementation of capy.runStep in the build script in there and you can even copy-paste its implementation directly in your build.zig, if you don’t like things happening without your direct control.

2 Likes

Alright Im back to the capy plan. I managed to get the build file to (mostly) compile by using 0.13.0 and snippets from the capy example project.

My issue now is that capy cant find the .a and .so files.
The error is:

error: error: unable to find dynamic system library 'gtk4' using strategy 'paths_first'. searched paths:
  /usr/local/lib64/libgtk4.so
  /usr/local/lib64/libgtk4.a
  /usr/local/lib/libgtk4.so
  /usr/local/lib/libgtk4.a
  /usr/lib/x86_64-linux-gnu/libgtk4.so
  /usr/lib/x86_64-linux-gnu/libgtk4.a
  /lib64/libgtk4.so
  /lib64/libgtk4.a
  /lib/libgtk4.so
  /lib/libgtk4.a
  /usr/lib64/libgtk4.so
  /usr/lib64/libgtk4.a
  /usr/lib/libgtk4.so
  /usr/lib/libgtk4.a
  /lib/x86_64-linux-gnu/libgtk4.so
  /lib/x86_64-linux-gnu/libgtk4.a

I am on voidlinux x86_64 glibc.
Is there a chance that naming conventions for libraries are just different on void to whatever the capy devs use or am I missing a package?

Try pkg-config --libs gtk4
The library in debian is /usr/lib/x86_64-linux-gnu/libgtk-4.so.1, its name is gtk-4.

Weird, I just found /usr/lib/libgtk-4.so too. This probably means I have to change the code of capyui. Where is it usually stored?

Edit:
zig env gives me

{
 "zig_exe": "/home/segfault-enjoyer/zig/0.13.0/files/zig",
 "lib_dir": "/home/segfault-enjoyer/zig/0.13.0/files/lib",
 "std_dir": "/home/segfault-enjoyer/zig/0.13.0/files/lib/std",
 "global_cache_dir": "/home/segfault-enjoyer/.cache/zig",
 "version": "0.13.0",
 "target": "x86_64-linux.6.6.431...6.6.431-gnu.2.39",
 "env": {
  "ZIG_GLOBAL_CACHE_DIR": null,
  "ZIG_LOCAL_CACHE_DIR": null,
  "ZIG_LIB_DIR": null,
  "ZIG_LIBC": null,
  "ZIG_BUILD_RUNNER": null,
  "ZIG_VERBOSE_LINK": null,
  "ZIG_VERBOSE_CC": null,
  "ZIG_BTRFS_WORKAROUND": null,
  "ZIG_DEBUG_CMD": null,
  "CC": null,
  "NO_COLOR": null,
  "CLICOLOR_FORCE": null,
  "XDG_CACHE_HOME": null,
  "HOME": "/home/segfault-enjoyer"
 }
}

In build.zig, change "gtk4" to "gtk-4".

Thats what Im trying to do. The issue is just that its not me importing it, but capy. So I wanna change that where its cached.

On second thought, zig have pkg-config integration. So normally “gtk4” must work.
Try pkg-config --libs gtk4
If gtk4 is not found, try to install a development package for gtk4.
If gtk4 is found by pkg-config, build.zig must work.

=> pkg-config --libs gtk4
-lgtk-4 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -lgdk_pixbuf-2.0 -lcairo-gobject -lcairo -lvulkan -lgraphene-1.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

I just ran zig build run and it actually compiled, but crashed at runtime

=> zig build run
warning: 'many-counters' is broken (disabled by default)
Segmentation fault at address 0x0
???:?:?: 0x7fc2462c8eed in ??? (libgtk-4.so.1)
Unwind information for `libgtk-4.so.1:0x7fc2462c8eed` was not available, trace may be incomplete

Edit: nvm I just forgot to do backend.capy.init

Everything works now. I have no idea what didn’t work in the beginning. Or rather why it works now. It failed to find gtk-4.so. Now it doesn’t.

Anyway I greatly appreciate the support :sweat_smile:

1 Like

If you’re considering dear imgui, then consider DVUI - it’s zig-native and immediate mode.

1 Like

Lmao I just got capy working, but yea I remember that that also exists.

How does it compare to capy? It does look quite nice.

Also how easy is it to import? Is it just zig fetch and go?

And my biggest question: Does it depend on other ui toolkits or is it just based on a raw canvas (sdl opengl etc), can I statically link it and is it pure zig?

Just tried it, seems really awesome

Thanks!

Compared to capy (I’ve only read about capy not used it):

  • capy uses native widgets, dvui has its own
  • capy is declarative (retained mode), dvui is immediate mode
  • dvui gets events from backend (SDL for your case) and outputs triangles to backend (so raw canvas)
  • dvui is designed to be more of a library, so can use for everything or bolt on to existing game/app
  • dvui is all zig, and is linked statically with SDL, using opengl under the hood

Hope that helps!

1 Like

It certainly does!

All those points you listed are ones I really appreciate.

I have been reading the example a bit and adapting the code style more to mine. So far I can say that the api does feel a bit raw (like the variable framerate time tracking, how you have to attack frontend to backend), but I really really like it.