I've built a new macOS menu bar app called Foreground. Press ⌥⌘F and the focused window glides to the centre of the screen at full height while everything else fades to black. You can still resize the window while focus mode is active. Press the shortcut again, or hit Esc, and everything goes back to how it was.

It started as an excuse to try Odin on a real project rather than toy examples. I'd seen cabeen/zen-mode, a Hammerspoon Lua script that does something similar. Building a standalone native app gave me plenty to try: global hotkeys, moving windows, animation, threading and macOS C and Objective-C APIs.

The app itself is written in Odin, with a SwiftUI settings window.

Why Odin?

I wanted a systems programming language I could pair with a native macOS UI for future projects. I've tried Rust a few times and never really got on with it. I struggled with the borrow checker and often felt like I was wrestling the compiler to express something simple. Rust works well for plenty of people, but it hasn't clicked for me. That left Zig and Odin.

I knew Zig could work well here: Ghostty pairs a Zig core with a native macOS app, and I'd experimented with Zig and Swift myself. I wanted to try Odin and Swift before settling on either language.

I write Go most of the time, and Odin immediately felt familiar. It's small, explicit and practical. I can hold the language in my head and get on with the problem rather than fighting the type system or designing abstractions.

Odin gives me low-level control over memory without the friction I hit in Rust. There's no garbage collector or borrow checker. Like Zig, it lets me choose the allocator and leaves me responsible for freeing memory. I find that explicit approach easier to reason about than Rust's borrowing rules.

defer handles cleanup in a way that feels natural coming from Go. In Foreground, any Core Foundation object returned from a Copy or Create call needs a matching CF.Release, and debug builds run with Odin's tracking allocator enabled so leaks or bad frees are reported on exit.

Compared to Zig, Odin feels more complete out of the box. Its core and vendor libraries include threads, locks, time, maths, testing, cryptography and OS bindings.

The good documentation and bundled libraries also helped when working with LLMs. In my experience, they picked up Odin quickly. They had clear examples to work from, and fast feedback from the compiler and tests helped them correct mistakes. That was especially useful when debugging complex issues.

I still had to write bindings for parts of AppKit and Core Animation that Odin's Darwin packages don't expose yet, but Odin can call C and Objective-C directly. Adding a missing API meant declaring the function or selector and carrying on.

Keeping the animation smooth

The hardest part of Foreground isn't drawing the dark overlay. It's moving and resizing another application's window without making the animation stutter.

To move windows on macOS, you use the Accessibility API. The catch is that each request waits for the other app to respond. If it takes 25ms to update its layout or recalculate a terminal's rows and columns, your call blocks for 25ms. In the original Hammerspoon script, the animation timer, window movement and overlay updates all run on the same thread. One slow app makes the backdrop fade stutter too.

To reduce that stuttering, Foreground splits the work across two threads:

  • The main thread runs the animation timer, fades the overlay and updates the opening around the focused window (the cutout).
  • A worker thread handles the Accessibility calls and moves the window.

The threads share a single slot rather than a queue. On each animation tick, the main thread writes the position and size it wants. If the worker is still waiting on a slow app, newer updates overwrite the ones it missed. When it's ready, it applies only the latest update.

The worker then reads the window's actual position and size back from macOS. Apps can enforce a minimum size, resize in steps to fit a terminal grid, or refuse a resize altogether. The main thread keeps the cutout where the window actually landed, not where it was asked to go:

desired := geom.lerp_rect(g_zen.rect_from, g_zen.rect_to, e)
ax_set_desired(desired)
set_overlay_cutout(ax_get_actual())

That helps keep the backdrop fade smooth and the cutout aligned, even when the window lags behind. I still see some stuttering and visual glitches, though, and I'm not sure I'll ever get rid of them entirely.

Two coordinate systems

The macOS APIs Foreground uses have two coordinate systems. The Accessibility API puts (0, 0) at the top-left of the primary screen, with y growing downwards. AppKit puts (0, 0) at the bottom-left, with y growing upwards.

In either system, a rectangle contains four f64 values. Pass one to an API expecting the other, and the cutout ends up in the wrong place.

Like Go's named types, Odin's distinct types let the compiler catch the mix-up:

Rect :: struct {
	x, y, w, h: f64,
}

// Top-left origin, y grows downward.
AX_Rect :: distinct Rect

// Bottom-left origin, y grows upward.
Cocoa_Rect :: distinct Rect

ax_to_cocoa :: proc(r: AX_Rect, primary_h: f64) -> Cocoa_Rect {
	return Cocoa_Rect{r.x, primary_h - r.y - r.h, r.w, r.h}
}

cocoa_to_ax :: proc(r: Cocoa_Rect, primary_h: f64) -> AX_Rect {
	return AX_Rect{r.x, primary_h - r.y - r.h, r.w, r.h}
}

AX_Rect and Cocoa_Rect have the same memory layout, but the compiler won't let you pass one where the other is expected. The geometry and animation maths lives in a separate package that only imports core:math, so I could test the coordinate conversions and layout calculations with odin test without launching the app.

Calling into macOS from Odin

Working directly with macOS APIs has a few sharp edges, regardless of the language.

Objective-C looks up method names (selectors) at runtime. Mistype one in a binding and the code compiles, but crashes when you call it.

C callbacks have their own catch in Odin. Normal Odin procedures receive a context parameter automatically, holding the current allocator and logger. C callbacks (proc "c") don't. When macOS calls a hotkey handler or an NSTimer callback, you have to restore the context before calling code that allocates or logs:

zen_tick :: proc "c" (user_data: rawptr, timer: ^NS.Timer) {
	context = g_ctx
	tick()
}

Once you know the rule, it's one line at the top of each callback, and the rest of the code looks like ordinary Odin.

Where SwiftUI fits

Odin handles the core of the application: hotkeys, window management, animation, configuration, licensing and crash recovery. The one place I didn't use Odin was the settings window.

Building the settings window with AppKit from Odin would have meant a lot of repetitive UI code. SwiftUI was simpler for the tabs, sliders, toggles and keyboard shortcut recorder.

Rather than making the whole project an Xcode app, I put the SwiftUI views in a small Swift package. It compiles to a static library that links into the Odin binary. The two sides talk through C: Odin passes SwiftUI a copy of the current settings, and SwiftUI calls back whenever a setting changes.

A single shell script (build.sh) compiles the Swift library and Odin code for Apple silicon and Intel, combines the two builds into a universal binary with lipo, and signs the .app bundle.

Final thoughts

I've enjoyed building Foreground with Odin. It has the practical feel I like in Go, with low-level control when I need it.

I'm keen to use it for more systems programming and native macOS projects. If you want to try Foreground, you can find it at foreground.aran.dev.