public void ContextMenu_ProcessCmdKey_HasItemWithShoutcutKey_ReturnsExpected(MenuItem menuItem, Control control, bool expectedResult, int expectedOnClickCallCount, int expectedOnPopupCallCount) { int onClickCallCount = 0; menuItem.Click += (sender, e) => { onClickCallCount++; Assert.Same(menuItem, sender); Assert.Equal(EventArgs.Empty, e); }; int onPopupCallCount = 0; menuItem.Popup += (sender, e) => { onPopupCallCount++; Assert.Same(menuItem, sender); Assert.Equal(EventArgs.Empty, e); }; using var menu = new SubContextMenu(new MenuItem[] { menuItem }); var msg = new Message(); Assert.Equal(expectedResult, menu.ProcessCmdKey(ref msg, Keys.Control | Keys.A, control)); Assert.Same(control, menu.SourceControl); Assert.Equal(expectedOnClickCallCount, onClickCallCount); Assert.Equal(expectedOnPopupCallCount, onPopupCallCount); }
public void ShowSubMenu(SubContextMenu contextMenu) { ContextMenuOpenType openType; if (contextMenu.ActualWidth > SpaceForSubMenuX) { if ((contextMenu.ActualHeight + contextMenu.Location.Top) > (ActualHeight + SpaceForSubMenuY)) { openType = ContextMenuOpenType.LeftTop; } else { openType = ContextMenuOpenType.LeftBottom; } } else { if ((contextMenu.ActualHeight + contextMenu.Location.Top) > (ActualHeight + SpaceForSubMenuY)) { openType = ContextMenuOpenType.RightTop; } else { openType = ContextMenuOpenType.RightBottom; } } switch (openType) { case ContextMenuOpenType.RightBottom: contextMenu.Margin = contextMenu.Location; break; case ContextMenuOpenType.RightTop: var locationRightTop = contextMenu.Location; locationRightTop.Top -= (contextMenu.ActualHeight - 30); contextMenu.Margin = locationRightTop; break; case ContextMenuOpenType.LeftBottom: var locationLeftBottom = contextMenu.Location; locationLeftBottom.Left -= (ActualWidth + contextMenu.ActualWidth - 10); contextMenu.Margin = locationLeftBottom; break; case ContextMenuOpenType.LeftTop: var locationLeftTop = contextMenu.Location; locationLeftTop.Left -= (ActualWidth + contextMenu.ActualWidth - 10); locationLeftTop.Top -= (contextMenu.ActualHeight - 30); contextMenu.Margin = locationLeftTop; break; } contextMenu.Visibility = Visibility.Visible; }
public void ShowSubMenu(SubContextMenu contextMenu) { ContextMenuOpenType openType; if (contextMenu.ActualWidth > SpaceForSubMenuX) { if ((contextMenu.ActualHeight + contextMenu.Location.Top) > (ActualHeight + SpaceForSubMenuY)) { openType = ContextMenuOpenType.LeftTop; } else { openType = ContextMenuOpenType.LeftBottom; } } else { if ((contextMenu.ActualHeight + contextMenu.Location.Top) > (ActualHeight + SpaceForSubMenuY)) { openType = ContextMenuOpenType.RightTop; } else { openType = ContextMenuOpenType.RightBottom; } } switch (openType) { case ContextMenuOpenType.RightBottom: contextMenu.Margin = contextMenu.Location; break; case ContextMenuOpenType.RightTop: var locationRightTop = contextMenu.Location; locationRightTop.Top -= (contextMenu.ActualHeight - 30); contextMenu.Margin = locationRightTop; break; case ContextMenuOpenType.LeftBottom: var locationLeftBottom = contextMenu.Location; locationLeftBottom.Left -= (ActualWidth + contextMenu.ActualWidth - 10); contextMenu.Margin = locationLeftBottom; break; case ContextMenuOpenType.LeftTop: var locationLeftTop = contextMenu.Location; locationLeftTop.Left -= (ActualWidth + contextMenu.ActualWidth - 10); locationLeftTop.Top -= (contextMenu.ActualHeight - 30); contextMenu.Margin = locationLeftTop; break; } contextMenu.Visibility = Visibility.Visible; }
public void ContextMenu_Ctor_Default() { using var menu = new SubContextMenu(); Assert.True(menu.CanRaiseEvents); Assert.Null(menu.Container); Assert.False(menu.DesignMode); Assert.NotNull(menu.Events); Assert.Same(menu.Events, menu.Events); Assert.False(menu.IsParent); Assert.Empty(menu.MenuItems); Assert.Same(menu.MenuItems, menu.MenuItems); Assert.Empty(menu.Name); Assert.Equal(RightToLeft.No, menu.RightToLeft); Assert.Null(menu.Site); Assert.Null(menu.SourceControl); Assert.Null(menu.Tag); }
public void ContextMenu_OnPopup_Invoke_CallsPopup(EventArgs eventArgs) { using var menu = new SubContextMenu(); int callCount = 0; EventHandler handler = (sender, e) => { Assert.Same(menu, sender); Assert.Same(eventArgs, e); callCount++; }; // Call with handler. menu.Popup += handler; menu.OnPopup(eventArgs); Assert.Equal(1, callCount); // Remove handler. menu.Popup -= handler; menu.OnPopup(eventArgs); Assert.Equal(1, callCount); }
public void ContextMenu_Ctor_MenuItemArray(MenuItem[] items, bool expectedIsParent, MenuItem[] expectedItems) { using var menu = new SubContextMenu(items); Assert.True(menu.CanRaiseEvents); Assert.Null(menu.Container); Assert.False(menu.DesignMode); Assert.NotNull(menu.Events); Assert.Same(menu.Events, menu.Events); Assert.Equal(expectedIsParent, menu.IsParent); Assert.Equal(expectedItems, menu.MenuItems.Cast <MenuItem>()); for (int i = 0; i < menu.MenuItems.Count; i++) { Assert.Equal(i, menu.MenuItems[i].Index); Assert.Equal(menu, menu.MenuItems[i].Parent); } Assert.Same(menu.MenuItems, menu.MenuItems); Assert.Empty(menu.Name); Assert.Equal(RightToLeft.No, menu.RightToLeft); Assert.Null(menu.Site); Assert.Null(menu.SourceControl); Assert.Null(menu.Tag); }
public ContextMenu Item(string text, SubContextMenu subContextMenu) { Items.Add(new ContextMenuItem { Text = text, Items = subContextMenu}); return this; }