gardesk/ers / 3923eba

Browse files

fall back to z-order focus when front process has no visible windows

Authored by espadonne
SHA
3923eba75578edadafb102c2d8332a31e7976b19
Parents
e3a0d31
Tree
f7690a0

1 changed file

StatusFile+-
M src/main.rs 17 2
src/main.rsmodified
@@ -257,6 +257,7 @@ fn get_front_window(own_pid: i32) -> u32 {
257257
         let layer_key = CFStringCreateWithCString(ptr::null(), b"kCGWindowLayer\0".as_ptr(), kCFStringEncodingUTF8);
258258
 
259259
         let mut front_wid: u32 = 0;
260
+        let mut fallback_wid: u32 = 0;
260261
         for i in 0..count {
261262
             let dict = CFArrayGetValueAtIndex(list, i);
262263
             if dict.is_null() { continue; }
@@ -273,18 +274,32 @@ fn get_front_window(own_pid: i32) -> u32 {
273274
             if CFDictionaryGetValueIfPresent(dict, pid_key as CFTypeRef, &mut v) {
274275
                 CFNumberGetValue(v, kCFNumberSInt32Type, &mut pid as *mut _ as *mut _);
275276
             }
276
-            if pid != front_pid { continue; }
277
+            if pid == own_pid { continue; }
277278
 
278279
             let mut wid: u32 = 0;
279280
             if CFDictionaryGetValueIfPresent(dict, wid_key as CFTypeRef, &mut v) {
280281
                 CFNumberGetValue(v, kCFNumberSInt32Type, &mut wid as *mut _ as *mut _);
281282
             }
282
-            if wid != 0 {
283
+            if wid == 0 { continue; }
284
+
285
+            // Track first non-self window as fallback (z-order based)
286
+            if fallback_wid == 0 {
287
+                fallback_wid = wid;
288
+            }
289
+
290
+            // Prefer a window from the front process
291
+            if pid == front_pid {
283292
                 front_wid = wid;
284293
                 break;
285294
             }
286295
         }
287296
 
297
+        // Fall back to z-order if front process has no visible windows
298
+        // (e.g., switched to a workspace where the front app has no windows)
299
+        if front_wid == 0 {
300
+            front_wid = fallback_wid;
301
+        }
302
+
288303
         CFRelease(wid_key as CFTypeRef);
289304
         CFRelease(pid_key as CFTypeRef);
290305
         CFRelease(layer_key as CFTypeRef);