mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02: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 introduces new types of IContent: - Plain text content – simple and straightforward, with options to switch between UI and monospace fonts and toggle word wrap. - It's super safe to display any random text content without having to worry about escaping the pesky markdown. - Image viewer content – a more polished way to display images: - When placed in the ContentPage, the component automatically resizes to fit the viewport, ensuring the entire image is visible at once. - Images can be opened in a built-in mini-viewer that lets you view, pan, and zoom without leaving the Command Palette. (Doing this directly on the page proved to be a UX and development headache.) Fully keyboard-controllable, so there’s no need to take your hands off the keys. ## Pictures? Pictures! Plain text content: <img width="960" height="604" alt="image" src="https://github.com/user-attachments/assets/a4ec36f3-2f7f-4a2a-a646-53056c512023" /> Image viewer content: <img width="939" height="605" alt="image" src="https://github.com/user-attachments/assets/c87f5726-8cd0-4015-b2d9-f1457fa1ec96" /> https://github.com/user-attachments/assets/915cd9d2-e4e3-4baf-8df6-6a328a3592ba <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #41038 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **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
266 lines
11 KiB
C#
266 lines
11 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.Common;
|
|
using Microsoft.CmdPal.Common.Helpers;
|
|
using Microsoft.CmdPal.Common.Services;
|
|
using Microsoft.CmdPal.Common.Text;
|
|
using Microsoft.CmdPal.Ext.Apps;
|
|
using Microsoft.CmdPal.Ext.Bookmarks;
|
|
using Microsoft.CmdPal.Ext.Calc;
|
|
using Microsoft.CmdPal.Ext.ClipboardHistory;
|
|
using Microsoft.CmdPal.Ext.Indexer;
|
|
using Microsoft.CmdPal.Ext.PerformanceMonitor;
|
|
using Microsoft.CmdPal.Ext.Registry;
|
|
using Microsoft.CmdPal.Ext.RemoteDesktop;
|
|
using Microsoft.CmdPal.Ext.Shell;
|
|
using Microsoft.CmdPal.Ext.System;
|
|
using Microsoft.CmdPal.Ext.TimeDate;
|
|
using Microsoft.CmdPal.Ext.WebSearch;
|
|
using Microsoft.CmdPal.Ext.WindowsServices;
|
|
using Microsoft.CmdPal.Ext.WindowsSettings;
|
|
using Microsoft.CmdPal.Ext.WindowsTerminal;
|
|
using Microsoft.CmdPal.Ext.WindowWalker;
|
|
using Microsoft.CmdPal.Ext.WinGet;
|
|
using Microsoft.CmdPal.UI.Helpers;
|
|
using Microsoft.CmdPal.UI.Services;
|
|
using Microsoft.CmdPal.UI.ViewModels;
|
|
using Microsoft.CmdPal.UI.ViewModels.BuiltinCommands;
|
|
using Microsoft.CmdPal.UI.ViewModels.Dock;
|
|
using Microsoft.CmdPal.UI.ViewModels.Models;
|
|
using Microsoft.CmdPal.UI.ViewModels.Services;
|
|
using Microsoft.CommandPalette.Extensions;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.PowerToys.Telemetry;
|
|
using Microsoft.UI.Dispatching;
|
|
using Microsoft.UI.Xaml;
|
|
|
|
// To learn more about WinUI, the WinUI project structure,
|
|
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
|
namespace Microsoft.CmdPal.UI;
|
|
|
|
/// <summary>
|
|
/// Provides application-specific behavior to supplement the default Application class.
|
|
/// </summary>
|
|
public partial class App : Application, IDisposable
|
|
{
|
|
private readonly GlobalErrorHandler _globalErrorHandler = new();
|
|
|
|
/// <summary>
|
|
/// Gets the current <see cref="App"/> instance in use.
|
|
/// </summary>
|
|
public static new App Current => (App)Application.Current;
|
|
|
|
public Window? AppWindow { get; private set; }
|
|
|
|
public ETWTrace EtwTrace { get; private set; } = new ETWTrace();
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
|
|
/// </summary>
|
|
public IServiceProvider Services { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="App"/> class.
|
|
/// Initializes the singleton application object. This is the first line of authored code
|
|
/// executed, and as such is the logical equivalent of main() or WinMain().
|
|
/// </summary>
|
|
public App()
|
|
{
|
|
var appInfoService = new ApplicationInfoService();
|
|
|
|
#if !CMDPAL_DISABLE_GLOBAL_ERROR_HANDLER
|
|
_globalErrorHandler.Register(this, GlobalErrorHandler.Options.Default, appInfoService);
|
|
#endif
|
|
|
|
Services = ConfigureServices(appInfoService);
|
|
|
|
IconProvider.Initialize(Services);
|
|
|
|
this.InitializeComponent();
|
|
|
|
// Ensure types used in XAML are preserved for AOT compilation
|
|
TypePreservation.PreserveTypes();
|
|
|
|
NativeEventWaiter.WaitForEventLoop(
|
|
"Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd", () =>
|
|
{
|
|
EtwTrace?.Dispose();
|
|
AppWindow?.Close();
|
|
Environment.Exit(0);
|
|
});
|
|
|
|
// Connect the PT logging to the core project's logging.
|
|
// This way, log statements from the core project will be captured by the PT logs
|
|
var logWrapper = new LogWrapper();
|
|
CoreLogger.InitializeLogger(logWrapper);
|
|
|
|
// Now that CoreLogger is initialized, initialize the logger delegate in ApplicationInfoService
|
|
appInfoService.SetLogDirectory(() => Logger.CurrentVersionLogDirectoryPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when the application is launched.
|
|
/// </summary>
|
|
/// <param name="args">Details about the launch request and process.</param>
|
|
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
|
|
{
|
|
AppWindow = new MainWindow();
|
|
|
|
var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
|
|
((MainWindow)AppWindow).HandleLaunchNonUI(activatedEventArgs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures the services for the application
|
|
/// </summary>
|
|
private static ServiceProvider ConfigureServices(IApplicationInfoService appInfoService)
|
|
{
|
|
// TODO: It's in the Labs feed, but we can use Sergio's AOT-friendly source generator for this: https://github.com/CommunityToolkit/Labs-Windows/discussions/463
|
|
ServiceCollection services = new();
|
|
|
|
// Root services
|
|
services.AddSingleton(TaskScheduler.FromCurrentSynchronizationContext());
|
|
var dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
|
|
|
AddBuiltInCommands(services, appInfoService.ConfigDirectory);
|
|
|
|
AddCoreServices(services, appInfoService);
|
|
|
|
AddUIServices(services, dispatcherQueue);
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
private static void AddBuiltInCommands(ServiceCollection services, string configDirectory)
|
|
{
|
|
var providerLoadGuard = new ProviderLoadGuard(configDirectory);
|
|
|
|
// Built-in Commands. Order matters - this is the order they'll be presented by default.
|
|
var allApps = new AllAppsCommandProvider();
|
|
var files = new IndexerCommandsProvider();
|
|
files.SuppressFallbackWhen(ShellCommandsProvider.SuppressFileFallbackIf);
|
|
services.AddSingleton<ICommandProvider>(allApps);
|
|
|
|
services.AddSingleton<ICommandProvider, ShellCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, CalculatorCommandProvider>();
|
|
services.AddSingleton<ICommandProvider>(files);
|
|
services.AddSingleton<ICommandProvider, BookmarksCommandProvider>(_ => BookmarksCommandProvider.CreateWithDefaultStore());
|
|
|
|
services.AddSingleton<ICommandProvider, WindowWalkerCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, WebSearchCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, ClipboardHistoryCommandsProvider>();
|
|
|
|
// GH #38440: Users might not have WinGet installed! Or they might have
|
|
// a ridiculously old version. Or might be running as admin.
|
|
// We shouldn't explode in the App ctor if we fail to instantiate an
|
|
// instance of PackageManager, which will happen in the static ctor
|
|
// for WinGetStatics
|
|
try
|
|
{
|
|
var winget = new WinGetExtensionCommandsProvider();
|
|
winget.SetAllLookup(
|
|
query => allApps.LookupAppByPackageFamilyName(query, requireSingleMatch: true),
|
|
query => allApps.LookupAppByProductCode(query, requireSingleMatch: true));
|
|
services.AddSingleton<ICommandProvider>(winget);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError("Couldn't load winget", ex);
|
|
}
|
|
|
|
services.AddSingleton<ICommandProvider, WindowsTerminalCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, WindowsSettingsCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, RegistryCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, WindowsServicesCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, BuiltInsCommandProvider>();
|
|
services.AddSingleton<ICommandProvider, TimeDateCommandsProvider>();
|
|
services.AddSingleton<ICommandProvider, SystemCommandExtensionProvider>();
|
|
services.AddSingleton<ICommandProvider, RemoteDesktopCommandProvider>();
|
|
|
|
var performanceMonitorSoftDisabled = providerLoadGuard.IsProviderDisabled(PerformanceMonitorCommandsProvider.ProviderIdValue);
|
|
if (performanceMonitorSoftDisabled)
|
|
{
|
|
Logger.LogWarning("Performance monitor is temporarily disabled after repeated crashes. Loading placeholder pages instead of activating performance counters.");
|
|
}
|
|
|
|
if (!performanceMonitorSoftDisabled)
|
|
{
|
|
providerLoadGuard.Enter(PerformanceMonitorCommandsProvider.ProviderLoadGuardBlockId, PerformanceMonitorCommandsProvider.ProviderIdValue);
|
|
}
|
|
|
|
try
|
|
{
|
|
var performanceMonitor = new PerformanceMonitorCommandsProvider(performanceMonitorSoftDisabled);
|
|
services.AddSingleton<ICommandProvider>(performanceMonitor);
|
|
|
|
if (!performanceMonitorSoftDisabled)
|
|
{
|
|
providerLoadGuard.Exit(PerformanceMonitorCommandsProvider.ProviderLoadGuardBlockId);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (!performanceMonitorSoftDisabled)
|
|
{
|
|
providerLoadGuard.Exit(PerformanceMonitorCommandsProvider.ProviderLoadGuardBlockId);
|
|
}
|
|
|
|
Logger.LogError("Couldn't load performance monitor", ex);
|
|
}
|
|
}
|
|
|
|
private static void AddUIServices(ServiceCollection services, DispatcherQueue dispatcherQueue)
|
|
{
|
|
// Models & persistence services
|
|
services.AddSingleton<IPersistenceService, PersistenceService>();
|
|
services.AddSingleton<ISettingsService, SettingsService>();
|
|
services.AddSingleton<IAppStateService, AppStateService>();
|
|
|
|
// Services
|
|
services.AddSingleton<ICommandProviderCache, DefaultCommandProviderCache>();
|
|
services.AddSingleton<TopLevelCommandManager>();
|
|
services.AddSingleton<AliasManager>();
|
|
services.AddSingleton<HotkeyManager>();
|
|
|
|
services.AddSingleton<MainWindowViewModel>();
|
|
services.AddSingleton<TrayIconService>();
|
|
|
|
services.AddSingleton<IThemeService, ThemeService>();
|
|
services.AddSingleton<ResourceSwapper>();
|
|
|
|
services.AddIconServices(dispatcherQueue);
|
|
}
|
|
|
|
private static void AddCoreServices(ServiceCollection services, IApplicationInfoService appInfoService)
|
|
{
|
|
// Core services
|
|
services.AddSingleton(appInfoService);
|
|
|
|
services.AddSingleton<IExtensionService, ExtensionService>();
|
|
services.AddSingleton<IRunHistoryService, RunHistoryService>();
|
|
|
|
services.AddSingleton<IRootPageService, PowerToysRootPageService>();
|
|
services.AddSingleton<IAppHostService, PowerToysAppHostService>();
|
|
services.AddSingleton<ITelemetryService, TelemetryForwarder>();
|
|
|
|
services.AddSingleton<IFuzzyMatcherProvider, FuzzyMatcherProvider>(
|
|
_ => new FuzzyMatcherProvider(new PrecomputedFuzzyMatcherOptions(), new PinyinFuzzyMatcherOptions()));
|
|
|
|
// ViewModels
|
|
services.AddSingleton<ShellViewModel>();
|
|
services.AddSingleton<DockViewModel>();
|
|
services.AddSingleton<IContextMenuFactory, CommandPaletteContextMenuFactory>();
|
|
services.AddSingleton<IPageViewModelFactoryService, CommandPalettePageViewModelFactory>();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_globalErrorHandler.Dispose();
|
|
EtwTrace.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|