[PR #1255] [MERGED] Fix/monitor disconnect #1278

Closed
opened 2026-01-05 14:54:44 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/LGUG2Z/komorebi/pull/1255
Author: @alex-ds13
Created: 1/31/2025
Status: Merged
Merged: 2/21/2025
Merged by: @LGUG2Z

Base: masterHead: fix/monitor-disconnect


📝 Commits (10+)

  • 3d7843e chore(release): v0.1.34
  • b189b0b fix(wm): properly store tile state when caching ws
  • 699f830 fix(wm): store config on workspace
  • d211d84 fix(wm): correctly use the display_index_preferences
  • d69ff5b fix(wm): cache monitor configs for unloaded monitors
  • 78930fb fix(wm): don't store empty layout_rules on monitor cache
  • 638a43f fix(wm): restore orphaned containers
  • 66ce028 fix(wm): skip workspace_config on workspace serde
  • ab163dc fix(wm): use serial numbers along device ids
  • 69d8d84 fix(wm): handle serial id on load_monitor_information

📊 Changes

22 files changed (+1069 additions, -366 deletions)

View changed files

📝 Cargo.lock (+52 -59)
📝 Cargo.toml (+3 -1)
📝 README.md (+1 -1)
📝 docs/cli/flip-layout.md (+1 -1)
docs/cli/focus-monitor-at-cursor.md (+12 -0)
📝 docs/cli/query.md (+1 -1)
📝 docs/komorebi.bar.example.json (+1 -1)
📝 docs/komorebi.example.json (+1 -1)
📝 komorebi-bar/src/bar.rs (+232 -145)
📝 komorebi-bar/src/komorebi.rs (+14 -1)
📝 komorebi-bar/src/main.rs (+6 -4)
📝 komorebi-client/src/lib.rs (+1 -0)
📝 komorebi/src/main.rs (+8 -2)
📝 komorebi/src/monitor.rs (+13 -13)
📝 komorebi/src/monitor_reconciliator/hidden.rs (+98 -3)
📝 komorebi/src/monitor_reconciliator/mod.rs (+279 -82)
📝 komorebi/src/process_event.rs (+2 -0)
📝 komorebi/src/static_config.rs (+262 -48)
📝 komorebi/src/window_manager.rs (+7 -1)
📝 komorebi/src/windows_api.rs (+68 -2)

...and 2 more files

📄 Description

This PR tries to properly handle monitors disconnecting and reconnecting

This PR 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 PR 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.

It also changes the way the layout is stored from the current monitor, by making sure to set it to None if the workspace is not tiling. Previously any floating workspace would be restored from cache with a tiling BSP layout.

Related to this discussion on Discord

However this entire code is based on the premise that the device_id is always the same for the same devices. However there have been comments on discord stating that this isn't true, however I've not been able to confirm this and the users have not given any proof of this happening (they might simply be confusing it since it's a weird string similar between monitors...)

I think the changes from this PR should fix most issues that people have been having with multiple monitors (specially with 3 or more monitors). But it's probably better to not merge this until we can get some users to test it.

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.

This PR also introduces a few changes to the bar so that it can handle the monitor disconnect/reconnect properly and so that it can map the bar config to the correct monitor.

Previously if you had 3 monitor configs setup on komorebi.json with the display_index_preferences set like this:

"display_index_preferences": [
    "0": "MONITOR_A",
    "1": "MONITOR_B",
    "2": "MONITOR_C",
]

But if you only had connected monitors A and C you would have to manually change the bar configurations monitor index because now monitor A would have index 0, but monitor C would have index 1 instead of 2.
Now with this PR this is no longer needed. Now the monitor index setup on the bar configuration NEEDS TO BE the index you've used on display_index_preferences for that monitor. So in the case above you would setup the bar configurations using the indices 0, 1 and 2 for monitors A, B and C respectively.

If the monitor B is disconnected, komorebi will know that now the monitor with index 1 is monitor C so the bar will also be aware of this and automatically change it's internal index to 1 (the user doesn't have to do anything!). Meaning that the bar will still show on the correct monitor with the correct coordinates. After the monitor reconnects the bar will update it's internal index again automatically and continue to show on the correct monitor. This means that even if you connect other monitors that are not mentioned on the display_index_preferences the bar should still point to the correct monitor.

It would be great if some users could test this PR since it introduces quite a few changes to make sure there are no regressions!


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/LGUG2Z/komorebi/pull/1255 **Author:** [@alex-ds13](https://github.com/alex-ds13) **Created:** 1/31/2025 **Status:** ✅ Merged **Merged:** 2/21/2025 **Merged by:** [@LGUG2Z](https://github.com/LGUG2Z) **Base:** `master` ← **Head:** `fix/monitor-disconnect` --- ### 📝 Commits (10+) - [`3d7843e`](https://github.com/LGUG2Z/komorebi/commit/3d7843ef84a7652e4f8b31b55aca4ab752fb747d) chore(release): v0.1.34 - [`b189b0b`](https://github.com/LGUG2Z/komorebi/commit/b189b0b7e45590b7455e43f6410ee723f50fcc26) fix(wm): properly store tile state when caching ws - [`699f830`](https://github.com/LGUG2Z/komorebi/commit/699f8303fdae72bc339c840c7b49d858770f53b5) fix(wm): store config on workspace - [`d211d84`](https://github.com/LGUG2Z/komorebi/commit/d211d846f37687e44ddfd579058754987cb204e2) fix(wm): correctly use the display_index_preferences - [`d69ff5b`](https://github.com/LGUG2Z/komorebi/commit/d69ff5bd3d2c6156b902f6bc71215efb225a3f8c) fix(wm): cache monitor configs for unloaded monitors - [`78930fb`](https://github.com/LGUG2Z/komorebi/commit/78930fb5bb4d50f4f2aaff0bee62738dccdd05de) fix(wm): don't store empty layout_rules on monitor cache - [`638a43f`](https://github.com/LGUG2Z/komorebi/commit/638a43f6d004712981f253dceb3549c4be568000) fix(wm): restore orphaned containers - [`66ce028`](https://github.com/LGUG2Z/komorebi/commit/66ce0288c9ef94b5872720ed4a814e54cabf5549) fix(wm): skip workspace_config on workspace serde - [`ab163dc`](https://github.com/LGUG2Z/komorebi/commit/ab163dc611c8b357ff341fc6318401f3dc1cdc5a) fix(wm): use serial numbers along device ids - [`69d8d84`](https://github.com/LGUG2Z/komorebi/commit/69d8d848db2b22381d347b045c6e20599b3b4a24) fix(wm): handle serial id on `load_monitor_information` ### 📊 Changes **22 files changed** (+1069 additions, -366 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+52 -59) 📝 `Cargo.toml` (+3 -1) 📝 `README.md` (+1 -1) 📝 `docs/cli/flip-layout.md` (+1 -1) ➕ `docs/cli/focus-monitor-at-cursor.md` (+12 -0) 📝 `docs/cli/query.md` (+1 -1) 📝 `docs/komorebi.bar.example.json` (+1 -1) 📝 `docs/komorebi.example.json` (+1 -1) 📝 `komorebi-bar/src/bar.rs` (+232 -145) 📝 `komorebi-bar/src/komorebi.rs` (+14 -1) 📝 `komorebi-bar/src/main.rs` (+6 -4) 📝 `komorebi-client/src/lib.rs` (+1 -0) 📝 `komorebi/src/main.rs` (+8 -2) 📝 `komorebi/src/monitor.rs` (+13 -13) 📝 `komorebi/src/monitor_reconciliator/hidden.rs` (+98 -3) 📝 `komorebi/src/monitor_reconciliator/mod.rs` (+279 -82) 📝 `komorebi/src/process_event.rs` (+2 -0) 📝 `komorebi/src/static_config.rs` (+262 -48) 📝 `komorebi/src/window_manager.rs` (+7 -1) 📝 `komorebi/src/windows_api.rs` (+68 -2) _...and 2 more files_ </details> ### 📄 Description ## This PR tries to properly handle monitors disconnecting and reconnecting This PR 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 PR 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. It also changes the way the layout is stored from the current monitor, by making sure to set it to `None` if the workspace is not tiling. Previously any floating workspace would be restored from cache with a tiling BSP layout. Related to [this discussion](https://discord.com/channels/898554690126630914/1329508394205184104/1329518859383865456) on Discord However this entire code is based on the premise that the `device_id` is always the same for the same devices. However there have been [comments](https://discord.com/channels/898554690126630914/898554690608967786/1330587647462084609) on discord stating that this isn't true, however I've not been able to confirm this and the users have not given any proof of this happening (they might simply be confusing it since it's a weird string similar between monitors...) I think the changes from this PR should fix most issues that people have been having with multiple monitors (specially with 3 or more monitors). But it's probably better to not merge this until we can get some users to test it. 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. This PR also introduces a few changes to the bar so that it can handle the monitor disconnect/reconnect properly and so that it can map the bar config to the correct monitor. Previously if you had 3 monitor configs setup on `komorebi.json` with the `display_index_preferences` set like this: ```json "display_index_preferences": [ "0": "MONITOR_A", "1": "MONITOR_B", "2": "MONITOR_C", ] ``` But if you only had connected monitors A and C you would have to manually change the bar configurations monitor index because now monitor A would have index 0, but monitor C would have index 1 instead of 2. Now with this PR this is no longer needed. Now the monitor index setup on the bar configuration **NEEDS TO BE** the index you've used on `display_index_preferences` for that monitor. So in the case above you would setup the bar configurations using the indices 0, 1 and 2 for monitors A, B and C respectively. If the monitor B is disconnected, komorebi will know that now the monitor with index 1 is monitor C so the bar will also be aware of this and automatically change it's internal index to 1 (the user doesn't have to do anything!). Meaning that the bar will still show on the correct monitor with the correct coordinates. After the monitor reconnects the bar will update it's internal index again automatically and continue to show on the correct monitor. This means that even if you connect other monitors that are not mentioned on the `display_index_preferences` the bar should still point to the correct monitor. It would be great if some users could test this PR since it introduces quite a few changes to make sure there are no regressions! <!-- Please follow the Conventional Commits specification. If you need to update your PR with changes from `master`, please run `git rebase master`. By opening this PR, you confirm that you have read and understood this project's `CONTRIBUTING.md`. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-01-05 14:54:44 +01:00
adam closed this issue 2026-01-05 14:54:44 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/komorebi#1278