Skip to content

CmdPal: Refactoring DI, Logging, and Extension Services#45437

Draft
michaeljolley wants to merge 19 commits intomainfrom
dev/mjolley/served-fresh
Draft

CmdPal: Refactoring DI, Logging, and Extension Services#45437
michaeljolley wants to merge 19 commits intomainfrom
dev/mjolley/served-fresh

Conversation

@michaeljolley
Copy link
Contributor

@michaeljolley michaeljolley commented Feb 5, 2026

IMPORTANT: This is a draft for a reason. You do not want to unintentionally make this real. Let's be honest, you probably don't want this real intentionally.

This is a freaking huge PR. In it we:

  • Refactor our usage of Dependency Injection to support not passing the service provider through a property on the App (ala App.Current.Services). Rather, services and models are injected into their requesting classes & controls via their constructor.
  • In previous versions, logging was handled in 3 different ways. With the new DI in place, all logging uses the .NET Microsoft.Extensions.Logging ILogger interface and a logger is injected into anything that might need to log something.
  • Modified how extensions are loaded. Rather than loading built-ins in the TopLevelCommandManager and, from there, loading 3P extensions in a combination of the TLCM and the ExtensionsService, we now register IExtensionServices and TLCM uses any that are provided to load extensions & commands. This includes a new BuiltInExtensionService and WinRTExtensionService to load built-ins and 3P extension respectively. In the future, we hope to extend this to support extensions written in other languages, ala JsonRPCExtensionService.
  • Added a new PersistenceService to be the backer of all services that need to persist data (e.g. SettingsService, AppStateService, PersonalizationService). It provides consolidated and generic methods for saving and loading persisted objects in JSON.
  • Removes .Core. All those viewmodels, etc. are now moved to either the Common, UI, or UI.ViewModels projects.

TODO

  • Some bug with settings
  • Additional testing

michaeljolley and others added 14 commits January 26, 2026 15:52
- Move project from Core/ subfolder to cmdpal/ root
- Update namespace from Microsoft.CmdPal.Core.Common to Microsoft.CmdPal.Common
- Update all project references and using statements
- Update solution files (PowerToys.slnx, CommandPalette.slnf)
…wModels

- Move all source files from Core.ViewModels to UI.ViewModels
- Update namespace from Microsoft.CmdPal.Core.ViewModels to Microsoft.CmdPal.UI.ViewModels
- Remove Core.ViewModels project from solution files
- Update all using statements in dependent projects
- Delete the Core folder which is now empty
- Extract persistence logic into PersistenceService
- Add AppStateService to manage app state
- Add SettingsService to manage settings
- Add JsonSerializationContext for AOT-compatible JSON
- Add EscapeKeyBehavior and MonitorBehavior enums
UI.ViewModels:
- CommandSettingsViewModel: Add ILogger injection
- CommandItemViewModel: Add ILogger with LoggerMessage methods
- ContextMenuViewModel: Add ILogger injection
- ExtensionObjectViewModel: Add Logger property with NullLogger default
- ShellViewModel: Replace CoreLogger calls with LoggerMessage methods
- UpdateCommandBarMessage: Remove logging from interface default method

UI helpers/services:
- WallpaperHelper: Add ILogger injection with LoggerMessage methods
- LocalKeyboardListener: Add ILogger injection with LoggerMessage methods
- ImageProvider: Add ILogger injection with LoggerMessage methods
- ThemeService: Add ILogger injection with LoggerMessage methods

Note: UI project migration is partial - XAML code-behind files still use static Logger
- Update all ViewModels to receive SettingsService via DI
- Implement IDisposable pattern for proper event cleanup
- Add App.Services property for DI access where needed
- Restore GetProviderSettings/GetGlobalFallbacks to SettingsModel
- Fix CommandProviderWrapper and TopLevelCommandManager logging
- Update all Settings pages to use SettingsService
- Add DefaultAppExtensionHost to replace CommandPaletteHost.Instance
- Add parameterless constructors for XAML-instantiated types
- Fix various ILogger injection issues throughout UI layer

[LoggerMessage(
Level = LogLevel.Warning,
Message = "Unrecognized target for shell navigation: {paramter}")]

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

paramter is not a recognized word. (unrecognized-spelling)
[LoggerMessage(
Level = LogLevel.Warning,
Message = "Unrecognized target for shell navigation: {paramter}")]
partial void Log_UnrecognizedShellNavigationTargetWarning(object paramter);

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

paramter is not a recognized word. (unrecognized-spelling)
@jiripolasek
Copy link
Collaborator

Holy squirrel!
image

@jiripolasek jiripolasek added the Product-Command Palette Refers to the Command Palette utility label Feb 5, 2026
Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I'm at 232/236

Most of this is fairly straightforward - I just haven't gotten to the extension service & TLVM stuff yet.

Probably would have been easier as like, 4 diffs:

  • consolidate the core namespace
  • do the settings -> persistence service refactor
  • do the TLVM & extension refactor
  • do the remaining DI refactoring

but beggars can't be choosers. I'm not sure we ever linked an issue to #38831, but I'm including that as an x-link of a previous attempt here


private void BindScreenPreview(ScreenPreview screenPreview)
{
// Create CommandPalettePreview and set up bindings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just move the ViewModel = settingsViewModel; line above the InitializeComponent() call, so that we can still bind this in XAML (rather than codebehind?)

// Core services
services.AddSingleton<IExtensionService, BuiltInExtensionService>();

// services.AddSingleton<IExtensionService, WinRTExtensionService>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this stay static?

Comment on lines 35 to 38
// new Lazy<MainListPage>(() =>
// {
// return new MainListPage(_tlcManager, settingsService, aliasManager, appStateService, _logger);
// });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

private readonly TopLevelCommandManager _tlcManager;

private readonly MainListPage _mainListPage; // Lazy<MainListPage> _mainListPage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead comments

Comment on lines +54 to +61
var markdownConfig = new MarkdownConfig()
{
ImageProvider = imageProvider,
Themes = _markdownThemes,
};

// Add the MarkdownConfig as a resource so DataTemplates can reference it
Resources["DefaultMarkdownConfig"] = markdownConfig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we move all of this out of XAML into codebehind?


// Add the MarkdownConfig as a resource so DataTemplates can reference it
Resources["DefaultMarkdownConfig"] = markdownConfig;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just duplicated work now?

{
topLevelCommand.AliasText = string.Empty;
}
// var topLevelCommand = _topLevelCommandManager.LookupCommand(kv.Value.CommandId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

WeakReferenceMessenger.Default.Send<PerformCommandMessage>(topLevelCommand.GetPerformCommandMessage());
return true;
}
// var topLevelCommand = _topLevelCommandManager.LookupCommand(alias.CommandId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 wait where did this actual implementation go?)

public partial class AliasManager : ObservableObject
{
private readonly TopLevelCommandManager _topLevelCommandManager;
// private readonly TopLevelCommandManager _topLevelCommandManager;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(dead code here and on on L17 & L20 too)

@zadjii-msft zadjii-msft marked this pull request as ready for review February 12, 2026 19:37

if (exception is not null)
{
Logger.LogError(exception.Message, exception);
Copy link
Collaborator

@jiripolasek jiripolasek Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this, as a developer I know what severity I want to use. The logger has no clue about the context.

{
_topLevelCommandManager = tlcManager;
_aliases = settings.Aliases;
// _topLevelCommandManager = tlcManager;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover

@@ -60,7 +67,7 @@ public IAsyncAction LogMessage(ILogMessage? message)
return Task.CompletedTask.AsAsyncAction();
}

CoreLogger.LogDebug(message.Message);
Log_Message(message.Message);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hilarious :D

@@ -51,7 +52,7 @@ public sealed partial class SearchBar : UserControl,
// 0.6+ suggestions
private string? _textToSuggest;

private SettingsModel Settings => App.Current.Services.GetRequiredService<SettingsModel>();
private SettingsModel settings;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_settings

@michaeljolley michaeljolley marked this pull request as draft February 13, 2026 20:28
@michaeljolley
Copy link
Contributor Author

Thanks for the reviews. For clarity, this was not ready for review. 😄

Much clean-up and testing to do before the draft status is removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Command Palette Refers to the Command Palette utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants