mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
This PR stops us from synchronously initializing the settings page for every extension (including built-in's) on startup. That incurs a small penalty that really adds up the more extensions a user has. Instead, we'll now only initialize the `CommandSettings` object when we first actually need it. From a relatively unscientific test, this saves approximately 10% on the initialization of builtin commands, and for my setup, it trims about 28% off extension initialization (across all built-in's / extensions): branch | Built-in load (ms) | Extension load (ms) | %Δ builtin | %Δ extensions | -- | -- | -- | -- | -- | main | 1455 | 6867.6 | | | this PR | 1309.2 | 4919 | -10.02% | -28.37% Closes #38321
61 lines
1.7 KiB
C#
61 lines
1.7 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 ManagedCommon;
|
|
using Microsoft.CmdPal.UI.ViewModels.Models;
|
|
using Microsoft.CommandPalette.Extensions;
|
|
|
|
namespace Microsoft.CmdPal.UI.ViewModels;
|
|
|
|
public partial class CommandSettingsViewModel(ICommandSettings? _unsafeSettings, CommandProviderWrapper provider, TaskScheduler mainThread)
|
|
{
|
|
private readonly ExtensionObject<ICommandSettings> _model = new(_unsafeSettings);
|
|
|
|
public ContentPageViewModel? SettingsPage { get; private set; }
|
|
|
|
public bool Initialized { get; private set; }
|
|
|
|
public bool HasSettings =>
|
|
_model.Unsafe != null && // We have a settings model AND
|
|
(!Initialized || SettingsPage != null); // we weren't initialized, OR we were, and we do have a settings page
|
|
|
|
private void UnsafeInitializeProperties()
|
|
{
|
|
var model = _model.Unsafe;
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (model.SettingsPage is IContentPage page)
|
|
{
|
|
SettingsPage = new(page, mainThread, provider.ExtensionHost);
|
|
SettingsPage.InitializeProperties();
|
|
}
|
|
}
|
|
|
|
public void SafeInitializeProperties()
|
|
{
|
|
try
|
|
{
|
|
UnsafeInitializeProperties();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError($"Failed to load settings page", ex: ex);
|
|
}
|
|
|
|
Initialized = true;
|
|
}
|
|
|
|
public void DoOnUiThread(Action action)
|
|
{
|
|
Task.Factory.StartNew(
|
|
action,
|
|
CancellationToken.None,
|
|
TaskCreationOptions.None,
|
|
mainThread);
|
|
}
|
|
}
|