From cb43f746293b583776a1b0775b84046568e1e91c Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Fri, 10 Jun 2022 18:52:26 +1200 Subject: [PATCH] Fixed potential bug with window taskbar location during screen generation There is a possibility that there are two displays with the same UID. This happens with software created displays such as SpaceDesk and Superdisplay. This means that we need to try and figure out which screen is the right screen, and then when we know which screen we were really wanting to --- DisplayMagician/Properties/AssemblyInfo.cs | 4 +- DisplayMagicianShared/ProfileItem.cs | 59 +++++++++++++++++-- DisplayMagicianShared/ProfileRepository.cs | 3 + .../Windows/TaskBarLayout.cs | 2 +- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index e709480..9f68c02 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.4.0.26")] -[assembly: AssemblyFileVersion("2.4.0.26")] +[assembly: AssemblyVersion("2.4.0.28")] +[assembly: AssemblyFileVersion("2.4.0.28")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index 725a1cf..19dfae9 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -1558,8 +1558,16 @@ namespace DisplayMagicianShared // rather than the MMStuckRect reg keys try { - screen.TaskBarEdge = _windowsDisplayConfig.TaskBarLayout.First(tb => tb.Value.RegKeyValue.Contains("Settings")).Value.Edge; - SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Position of the taskbar on the primary display {targetId} is on the {screen.TaskBarEdge } of the screen."); + if (_windowsDisplayConfig.TaskBarLayout.Count(tbr => tbr.Value.RegKeyValue.Contains("Settings")) > 0) + { + screen.TaskBarEdge = _windowsDisplayConfig.TaskBarLayout.First(tbr => tbr.Value.RegKeyValue.Contains("Settings")).Value.Edge; + SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Position of the taskbar on the primary display {targetId} is on the {screen.TaskBarEdge } of the screen."); + } + else + { + SharedLogger.logger.Warn($"ProfileItem/GetWindowsScreenPositions: Problem trying to get the position of the taskbar on primary display {targetId}. Assuming it's on the bottom edge."); + screen.TaskBarEdge = TaskBarLayout.TaskBarEdge.Bottom; + } } catch (Exception ex) { @@ -1573,8 +1581,51 @@ namespace DisplayMagicianShared { try { - screen.TaskBarEdge = _windowsDisplayConfig.TaskBarLayout.First(tbr => tbr.Value.RegKeyValue.Contains($"UID{targetId}")).Value.Edge; - SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Position of the taskbar on display {targetId} is on the {screen.TaskBarEdge } of the screen."); + int numMatches = _windowsDisplayConfig.TaskBarLayout.Count(tbr => tbr.Value.RegKeyValue.Contains($"UID{targetId}")); + if (numMatches > 1) + { + var matchingTbls = (from tbl in _windowsDisplayConfig.TaskBarLayout where tbl.Value.RegKeyValue.Contains($"UID{targetId}") select tbl.Value).ToList(); + bool foundIt = false; + foreach (var matchingTbl in matchingTbls) + { + // find display source that matches. + foreach (var displaySource in _windowsDisplayConfig.DisplaySources) + { + foreach (var displayDevice in displaySource.Value) + { + // We want to find the displaydevice that has the same adapter id + if (displayDevice.AdapterId.Value == adapterId && displayDevice.DevicePath.Contains(matchingTbl.RegKeyValue)) + { + // This is the actual display we want! + foundIt = true; + screen.TaskBarEdge = matchingTbl.Edge; + SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Position of the taskbar on display {targetId} is on the {screen.TaskBarEdge } of the screen."); + break; + } + } + // If we've found it already then stop looking + if (foundIt) + { + break; + } + } + } + if (!foundIt) + { + screen.TaskBarEdge = _windowsDisplayConfig.TaskBarLayout.First(tbr => tbr.Value.RegKeyValue.Contains($"UID{targetId}")).Value.Edge; + SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Couldn't find the taskbar location for display {targetId} when it had multiple matching UIDs. Assuming the screen edge is at the bottom of the screen."); + } + } + else if (numMatches == 1) + { + screen.TaskBarEdge = _windowsDisplayConfig.TaskBarLayout.First(tbr => tbr.Value.RegKeyValue.Contains($"UID{targetId}")).Value.Edge; + SharedLogger.logger.Trace($"ProfileItem/GetWindowsScreenPositions: Position of the taskbar on display {targetId} is on the {screen.TaskBarEdge } of the screen."); + } + else + { + SharedLogger.logger.Warn($"ProfileItem/GetWindowsScreenPositions: Problem trying to get the position of the taskbar on display {targetId} as UID doesn't exist. Assuming it's on the bottom edge."); + screen.TaskBarEdge = TaskBarLayout.TaskBarEdge.Bottom; + } } catch (Exception ex) { diff --git a/DisplayMagicianShared/ProfileRepository.cs b/DisplayMagicianShared/ProfileRepository.cs index ba69f45..50e882a 100644 --- a/DisplayMagicianShared/ProfileRepository.cs +++ b/DisplayMagicianShared/ProfileRepository.cs @@ -356,6 +356,7 @@ namespace DisplayMagicianShared { SaveProfiles(); IsPossibleRefresh(); + UpdateActiveProfile(); return true; } else if (numRemoved == 0) @@ -438,6 +439,7 @@ namespace DisplayMagicianShared { SaveProfiles(); IsPossibleRefresh(); + UpdateActiveProfile(); return true; } else if (numRemoved == 0) @@ -519,6 +521,7 @@ namespace DisplayMagicianShared { SaveProfiles(); IsPossibleRefresh(); + UpdateActiveProfile(); return true; } else if (numRemoved == 0) diff --git a/DisplayMagicianShared/Windows/TaskBarLayout.cs b/DisplayMagicianShared/Windows/TaskBarLayout.cs index b08d9b6..8fecc4a 100644 --- a/DisplayMagicianShared/Windows/TaskBarLayout.cs +++ b/DisplayMagicianShared/Windows/TaskBarLayout.cs @@ -727,7 +727,7 @@ namespace DisplayMagicianShared.Windows } else { - SharedLogger.logger.Error($"TaskBarLayout/GetAllCurrentTaskBarPositions: Skipping grabbing Taskbar position from a cloned display {monitorInfo.szDevice}"); + SharedLogger.logger.Trace($"TaskBarLayout/GetAllCurrentTaskBarPositions: Skipping grabbing Taskbar position from a cloned display {monitorInfo.szDevice}"); } // Prep the next taskbar window so we continue through them