Example #1
0
        MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, MenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu)
        {
            if (menuItem == null)
            {
                menuItem = new MenuItem();
            }
            menuItem.CommandTarget = commandTarget;

            string         header           = ResourceHelper.GetString(item, metadata.Header);
            string         inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText);
            ImageReference?iconImgRef       = ImageReferenceHelper.GetImageReference(item, metadata.Icon);

            header             = item.GetHeader(ctx) ?? header;
            inputGestureText   = item.GetInputGestureText(ctx) ?? inputGestureText;
            iconImgRef         = item.GetIcon(ctx) ?? iconImgRef;
            menuItem.IsChecked = item.IsChecked(ctx);

            menuItem.Header           = header;
            menuItem.InputGestureText = inputGestureText;

            var  cmdHolder = item as ICommandHolder;
            bool lastIsEnabledCallValue = false;

            if (iconImgRef != null)
            {
                if (cmdHolder == null)
                {
                    lastIsEnabledCallValue = item.IsEnabled(ctx);
                }
                else
                {
                    var routedCommand = cmdHolder.Command as RoutedCommand;
                    lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget);
                }
                Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue);
            }

            if (metadata.Guid != null)
            {
                var itemGuid = Guid.Parse(metadata.Guid);
                if (guidToGroups.ContainsKey(itemGuid))
                {
                    menuItem.Items.Add(new MenuItem());
                    menuItem.SubmenuOpened += (s, e) => {
                        if (e.Source == menuItem)
                        {
                            InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu);
                        }
                    };
                    menuItem.SubmenuClosed += (s, e) => {
                        if (e.Source == menuItem)
                        {
                            menuItem.Items.Clear();
                            menuItem.Items.Add(new MenuItem());
                        }
                    };
                }
            }

            ctx.OnDisposed += (_, __) => {
                // Buggy automation peers could hold a reference to us, so clear the captured variables (we can't clear the captured 'this')
                menuItem      = null;
                ctx           = null;
                commandTarget = null;
                item          = null;
                iconImgRef    = null;
            };

            menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => {
                Debug.Assert(!ctx.IsDisposed);
                if (ctx?.IsDisposed == false)
                {
                    item.Execute(ctx);
                    ctx?.Dispose();
                }
            }, a => {
                if (ctx?.IsDisposed != false)
                {
                    return(false);
                }
                bool b = item.IsEnabled(ctx);
                if (lastIsEnabledCallValue != b && iconImgRef != null)
                {
                    Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue = b);
                }
                return(b);
            });

            return(menuItem);
        }
Example #2
0
        MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, IMenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu)
        {
            if (menuItem == null)
                menuItem = new MenuItem();
            menuItem.CommandTarget = commandTarget;

            string header = ResourceHelper.GetString(item, metadata.Header);
            string inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText);
            string iconName = metadata.Icon;

            var mi2 = item as IMenuItem2;
            if (mi2 != null) {
                header = mi2.GetHeader(ctx) ?? header;
                inputGestureText = mi2.GetInputGestureText(ctx) ?? inputGestureText;
                iconName = mi2.GetIcon(ctx) ?? iconName;
                menuItem.IsChecked = mi2.IsChecked(ctx);
            }

            menuItem.Header = header;
            menuItem.InputGestureText = inputGestureText;

            var cmdHolder = item as ICommandHolder;
            bool lastIsEnabledCallValue = false;
            if (!string.IsNullOrEmpty(iconName)) {
                if (cmdHolder == null)
                    lastIsEnabledCallValue = item.IsEnabled(ctx);
                else {
                    var routedCommand = cmdHolder.Command as RoutedCommand;
                    lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget);
                }
                imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, lastIsEnabledCallValue);
            }

            if (metadata.Guid != null) {
                var itemGuid = Guid.Parse(metadata.Guid);
                List<MenuItemGroupMD> list;
                if (guidToGroups.TryGetValue(itemGuid, out list)) {
                    menuItem.Items.Add(new MenuItem());
                    menuItem.SubmenuOpened += (s, e) => {
                        if (e.Source == menuItem)
                            InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu);
                    };
                    menuItem.SubmenuClosed += (s, e) => {
                        if (e.Source == menuItem) {
                            menuItem.Items.Clear();
                            menuItem.Items.Add(new MenuItem());
                        }
                    };
                }
            }

            menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => item.Execute(ctx), a => {
                bool b = item.IsEnabled(ctx);
                if (lastIsEnabledCallValue != b && !string.IsNullOrEmpty(iconName))
                    imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, lastIsEnabledCallValue = b);
                return b;
            });

            return menuItem;
        }
Example #3
0
        MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, IMenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu)
        {
            if (menuItem == null)
            {
                menuItem = new MenuItem();
            }
            menuItem.CommandTarget = commandTarget;

            string header           = ResourceHelper.GetString(item, metadata.Header);
            string inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText);
            string iconName         = metadata.Icon;

            var mi2 = item as IMenuItem2;

            if (mi2 != null)
            {
                header             = mi2.GetHeader(ctx) ?? header;
                inputGestureText   = mi2.GetInputGestureText(ctx) ?? inputGestureText;
                iconName           = mi2.GetIcon(ctx) ?? iconName;
                menuItem.IsChecked = mi2.IsChecked(ctx);
            }

            menuItem.Header           = header;
            menuItem.InputGestureText = inputGestureText;

            var  cmdHolder = item as ICommandHolder;
            bool lastIsEnabledCallValue = false;

            if (!string.IsNullOrEmpty(iconName))
            {
                if (cmdHolder == null)
                {
                    lastIsEnabledCallValue = item.IsEnabled(ctx);
                }
                else
                {
                    var routedCommand = cmdHolder.Command as RoutedCommand;
                    lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget);
                }
                imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, lastIsEnabledCallValue);
            }

            if (metadata.Guid != null)
            {
                var itemGuid = Guid.Parse(metadata.Guid);
                List <MenuItemGroupMD> list;
                if (guidToGroups.TryGetValue(itemGuid, out list))
                {
                    menuItem.Items.Add(new MenuItem());
                    menuItem.SubmenuOpened += (s, e) => {
                        if (e.Source == menuItem)
                        {
                            InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu);
                        }
                    };
                    menuItem.SubmenuClosed += (s, e) => {
                        if (e.Source == menuItem)
                        {
                            menuItem.Items.Clear();
                            menuItem.Items.Add(new MenuItem());
                        }
                    };
                }
            }

            menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => item.Execute(ctx), a => {
                bool b = item.IsEnabled(ctx);
                if (lastIsEnabledCallValue != b && !string.IsNullOrEmpty(iconName))
                {
                    imageManager.Add16x16Image(menuItem, item.GetType().Assembly, iconName, isCtxMenu, lastIsEnabledCallValue = b);
                }
                return(b);
            });

            return(menuItem);
        }
Example #4
0
		MenuItem Create(IMenuItem item, IMenuItemMetadata metadata, MenuItemContext ctx, IInputElement commandTarget, MenuItem menuItem, bool isCtxMenu) {
			if (menuItem == null)
				menuItem = new MenuItem();
			menuItem.CommandTarget = commandTarget;

			string header = ResourceHelper.GetString(item, metadata.Header);
			string inputGestureText = ResourceHelper.GetString(item, metadata.InputGestureText);
			ImageReference? iconImgRef = ImageReferenceHelper.GetImageReference(item, metadata.Icon);

			header = item.GetHeader(ctx) ?? header;
			inputGestureText = item.GetInputGestureText(ctx) ?? inputGestureText;
			iconImgRef = item.GetIcon(ctx) ?? iconImgRef;
			menuItem.IsChecked = item.IsChecked(ctx);

			menuItem.Header = header;
			menuItem.InputGestureText = inputGestureText;

			var cmdHolder = item as ICommandHolder;
			bool lastIsEnabledCallValue = false;
			if (iconImgRef != null) {
				if (cmdHolder == null)
					lastIsEnabledCallValue = item.IsEnabled(ctx);
				else {
					var routedCommand = cmdHolder.Command as RoutedCommand;
					lastIsEnabledCallValue = commandTarget == null || routedCommand == null || routedCommand.CanExecute(ctx, commandTarget);
				}
				Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue);
			}

			if (metadata.Guid != null) {
				var itemGuid = Guid.Parse(metadata.Guid);
				List<MenuItemGroupMD> list;
				if (guidToGroups.TryGetValue(itemGuid, out list)) {
					menuItem.Items.Add(new MenuItem());
					menuItem.SubmenuOpened += (s, e) => {
						if (e.Source == menuItem)
							InitializeSubMenu(menuItem, ctx, itemGuid, commandTarget, isCtxMenu);
					};
					menuItem.SubmenuClosed += (s, e) => {
						if (e.Source == menuItem) {
							menuItem.Items.Clear();
							menuItem.Items.Add(new MenuItem());
						}
					};
				}
			}

			menuItem.Command = cmdHolder != null ? cmdHolder.Command : new RelayCommand(a => {
				Debug.Assert(!ctx.IsDisposed);
				if (!ctx.IsDisposed) {
					item.Execute(ctx);
					ctx.Dispose();
				}
			}, a => {
				if (ctx.IsDisposed)
					return false;
				bool b = item.IsEnabled(ctx);
				if (lastIsEnabledCallValue != b && iconImgRef != null)
					Add16x16Image(menuItem, iconImgRef.Value, lastIsEnabledCallValue = b);
				return b;
			});

			return menuItem;
		}