Skip to content

Commit

Permalink
[Improving] Windows app's activation & deactivation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhPatange committed Oct 1, 2021
1 parent 2a47748 commit 2d59d91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
4 changes: 0 additions & 4 deletions XClipper.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 24 additions & 15 deletions XClipper.Core/Helpers/ApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,32 @@ public static void SendAction(Action action)

private static DispatcherTimer dtimer;

private static volatile bool applicationActive = false;

/// <summary>
/// This will provide a callback whenever the application process is not foreground.
/// </summary>
/// <param name="block"></param>
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();
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
// }
//});
}
}

Expand Down

0 comments on commit 2d59d91

Please sign in to comment.