ToolStripMenuItem IUIMenuTreeVisitor <ToolStripMenuItem> .VisitElement(UIMenuNode.Element element) { if (element.TextProvider == null && element.IconProvider == null) { return(null); } UIActionState currentActionState = ActionHandler.TryPerformAction(element.Action, false); if (!currentActionState.Visible) { return(null); } var menuItem = UIActionToolStripMenuItem.CreateFrom(element); menuItem.Update(currentActionState); var actionHandler = ActionHandler; menuItem.Click += (_, __) => { try { actionHandler.TryPerformAction(element.Action, true); } catch (Exception exc) { MessageBox.Show(exc.Message); } }; return(menuItem); }
private void MainMenuItem_DropDownOpening(object sender, EventArgs e) { var mainMenuItem = (ToolStripDropDownItem)sender; // Use DefaultForeColor rather than titleBarForeColor when dropped down. mainMenuItem.ForeColor = DefaultForeColor; // Remember last toolstrip separator before a developer tool item with FirstInGroup set. ToolStripSeparator lastSeparator = null; foreach (ToolStripItem toolStripItem in mainMenuItem.DropDownItems) { if (toolStripItem is UIActionToolStripMenuItem menuItem) { UIActionState actionState = mainMenuActionHandler.TryPerformAction(menuItem.Action, false); menuItem.Update(actionState); if (Session.IsDeveloperTool(menuItem.Action)) { // Hide instead of disable developer tool items. bool visible = actionState.UIActionVisibility == UIActionVisibility.Enabled; menuItem.Visible = visible; if (lastSeparator != null) { lastSeparator.Visible = visible; } } } lastSeparator = toolStripItem as ToolStripSeparator; } }
public void Update(UIActionState currentActionState) { Enabled = currentActionState.Enabled; Checked = currentActionState.Checked; }
private protected MenuCaptionBarForm() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.UserMouse | ControlStyles.OptimizedDoubleBuffer | ControlStyles.FixedHeight | ControlStyles.FixedWidth | ControlStyles.ResizeRedraw | ControlStyles.Opaque, true); minimizeButton = CreateCaptionButton(); minimizeButton.Click += (_, __) => WindowState = FormWindowState.Minimized; maximizeButton = CreateCaptionButton(); maximizeButton.Click += (_, __) => { inRestoreCommand = WindowState == FormWindowState.Maximized; try { WindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized; } finally { inRestoreCommand = false; } UpdateMaximizeButtonIcon(); }; // Specialized save button which binds on the SaveToFile UIAction. saveButton = CreateCaptionButton(); saveButton.Visible = false; saveButton.Click += (_, __) => { try { ActionHandler.TryPerformAction(SharedUIAction.SaveToFile.Action, true); } catch (Exception exc) { MessageBox.Show(exc.Message); } }; ActionHandler.UIActionsInvalidated += _ => { // Update the save button each time the handler is invalidated. // Some kind of checked state doesn't seem to be supported, so ignore UIActionState.Checked. UIActionState currentActionState = ActionHandler.TryPerformAction(SharedUIAction.SaveToFile.Action, false); saveButton.Visible = currentActionState.Visible; saveButton.Enabled = currentActionState.Enabled; if (!saveButton.Enabled) { saveButton.FlatAppearance.BorderColor = ObservableStyle.BackColor; } }; closeButton = CreateCaptionButton(); closeButton.Click += (_, __) => Close(); MainMenuStrip = new MenuStrip(); SuspendLayout(); Controls.Add(minimizeButton); Controls.Add(maximizeButton); Controls.Add(saveButton); Controls.Add(closeButton); Controls.Add(MainMenuStrip); ResumeLayout(); ToolTip = new ToolTip(); UpdateToolTips(); ObservableStyle.NotifyChange += ObservableStyle_NotifyChange; AllowTransparency = true; TransparencyKey = ObservableStyle.SuggestedTransparencyKey; mainMenuActionHandler = new UIActionHandler(); this.BindActions(StandardUIActionBindings); Session.Current.CurrentLocalizerChanged += CurrentLocalizerChanged; }