@ -21,6 +21,13 @@ namespace ICSharpCode.Core.Presentation
@@ -21,6 +21,13 @@ namespace ICSharpCode.Core.Presentation
/// </summary>
public static class MenuService
{
internal sealed class MenuCreateContext
{
public UIElement InputBindingOwner ;
public string ActivationMethod ;
public bool ImmediatelyExpandMenuBuildersForShortcuts ;
}
static Dictionary < string , System . Windows . Input . ICommand > knownCommands = LoadDefaultKnownCommands ( ) ;
static Dictionary < string , System . Windows . Input . ICommand > LoadDefaultKnownCommands ( )
@ -106,7 +113,9 @@ namespace ICSharpCode.Core.Presentation
@@ -106,7 +113,9 @@ namespace ICSharpCode.Core.Presentation
public static ContextMenu CreateContextMenu ( object owner , string addInTreePath )
{
IList items = CreateUnexpandedMenuItems ( null , AddInTree . BuildItems < MenuItemDescriptor > ( addInTreePath , owner , false ) , "ContextMenu" ) ;
IList items = CreateUnexpandedMenuItems (
new MenuCreateContext { ActivationMethod = "ContextMenu" } ,
AddInTree . BuildItems < MenuItemDescriptor > ( addInTreePath , owner , false ) ) ;
return CreateContextMenu ( items ) ;
}
@ -131,9 +140,15 @@ namespace ICSharpCode.Core.Presentation
@@ -131,9 +140,15 @@ namespace ICSharpCode.Core.Presentation
return contextMenu ;
}
public static IList CreateMenuItems ( UIElement inputBindingOwner , object owner , string addInTreePath , string activationMethod = null )
public static IList CreateMenuItems ( UIElement inputBindingOwner , object owner , string addInTreePath , string activationMethod = null , bool immediatelyExpandMenuBuildersForShortcuts = false )
{
IList items = CreateUnexpandedMenuItems ( inputBindingOwner , AddInTree . BuildItems < MenuItemDescriptor > ( addInTreePath , owner , false ) , activationMethod ) ;
IList items = CreateUnexpandedMenuItems (
new MenuCreateContext {
InputBindingOwner = inputBindingOwner ,
ActivationMethod = activationMethod ,
ImmediatelyExpandMenuBuildersForShortcuts = immediatelyExpandMenuBuildersForShortcuts
} ,
AddInTree . BuildItems < MenuItemDescriptor > ( addInTreePath , owner , false ) ) ;
return ExpandMenuBuilders ( items , false ) ;
}
@ -156,12 +171,12 @@ namespace ICSharpCode.Core.Presentation
@@ -156,12 +171,12 @@ namespace ICSharpCode.Core.Presentation
}
}
internal static IList CreateUnexpandedMenuItems ( UIElement inputBindingOwner , IEnumerable descriptors , string activationMethod )
internal static IList CreateUnexpandedMenuItems ( MenuCreateContext context , IEnumerable descriptors )
{
ArrayList result = new ArrayList ( ) ;
if ( descriptors ! = null ) {
foreach ( MenuItemDescriptor descriptor in descriptors ) {
result . Add ( CreateMenuItemFromDescriptor ( inputBindingOwner , descriptor , activationMethod ) ) ;
result . Add ( CreateMenuItemFromDescriptor ( context , descriptor ) ) ;
}
}
return result ;
@ -191,7 +206,7 @@ namespace ICSharpCode.Core.Presentation
@@ -191,7 +206,7 @@ namespace ICSharpCode.Core.Presentation
return result ;
}
static object CreateMenuItemFromDescriptor ( UIElement inputBindingOwner , MenuItemDescriptor descriptor , string activationMethod )
static object CreateMenuItemFromDescriptor ( MenuCreateContext context , MenuItemDescriptor descriptor )
{
Codon codon = descriptor . Codon ;
string type = codon . Properties . Contains ( "type" ) ? codon . Properties [ "type" ] : "Command" ;
@ -205,17 +220,19 @@ namespace ICSharpCode.Core.Presentation
@@ -205,17 +220,19 @@ namespace ICSharpCode.Core.Presentation
//return new MenuCheckBox(codon, descriptor.Caller);
case "Item" :
case "Command" :
return new MenuCommand ( i nputBindingOwner, codon , descriptor . Caller , createCommand , a ctivationMethod) ;
return new MenuCommand ( context . I nputBindingOwner, codon , descriptor . Caller , createCommand , context . A ctivationMethod) ;
case "Menu" :
var item = new CoreMenuItem ( codon , descriptor . Caller ) {
ItemsSource = new object [ 1 ] ,
SetEnabled = true
} ;
var subItems = CreateUnexpandedMenuItems ( inputBindingOwner , descriptor . SubItems , activationMethod ) ;
var subItems = CreateUnexpandedMenuItems ( context , descriptor . SubItems ) ;
item . SubmenuOpened + = ( sender , args ) = > {
item . ItemsSource = ExpandMenuBuilders ( subItems , true ) ;
args . Handled = true ;
} ;
if ( context . ImmediatelyExpandMenuBuildersForShortcuts )
ExpandMenuBuilders ( subItems , false ) ;
return item ;
case "Builder" :
IMenuItemBuilder builder = codon . AddIn . CreateObject ( codon . Properties [ "class" ] ) as IMenuItemBuilder ;