Skip to content

Instantly share code, notes, and snippets.

@rionmonster
Last active February 1, 2017 20:03
Show Gist options
  • Save rionmonster/9bd6b02397de04d3e123cb1941dc6fba to your computer and use it in GitHub Desktop.
Save rionmonster/9bd6b02397de04d3e123cb1941dc6fba to your computer and use it in GitHub Desktop.
Example Static Menuing In Glyphfriend
using System;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell;
using System.Collections.Generic;
namespace Glyphfriend.VS2017
{
internal sealed class ToggleLibraryCommand
{
public static readonly Guid CommandSet = new Guid("2cd75fc6-9478-422d-ab4b-022fc5fde9bf");
public const int CommandId = 0x0100;
// Library mappings
public const int ToggleBootstrapCommand = 0x1101;
public const int ToggleEntypoCommand = 0x1102;
public const int ToggleFontAwesomeCommand = 0x1103;
public const int ToggleFoundationCommand = 0x1104;
public const int ToggleIonicCommand = 0x1105;
public const int ToggleMaterialDesignCommand = 0x1106;
public const int ToggleMetroUiCommand = 0x1107;
public const int ToggleOcticonsCommand = 0x1108;
// Dictionary to map commands to their appropriate libraries
private Dictionary<int, string> _libraries = new Dictionary<int, string>()
{
{ ToggleBootstrapCommand, "Bootstrap" },
{ ToggleEntypoCommand, "Entypo" },
{ ToggleFontAwesomeCommand, "Font Awesome" },
{ ToggleFoundationCommand, "Foundation" },
{ ToggleIonicCommand, "Ionic" },
{ ToggleMaterialDesignCommand, "Material Design" },
{ ToggleMetroUiCommand, "MetroUI" },
{ ToggleOcticonsCommand, "Octicons" }
};
private readonly Package package;
private ToggleLibraryCommand(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
this.package = package;
OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService != null)
{
foreach(var library in _libraries.Keys)
{
var command = CreateCommand(CommandSet, library);
commandService.AddCommand(command);
}
}
}
private MenuCommand CreateCommand(Guid commandSet, int commandId)
{
// TODO: Check if enabled here
var menuCommandID = new CommandID(commandSet, commandId);
var command = new MenuCommand(ToggleLibraryCallback, menuCommandID) { Checked = true };
return command;
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static ToggleLibraryCommand Instance
{
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private IServiceProvider ServiceProvider
{
get
{
return this.package;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static void Initialize(Package package)
{
Instance = new ToggleLibraryCommand(package);
}
/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void ToggleLibraryCallback(object sender, EventArgs e)
{
var command = (MenuCommand)sender;
// Update the command to reflect which are enabled
command.Checked = !command.Checked;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Extern href="stdidcmd.h"/>
<Extern href="vsshlids.h"/>
<Commands package="guidToggleLibraryCommandPackage">
<Menus>
<Menu guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendMenu" priority="0x001" type="Menu">
<Parent guid="guidTopMenu" id="TopMenu" />
<CommandFlag>DynamicVisibility</CommandFlag>
<Strings>
<ButtonText>Glyphfriend</ButtonText>
</Strings>
</Menu>
</Menus>
<Groups>
<Group guid="guidTopMenu" id="TopMenu" priority="0x001" />
<Group guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" priority="0x0600">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendMenu"/>
</Group>
</Groups>
<Buttons>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleBootstrapCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Bootstrap</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleEntypoCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Entypo</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleFontAwesomeCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Font Awesome</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleFoundationCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Foundation</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleIonicCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Ionic</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleMaterialDesignCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Material Design</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleMetroUiCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>MetroUI</ButtonText>
</Strings>
</Button>
<Button guid="guidToggleLibraryCommandPackageCmdSet" id="ToggleOcticonsCommand" priority="0x0100" type="Button">
<Parent guid="guidToggleLibraryCommandPackageCmdSet" id="GlyphfriendLibrariesMenuGroup" />
<Strings>
<ButtonText>Octicons</ButtonText>
</Strings>
</Button>
</Buttons>
</Commands>
<CommandPlacements>
<CommandPlacement guid="guidTopMenu" id="TopMenu" priority="0x0300">
<Parent guid="HtmlCmdSet" id="HtmlContextMenu"/>
</CommandPlacement>
</CommandPlacements>
<Symbols>
<GuidSymbol name="guidToggleLibraryCommandPackage" value="{60c5f3b4-5e75-4884-be5a-406de025c1f8}" />
<GuidSymbol name="guidTopMenu" value="{f321cac5-e101-459b-b24e-ba72ad25a2f3}">
<IDSymbol name="TopMenu" value="0x3001" />
</GuidSymbol>
<GuidSymbol name="guidToggleLibraryCommandPackageCmdSet" value="{2cd75fc6-9478-422d-ab4b-022fc5fde9bf}">
<IDSymbol name="GlyphfriendMenu" value="0x1001" />
<IDSymbol name="GlyphfriendLibrariesMenuGroup" value="0x1100" />
<IDSymbol name="ToggleBootstrapCommand" value="0x1101" />
<IDSymbol name="ToggleEntypoCommand" value="0x1102" />
<IDSymbol name="ToggleFontAwesomeCommand" value="0x1103" />
<IDSymbol name="ToggleFoundationCommand" value="0x1104" />
<IDSymbol name="ToggleIonicCommand" value="0x1105" />
<IDSymbol name="ToggleMaterialDesignCommand" value="0x1106" />
<IDSymbol name="ToggleMetroUiCommand" value="0x1107" />
<IDSymbol name="ToggleOcticonsCommand" value="0x1108" />
</GuidSymbol>
<GuidSymbol name="HtmlCmdSet" value="{78F03954-2FB8-4087-8CE7-59D71710B3BB}" >
<IDSymbol name="HtmlContextMenu" value="1" />
</GuidSymbol>
</Symbols>
</CommandTable>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment