koekeishiya / yabai

A tiling window manager for macOS based on binary space partitioning
MIT License
24.1k stars 647 forks source link

Bug: Yabai shows incorrect window attributs before focusing it #2480

Open pyinto opened 2 days ago

pyinto commented 2 days ago

The Issue

I've noticed that yabai sometimes doesn't return the correct attributes of certain windows. Now I have caught an example of such behavior that I can reproduce. This bug messes up my signals sometimes; that's how I found it. I think it's related to https://github.com/koekeishiya/yabai/issues/2320, but not sure yet.

Steps To Reproduce

  1. Open native Calendar app on space 3; Now we have one calendar window on space 3 with: {"title": "Calendar", "app": "Calendar"};

  2. Move your focus away from the space 3 to any other space: yabai -m space --focus 1

  3. Restart yabai: yabai --restart-service

  4. Query windows on space 3: yabai -m query --windows --space 3 | jq

    [
      {
        "id": 2247,
        "pid": 32366,
        "app": "Calendar",
        "title": "Calendar",
        "scratchpad": "",
        "frame": {
          "x": 8.0000,
          "y": 33.0000,
          "w": 3184.0000,
          "h": 1759.0000
        },
        "role": "",
        "subrole": "",
        "root-window": true,
        "display": 1,
        "space": 3,
        "level": 0,
        "sub-level": -20,
        "layer": "normal",
        "sub-layer": "below",
        "opacity": 1.0000,
        "split-type": "none",
        "split-child": "none",
        "stack-index": 0,
        "can-move": false,
        "can-resize": false,
        "has-focus": false,
        "has-shadow": false,
        "has-parent-zoom": false,
        "has-fullscreen-zoom": false,
        "has-ax-reference": false,
        "is-native-fullscreen": false,
        "is-visible": false,
        "is-minimized": false,
        "is-hidden": false,
        "is-floating": false,
        "is-sticky": false,
        "is-grabbed": false
      }
    ]
    • Note that can-move & can-resize attributes - both set to false for some reason! Which is not true, you can resize those windows!
  5. Focus main Calendar app window, in my case it's 2247: yabai -m window 2247 --focus

  6. Repeat step 3: yabai -m query --windows --space 3 | jq

    [
      {
        "id": 2247,
        "pid": 32366,
        "app": "Calendar",
        "title": "Calendar",
        "scratchpad": "",
        "frame": {
          "x": 8.0000,
          "y": 33.0000,
          "w": 3184.0000,
          "h": 1759.0000
        },
        "role": "AXWindow",
        "subrole": "AXStandardWindow",
        "root-window": true,
        "display": 1,
        "space": 3,
        "level": 0,
        "sub-level": 0,
        "layer": "normal",
        "sub-layer": "normal",
        "opacity": 1.0000,
        "split-type": "none",
        "split-child": "second_child",
        "stack-index": 0,
        "can-move": true,
        "can-resize": true,
        "has-focus": false,
        "has-shadow": false,
        "has-parent-zoom": false,
        "has-fullscreen-zoom": false,
        "has-ax-reference": true,
        "is-native-fullscreen": false,
        "is-visible": true,
        "is-minimized": false,
        "is-hidden": false,
        "is-floating": false,
        "is-sticky": false,
        "is-grabbed": false
      }
    ]
    • Note that can-move & can-resize attributes - both set to true now, which is correct. Also role and subrole are set now too, before it was empty.

My Environment

$ yabai --version
yabai-v7.1.5

scripting-addition
  payload v2.1.18

$ sw_vers
ProductName:            macOS
ProductVersion:         15.1.1
BuildVersion:           24B91
pyinto commented 2 days ago

To fix this behavior I have a script that walks through all the spaces, and focus every window individually, but it's quite a work and takes time. In that script I had to focus a space where those windows are located, because sometimes yabai will just throw an error on a legit "focusable" window: could not locate window with the specified id or could not locate the window to act on!. This part is not that easy to reproduce, but once I will found a way - will report it.

koekeishiya commented 2 days ago

This is a macOS API limitation.

Some window properties are only accessible when yabai has a valid AX-reference for that window. This AX-reference can only be retrieved when the space that the window is visible on, is active. If windows are already opened on inactive spaces when yabai is launched, yabai can detect those windows and retrieve a limited amount of information about them. In addition, yabai window commands will NOT WORK for these windows. These windows can be identified by looking at the has-ax-reference property. Once the space that the window belongs to becomes active, yabai will automatically create an AX-reference. The queries will from that point forwards contain complete information, and the window can be used with yabai window commands.

The properties that contain incorrect information for windows with has-ax-reference: false are as follows:

{ "role": string, "subrole": string, "can-move": bool, "can-resize": bool }

There is no known workaround except for what you're already doing.

Edit: Note that window rules (or window commands in general with the exception of the query command) does not apply to these windows before has-ax-reference is reported as true. That is when yabai is actually in control of that window.