Skip to content

Commit

Permalink
Avoid DisplayInformation crash on older hardware
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Nov 2, 2024
1 parent 70e2eeb commit 9273156
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
36 changes: 33 additions & 3 deletions CollapseLauncher/Classes/Helper/WindowUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Graphics.Display;
using Microsoft.UI;
using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Input;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -59,9 +60,38 @@ internal static DisplayArea? CurrentWindowDisplayArea
}
}

internal static DisplayInformation? CurrentWindowDisplayInformation => CurrentWindowId.HasValue
? DisplayInformation.CreateForWindowId(CurrentWindowId.Value)
: null;
internal static DisplayInformation? CurrentWindowDisplayInformation
{
get
{
try
{
DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();
if (dispatcherQueue.HasThreadAccess)
{
return CurrentWindowId.HasValue
? DisplayInformation.CreateForWindowId(CurrentWindowId.Value)
: null;
}
else
{
DisplayInformation? displayInfoInit = null;
dispatcherQueue.TryEnqueue(() =>
{
displayInfoInit = CurrentWindowId.HasValue
? DisplayInformation.CreateForWindowId(CurrentWindowId.Value)
: null;
});
return displayInfoInit;
}
}
catch (Exception ex)
{
Logger.LogWriteLine($"An error has occured while getting display information\r\n{ex}", LogType.Error, true);
}
return null;
}
}

internal static DisplayAdvancedColorInfo? CurrentWindowDisplayColorInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ public GenshinGameSettingsPage()
RegistryWatcher = new RegistryMonitor(RegistryHive.CurrentUser, Path.Combine($"Software\\{CurrentGameProperty._GameVersion.VendorTypeProp.VendorType}", CurrentGameProperty._GameVersion.GamePreset.InternalGameNameInConfig!));
ToggleRegistrySubscribe(true);
});


#nullable enable
// ReSharper disable once UnusedVariable
DisplayAdvancedColorInfo colorInfo = WindowUtility.CurrentWindowDisplayColorInfo;
DisplayAdvancedColorInfo? colorInfo = WindowUtility.CurrentWindowDisplayColorInfo;
#if SIMULATEGIHDR
IsHDREnabled = true;
IsHDRSupported = true;
#else
IsHDREnabled = colorInfo.CurrentAdvancedColorKind == DisplayAdvancedColorKind.HighDynamicRange;
IsHDRSupported = colorInfo.IsAdvancedColorKindAvailable(DisplayAdvancedColorKind.HighDynamicRange);
IsHDREnabled = colorInfo?.CurrentAdvancedColorKind == DisplayAdvancedColorKind.HighDynamicRange;
IsHDRSupported = colorInfo?.IsAdvancedColorKindAvailable(DisplayAdvancedColorKind.HighDynamicRange) ?? false;
#endif
#nullable restore

LoadPage();
}
Expand Down

0 comments on commit 9273156

Please sign in to comment.