Files
PowerToys/src/settings-ui/Settings.UI/Services/ActivationService.cs

106 lines
3.6 KiB
C#
Raw Normal View History

// 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;
2020-03-11 10:43:32 -07:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Activation;
[settings-ui] Settings WinUI3 (#17797) * Add Settings.WinUI3 project * New namespace * Activation and Services * Assets and Behaviors * Converters and Helpers * Controls * View and ViewModels * Styles and Themes * OOBE * Strings * Small App moves * [check] Project files - publish profiles and launchSettings.json * [using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name workaround * [WIP] Workarounds to make it work * Fix suppressed warnings - naming * Add code analysis * Fix KBMPage and App dispatcher Fix MessageBox - replace with MessageDialog * Fix ImageResizerPage & mark ColorPickerButton with TODO * Add icon to windows Cleanup MainWindow.xaml.cs and OobeWindow.xaml.cs MainWindows and OobeWindow management * App Icon No framework and runtime subdirs * Remove PowerToys.Settings and Settings.UI from solution Update output paths * Installer work & publish.cmd * Fix dispatcher crashes * Fix crashes * Add all dlls to installer Cleanup installer Add OpenOOBE and OpenScoobe logic Fix minor issues Fix update scenario - REINSTALLMODE * Rename back namespaces, project name and project dir * [wip] move to winappsdk 1.1 * Fix propagating isElevated & installer runtimes dlls * Remove obsolete dir/file * PowerToys.Interop to netstandard2.0 * Move everything to .Net6 * [Settings] Always launch settings process non-elevated (#17791) * Move back to WinAppSdk 1.0.1 * Add Settings.WinUI3 project * New namespace * Activation and Services * Assets and Behaviors * Converters and Helpers * Controls * View and ViewModels * Styles and Themes * OOBE * Strings * Small App moves * [check] Project files - publish profiles and launchSettings.json * [using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name workaround * [WIP] Workarounds to make it work * Fix suppressed warnings - naming * Add code analysis * Fix KBMPage and App dispatcher Fix MessageBox - replace with MessageDialog * Fix ImageResizerPage & mark ColorPickerButton with TODO * Add icon to windows Cleanup MainWindow.xaml.cs and OobeWindow.xaml.cs MainWindows and OobeWindow management * App Icon No framework and runtime subdirs * Remove PowerToys.Settings and Settings.UI from solution Update output paths * Installer work & publish.cmd * Fix dispatcher crashes * Fix crashes * Add all dlls to installer Cleanup installer Add OpenOOBE and OpenScoobe logic Fix minor issues Fix update scenario - REINSTALLMODE * Rename back namespaces, project name and project dir * [wip] move to winappsdk 1.1 * Fix propagating isElevated & installer runtimes dlls * Remove obsolete dir/file * PowerToys.Interop to netstandard2.0 * Move everything to .Net6 * [Settings] Always launch settings process non-elevated (#17791) * Move back to WinAppSdk 1.0.1 * Revert merge conflict ARM64 removal * Fix KBM Browse overlay image button * Bring back settings publish profile * Update release.yml * Change target frameworkd windows version * [Setup] Add Windows Application Runtime SDK (#17809) * Update requirements doc * Update compiling docs * Fix signing * Fix Settings exe and dll versions * Add exception for dlls that have version 1.0.0.0 * Fix powershell condition Co-authored-by: Andrey Nekrasov <yuyoyuppe@users.noreply.github.com>
2022-04-19 22:00:28 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
2020-03-11 10:43:32 -07:00
using Windows.ApplicationModel.Activation;
namespace Microsoft.PowerToys.Settings.UI.Services
{
// For more information on understanding and extending activation flow see
// https://github.com/Microsoft/WindowsTemplateStudio/blob/master/docs/activation.md
internal sealed class ActivationService
2020-03-11 10:43:32 -07:00
{
private readonly App app;
private readonly Type defaultNavItem;
private Lazy<UIElement> shell;
2020-03-11 10:43:32 -07:00
private object lastActivationArgs;
2020-03-11 10:43:32 -07:00
public ActivationService(App app, Type defaultNavItem, Lazy<UIElement> shell = null)
{
this.app = app;
this.shell = shell;
this.defaultNavItem = defaultNavItem;
2020-03-11 10:43:32 -07:00
}
public async Task ActivateAsync(object activationArgs)
{
if (IsInteractive(activationArgs))
2020-03-11 10:43:32 -07:00
{
// Initialize services that you need before app activation
// take into account that the splash screen is shown while this code runs.
await InitializeAsync().ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (Window.Current.Content == null)
{
// Create a Shell or Frame to act as the navigation context
Window.Current.Content = shell?.Value ?? new Frame();
2020-03-11 10:43:32 -07:00
}
}
// Depending on activationArgs one of ActivationHandlers or DefaultActivationHandler
// will navigate to the first page
await HandleActivationAsync(activationArgs).ConfigureAwait(false);
lastActivationArgs = activationArgs;
2020-03-11 10:43:32 -07:00
if (IsInteractive(activationArgs))
2020-03-11 10:43:32 -07:00
{
// Ensure the current window is active
Window.Current.Activate();
// Tasks after activation
await StartupAsync().ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
}
}
private static async Task InitializeAsync()
2020-03-11 10:43:32 -07:00
{
await Task.CompletedTask.ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
}
private async Task HandleActivationAsync(object activationArgs)
{
var activationHandler = GetActivationHandlers()
2020-03-11 10:43:32 -07:00
.FirstOrDefault(h => h.CanHandle(activationArgs));
if (activationHandler != null)
{
await activationHandler.HandleAsync(activationArgs).ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
}
if (IsInteractive(activationArgs))
2020-03-11 10:43:32 -07:00
{
var defaultHandler = new DefaultActivationHandler(defaultNavItem);
2020-03-11 10:43:32 -07:00
if (defaultHandler.CanHandle(activationArgs))
{
await defaultHandler.HandleAsync(activationArgs).ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
}
}
}
private static async Task StartupAsync()
2020-03-11 10:43:32 -07:00
{
await Task.CompletedTask.ConfigureAwait(false);
2020-03-11 10:43:32 -07:00
}
private static IEnumerable<ActivationHandler> GetActivationHandlers()
2020-03-11 10:43:32 -07:00
{
yield break;
}
private static bool IsInteractive(object args)
2020-03-11 10:43:32 -07:00
{
return args is IActivatedEventArgs;
}
}
}