Check if another window is stacked above a given window

Is there a reliable way to check if a window is covered by another window?

I want to do something like:

    if (!win.covered) {
        // my code
    } else {
        // Window is on top
        win.minimize();
    }

Probably not on X11 and definitely not on Wayland.

Also, why? What is it you actually want to do that prompted you to attempt this?

Here is the code workspaces-organizer-by-blueray453/extension.js at 2f859e2b44e4cf1c798d9fb878747a3b01601355 · blueray453/workspaces-organizer-by-blueray453 · GitHub

I need to minimize the window only if it is not covered by another window. If it is covered (there is another window stacked on top of it), i do not need to minimize it.

Ah right, I’ve missed the extensions tag.

Thought your were talking about an app in GTK…

Sorry about that.

Thank you very much for trying to help me out.

Alhamdulillah, the working code is:

_is_covered(window) {
    if (window.minimized) { return false; }
    let current_workspace = WorkspaceManager.get_active_workspace();

    // Get windows on the current workspace in stacking order
    let windows_by_stacking = Display.sort_windows_by_stacking(global.get_window_actors().map(actor => actor.meta_window).filter(win => win.get_window_type() === Meta.WindowType.NORMAL)).filter(win =>
        win.get_workspace() === current_workspace
    );

    // // Find the target window
    // let targetWin = windows_by_stacking.find(win => win.get_id() === window.get_id());
    // if (!targetWin) return false;
    let targetRect = window.get_frame_rect();
    let targetIndex = windows_by_stacking.indexOf(window);
    journal(`${targetIndex}`);

    // Check only windows above the target in stacking order
    for (let i = targetIndex + 1; i < windows_by_stacking.length; i++) {
        let topWin = windows_by_stacking[i];
        let topRect = topWin.get_frame_rect();

        // Check if topWin fully covers window
        if (
            topRect.x <= targetRect.x &&
            topRect.y <= targetRect.y &&
            topRect.x + topRect.width >= targetRect.x + targetRect.width &&
            topRect.y + topRect.height >= targetRect.y + targetRect.height
        ) {
            return true;
        }
    }

    return false; // no window fully covers it
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.