Skip to content

Commit 74306f0

Browse files
committed
Use PInvoke to register SystemEvents
1 parent 3cd7c3c commit 74306f0

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ WM_GETTEXT
8383
OpenProcess
8484
QueryFullProcessImageName
8585
EVENT_OBJECT_HIDE
86-
EVENT_SYSTEM_DIALOGEND
86+
EVENT_SYSTEM_DIALOGEND
87+
88+
WM_POWERBROADCAST
89+
PBT_APMRESUMEAUTOMATIC

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
338338
public const int SC_MAXIMIZE = (int)PInvoke.SC_MAXIMIZE;
339339
public const int SC_MINIMIZE = (int)PInvoke.SC_MINIMIZE;
340340

341+
public const int WM_POWERBROADCAST = (int)PInvoke.WM_POWERBROADCAST;
342+
public const int PBT_APMRESUMEAUTOMATIC = (int)PInvoke.PBT_APMRESUMEAUTOMATIC;
343+
341344
#endregion
342345

343346
#region Window Handle

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
using Flow.Launcher.Plugin.SharedCommands;
2626
using Flow.Launcher.Plugin.SharedModels;
2727
using Flow.Launcher.ViewModel;
28-
using Microsoft.Win32;
2928
using ModernWpf.Controls;
3029
using DataObject = System.Windows.DataObject;
3130
using Key = System.Windows.Input.Key;
@@ -96,7 +95,6 @@ public MainWindow()
9695

9796
InitSoundEffects();
9897
DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste);
99-
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
10098
_viewModel.ActualApplicationThemeChanged += ViewModel_ActualApplicationThemeChanged;
10199
}
102100

@@ -670,6 +668,16 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
670668
handled = true;
671669
}
672670
break;
671+
case Win32Helper.WM_POWERBROADCAST: // Handle power broadcast messages
672+
// https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast
673+
if (wParam.ToInt32() == Win32Helper.PBT_APMRESUMEAUTOMATIC)
674+
{
675+
// Fix for sound not playing after sleep / hibernate
676+
// https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps
677+
InitSoundEffects();
678+
}
679+
handled = true;
680+
break;
673681
}
674682

675683
return IntPtr.Zero;
@@ -679,16 +687,6 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
679687

680688
#region Window Sound Effects
681689

682-
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
683-
{
684-
// Fix for sound not playing after sleep / hibernate
685-
// https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps
686-
if (e.Mode == PowerModes.Resume)
687-
{
688-
InitSoundEffects();
689-
}
690-
}
691-
692690
private void InitSoundEffects()
693691
{
694692
if (_settings.WMPInstalled)
@@ -1443,7 +1441,6 @@ protected virtual void Dispose(bool disposing)
14431441
animationSoundWMP?.Close();
14441442
animationSoundWPF?.Dispose();
14451443
_viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged;
1446-
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
14471444
}
14481445

14491446
_disposed = true;

0 commit comments

Comments
 (0)