// 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.Helpers; using Microsoft.CmdPal.Common.Services; using Microsoft.CmdPal.Ext.Apps; using Microsoft.CmdPal.Ext.Bookmarks; using Microsoft.CmdPal.Ext.Calc; using Microsoft.CmdPal.Ext.Indexer; using Microsoft.CmdPal.Ext.Registry; 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.ViewModels; using Microsoft.CmdPal.UI.ViewModels.BuiltinCommands; using Microsoft.CmdPal.UI.ViewModels.Models; using Microsoft.CommandPalette.Extensions; using Microsoft.Extensions.DependencyInjection; using Microsoft.PowerToys.Telemetry; 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; /// /// Provides application-specific behavior to supplement the default Application class. /// public partial class App : Application { /// /// Gets the current instance in use. /// public static new App Current => (App)Application.Current; public Window? AppWindow { get; private set; } public ETWTrace EtwTrace { get; private set; } = new ETWTrace(); /// /// Gets the instance to resolve application services. /// public IServiceProvider Services { get; } /// /// Initializes a new instance of the 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(). /// public App() { Services = ConfigureServices(); this.InitializeComponent(); NativeEventWaiter.WaitForEventLoop( "Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd", () => { EtwTrace?.Dispose(); Environment.Exit(0); }); } /// /// Invoked when the application is launched. /// /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs args) { AppWindow = new MainWindow(); var cmdArgs = Environment.GetCommandLineArgs(); var runFromPT = false; foreach (var arg in cmdArgs) { if (arg == "RunFromPT") { runFromPT = true; break; } } if (!runFromPT) { AppWindow.Activate(); } } /// /// Configures the services for the application /// private static ServiceProvider ConfigureServices() { // 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()); // Built-in Commands. Order matters - this is the order they'll be presented by default. var allApps = new AllAppsCommandProvider(); services.AddSingleton(allApps); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); // TODO GH #527 re-enable the clipboard commands // services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); // 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(); var callback = allApps.LookupApp; winget.SetAllLookup(callback); services.AddSingleton(winget); } catch (Exception ex) { Logger.LogError("Couldn't load winget"); Logger.LogError(ex.ToString()); } services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); // Models services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); var sm = SettingsModel.LoadSettings(); services.AddSingleton(sm); var state = AppStateModel.LoadState(); services.AddSingleton(state); services.AddSingleton(); // ViewModels services.AddSingleton(); return services.BuildServiceProvider(); } }