From b0c9d21c73fb88e2db26db1ced36fa193119eb09 Mon Sep 17 00:00:00 2001 From: Luis Goll Date: Thu, 8 Jan 2026 02:53:41 -0300 Subject: [PATCH] Toggle View/ViewModel now supports Avalonia (axaml) --- .../Commands/ToggleViewModelCommand.cs | 13 +++++++++---- .../Utilities/ViewModelHelper.cs | 9 ++++++++- .../Utilities/ViewModelHelperTests.cs | 5 +++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/BrightXaml.Extensibility/Commands/ToggleViewModelCommand.cs b/BrightXaml.Extensibility/Commands/ToggleViewModelCommand.cs index 5e13bd6..072aa9f 100644 --- a/BrightXaml.Extensibility/Commands/ToggleViewModelCommand.cs +++ b/BrightXaml.Extensibility/Commands/ToggleViewModelCommand.cs @@ -31,7 +31,7 @@ public ToggleViewModelCommand(TraceSource traceSource, IDialogService dialogServ Icon = new(ImageMoniker.KnownValues.Binding, IconSettings.IconAndText), //Icon = new(ImageMoniker.KnownValues.Code, IconSettings.IconAndText), Shortcuts = [new CommandShortcutConfiguration(ModifierKey.Control, Key.E, ModifierKey.Control, Key.Q)], - EnabledWhen = ActivationConstraint.ClientContext(ClientContextKey.Shell.ActiveSelectionPath, @"\.(cs|xaml)$"), + EnabledWhen = ActivationConstraint.ClientContext(ClientContextKey.Shell.ActiveSelectionPath, @"\.(cs|xaml|axaml)$"), // TODO: These don't work specifically for this command! //EnabledWhen = ActivationConstraint.ClientContext(ClientContextKey.Shell.ActiveEditorContentType, @"\.(cs)$"), @@ -81,10 +81,14 @@ public override async Task ExecuteCommandAsync(IClientContext context, Cancellat activeProjectPath += Path.DirectorySeparatorChar; if (fileName.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase) || - fileName.EndsWith(".xaml.cs", StringComparison.InvariantCultureIgnoreCase)) + fileName.EndsWith(".xaml.cs", StringComparison.InvariantCultureIgnoreCase) || + fileName.EndsWith(".axaml", StringComparison.InvariantCultureIgnoreCase) || + fileName.EndsWith(".axaml.cs", StringComparison.InvariantCultureIgnoreCase)) { // Existing logic to find and open ViewModel. - string fileNameWithoutExtension = fileName.Replace(".xaml.cs", string.Empty).Replace(".xaml", string.Empty); + string fileNameWithoutExtension = fileName + .Replace(".xaml.cs", string.Empty).Replace(".xaml", string.Empty) + .Replace(".axaml.cs", string.Empty).Replace(".axaml", string.Empty); var possibleViewModels = ViewModelHelper.GetViewModelNamePossibilities(fileNameWithoutExtension); var files = await workspace @@ -138,7 +142,8 @@ public override async Task ExecuteCommandAsync(IClientContext context, Cancellat .QueryProjectsAsync(project => project .Get(p => p.Files) .Where(f => f.Path.StartsWith(activeProjectPath, StringComparison.InvariantCultureIgnoreCase) && - f.FileName.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase)) + (f.FileName.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase) || + f.FileName.EndsWith(".axaml", StringComparison.InvariantCultureIgnoreCase))) .With(f => new { f.FileName, f.Path }), cancellationToken); var possibleFiles = files.Where(f => possibleViews.Contains(f.FileName)).Select(f => f.Path).ToList(); diff --git a/BrightXaml.Extensibility/Utilities/ViewModelHelper.cs b/BrightXaml.Extensibility/Utilities/ViewModelHelper.cs index d9b4c2a..f55fa7b 100644 --- a/BrightXaml.Extensibility/Utilities/ViewModelHelper.cs +++ b/BrightXaml.Extensibility/Utilities/ViewModelHelper.cs @@ -34,6 +34,7 @@ public static List GetViewModelNamePossibilities(string xamlName) public static List GetViewNamePossibilities(string viewModelName) { const string suffixXaml = ".xaml"; + const string suffixAxaml = ".axaml"; string treatedName = viewModelName.Replace("ViewModel", string.Empty, StringComparison.InvariantCultureIgnoreCase); var possibilities = new List { @@ -42,7 +43,13 @@ public static List GetViewNamePossibilities(string viewModelName) treatedName + "Page" + suffixXaml, treatedName + "Window" + suffixXaml, treatedName + "Content" + suffixXaml, // (only VS Extensibility uses this). - treatedName + suffixXaml + treatedName + suffixXaml, + // Avalonia extensions + treatedName + "View" + suffixAxaml, + treatedName + "Page" + suffixAxaml, + treatedName + "Window" + suffixAxaml, + treatedName + "Content" + suffixAxaml, + treatedName + suffixAxaml }; return possibilities; diff --git a/BrightXaml.ExtensibilityTests/Utilities/ViewModelHelperTests.cs b/BrightXaml.ExtensibilityTests/Utilities/ViewModelHelperTests.cs index e16821c..bc19c6a 100644 --- a/BrightXaml.ExtensibilityTests/Utilities/ViewModelHelperTests.cs +++ b/BrightXaml.ExtensibilityTests/Utilities/ViewModelHelperTests.cs @@ -23,6 +23,11 @@ public void GetViewModelNamePossibilitiesTest(string xamlName, string expected) [DataRow("MainViewModel", "MainWindow.xaml")] [DataRow("MainViewModel", "Main.xaml")] [DataRow("MainWindowViewModel", "MainWindowContent.xaml")] + [DataRow("MainViewModel", "MainView.axaml")] + [DataRow("MainViewModel", "MainPage.axaml")] + [DataRow("MainViewModel", "MainWindow.axaml")] + [DataRow("MainViewModel", "Main.axaml")] + [DataRow("MainWindowViewModel", "MainWindowContent.axaml")] public void GetViewNamePossibilitiesTest(string viewModelName, string expected) { var possibilities = ViewModelHelper.GetViewNamePossibilities(viewModelName);