mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-24 04:00:02 +01:00
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This PR: - Fixes the passing of activation arguments between instances. - Adds defensive handling of exceptions if the activation argument is corrupted. - Ensures that the activation method is consistently handled on the main thread. - Updates the condition under which the main window is displayed after startup or activation. The main window will now be automatically shown when the app is started or activated, except in the following cases: - when started as a startup task, - when started using x-cmdpal://background, - when started using x-cmdpal://settings. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] **Closes:** #39681 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Mike Griese <migrie@microsoft.com>
137 lines
4.6 KiB
C#
137 lines
4.6 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Runtime.InteropServices;
|
|
using ManagedCommon;
|
|
using Microsoft.CmdPal.UI.Events;
|
|
using Microsoft.PowerToys.Telemetry;
|
|
using Microsoft.UI.Dispatching;
|
|
using Microsoft.Windows.AppLifecycle;
|
|
using Windows.Win32;
|
|
using Windows.Win32.Foundation;
|
|
using Windows.Win32.System.Com;
|
|
using Windows.Win32.UI.WindowsAndMessaging;
|
|
|
|
namespace Microsoft.CmdPal.UI;
|
|
|
|
// cribbed heavily from
|
|
//
|
|
// https://github.com/microsoft/WindowsAppSDK-Samples/tree/main/Samples/AppLifecycle/Instancing/cs2/cs-winui-packaged/CsWinUiDesktopInstancing
|
|
internal sealed class Program
|
|
{
|
|
private static DispatcherQueueSynchronizationContext? uiContext;
|
|
private static App? app;
|
|
|
|
// LOAD BEARING
|
|
//
|
|
// Main cannot be async. If it is, then the clipboard won't work, and neither will narrator.
|
|
// That means you, the person thinking about making this a MTA thread. Don't
|
|
// do it. It won't work. That's not the solution.
|
|
[STAThread]
|
|
private static int Main(string[] args)
|
|
{
|
|
if (Helpers.GpoValueChecker.GetConfiguredCmdPalEnabledValue() == Helpers.GpoRuleConfiguredValue.Disabled)
|
|
{
|
|
// There's a GPO rule configured disabling CmdPal. Exit as soon as possible.
|
|
return 0;
|
|
}
|
|
|
|
try
|
|
{
|
|
Logger.InitializeLogger("\\CmdPal\\Logs\\");
|
|
}
|
|
catch (COMException e)
|
|
{
|
|
// This is unexpected. For the sake of debugging:
|
|
// pop a message box
|
|
PInvoke.MessageBox(
|
|
(HWND)IntPtr.Zero,
|
|
$"Failed to initialize the logger. COMException: \r{e.Message}",
|
|
"Command Palette",
|
|
MESSAGEBOX_STYLE.MB_OK | MESSAGEBOX_STYLE.MB_ICONERROR);
|
|
return 0;
|
|
}
|
|
catch (Exception e2)
|
|
{
|
|
// This is unexpected. For the sake of debugging:
|
|
// pop a message box
|
|
PInvoke.MessageBox(
|
|
(HWND)IntPtr.Zero,
|
|
$"Failed to initialize the logger. Unknown Exception: \r{e2.Message}",
|
|
"Command Palette",
|
|
MESSAGEBOX_STYLE.MB_OK | MESSAGEBOX_STYLE.MB_ICONERROR);
|
|
return 0;
|
|
}
|
|
|
|
Logger.LogDebug($"Starting at {DateTime.UtcNow}");
|
|
PowerToysTelemetry.Log.WriteEvent(new CmdPalProcessStarted());
|
|
|
|
WinRT.ComWrappersSupport.InitializeComWrappers();
|
|
var isRedirect = DecideRedirection();
|
|
if (!isRedirect)
|
|
{
|
|
Microsoft.UI.Xaml.Application.Start((p) =>
|
|
{
|
|
uiContext = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
|
|
SynchronizationContext.SetSynchronizationContext(uiContext);
|
|
app = new App();
|
|
});
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static bool DecideRedirection()
|
|
{
|
|
var isRedirect = false;
|
|
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
|
|
var keyInstance = AppInstance.FindOrRegisterForKey("randomKey");
|
|
|
|
if (keyInstance.IsCurrent)
|
|
{
|
|
PowerToysTelemetry.Log.WriteEvent(new ColdLaunch());
|
|
keyInstance.Activated += OnActivated;
|
|
}
|
|
else
|
|
{
|
|
isRedirect = true;
|
|
PowerToysTelemetry.Log.WriteEvent(new ReactivateInstance());
|
|
RedirectActivationTo(args, keyInstance);
|
|
}
|
|
|
|
return isRedirect;
|
|
}
|
|
|
|
private static void RedirectActivationTo(AppActivationArguments args, AppInstance keyInstance)
|
|
{
|
|
// Do the redirection on another thread, and use a non-blocking
|
|
// wait method to wait for the redirection to complete.
|
|
var redirectSemaphore = new Semaphore(0, 1);
|
|
Task.Run(() =>
|
|
{
|
|
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
|
|
redirectSemaphore.Release();
|
|
});
|
|
_ = PInvoke.CoWaitForMultipleObjects(
|
|
(uint)CWMO_FLAGS.CWMO_DEFAULT,
|
|
PInvoke.INFINITE,
|
|
[new HANDLE(redirectSemaphore.SafeWaitHandle.DangerousGetHandle())],
|
|
out _);
|
|
}
|
|
|
|
private static void OnActivated(object? sender, AppActivationArguments args)
|
|
{
|
|
// If we already have a form, display the message now.
|
|
// Otherwise, add it to the collection for displaying later.
|
|
if (App.Current is App thisApp)
|
|
{
|
|
if (thisApp.AppWindow is not null and
|
|
MainWindow mainWindow)
|
|
{
|
|
uiContext?.Post(_ => mainWindow.HandleLaunch(args), null);
|
|
}
|
|
}
|
|
}
|
|
}
|