Keep windows attached to spaces as displays are disconnected and reconnected? #238
-
I move my laptop between a couple of desks with different external monitor configs during the day as well as using it continuously on its own in meetings or elsewhere. One frustrating thing is that when I disconnect an external display, some of the spaces on it simply migrate to the laptop display while keeping their windows nicely attached. But at least one display (I think the one visible when I disconnect) simply has its windows merged into some existing space on the laptop display. What I would much prefer is that all of the spaces on external displays remained in tact and simply became non-visible spaces on the main display (with their windows still contained). Is this something that Yabai can even accomplish? I've considered trying to do this manually by creating a fresh space on each display just before disconnecting. Should be easy to script that yabai command. But then I'll have to remember to mash a key combo before disconnecting, and I'm worried it may not work reliably. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
If you disconnect a display, the (most recently) focused non-fullscreen space on that display gets destroyed, moving all windows from the destroyed space to the focused space. All other spaces (up to 15?) on the display get moved to the main display. When you connect a display, a non-fullscreen space gets created on that display. When you re-connect a display immediately, windows will also be moved back to the newly created space on the display and non-visible spaces will also be moved back to their original display. Sometimes, however, the moving-back part stops working. I am yet unsure if this is because of some interaction, time-based, because macOS display identifiers have always been weird (especially with high resolution displays), or a combination of the three. As of right now, you can easily create a script that creates a new space on the display to be disconnected and then moves all spaces to the main display (identifiable by its x/y coordinates being 0/0). if you need help with such a script feel free to ask, I can help. I don't think this can be automated completely. |
Beta Was this translation helpful? Give feedback.
-
I may be mistaken, but I remember the following happening to me when disconnecting external displays: Windows located on the first space of an external display is always moved to the first space of the primary (macOS "Main") display. Alternate spaces on the external display will simply be moved to the primary display while retaining their internal id and uuid. When the same display is reconnected, spaces are automatically returned to their original display. Windows that were previously moved to the first space of the primary monitor will also be moved back to their original display. I believe this behaviour should be the same with yabai running as well. It's been quite some time since I actually implemented multi-monitor support in yabai and I also don't use multiple monitors with my macbook these days. While this may not be a practical solution, the quickest way I would imagine is to simply never utilize the first space of an external display, to completely avoid this issue. Then again, that could make your workflow less practical, depending on how you navigate between spaces. Heavy usage of prev/next won't be as convenient anymore, where as keyboard shortcuts to activate a specific space by number (and hopefully by name in the future) should still be usable. Alternatively a script is probably the best option as of now, but there isn't really a way to automate this. By the time a display_removed signal would trigger, the damage would have already happened. |
Beta Was this translation helpful? Give feedback.
-
Thanks for commenting with your understanding @koekeishiya! I've been frustrated by this behavior but couldn't quite put my finger on what exactly MacOS was doing. While I think it's insanity to destroy that first space I think your suggestion to avoid the first space on the external display will help avoid that pain. |
Beta Was this translation helpful? Give feedback.
-
@chandlerc Now that we have persistent display uuids (#346), this can actually be achieved semi-reliably with yabai. For this to work, you need to manually persist the display uuid for all windows and spaces regularly (e.g., on some signals), and save this state whenever a display is removed (signal Then, when a display is connected (signal Fullscreen spaces will be especially hard to get right, and split-fullscreen spaces cannot be restored using yabai. |
Beta Was this translation helpful? Give feedback.
-
@dominiklohmann Any suggestions for how to do this? |
Beta Was this translation helpful? Give feedback.
-
Is it possible for I don't quite understand how to achieve the solution given by @dominiklohmann, but another easy way is just to add a shortcut to create a new empty space as the first space on each display, and I just need to remember to press that before unplugging the display... This should be doable through some simple script |
Beta Was this translation helpful? Give feedback.
@chandlerc Now that we have persistent display uuids (#346), this can actually be achieved semi-reliably with yabai.
For this to work, you need to manually persist the display uuid for all windows and spaces regularly (e.g., on some signals), and save this state whenever a display is removed (signal
event=display_removed
) in a file associated with the removed displays uuid.Then, when a display is connected (signal
event=display_added
), look for the save state for the added displays uuid, restore all spaces on the the target display / move all windows to the display by their window id.Fullscreen spaces will be especially hard to get right, and split-fullscreen spaces cannot be restored u…