Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

nanocua: land background clicks again, and show the window where the user is

Rach Pradhaneca3cd6e322e
diff --git a/apps/nanocua/src/app.zig b/apps/nanocua/src/app.zig
index 25f75c65..82daa9ff 100644
--- a/apps/nanocua/src/app.zig
+++ b/apps/nanocua/src/app.zig
@@ -262,8 +262,9 @@ pub const App = struct {
fn bringForward(a: *App, stay_above: bool) void {
if (a.hidden) return;
const w = a.window orelse return;
- if (stay_above) if (a.raise) |f| f(w, true);
+ if (a.raise) |f| f(w, true);
w.focus();
+ if (!stay_above) if (a.raise) |f| f(w, false);
}
/// The panel's Allow or Deny. Allow needs a person's click, and the question
diff --git a/apps/nanocua/src/mac/backend.zig b/apps/nanocua/src/mac/backend.zig
index 15045b01..d3874d90 100644
--- a/apps/nanocua/src/mac/backend.zig
+++ b/apps/nanocua/src/mac/backend.zig
@@ -119,12 +119,24 @@ pub fn screenRecording() bool {
return capture.allowed();
}
-/// Keeps nanocua's window above other apps' while a question waits: macOS may
-/// refuse to bring an app forward that nobody clicked.
+/// Puts nanocua's window where the user is, above other apps', until `on` is
+/// false. macOS may refuse to activate an app nobody clicked, and the window can
+/// be on another Space, while the user's app is full screen on its own.
pub fn raise(window: *nanoui.Window, on: bool) void {
const floating: objc.NSInteger = 3;
+ const move_to_active_space: usize = 1 << 1;
+ const full_screen_auxiliary: usize = 1 << 8;
objc.msg(void, window.nswindow, "setLevel:", .{if (on) floating else @as(objc.NSInteger, 0)});
- if (on) objc.msg(void, window.nswindow, "orderFrontRegardless", .{});
+ // Only an accessory app's window can appear over another app's full screen.
+ const regular: objc.NSInteger = 0;
+ const accessory: objc.NSInteger = 1;
+ const nsapp = objc.msg(objc.Object, objc.class("NSApplication"), "sharedApplication", .{});
+ _ = objc.msg(objc.BOOL, nsapp, "setActivationPolicy:", .{if (on) accessory else regular});
+ if (!on) return;
+ objc.msg(void, window.nswindow, "setCollectionBehavior:", .{move_to_active_space | full_screen_auxiliary});
+ // Moving to the active Space happens as a window is shown, not while it's up.
+ objc.msg(void, window.nswindow, "orderOut:", .{@as(?objc.Object, null)});
+ objc.msg(void, window.nswindow, "orderFrontRegardless", .{});
}
pub const Permission = enum(usize) { accessibility = 1, screen = 2 };
diff --git a/apps/nanocua/src/mac/input.zig b/apps/nanocua/src/mac/input.zig
index 49bbfb08..41380eca 100644
--- a/apps/nanocua/src/mac/input.zig
+++ b/apps/nanocua/src/mac/input.zig
@@ -105,13 +105,15 @@ pub fn move(t: Target, x: f64, y: f64) void {
/// Gestures so far. Only the backend's queue sends events.
var next_group: i64 = 0;
-/// Starts a gesture: a move first, so an app in the background knows where the
+/// Starts a gesture: makes the window key, without raising it, or the app spends
+/// the click on that; then a move, so an app in the background knows where the
/// pointer now is before a press arrives.
fn prepare(t: Target, x: f64, y: f64) Target {
var out = t;
if (out == .app) {
next_group += 1;
out.app.group = next_group;
+ if (out.app.window != 0 and skylight.focusWindow(out.app.pid, out.app.window)) _ = usleep(50_000);
}
move(out, x, y);
_ = usleep(15_000);
diff --git a/apps/nanocua/src/mac/skylight.zig b/apps/nanocua/src/mac/skylight.zig
index b5067d4a..560f81f1 100644
--- a/apps/nanocua/src/mac/skylight.zig
+++ b/apps/nanocua/src/mac/skylight.zig
@@ -1,7 +1,9 @@
//! What plain CGEventPostToPid can't do for a window in the background: mouse
-//! events an app acts on. These are private functions, looked up at run time, so
+//! events an app acts on, and making a window key without raising it or taking
+//! the keyboard from the user's app. These are private functions, looked up at run time, so
//! a macOS without them falls back to the public post (and the reply already says
-//! a background click isn't confirmed). The recipe follows trycua's cua-driver (MIT).
+//! a background click isn't confirmed). The recipe follows yabai's focus-without-raise
+//! and trycua's cua-driver (MIT).
const std = @import("std");
const cf = @import("cf.zig");
@@ -16,6 +18,8 @@ const rtld_lazy: c_int = 1;
const Api = struct {
post_to_pid: ?*const fn (pid: i32, ev: *anyopaque) callconv(.c) void = null,
set_window_location: ?*const fn (ev: *anyopaque, at: cf.CGPoint) callconv(.c) void = null,
+ post_record: ?*const fn (psn: *const [8]u8, record: *const [record_size]u8) callconv(.c) i32 = null,
+ process_for_pid: ?*const fn (pid: i32, psn: *[8]u8) callconv(.c) i32 = null,
};
var api: Api = .{};
@@ -29,6 +33,8 @@ fn get() *const Api {
api = .{
.post_to_pid = @ptrCast(@alignCast(dlsym(rtld_default, "SLEventPostToPid"))),
.set_window_location = @ptrCast(@alignCast(dlsym(rtld_default, "CGEventSetWindowLocation"))),
+ .post_record = @ptrCast(@alignCast(dlsym(rtld_default, "SLPSPostEventRecordTo"))),
+ .process_for_pid = @ptrCast(@alignCast(dlsym(rtld_default, "GetProcessForPID"))),
};
return &api;
}
@@ -48,3 +54,37 @@ pub fn setWindowLocation(ev: *anyopaque, x: f64, y: f64) void {
pub fn setField(ev: *anyopaque, field: u32, value: i64) void {
CGEventSetIntegerValueField(ev, field, value);
}
+
+const record_size = 0xF8;
+
+/// The record that tells a window it has focus: its id little-endian at 0x3c,
+/// and the direction at 0x8a (1 gains focus).
+pub fn focusRecord(window: u32) [record_size]u8 {
+ var r: [record_size]u8 = @splat(0);
+ r[0x04] = record_size;
+ r[0x08] = 0x0D;
+ std.mem.writeInt(u32, r[0x3C..0x40], window, .little);
+ r[0x8A] = 0x01;
+ return r;
+}
+
+/// Makes `window` key inside app `pid`, so its first click isn't spent on that.
+/// Goes to that app alone: the user's front app isn't told, and keeps the
+/// keyboard. False if macOS has no way to do this, or refused.
+pub fn focusWindow(pid: i32, window: u32) bool {
+ const a = get();
+ const send = a.post_record orelse return false;
+ const psn_of = a.process_for_pid orelse return false;
+ var psn: [8]u8 = undefined;
+ if (psn_of(pid, &psn) != 0) return false;
+ const record = focusRecord(window);
+ return send(&psn, &record) == 0;
+}
+
+test "the focus record" {
+ const r = focusRecord(0x11223344);
+ try std.testing.expectEqual(@as(u8, 0xF8), r[0x04]);
+ try std.testing.expectEqual(@as(u8, 0x0D), r[0x08]);
+ try std.testing.expectEqualSlices(u8, &.{ 0x44, 0x33, 0x22, 0x11 }, r[0x3C..0x40]);
+ try std.testing.expectEqual(@as(u8, 1), r[0x8A]);
+}