Skip to content

Commit

Permalink
fix(wm): cache monitor configs for unloaded monitors
Browse files Browse the repository at this point in the history
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.

Now when the display is connected it can load the correct config from
the cache.
  • Loading branch information
alex-ds13 authored and LGUG2Z committed Feb 21, 2025
1 parent c91cb9f commit 9ad32e4
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions komorebi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,15 +1247,15 @@ impl StaticConfig {
drop(workspace_matching_rules);

for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() {
let config_idx = {
let preferred_config_idx = {
let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock();
let c_idx = display_index_preferences
.iter()
.find_map(|(c_idx, m_id)| (monitor.device_id() == m_id).then_some(*c_idx));
drop(display_index_preferences);
c_idx
};
let idx = config_idx.or({
let idx = preferred_config_idx.or({
// Monitor without preferred config idx.
// Get index of first config that is not a preferred config of some other monitor
// and that has not been used yet. This might return `None` as well, in that case
Expand All @@ -1275,7 +1275,7 @@ impl StaticConfig {
{
// Check if this monitor config is the preferred config for this monitor and store
// a copy of the config on the monitor cache if it is.
if idx == config_idx {
if idx == preferred_config_idx {
monitor_reconciliator::insert_in_monitor_cache(
monitor.device_id(),
monitor_config.clone(),
Expand Down Expand Up @@ -1330,6 +1330,29 @@ impl StaticConfig {
}
}

// Check for configs that should be tied to a specific display that isn't loaded right now
// and cache those configs with the specific `device_id` so that when those devices are
// connected later we can use the correct config from the cache.
if configs_with_preference.len() > configs_used.len() {
for i in configs_with_preference
.iter()
.filter(|i| !configs_used.contains(i))
{
let device_id = {
let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock();
display_index_preferences.get(i).cloned()
};
if let (Some(device_id), Some(monitor_config)) =
(device_id, value.monitors.as_ref().and_then(|ms| ms.get(*i)))
{
monitor_reconciliator::insert_in_monitor_cache(
&device_id,
monitor_config.clone(),
);
}
}
}

wm.enforce_workspace_rules()?;

if value.border == Some(true) {
Expand All @@ -1353,15 +1376,15 @@ impl StaticConfig {
drop(workspace_matching_rules);

for (i, monitor) in wm.monitors_mut().iter_mut().enumerate() {
let config_idx = {
let preferred_config_idx = {
let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock();
let c_idx = display_index_preferences
.iter()
.find_map(|(c_idx, m_id)| (monitor.device_id() == m_id).then_some(*c_idx));
drop(display_index_preferences);
c_idx
};
let idx = config_idx.or({
let idx = preferred_config_idx.or({
// Monitor without preferred config idx.
// Get index of first config that is not a preferred config of some other monitor
// and that has not been used yet. This might return `None` as well, in that case
Expand All @@ -1381,7 +1404,7 @@ impl StaticConfig {
{
// Check if this monitor config is the preferred config for this monitor and store
// a copy of the config on the monitor cache if it is.
if idx == config_idx {
if idx == preferred_config_idx {
monitor_reconciliator::insert_in_monitor_cache(
monitor.device_id(),
monitor_config.clone(),
Expand Down Expand Up @@ -1438,6 +1461,29 @@ impl StaticConfig {
}
}

// Check for configs that should be tied to a specific display that isn't loaded right now
// and cache those configs with the specific `device_id` so that when those devices are
// connected later we can use the correct config from the cache.
if configs_with_preference.len() > configs_used.len() {
for i in configs_with_preference
.iter()
.filter(|i| !configs_used.contains(i))
{
let device_id = {
let display_index_preferences = DISPLAY_INDEX_PREFERENCES.lock();
display_index_preferences.get(i).cloned()
};
if let (Some(device_id), Some(monitor_config)) =
(device_id, value.monitors.as_ref().and_then(|ms| ms.get(*i)))
{
monitor_reconciliator::insert_in_monitor_cache(
&device_id,
monitor_config.clone(),
);
}
}
}

wm.enforce_workspace_rules()?;

if let Some(enabled) = value.border {
Expand Down

0 comments on commit 9ad32e4

Please sign in to comment.