Open benediktwerner opened 1 year ago
Hi @benediktwerner,
I played around with my macbook internal monitor + an external monitor. I was not able to reproduce the issue you describe.
I tried to move windows from one monitor to the other. I tried plugging and unplugging the external monitor. In both cases, the previews are always correct (i.e. right size, right position).
Could you please share consistent steps to reproduce and a video demonstrating the issue?
Thank you
Thanks for looking into it!
Unfortunately, I don't know how to consistently reproduce it either. I'll try to pay more attention to when exactly it happens and what I did before that. I guess it being related to changing my screens is ultimately just speculation because I think it never happend when I left my laptop plugged in for a week.
Actually, it just happened again and I think I know what part of the problem is. One of the screens is connected via HDMI and when I wake my laptop from sleep, that screen takes significantly longer to wake up than the one connected via USB-C. In that case, macOS moves all the windows from that screen to the others. But when I then use AltTab, all those windows still appear on the original screen in their original positions. And when the screen they were moved to has different dimensions (the built-in screen is obviously smaller and one of my monitors is vertical), the windows that don't fit also get resized and they then get shown zoomed in or out by AltTab.
I'm now able to consistently reproduce this by waiting for my laptop to fall asleep and lock and the screens to go into standby and then waking the laptop and quickly unlocking it with my fingerprint. Though not sure this is reproducible with just one screen. I guess maybe if that screen is connected via HDMI? Haven't tested if that matters, but when I tried it a bit just now I also had no windows on my laptop screen when it went to sleep and all the windows got moved there (even though it's not configured as the main screen).
The more messed up patterns probably happen when I then also unplug the screens or it happens multiple times in a row. Maybe there are also other situations where macOS moves windows around.
Usually, I explicitly lock my Mac and turn off the screens and then turn them on again when unlocking it and while I still sometimes only turn them on after unlocking, I guess there's never that slow waking up from standby state in that case.
I guess this feels a little bit like a macOS bug but maybe there's still a way to get the correct window sizes and positions?
Thank you for sharing more info about this issue. Could you please share a video of this happening? You could film from your phone for instance. It would help because i'm not sure about some of the important details from your description.
It's expected that macOS moves window from a monitor onto the internal monitor, if you unplug the external monitor. When i tested last time this happened, and AltTab picked up the new positions and sizes correctly. So there is more to it which i need to understand to be able to reproduce. When i can reproduce the issue, i can fix it most likely.
Thank you
Sure: (I cut out roughly a minute of waiting in the middle since it only locks one minute after falling asleep, enforced by my company policy. You can see that both the Notes and Discord previews show up on the right screen even though the real window is on the left, and for Discord, the blue highlight has the size the window had originally but the preview has the new size centered in the old size).
https://github.com/lwouis/alt-tab-macos/assets/19309705/caec9c38-a2a0-4cff-9829-b91133985ad6
The screen on the right is the one connected via HDMI. Maybe also worth pointing out, the screen in the middle is designated as the main screen in the display settings.
I also did a few more experiments: It also happened when Discord was the only window on the right screen, when I used the internal keyboard to wake it, or when I didn't wait for the Mac to lock after it went to sleep (in that case, the AltTab preview actually was on the internal screen but about two-thirds down the screen). It also sometimes happened when I manually locked the Mac with Ctrl+Cmd+Q, but in that case, it only happened sometimes, seemingly only if I waited a few minutes. Though in the cases where it didn't happen, the windows also didn't move to the internal screen.
Also, in all the cases where it did happen, all the windows were first on the internal screen for a brief moment before some moved back to the main screen, or when the screen was locked, the lock screen first showed up on the internal screen and then moved (you can see that in the video, right as the screen turns on, the lock screen is there (kinda buggy in the corner) and then it disappears/moves to the main screen). If I unplug the screen, the windows also move but they move back once I plug the screen back in. It definitely seems like it has something to do with the windows not moving back because the screen takes so long to wake up properly.
You could try to manually refresh the window frames on computer/screen waking/sleeping, with/without delay. For example, by adding the following to applicationDidFinishLaunching
:
let notificationNames = [
NSWorkspace.didWakeNotification,
NSWorkspace.willSleepNotification,
NSWorkspace.screensDidWakeNotification,
NSWorkspace.screensDidSleepNotification]
let notificationName = notificationNames[0]
let delayInMilliseconds = 0
NSWorkspace.shared.notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
BackgroundWork.mainQueueConcurrentWorkQueue.asyncAfter(deadline: .now() + .milliseconds(delayInMilliseconds)) {
Windows.list.forEachAsync { window in
retryAxCallUntilTimeout {
window.position = try window.axUiElement.position()
window.size = try window.axUiElement.size()
}
}
}
}
I've also had some sizing weirdness when switching between laptop display and an external display (with my MacBook closed). Not sure if it helps reproduce but I am also using a display rotated vertically 90 degrees, like @benediktwerner has:
Maybe that's the key to this?
I'm encountering the same issue. My setup is pretty straightforward:
The problem arises when reconnecting the monitor to the Mac. If the external monitor has been disconnected for a substantial period, the window previews on the Mac revert to the size of the built-in screen. Since my external monitor has a higher resolution, the previews end up looking scaled down. This doesn't occur with every switch, only when there's a longer disconnect.
With "Preview selected window" enabled, the positioning and size of the preview sometimes goes out of sync with the actual position of the window, e.g. the preview is way smaller (e.g. only covering the top left quarter of the screen for a full-size window), way larger (only showing less than the top-left quarter of the window because the rest is off-screen), or just slightly off, e.g. moved down a little bit.
The misalignment stays consistent once it appears. Re-starting AltTab fixes it.
I'm not 100% certain about this but I believe this usually happens when I unplug or re-plug my monitors. It might also have to do with waking from sleep though. At least it doesn't seem to happen randomly while I'm using my PC. But it also doesn't consistently happen when I do either of those.
Not sure if it's relevant, but I have a MacBook Pro with 2 external monitors, one connected via USB-C and one via HDMI.
If you could point me to the area where this issue likely would have to be fixed or maybe have some tips on how I could go about debugging this, I'd also be happy to look into creating a PR to fix it myself.
My environment