microsoft / microsoft/WindowsAppSDK

Window.Activated do not fire the event for "Deactivated" and "Activated" status if being activated by "RedirectActivationToAsync" just now

Open
#6,579 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-Activation needs-triage
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

Describe the bug

If a process that just being activated by another process instance using AppInstance.RedirectActivationToAsync(). Then the Window.Activated would not fire for "Deactivated" status when the windows lost its focus, as consequence, "Window.Activated" event will not fire for the following "PointerActivated" status too.

Details:

1. Process A has Window
2. Process A is the target instance of redirect
3. Process A window fire "Activated" event with WindowActivationState.CodeActivated
4. Process A window doesn't look like get the window focus because no focus border around the window even "Activated" event fired just now [ERROR]
5. Click anywhere out of Process A window
6. Process A lost window focus
7. Process A do not fire "Activated" event with WindowActivationState.Deactivated [ERROR]
8. Click anywhere within Process A window
9. Process A get window focus
10. Process A do not fire "Activated" event with WindowActivationState.PointerActivated  [ERROR]
11. Click anywhere out of Process A window
12. Process A do not fire "Activated" event with WindowActivationState.Deactivated [CORRECT]
13. Process A do not fire "Activated" event with WindowActivationState.PointerActivated [CORRECT]
if (AppInstance.GetCurrent()?.GetActivatedEventArgs() is AppActivationArguments ActivationArguments)
{
    AppInstance TargetInstance = AppInstance.FindOrRegisterForKey("<My Process Instance ID>");

    if (!TargetInstance.IsCurrent)
    {
        await TargetInstance.RedirectActivationToAsync(ActivationArguments);
    }
}
// Hook activated event first
// Activated += MainWindow_Activated;
private async void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
{
    args.Handled = true;

    Debug.WriteLine($"<Your Process ID Here> Window Status Changed: {args.WindowActivationState}");

    if (args.WindowActivationState != WindowActivationState.Deactivated)
    {
            Debug.WriteLine($"<Your Process ID Here> Window Activated");
    }
}
Activate the Window and deactivate it
1. Window activated and deactivated by mouse (Correct):
[44cada64-82c4-43f4-87a7-329184d1c312] Window Status Changed: CodeActivated
[44cada64-82c4-43f4-87a7-329184d1c312] Window Activated
[44cada64-82c4-43f4-87a7-329184d1c312] Window Status Changed: Deactivated
2. Window activated by AppInstance.RedirectActivationToAsync() and deactivated by mouse (Missing Deactivated):
[44cada64-82c4-43f4-87a7-329184d1c312] Window Status Changed: CodeActivated
[44cada64-82c4-43f4-87a7-329184d1c312] Window Activated
// No log for Deactivated
3. Expected Log:
[44cada64-82c4-43f4-87a7-329184d1c312] Window Status Changed: CodeActivated
[44cada64-82c4-43f4-87a7-329184d1c312] Window Activated
[44cada64-82c4-43f4-87a7-329184d1c312] Window Status Changed: Deactivated
Steps to reproduce the bug
  1. Download the demo: DemoDeactivatedApp.zip
  2. Build and run the demo
  3. See main window show
  4. Activate or deactivate the window using mouse
  5. Log print to Visual Studio normal
  6. Launch another process by clicking the app icon in Windows Start
  7. New launch will be redirected to the first process
  8. First process's window activated
  9. Click anywhere else out of the demo window
  10. No log for window deactivated (Event not fired)
Expected behavior

Deactivated event should fire when window deactivated

Screenshots

No response

NuGet package version

2.2.0

Packaging type

Packaged (MSIX)

Windows version

Windows 11 version 24H2 LTSC (26100, June Update)

IDE

Visual Studio 2022

Additional context

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied DemoDeactivatedApp.zip and the AppInstance.RedirectActivationToAsync flow, then inspect how Window.Activated reports CodeActivated, Deactivated, and PointerActivated. Reproduce on Windows 11 24H2 with Windows App SDK 2.2.0 and compare the redirected activation logs with ordinary mouse activation. Done means the redirected window reports Deactivated and subsequent PointerActivated events as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.