CursorPie

The pie that ghosted your full-screen app

You hold the button, you flick, your action runs… and you never see the pie. Here’s the bug hunt behind that on macOS: two fixes (the first one flopped), four bugs in a trench coat, and what we learned about how windows stack.

Bug story macOS Rust Window levels 9 min read

1What it looked like

CursorPie is a pie menu for your cursor: hold the middle button (or touch and hold a trackpad), flick toward a slice, release. Simple. Now do that inside a full-screen browser, a video player or a game on macOS.

The hold registered. You flicked. The slice you aimed at actually ran: the shortcut fired, the app opened. And the whole time there was no pie on screen. Not glitchy, not half-drawn. Just gone. A phantom pie.

That’s the evil part. Everything that normally breaks first, the input hook, the gesture state machine, the action worker, was fine. The logs said “all good.” The only thing wrong was a rectangle of pixels that wasn’t being composited where you could see it.

2Why macOS does this: full screen is its own Space

When an app goes full screen on macOS, it doesn’t just get bigger. It moves into a Space of its own. A window from a different app doesn’t get drawn in that Space unless the window explicitly says it’s allowed to. CursorPie’s pie is a window from a different app. So it has to pass three checks at the same time:

  1. 1Say it in the collection behavior CanJoinAllSpaces (follow the person), FullScreenAuxiliary (may sit beside a full-screen window) and, since macOS 13, CanJoinAllApplications (may be drawn in another app’s Space).
  2. 2Be above what that Space draws The window level has to be higher than what a full-screen Space paints. The screen-saver level works. Lower ones may not.
  3. 3Don’t steal focus CursorPie is an accessory app (no Dock icon), so showing the window doesn’t activate the app and yank you out of the Space.

Miss any one of the three and the window is “shown” as far as the app knows, and invisible as far as you know.

Window levels, from normal windows up to the screen-saver level A stack of five layers. From the bottom: normal windows at level 0, floating at 3, status at 25, a dashed band for what a full-screen Space draws, and the screen-saver level at 1000 where the pie now lives. DRAWN ON TOP Screen saver · 1000 where the pie lives now (0.9.1) What a full-screen Space draws somewhere above 25 on current macOS Status · 25 where the pie was in 0.9.0 (too low) Floating · 3 the toolkit’s “always on top” Normal windows · 0 your apps; Settings gets this back ✓ shows over full-screen apps ✗ ordered in, never seen ✗ the toolkit writes this on every show
Higher number, drawn on top. The status level we used first sits below what a full-screen Space paints, so the pie was ordered in and never seen.

3Attempt #1 (0.9.0): looks right, does nothing

The first fix was reasonable. In the changelog it reads great: the pie window now joins every Space, may sit beside a full-screen window, and is raised above the Dock while it’s shown. In code that meant two collection-behavior bits and the status window level.

Shipped. Pie: still invisible over full-screen apps.

Which is the kind of result that makes you stare at a diff and go “but… it’s right.” It was right. It just wasn’t reaching anything.

4Four bugs in a trench coat

Version 0.9.1 is where the real post-mortem happened. Teaching the app to log what AppKit says about its own window (more on that below) showed it wasn’t one bug. It was four, stacked:

  1. A missing bit. Without CanJoinAllApplications (macOS 13+), a window of one app never reaches the full-screen Space of another app, whatever the other bits say. 0.9.0 only set the first two.
  2. A level that was too low. The status level (25) is below what a full-screen Space draws on current macOS. The screen-saver level (1000) is not.
  3. The toolkit kept fighting back. Every time the pie was shown, the toolkit wrote its own “always on top” (the floating level, 3). So the window was ordered in at the wrong level, and our code only raised it a frame later. A window ordered in at the wrong level wasn’t drawn over a full-screen app. Our fix ran before the toolkit’s commands of the same frame, so the toolkit always had the last word.
  4. The one that hurts: we were configuring a window that wasn’t ours. The code looked for the toolkit’s window with starts_with("Winit"). But the window is observed with KVO, and AppKit gives observed objects a runtime subclass, so its class is really NSKVONotifying_WinitWindow. The check never matched. None of our settings reached the pie window. A looser check that took every non-NS… class landed on the tray icon’s window instead. Wrong building entirely.
So 0.9.0 wasn’t just “not enough” Even a perfect set of bits would have changed nothing, because bug #4 meant the code never talked to the pie window. Always check that the thing you’re configuring is the thing you think it is.

5The fix (0.9.1)

Four moves, one per bug, plus a rule to keep them from drifting apart: one function owns the window’s level and behavior. Everyone else keeps their hands off.

First, find the right window. Accept both the plain class and the KVO subclass:

/// `WinitWindow`, or the `NSKVONotifying_` subclass AppKit
/// makes of it while it is observed.
fn is_toolkit_window_class(name: &str) -> bool {
    name.strip_prefix("NSKVONotifying_").unwrap_or(name) == "WinitWindow"
}

Second, give it every bit it needs (and drop the ones that conflict, because AppKit throws an exception if a window has two bits that exclude each other), and the screen-saver level:

const OVER_FULL_SCREEN_BITS: usize = CB_CAN_JOIN_ALL_SPACES
    | CB_CAN_JOIN_ALL_APPLICATIONS   // macOS 13+: the Space of another app
    | CB_FULL_SCREEN_AUXILIARY
    | CB_STATIONARY
    | CB_IGNORES_CYCLE;

fn level_for(over: bool, current: isize) -> Option<isize> {
    if over {
        (current != LEVEL_OVER_FULL_SCREEN).then_some(LEVEL_OVER_FULL_SCREEN)
    } else {
        (current == LEVEL_OVER_FULL_SCREEN).then_some(LEVEL_FLOATING)
    }
}

Third, stop the toolkit from fighting. On macOS CursorPie simply never sends the toolkit’s level at all:

fn pin_on_top(ctx: &egui::Context) {
    if platform::OS != platform::Os::Mac {
        ctx.send_viewport_cmd(egui::ViewportCommand::WindowLevel(
            egui::WindowLevel::AlwaysOnTop,
        ));
    }
}

Fourth, run the whole thing every frame. That sounds wasteful, but it’s a few messages to AppKit, and it sends nothing when the window is already right. That’s what makes it safe: because the toolkit rewrites window state whenever it likes, an idempotent “make it right again” step beats trying to win a race once.

Two details worth stealing:

  • Settings gets its normal level back. The function takes an over flag. While Settings is open it removes the bits and gives back a normal level, because a file chooser must be able to cover Settings. The “always above everything” treatment is only for the pie, notices and panels.
  • There’s a unit test for the bits. the_pie_joins_every_space_and_every_app_and_sits_beside_a_full_screen_window checks that every required bit is present, so a future refactor can’t silently drop CanJoinAllApplications again.

6What about Windows, Linux, and other apps?

The full-screen Space is a macOS thing, so the other systems are calmer, with caveats:

  • Windows: the pie window is created WS_EX_TOPMOST, which Windows already draws over a full-screen window. keep_over_full_screen is a deliberate no-op there. A different “covered by another app” problem exists, though: Windows never calls a low-level hook for windows of a program running as administrator (an elevated editor, an installer, a terminal) unless CursorPie is elevated too. So the pie doesn’t open there at all. Since 0.8.22 a watchdog notices the program in front is one of those and shows a notice, once per program and run, that names it and says what to do.
  • Linux: the function is also a no-op. Stacking is left to the toolkit’s always-on-top request, which depends on your window manager honoring it. X11 is pre-stable and Wayland is still experimental, so check Known issues before you trust it on your desktop.

7The sequel: one window, too many owners

Here’s the architecture detail that bit us next. The pie, the notice pill (the little badge that names the slice), the lock animation, a flow’s panel and the Settings card all share one native window. Cheap on memory, nice for latency. But who owns the window right now used to be implied by about a dozen boolean flags (pie_open, visible, hud, panel, lock_anim, settings_open…), and every transition had to keep them in step by hand.

When one flag got forgotten, you got a pie drawn stretched, or at the old place, for a few frames, because it was painted into a window that was still mapped for the previous owner. On macOS there was a second twist: a window that is shown is reported as occluded by the system for roughly 3 to 60 ms, and during that time the toolkit draws nothing. The first frame of the pill, which starts its animation clock, was being burned before the window was even visible.

The fix, in the next release (it’s under Unreleased in the changelog):

  • One place names the owner. ui/app/owner.rs derives the owner (Idle, Standby, Pie, Lock, Notice, Panel, Settings) from the flags in the same priority the UI draws them, and a pure, tested function says what a hand-over does to the window.
  • Hand over a mapped window instead of hiding and re-showing it. On macOS the faded pie and the lock animation pass the window straight to the notice that’s waiting, rather than hiding it and mapping it again 10 to 12 ms later.
  • A geometry gate. Whoever takes a window that’s hidden, or at another size or place, draws a transparent frame (which also wipes the last owner’s picture) until the system has carried out the size and place, then starts its clock. Capped at 150 ms.
  • A watchdog for lost frame requests. While an animation is on screen, every pass also asks for a frame on a timer, so a lost request costs one frame, not seconds.
  • Every change of owner is logged (ui / owner), so a glitch reads back as “from X to Y.”

8When the pie is open but you can’t see it

The best thing to come out of this was a way to see the problem. In a diagnostic build (Developer mode, in Settings → About), the macOS log has two lines for exactly this:

  • ui:mac_window_class prints the class of every window the app owns, once per class. The pie window should show up as NSKVONotifying_WinitWindow. If it doesn’t, you’re talking to the wrong window (hello, bug #4).
  • ui:mac_window_state prints one line each time the window changes: over, visible, on_space, occlusion, behavior (hex), level, policy and app_active.

How to read it: level should be 1000 while the pie is over a full-screen app (3 or 0 for Settings), policy should be 1 (accessory, no Dock icon), and on_space=false means the window isn’t being drawn in the Space you’re in. The full list is in the debugging guide.

The real lesson “The action ran” and “the user can see it” are two different tests. Log the state of the thing the user looks at, not only the thing your code does.

9What we’d tell past us

  • Verify the target, not just the call. A correct message sent to the wrong object is a silent no-op. Name-match runtime classes carefully (KVO subclasses!).
  • Don’t race your framework. Own one thing in one place. If the toolkit rewrites a property, don’t sprinkle fixes around it; centralize and re-assert idempotently.
  • Make the invisible visible. Two log lines about the window’s real state beat a week of guessing.
  • Pin the tricky bit with a test. Bit sets are easy to break on a refactor and impossible to eyeball.
  • Make shared resources have a single owner. A dozen flags is a state machine pretending not to be one.

Wanna see it work?

CursorPie is free, open source (MIT), has no account and no telemetry. Hold, flick, release. If a pie ever hides behind something it shouldn’t, tell us: attach the log lines above and we’ll hunt it down.