Skip to content

Commit

Permalink
feat: Allow AvaloniaVS defered load
Browse files Browse the repository at this point in the history
Allow loading the AvaloniaVS extension when referencing the Avalonia nuget package or add ProjectCapability Avalonia
  • Loading branch information
workgroupengineering committed Mar 21, 2023
1 parent c29121f commit 7418b6e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 16 deletions.
3 changes: 0 additions & 3 deletions AvaloniaVS.Shared/AvaloniaPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace AvaloniaVS
[Guid(Constants.PackageGuidString)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExistsAndFullyLoaded_string, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideEditorExtension(
typeof(EditorFactory),
"." + Constants.axaml,
Expand Down Expand Up @@ -64,8 +63,6 @@ namespace AvaloniaVS
DebuggingLogicalViewEditor = typeof(EditorFactory),
TextLogicalViewEditor = typeof(EditorFactory))]
[ProvideOptionPage(typeof(OptionsDialogPage), Constants.PackageName, "General", 113, 0, supportsAutomation: true)]


[ProvideBindingPath]
internal sealed class AvaloniaPackage : AsyncPackage
{
Expand Down
2 changes: 1 addition & 1 deletion AvaloniaVS.Shared/AvaloniaVS.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Converters\EnumValuesConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\NotNullOrEmptyToVisibilityConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Constants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Guids.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IntelliSense\TextChangeAdapter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IntelliSense\XamlCompletion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IntelliSense\XamlCompletionCommandHandler.cs" />
Expand All @@ -31,6 +30,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Models\ProjectInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\ProjectOutputInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\XamlBufferMetadata.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ProjectSystem\AvaloniaProject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ServiceProviderExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\AvaloniaVSSettings.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\EditorFactory.cs" />
Expand Down
7 changes: 6 additions & 1 deletion AvaloniaVS.Shared/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
namespace AvaloniaVS;
using System;

namespace AvaloniaVS;

internal static class Constants
{
public const string PackageGuidString = "865ba8d5-1180-4bf8-8821-345f72a4cb79";
public static readonly Guid PackageGuid = new (PackageGuidString);
public const string PackageName = "Avalonia Xaml Editor";
public const string axaml = nameof(axaml);
public const string xaml = nameof(xaml);
public const string paml = nameof(paml);

public const string AvaloviaFactoryEditorGuidString = @"6D5344A2-2FCD-49DE-A09D-6A14FD1B1224";
public static readonly Guid AvaloviaFactoryEditorGuid = new (AvaloviaFactoryEditorGuidString);

public const string AvaloniaCapability = nameof(Avalonia);
}
9 changes: 0 additions & 9 deletions AvaloniaVS.Shared/Guids.cs

This file was deleted.

38 changes: 38 additions & 0 deletions AvaloniaVS.Shared/ProjectSystem/AvaloniaProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Composition;
using IAsyncServiceProvider = Microsoft.VisualStudio.Shell.IAsyncServiceProvider;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;

namespace AvaloniaVS.ProjectSystem;

[Export(ExportContractNames.Scopes.UnconfiguredProject, typeof(IProjectDynamicLoadComponent))]
[AppliesTo(Constants.AvaloniaCapability)]
internal class AvaloniaProject : IProjectDynamicLoadComponent
{
private IAsyncServiceProvider asyncServiceProvider;

public async Task LoadAsync()
{
if (asyncServiceProvider is null)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (ServiceProvider.GlobalProvider.GetService(typeof(IVsShell)) is IVsShell shell)
{
if (shell.IsPackageLoaded(Constants.PackageGuid, out var vsPackage)
!= Microsoft.VisualStudio.VSConstants.S_OK)
{
shell.LoadPackage(Constants.PackageGuid, out vsPackage);
}
asyncServiceProvider = (IAsyncServiceProvider)vsPackage;
}
}
}

public async Task UnloadAsync()
{
// Unload the feature
await Task.CompletedTask;
}
}
2 changes: 1 addition & 1 deletion AvaloniaVS.Shared/Services/EditorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public int CreateEditorInstance(

ppunkDocView = IntPtr.Zero;
ppunkDocData = IntPtr.Zero;
pguidCmdUI = Guids.AvaloniaDesignerEditorFactory;
pguidCmdUI = Constants.AvaloviaFactoryEditorGuid;
pgrfCDW = 0;
pbstrEditorCaption = string.Empty;

Expand Down
2 changes: 1 addition & 1 deletion AvaloniaVS.Shared/Views/EditorPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private void HandleBuildDone(vsBuildScope Scope, vsBuildAction Action)

int IVsDeferredDocView.get_CmdUIGuid(out System.Guid pGuidCmdId)
{
pGuidCmdId = Guids.AvaloniaDesignerEditorFactory;
pGuidCmdId = Constants.AvaloviaFactoryEditorGuid;
return VSConstants.S_OK;
}

Expand Down

0 comments on commit 7418b6e

Please sign in to comment.