{"record":{"id":"154b9cfd6503cf6d","repo":"LGUG2Z/komorebi","slug":"could-not-find-next-window","errorCode":null,"errorMessage":"could not find next window","messagePattern":"could not find next window","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"komorebi/src/windows_api.rs","lineNumber":762,"sourceCode":"        )?;\n\n        Ok(hwnds)\n    }\n\n    #[allow(dead_code)]\n    pub fn top_visible_window() -> eyre::Result<isize> {\n        let hwnd = Self::top_window()?;\n        let mut next_hwnd = hwnd;\n\n        while next_hwnd != 0 {\n            if Self::is_window_visible(next_hwnd) {\n                return Ok(next_hwnd);\n            }\n\n            next_hwnd = Self::next_window(next_hwnd)?;\n        }\n\n        bail!(\"could not find next window\")\n    }\n\n    pub fn window_rect(hwnd: isize) -> eyre::Result<Rect> {\n        let mut rect = unsafe { std::mem::zeroed() };\n\n        if Self::dwm_get_window_attribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &mut rect).is_ok() {\n            // TODO(raggi): once we declare DPI awareness, we will need to scale the rect.\n            // let window_scale = unsafe { GetDpiForWindow(hwnd) };\n            // let system_scale = unsafe { GetDpiForSystem() };\n            // Ok(Rect::from(rect).scale(system_scale.try_into()?, window_scale.try_into()?))\n            Ok(Rect::from(rect))\n        } else {\n            unsafe { GetWindowRect(HWND(as_ptr!(hwnd)), &mut rect) }.process()?;\n            Ok(Rect::from(rect))\n        }\n    }\n\n    /// shadow_rect computes the offset of the shadow position of the window to","sourceCodeStart":744,"sourceCodeEnd":780,"githubUrl":"https://github.com/LGUG2Z/komorebi/blob/e0709f02bfae4e503bf4640f58ee75ecbbfdbb97/komorebi/src/windows_api.rs#L744-L780","documentation":"top_visible_window walks the z-order with GetWindow/GW_HWNDNEXT looking for the next visible top-level window. If the enumeration exhausts without finding one, komorebi bails with 'could not find next window'. It indicates no visible sibling window was reachable in the z-order chain.","triggerScenarios":"Calling WindowsApi::top_visible_window(hwnd) when the starting hwnd is the last in the z-order, all subsequent windows are invisible/minimized, or the hwnd is invalid so the loop immediately fails to find a next window.","commonSituations":"All windows on a monitor are minimized or hidden; a window was destroyed mid-enumeration; querying a virtual desktop whose windows are all cloaked; empty workspaces in multi-monitor setups.","solutions":["Check that at least one visible, non-minimized window exists on the target monitor before calling","Enumerate windows with IsWindowVisible filtering yourself to confirm state","Restart or rescan komorebi's window management state","Handle the empty-workspace case in your caller instead of treating it as fatal"],"exampleFix":"// before\nlet hwnd = WindowsApi::top_visible_window(start)?;\n// after\nlet hwnd = match WindowsApi::top_visible_window(start) {\n    Ok(h) => h,\n    Err(_) => return Ok(()), // empty/hidden workspace is not fatal\n};","handlingStrategy":"try-catch","validationCode":"// Check at least one visible window exists first\nfn has_visible_window() -> bool {\n    !komorebi_state().windows.iter().any(|w| w.visible) == false\n}","typeGuard":null,"tryCatchPattern":"match WindowsApi::top_visible_window(hwnd) {\n    Ok(next) => use(next),\n    Err(e) if e.to_string().contains(\"could not find next window\") => {\n        log::debug!(\"no visible window in z-order; treating workspace as empty\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Treat empty/fully-minimized workspaces as a normal case, not an error","Keep the starting hwnd fresh; stale hwnds break z-order walks","Handle window-destroy events before re-enumerating","Filter minimized/cloaked windows explicitly in your own logic"],"tags":["win32","window-enumeration"],"backgroundTag":"empty-result-set","analyzedSha":"e0709f02bfae4e503bf4640f58ee75ecbbfdbb97","analyzedAt":"2026-09-06T07:08:05.107Z","contentChangedAt":"2026-09-06T07:08:05.107Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}