gemini icon indicating copy to clipboard operation
gemini copied to clipboard

Commands

Open Airmonos opened this issue 9 years ago • 3 comments

I need to dynamically add and remove items from the menu. I can't see how to do that in the new model. I was able to in .5.x by doing the following.

//Add items to the menu.
var shell = IoC.Get<IShell>();
var modules = shell.MainMenu.Find("Mixer");
if (modules == null)
{
    shell.MainMenu.Add(new MenuItem("Mixer", "_Mixer"));
    modules = shell.MainMenu.Find("Mixer");
}
modules.Add(new MenuItem("MixerHold", "Mixer Hold", MixerHold).WithGlobalShortcut(ModifierKeys.Shift, Key.F5));

//Remove the menu.
var shell = IoC.Get<IShell>();
shell.MainMenu.Remove(shell.MainMenu.Find("Mixer"));

Airmonos avatar Nov 12 '15 20:11 Airmonos

That code should almost work in v0.6, but you'll need to do a couple of things:

  1. Create your own class that inherits from StandardMenuItem.
  2. Replace new MenuItem with your new class.
  3. WithGlobalShortcut doesn't exist anymore, so you'll need to re-implement that.

The eventual plan is to implement something similar to Visual Studio's "contexts", which hides and shows commands (and their related menu items and toolbar items) depending on context.

tgjones avatar Nov 15 '15 10:11 tgjones

I believe I am in a similar position. However StandardMenuItem is an abstract class with abstract getters and no setters. So when I derive from StandardMenuItem, I haven't been able to implement the setters unless I change the setter property name and I need to be able to change the Text and other properties of the menu item. Instead I derived from MenuItemBase and I can see my menus, but the execute Func is not called. I believe it is due to the missing CommandDefinition, but I need to dig further. I think I have the same problem with CommandDefinitionBase and it would be much easier if it were an interface instead of an abstract class with no setters. Otherwise, I would need to create alternate setters like SetText.

ryanvs avatar Dec 01 '15 04:12 ryanvs

@Airmonos, @ryanvs, I'm interested in this happening as well. I've yet to persuade @tgjones that this should be declarative in nature: i.e. leave it to the views/documents, what have you, to declare what menus, submenus, they want. Too many opportunities to muck things up when we have to "remove then add", etc. WPF/XAML is also, by its nature, declarative. Expecting a reconciliation step is just confusing the works.

I would be willing to contribute towards your "context" goals, but I must insist in the declarative approach. Let me know when a good time to visit with you via Skype, etc, might be to discuss the finer points, what's there, what needs adapting, and so on.

mwpowellhtx avatar Dec 29 '15 19:12 mwpowellhtx