2020-04-10 15:22:07 -07:00
|
|
|
|
// 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.
|
|
|
|
|
|
|
2020-10-29 14:24:16 -07:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-03-11 10:43:32 -07:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Activation
|
|
|
|
|
|
{
|
|
|
|
|
|
// For more information on understanding and extending activation flow see
|
|
|
|
|
|
// https://github.com/Microsoft/WindowsTemplateStudio/blob/master/docs/activation.md
|
|
|
|
|
|
internal abstract class ActivationHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
public abstract bool CanHandle(object args);
|
|
|
|
|
|
|
|
|
|
|
|
public abstract Task HandleAsync(object args);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 14:24:16 -07:00
|
|
|
|
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "abstract T and abstract")]
|
2020-03-11 10:43:32 -07:00
|
|
|
|
internal abstract class ActivationHandler<T> : ActivationHandler
|
|
|
|
|
|
where T : class
|
|
|
|
|
|
{
|
|
|
|
|
|
public override async Task HandleAsync(object args)
|
|
|
|
|
|
{
|
2020-10-29 14:24:16 -07:00
|
|
|
|
await HandleInternalAsync(args as T).ConfigureAwait(false);
|
2020-03-11 10:43:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool CanHandle(object args)
|
|
|
|
|
|
{
|
|
|
|
|
|
// CanHandle checks the args is of type you have configured
|
2020-04-10 15:22:07 -07:00
|
|
|
|
return args is T && CanHandleInternal(args as T);
|
2020-03-11 10:43:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-07 10:19:14 -07:00
|
|
|
|
// Override this method to add the activation logic in your activation handler
|
|
|
|
|
|
protected abstract Task HandleInternalAsync(T args);
|
|
|
|
|
|
|
2020-03-11 10:43:32 -07:00
|
|
|
|
// You can override this method to add extra validation on activation args
|
|
|
|
|
|
// to determine if your ActivationHandler should handle this activation args
|
|
|
|
|
|
protected virtual bool CanHandleInternal(T args)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|