Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/monitor disconnect #1255

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

alex-ds13
Copy link
Contributor

This PR builds upon #1241

This PR changes the monitor cache from caching a config to caching the actual state of the monitor itself.

With this PR when a monitor disconnects instead of "throwing" all windows to the main monitor, right now it does nothing and keeps your other monitors as is and simply stores the disconnected monitor state.
If then you reconnect (or wake up) the monitor again, then you'll get back to the same monitor with all the windows like you had before!

If while the monitor is disconnected you need to access some window that was on the disconnected monitor, you can simply alt + tab to it or press the taskbar icon and the window will show on the focused workspace of the focused monitor and it becomes handled by this monitor/workspace. When the previously disconnected monitor reconnects it will see that this specific window is already handled by another workspace so it ignores it.

@alex-ds13 alex-ds13 force-pushed the fix/monitor-disconnect branch 2 times, most recently from c4cb73f to 97245c8 Compare January 31, 2025 15:28
@LGUG2Z
Copy link
Owner

LGUG2Z commented Feb 7, 2025

I've been running this for a few days and I'm pretty happy with it - do you have any lingering concerns before we start getting ready to merge?

@alex-ds13 alex-ds13 force-pushed the fix/monitor-disconnect branch from 9c98faf to 5f5b83f Compare February 7, 2025 18:33
@alex-ds13
Copy link
Contributor Author

I've been running this for a few days and I'm pretty happy with it - do you have any lingering concerns before we start getting ready to merge?

Let me fix the bar issue tonight and then it should be ready to merge...

Previously, when caching a workspace config for a monitor it would
simply store the `DefaultLayout` on `layout` even if the original
workspace config had the `layout` as `None` which makes komorebi create
a workspace with the `layout` as default `BSP` and the `tile` set at
`false`. However this wasn't being taken into consideration, so floating
workspaces would becoming tiling `BSP` workspaces after a monitor
disconnect and reconnect. This commit fixes this by turning the `layout`
to `None` when `tile` is `false`.
Store the `WorkspaceConfig` on the `Workspace` itself so that when we
want to cache the workspace as `WorkspaceConfig` on the monitor cache it
properly saves things like the workspace rules and the custom layout and
custom layout rules.
This commit reworks the way the `postload` and the `reload` functions
apply the monitor configs to the monitors. Previously it was looping
through the monitor configs and applying them to the monitor with the
index corresponding to the config's index. However this isn't correct,
since the user might set the preferred indices for 3 monitors (like
monitor A, B and C), with the preferred index set to 0 for A, 1 for B
and 2 for C, but if only monitors A and C are connected then komorebi
would apply config 0 to A and config 1 to C, which is wrong it should be
2 for C.

This commit changes the way the configs are applied on those functions.
Now it loops through the existing monitors (already in order), then
checks if the monitor has a preferred config index, if it does it uses
that one, if it doesn't then it uses the first monitor config that isn't
a preferred index for some other monitor and that hasn't been used yet.

For the situation above it means that it would still apply config 2 to
monitor C. And in case there aren't any display_index_preferences set it
will still apply the configs in order.
If we have display_index_preferences that set a specific config index
for a specific display device, but that device isn't loaded yet, now we
store that config with the corresponding `device_id` on the monitor
cache so that when the display is connected it can load the correct
config from the cache.
When a monitor was disconnected the containers from the removed monitor
were being moved to the primary monitor. However they weren't restored
so containers that were on an unfocused workspace of the removed monitor
would have been cloak and were getting added to the main monitor still
cloaked creating ghost tiles. This commit fixes that.
This commit makes use of both `serial_number_id` and `device_id` with
the first taking priority on all monitor reconciliator code, monitor
cache and on postload and reload. This should allow using the serial
numbers on the `display_index_preferences` config, while keeping
compatibility with the use of `device_id`. Although using `device_id`
should be discourage since that value can change on restart while serial
number doesn't appear to do so.
This commit makes sure that any workspace_rules that tried to move a
window to a monitor that has been disconnected are removed. If the
monitor is latter reconnected the workspace_rules should be added from
the cached config.
This commit creates a new field on the `WindowManager` called
`monitor_usr_idx_map` which contains a map of user intended index for
monitors to their actual monitors' index. It will be rebuilt on
`load_monitor_information` by taking into account the
`display_index_preferences`.
This commit makes so a bar can be disabled when it's monitor is
disconnected and re-enabled when said monitor reconnects.
This commit also uses the new `monitor_usr_idx_map` to properly map the
monitor index given by the users on config to the actual monitor index
on the `WindowManager`, since in case some middle monitor gets
disconnected the index of the monitors to the "right" of that one will
be lowered by 1.
This should allow for cases with monitor A, B and C, if monitor B
disconnects, its bar will be disabled and monitor C will properly change
its monitor index internally to 1 so it still gets the infos from
monitor C (which now will have index 1 on the `WindowManager`).
This commit changes the MONITOR_CACHE to hold an actual monitor with its
state instead of the config. This allows us to keep the state of a
disconnected monitor, so that when the monitor reconnects we still have
access to all its workspaces and windows as it was before.
While the monitor is disconnected, all the windows from that monitor are
not considered as handled by komorebi and so they're free to be handled
at any point, if for example the user uses `alt+tab` or presses the
taskbar icon of one of these windows that window will show up producing
it's `Show` event and will become handled by the currently focused
workspace of the focused monitor. When the disconnected monitor
reconnects it checks all windows and once it notices that this specific
window is now being shown on another monitor/workspace then it ignores
it.
This commit makes sure that if the state on file isn't up to date with
the expected `State` struct (maybe after an update) it doesn't panic
komorebi entirely, instead it ignores the state and continues with a
clean state.
When there is a monitor disconnect/reconnect, usually it produces
multiple monitor events along with it, like the monitor resolution
change and the work area change. With the bound set to 1 it would
sometimes result in missed events. This commit increases the bound to 10
to prevent this from happening.
@alex-ds13 alex-ds13 force-pushed the fix/monitor-disconnect branch from 8225cde to f6fa86e Compare February 8, 2025 03:52
@alex-ds13
Copy link
Contributor Author

@LGUG2Z Should be ready to merge! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants