diff --git a/XClipper.App/App.xaml.cs b/XClipper.App/App.xaml.cs index 324a7700..4b58d2a7 100644 --- a/XClipper.App/App.xaml.cs +++ b/XClipper.App/App.xaml.cs @@ -121,13 +121,9 @@ protected override void OnStartup(StartupEventArgs e) { if (clipWindow.IsVisible) clipWindow.CloseWindow(); }); - //ApplicationHelper.AttachAppWindowDeactivation(clipWindow, clipWindow.CloseWindow); - - // clipWindow.Deactivated += OnDeactivated; licenseService.Initiate(err => { - // if (err is InvalidLicenseException) return; if (err != null && err is not InvalidLicenseException) { MsgBoxHelper.ShowError(err.Message); diff --git a/XClipper.Core/Helpers/ApplicationHelper.cs b/XClipper.Core/Helpers/ApplicationHelper.cs index e132301c..0944ea5b 100644 --- a/XClipper.Core/Helpers/ApplicationHelper.cs +++ b/XClipper.Core/Helpers/ApplicationHelper.cs @@ -38,6 +38,8 @@ public static void SendAction(Action action) private static DispatcherTimer dtimer; + private static volatile bool applicationActive = false; + /// /// This will provide a callback whenever the application process is not foreground. /// @@ -45,17 +47,23 @@ public static void SendAction(Action action) public static void AttachForegroundProcess(Action block) { /** I do not support this logic, maybe in future I'll find a better solution. */ - dtimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) }; + + Application.Current.Activated += (o, e) => { applicationActive = true; }; + Application.Current.Deactivated += (o, e) => { + block.Invoke(); + applicationActive = false; + }; + + dtimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(150) }; dtimer.Tick += delegate { - dtimer.Stop(); IntPtr handle = GetForegroundWindow(); bool isActive = IsActivated(handle); - if (!isActive) + if (!isActive && applicationActive) { block.Invoke(); + applicationActive = false; } - dtimer.Start(); }; dtimer.Start(); } @@ -91,7 +99,7 @@ public static void GlobalActivate(this Window w) { var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero); var thisWindowThreadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero); - Thread.Sleep(70); + //Thread.Sleep(70); w.Show(); w.Activate(); @@ -107,16 +115,17 @@ public static void GlobalActivate(this Window w) { SetForegroundWindow(hWnd); } - Task.Run(async () => - { - if (dtimer != null) - { - await Task.Delay(100); - Application.Current.Dispatcher.Invoke(() => SetFocus(hWnd)); - await Task.Delay(200); - dtimer.Start(); - } - }); + if (dtimer != null) dtimer.Start(); + //Task.Run(async () => + //{ + // if (dtimer != null) + // { + // await Task.Delay(100); + // Application.Current.Dispatcher.Invoke(() => SetFocus(hWnd)); + // await Task.Delay(200); + // dtimer.Start(); + // } + //}); } }