/// <summary> /// Returns a context menu containing commands with commandTags</summary> /// <param name="commandTags">Command tags for commands to include on menu</param> /// <returns>ContextMenu</returns> public ContextMenu GetContextMenu(IEnumerable<object> commandTags) { m_commandService.SuggestRequery(); var menu = new ContextMenu(); menu.SetResourceReference(ContextMenu.StyleProperty, Resources.MenuStyleKey); //menu.Style = (Style)Application.Current.FindResource(Resources.MenuStyleKey); // Generate view model List<ICommandItem> commands = new List<ICommandItem>(); foreach (var tag in commandTags) { var command = m_commandService.GetCommand(tag); if (command != null) { if (!AutoCompact || ((ICommand)command).CanExecute(command)) commands.Add(command); } } commands.Sort(new CommandComparer()); var dummyRootMenu = new Sce.Atf.Wpf.Models.Menu(null, null, null, null, null); foreach (var command in commands) MenuUtil.BuildSubMenus(command, dummyRootMenu); MenuUtil.InsertGroupSeparators(dummyRootMenu); menu.ItemsSource = dummyRootMenu.ChildCollection; return menu; }
private void RebuildMenu(Menu root) { root.ChildCollection.Clear(); foreach (var command in m_commandService.Commands.Where<ICommandItem>(x => CommandComparer.TagsEqual(x.MenuTag, root.MenuTag) && x.IsVisible(Sce.Atf.Applications.CommandVisibility.Menu))) { MenuUtil.BuildSubMenus(command as CommandItem, root); } MenuUtil.InsertGroupSeparators(root); }
private static void GetCommands(Menu menu, List<Tuple<ICommandItem, Menu>> commands) { foreach (var child in menu.ChildCollection) { if (child is ICommandItem) { commands.Add(new Tuple<ICommandItem, Menu>((ICommandItem)child, menu)); } else if (child is Menu) { GetCommands((Menu)child, commands); } } }
public static void InsertGroupSeparators(Menu menu) { // Create depth first list of commands var commands = new List<Tuple<ICommandItem, Menu>>(); GetCommands(menu, commands); for (int i = 1; i < commands.Count; i++) { var previous = commands[i - 1]; var current = commands[i]; if (!CommandComparer.TagsEqual(previous.Item1.GroupTag, current.Item1.GroupTag)) { InsertSeparator(previous, current); } } }
public static void BuildSubMenus(ICommandItem command, Menu menu) { var subMenus = menu.ChildCollection; foreach (var segment in command.MenuPath) { // Try and find an existing submenu var subMenu = (Menu)subMenus.FirstOrDefault<IMenuItem>(x => (x is Menu) && (((Menu)x).Text == segment)); if (subMenu == null) { // No existing submenu found - add a new one subMenu = new Menu(menu, command.MenuTag, null, segment, segment); subMenus.Add(subMenu); } subMenus = subMenu.ChildCollection; menu = subMenu; } subMenus.Add(command); }
/// <summary> /// Returns a context menu containing commands with commandTags</summary> /// <param name="commandTags">Command tags for commands to include on menu</param> /// <returns>ContextMenu</returns> public ContextMenu GetContextMenu(IEnumerable <object> commandTags) { m_commandService.SuggestRequery(); var menu = new ContextMenu(); menu.SetResourceReference(ContextMenu.StyleProperty, Resources.MenuStyleKey); //menu.Style = (Style)Application.Current.FindResource(Resources.MenuStyleKey); // Generate view model List <ICommandItem> commands = new List <ICommandItem>(); foreach (var tag in commandTags) { var command = m_commandService.GetCommand(tag); if (command != null) { if (!AutoCompact || ((ICommand)command).CanExecute(command)) { commands.Add(command); } } } commands.Sort(new CommandComparer()); var dummyRootMenu = new Sce.Atf.Wpf.Models.Menu(null, null, null, null, null); foreach (var command in commands) { MenuUtil.BuildSubMenus(command, dummyRootMenu); } MenuUtil.InsertGroupSeparators(dummyRootMenu); menu.ItemsSource = dummyRootMenu.ChildCollection; return(menu); }