diff --git a/windows/TweetDuck/Browser/Notification/FormNotificationTweet.cs b/windows/TweetDuck/Browser/Notification/FormNotificationTweet.cs index 540ecbec..95f8e157 100644 --- a/windows/TweetDuck/Browser/Notification/FormNotificationTweet.cs +++ b/windows/TweetDuck/Browser/Notification/FormNotificationTweet.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; +using TweetDuck.Management; using TweetDuck.Utils; using TweetLib.Browser.Interfaces; using TweetLib.Core.Features.Notifications; @@ -40,13 +41,27 @@ protected override bool CanDragWindow { InitializeComponent(); Config.MuteToggled += Config_MuteToggled; - Disposed += (sender, args) => Config.MuteToggled -= Config_MuteToggled; + WindowsSessionManager.LockStateChanged += WindowsSessionManager_LockStateChanged; + + Disposed += (sender, args) => { + Config.MuteToggled -= Config_MuteToggled; + WindowsSessionManager.LockStateChanged -= WindowsSessionManager_LockStateChanged; + }; if (Config.MuteNotifications) { PauseNotification(NotificationPauseReason.UserConfiguration); } } + private void WindowsSessionManager_LockStateChanged(object sender, EventArgs e) { + if (WindowsSessionManager.IsLocked) { + PauseNotification(NotificationPauseReason.WindowsSessionLocked); + } + else { + ResumeNotification(NotificationPauseReason.WindowsSessionLocked); + } + } + protected override void WndProc(ref Message m) { if (m.Msg == 0x00A7) { // WM_NCMBUTTONDOWN int hitTest = m.WParam.ToInt32(); diff --git a/windows/TweetDuck/Management/WindowsSessionManager.cs b/windows/TweetDuck/Management/WindowsSessionManager.cs new file mode 100644 index 00000000..61336305 --- /dev/null +++ b/windows/TweetDuck/Management/WindowsSessionManager.cs @@ -0,0 +1,34 @@ +using System; +using Microsoft.Win32; +using Win = System.Windows.Forms; + +namespace TweetDuck.Management { + static class WindowsSessionManager { + public static bool IsLocked { get; private set; } = false; + public static event EventHandler LockStateChanged; + + public static void Register() { + Win.Application.ApplicationExit += OnApplicationExit; + SystemEvents.SessionSwitch += OnSessionSwitch; + } + + private static void OnApplicationExit(object sender, EventArgs e) { + SystemEvents.SessionSwitch -= OnSessionSwitch; + } + + private static void OnSessionSwitch(object sender, SessionSwitchEventArgs e) { + var reason = e.Reason; + if (reason == SessionSwitchReason.SessionLock) { + SetLocked(true); + } + else if (reason == SessionSwitchReason.SessionUnlock) { + SetLocked(false); + } + } + + private static void SetLocked(bool newState) { + IsLocked = newState; + LockStateChanged?.Invoke(null, EventArgs.Empty); + } + } +} diff --git a/windows/TweetDuck/Program.cs b/windows/TweetDuck/Program.cs index c3eecad3..4818e905 100644 --- a/windows/TweetDuck/Program.cs +++ b/windows/TweetDuck/Program.cs @@ -50,6 +50,7 @@ internal static void SetupWinForms() { [STAThread] private static void Main() { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + WindowsSessionManager.Register(); SetupWinForms(); Cef.EnableHighDPISupport(); diff --git a/windows/TweetDuck/TweetDuck.csproj b/windows/TweetDuck/TweetDuck.csproj index 2e0ac95b..4179c715 100644 --- a/windows/TweetDuck/TweetDuck.csproj +++ b/windows/TweetDuck/TweetDuck.csproj @@ -141,6 +141,7 @@ <Compile Include="Management\LockManager.cs" /> <Compile Include="Management\ProfileManager.cs" /> <Compile Include="Management\VideoPlayer.cs" /> + <Compile Include="Management\WindowsSessionManager.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Reporter.cs" />