Part 17 of 20 in Tools we built and why. Previous: mojo-cv: working with pictures as numbers · Next: mojo-amdgpu: reading your graphics card's temperature and memory
What this is about
Most app windows are built like a house. You describe the window, its buttons and its panels once, and the toolkit keeps them in memory and updates them. This is called a retained interface.
There is a simpler style. Every frame, 30 or 60 times a second, your program just says what it wants on screen: a panel here, a button there, this text. The library draws it, and on the next frame it is all drawn again from scratch. This is called an immediate-mode interface. The best-known example is Dear ImGui.
mojo-imkit is our immediate-mode library for Mojo apps, and the drawing is done by the GPU.
Why it matters
Many of our tools need a small window: a chat box, a monitor, a map of a model's neurons. We did not want a heavy toolkit, a second language, or a separate UI process. With immediate mode, the interface is just code that runs each frame, so there is no tree of widgets to keep in step with your data.
For a beginner the idea is also a good lesson. When you redraw everything every frame, the screen can never be out of date, because it is rebuilt from the program's current state.
How it works
The library follows the idea of Dear ImGui and imgui-bundle: widgets are function calls made every frame, and there is no retained widget tree. It builds that around a GPU app's own frame.
- Call the widgets. Each frame, the program calls
ui.panel,ui.button,ui.text,ui.fieldand the rest. A button call tells your program whether it was clicked this frame. - Append to one list. Each call adds shapes to a single flat draw list.
- Compose on the GPU. One kernel called
composeturns that list into an RGBA picture layer. - Blend and show. The window blends the layer over the app's own picture and presents it.
The README says there is no Python UI process, no C++ library, and no bundled windowing. The window and text come from the system's SDL3 and FreeType libraries, called through Mojo's foreign function interface.
What is included
- Widgets: panel, button, icon, bar, sparkline, text, a single-line text field, rectangle, line, triangle and image.
- A window: an SDL3 window with vsync and input, such as mouse, wheel, keys and typed text. It can also send synthetic mouse and key events for scripted checks.
- Fonts: a glyph atlas built from a TrueType font at start.
- Themes: 29 generated styles, plus the amarbaro themes. A gallery example renders them.
- Image decoders: pure Mojo, behind one call,
image_decode. It handles PNG, JPEG, WebP and AVIF stills. The JPEG output is bit-identical to libjpeg-turbo. The AVIF color planes are equal to dav1d sample for sample.
When this style is the simple choice
Immediate mode is a good fit when:
- the window shows live data that changes every frame, such as a monitor or a chat stream;
- you want one small program and no UI framework;
- the interface is mostly buttons, bars, text and pictures.
It is a worse fit when you need things native toolkits provide, such as a system look, complex text editing or accessibility features. This library does not claim to provide them. It is a way to put a clear window on a GPU app, and nothing more.
How to do it, step by step
- Install with
pixi add mojo-imkit. You also need the system's SDL3 and FreeType libraries. - Open the gallery example,
examples/gallery.mojo. A script,scripts/gallery.sh, renders it on the GPU in the amarbaro themes and three built-in ones. - Read the frame loop: call
window.poll()to get input, callui.begin(input), then call widgets, thenui.finish(), then render the layer and present. - Add one button of your own that changes a number on screen.
- Change the style name and see the whole window change.
The code now lives in the libs/imkit folder of mojo-libs, and the old repository is read-only. The current version in the table is 0.10.0.
What it does not do
- It is for apps with a GPU, since it composes on one.
- It brings no native widgets, and no retained tree to inspect.
- The decoders cover still images. The AVIF decoder is for stills.
- It follows Mojo, which is young. Check the README for the toolchain it needs.
Words you may not know
- Immediate mode: drawing the interface fresh from code every frame.
- Retained mode: keeping a tree of widgets that the toolkit updates.
- Frame: one picture drawn to the screen.
- Draw list: a flat list of shapes to draw.
- Kernel: a program that runs on the graphics card.
- RGBA: red, green, blue and alpha, the numbers for a color with see-through.
- Glyph atlas: one picture that holds all the letter shapes.
- Vsync: drawing in step with the screen's refresh.
Try it
- Read the README at github.com/amarbaro/mojo-imkit. The gallery pictures at the top show the themes.
- See the full list at the Mojo libraries page.
- Run the gallery and change one widget.
- Read the mojo-neuron project page to see a desktop window with a neuron map and a dial.
Read next
- mojo-cv: working with pictures as numbers: the previous post.
- mojo-amdgpu: reading your graphics card's temperature and memory: the next post in this series.

Comentarios
Aún no hay comentarios.
Iniciar sesión para comentar.