VsixCommunity / Community.VisualStudio.Toolkit

Making it easier to write Visual Studio extensions
Other
249 stars 44 forks source link

Strange behavior with Dynamic Visbility #493

Closed mqhout closed 4 months ago

mqhout commented 4 months ago

I have implemented DynamicVisibility on a menu button. This works fine, my menu button only shows up on C# files in the context menu of the Solution Explorer. But after a first use of this button, it also shows up on other types of files. So the execution of the command impacts the DynamicVisibility. Could this be a bug or am I doing something wrong?

<Button guid="DCPPowerTools" id="FixCopyrightCommand" priority="0x0100" type="Button">
        <Parent guid="DCPPowerTools" id="CSharpItemMenuGroup" />
        <Icon guid="DCPPowerTools" id="DCPPowerToolsImage" />
        <CommandFlag>DynamicVisibility</CommandFlag>
        <Strings>
          <ButtonText>Fix Copyright</ButtonText>
        </Strings>
 </Button>

<VisibilityConstraints>
    <VisibilityItem guid="DCPPowerTools" id="FixCopyrightCommand" context="UIContextCSharpFile" />
</VisibilityConstraints>

<GuidSymbol name="UIContextCSharpFile" value="{24551deb-f034-43e9-a279-0e541241687e}" />
<IDSymbol name="FixCopyrightCommand" value="0x0120" />
    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [InstalledProductRegistration(Vsix.Name, Vsix.Description, Vsix.Version)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [Guid(PackageGuids.DCPPowerToolsString)]
    [ProvideUIContextRule(PackageGuids.UIContextCSharpFileString,
        name: "Supported Files",
        expression: "CSharp",
        termNames: new[] { "CSharp" },
        termValues: new[] { "HierSingleSelectionName:.cs$" })]
    public sealed class DCPPowerToolsPackage : ToolkitPackage {
        .....
    }

    [Command(PackageIds.FixCopyrightCommand)]
    internal sealed class FixCopyright : BaseCommand<FixCopyright> {
        protected override async Task ExecuteAsync() {
             .....
        }
    }
reduckted commented 4 months ago

It's a bit confusing, but the visibility constraint only works before the package is loaded. After the package is loaded (it's loaded when your command first runs), then it's the command's responsibility to control it's visibility.

See #487

mqhout commented 4 months ago

Yes that is it. Thanks for finding this for me.