I think the title identifies the core of the issue I'm asking about. I've recently (finally) upgraded from Monterey to Ventura. On Monterey, whenever I switched desktops, when I returned to a previous desktop the windows were stacked in the order in which I left them. But on Ventura, when I return, a different window or windows are in front of the window that I had been using, sometimes the window I that had just been foremost is not even visible to select. It's a very repetitious annoyance.
If this is fixed in later OSes, I'd love to know, but initial research with Google AI suggests that it has not. I put my first paragraph above in as a prompt and got this:
The window layer shuffling you are experiencing when switching Desktops (Spaces) is a known macOS focus-routing bug that worsened significantly with Ventura's redesigned window management architecture. [1, 2]
Is it fixed in later OS versions?
No, it is not completely fixed. While Apple has tweaked the window manager in macOS Sonoma and macOS Sequoia, users across various communities still heavily report "phantom focus" errors and background windows randomly jumping to the front when transitioning back to a Space.
Meanwhile, I'm working on the the idea of setting a Dictionary variable every time the foremost window changes to record the identity of the newly frontmost window. The Dictionary key is the ID of the current desktop. When I leave a
desktop, the Dictionary entry for that desktop is whatever had bee foremost previously and when I return, I bring that app forward. I'm back to where I had been and I'm happy (that's the plan).
A complication is when I drag a window from one desktop to another. The one that had been foremost is now in another desktop. So if I initiate a drag, I want to save the second window instead. That means my dictionary needs to save two window IDs in a push-down stack so that window #2 can be saved instead because window #1 is being dragged away.
The foremost window in a desktop can also change when the foremost window is deleted. The Focused Window Trigger can save the new window as window #1, but I can't simply save the previously frontmost window as the second in the pushdown stack because it's gone.
Hence my question:
When the focused window changes, is it possible to tell the difference between a change cause by selecting a new window and a change caused by closing the front window?
I would like to be able to handle those two situations differently.




