Fix setting window freeze issue & Improve singleton window opener

This commit is contained in:
Jack251970 2025-01-27 09:11:27 +08:00 committed by Jeremy
parent aa8f4727ab
commit 70c8ea18fc
2 changed files with 19 additions and 6 deletions

View file

@ -10,16 +10,29 @@ public static class SingletonWindowOpener
{
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(x => x.GetType() == typeof(T))
?? (T)Activator.CreateInstance(typeof(T), args);
// Fix UI bug
// Add `window.WindowState = WindowState.Normal`
// If only use `window.Show()`, Settings-window doesn't show when minimized in taskbar
// Not sure why this works tho
// Probably because, when `.Show()` fails, `window.WindowState == Minimized` (not `Normal`)
// https://stackoverflow.com/a/59719760/4230390
window.WindowState = WindowState.Normal;
window.Show();
// Ensure the window is not minimized before showing it
if (window.WindowState == WindowState.Minimized)
{
window.WindowState = WindowState.Normal;
}
// Ensure the window is visible
if (!window.IsVisible)
{
window.Show();
}
else
{
window.Activate(); // Bring the window to the foreground if already open
}
window.Focus();
return (T)window;

View file

@ -35,11 +35,11 @@ public partial class SettingWindow
private void OnLoaded(object sender, RoutedEventArgs e)
{
RefreshMaximizeRestoreButton();
// Fix (workaround) for the window freezes after lock screen (Win+L)
// Fix (workaround) for the window freezes after lock screen (Win+L) or sleep
// https://stackoverflow.com/questions/4951058/software-rendering-mode-wpf
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
HwndTarget hwndTarget = hwndSource.CompositionTarget;
hwndTarget.RenderMode = RenderMode.Default;
hwndTarget.RenderMode = RenderMode.SoftwareOnly; // Must use software only render mode here
InitializePosition();
}