Amethyst icon indicating copy to clipboard operation
Amethyst copied to clipboard

[Bug] Some applications don't tile when `CGSCopySpacesForWindows` returns empty

Open XOR-op opened this issue 2 years ago • 3 comments
trafficstars

Describe the bug When windows from some applications are created, they won't tile properly, until some updates(e.g. internal .add event) to the space containing the misbehaved window. This buggy behavior happens at random.

When I digged into the code base, I found the issue comes from WindowManager::add(). The code would run successfully until it reaches line 423, which tries to get the corresponding space for the window. After the failed trial, WindowManager will issue an unknown event instead of a normal add event. Therefore, some subsequent operations cannot be correctly performed, resulting some windows not properly tiled.

Looking deeper into the code. In CGWindowsInfo::windowSpace(), if this line returns an empty array, the subsequent operations will fail.

Interestingly, if we want to bypass the check in WindowManager::add(), line 425, we are still unable to get the proper behavior. I don't know if this can be resolved.

If we cannot bypass the space empty check, since CGSCopySpacesForWindows is a private MacOS API, we cannot do much with that. However, I wonder if we can have some workaround of that, for example some timeout or retry mechanism.

There have been a lot of bug reports of this problem, like #1335, #1364 and #1398. I guess my observation is the root cause of some of those. I want to see if any experienced MacOS developer could help.

Applications: Applications which have this bug frequently: Typora, Kitty Applications that never have this bug: Firefox, Vscode, Chrome and many more.

To Reproduce Steps to reproduce the behavior:

Expected behavior Properly handled add event and windows should tile.

Versions:

  • macOS: 13.6.2
  • Amethyst: 0.20.0(108)

XOR-op avatar Nov 21 '23 04:11 XOR-op

Thanks for the thorough analysis! I made a proposed fix for it. Are you able to build and try it out?

ianyh avatar Jan 08 '24 01:01 ianyh

Thanks for your help! Unfortunately, this fix doesn't work for me. I guess it could be something more subtle happened when calling the macOS API.

P.S. an update for the affected applications: Firefox now also suffers from this problem too after an update, as well as Jetbrains Gateway. I have no idea why this issue will happen, why it only targets to a subset of applications and what's the shared common between all these apps (Kitty is a native Swift app, Typora is Electron and JB Gateway is probably written in Java).

XOR-op avatar Jan 14 '24 06:01 XOR-op