CmdPal: Refactoring DI, Logging, and Extension Services#45437
CmdPal: Refactoring DI, Logging, and Extension Services#45437michaeljolley wants to merge 19 commits intomainfrom
Conversation
- 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
| [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
zadjii-msft
left a comment
There was a problem hiding this comment.
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
corenamespace - 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 |
There was a problem hiding this comment.
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>(); |
| // new Lazy<MainListPage>(() => | ||
| // { | ||
| // return new MainListPage(_tlcManager, settingsService, aliasManager, appStateService, _logger); | ||
| // }); |
| private readonly TopLevelCommandManager _tlcManager; | ||
|
|
||
| private readonly MainListPage _mainListPage; // Lazy<MainListPage> _mainListPage; |
| var markdownConfig = new MarkdownConfig() | ||
| { | ||
| ImageProvider = imageProvider, | ||
| Themes = _markdownThemes, | ||
| }; | ||
|
|
||
| // Add the MarkdownConfig as a resource so DataTemplates can reference it | ||
| Resources["DefaultMarkdownConfig"] = markdownConfig; |
There was a problem hiding this comment.
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; | ||
|
|
There was a problem hiding this comment.
is this just duplicated work now?
| { | ||
| topLevelCommand.AliasText = string.Empty; | ||
| } | ||
| // var topLevelCommand = _topLevelCommandManager.LookupCommand(kv.Value.CommandId); |
| WeakReferenceMessenger.Default.Send<PerformCommandMessage>(topLevelCommand.GetPerformCommandMessage()); | ||
| return true; | ||
| } | ||
| // var topLevelCommand = _topLevelCommandManager.LookupCommand(alias.CommandId); |
There was a problem hiding this comment.
📝 wait where did this actual implementation go?)
| public partial class AliasManager : ObservableObject | ||
| { | ||
| private readonly TopLevelCommandManager _topLevelCommandManager; | ||
| // private readonly TopLevelCommandManager _topLevelCommandManager; |
There was a problem hiding this comment.
(dead code here and on on L17 & L20 too)
|
|
||
| if (exception is not null) | ||
| { | ||
| Logger.LogError(exception.Message, exception); |
There was a problem hiding this comment.
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; |
| @@ -60,7 +67,7 @@ public IAsyncAction LogMessage(ILogMessage? message) | |||
| return Task.CompletedTask.AsAsyncAction(); | |||
| } | |||
|
|
|||
| CoreLogger.LogDebug(message.Message); | |||
| Log_Message(message.Message); | |||
| @@ -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; | |||
|
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. |

This is a freaking huge PR. In it we:
App.Current.Services). Rather, services and models are injected into their requesting classes & controls via their constructor.TopLevelCommandManagerand, from there, loading 3P extensions in a combination of the TLCM and theExtensionsService, we now register IExtensionServices and TLCM uses any that are provided to load extensions & commands. This includes a newBuiltInExtensionServiceandWinRTExtensionServiceto load built-ins and 3P extension respectively. In the future, we hope to extend this to support extensions written in other languages, alaJsonRPCExtensionService.PersistenceServiceto 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..Core. All those viewmodels, etc. are now moved to either theCommon,UI, orUI.ViewModelsprojects.TODO