From d38a45bea765253054cde82c01cee986f652e9bb Mon Sep 17 00:00:00 2001 From: jlewin Date: Tue, 18 Jun 2019 18:29:10 -0700 Subject: [PATCH] Add menu support for OperationGroups --- .../ApplicationView/ApplicationController.cs | 7 ++- .../View3D/PrinterBar/OverflowBar.cs | 4 +- .../PartPreviewWindow/ViewControls3D.cs | 56 +++++++++++++++---- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index f4116d403..b95a01796 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -763,6 +763,7 @@ namespace MatterHackers.MatterControl new SceneSelectionSeparator(), new OperationGroup("Align") { + TitleResolver = () => "Align".Localize(), StickySelection = true, Operations = new List() { @@ -871,6 +872,7 @@ namespace MatterHackers.MatterControl }, new OperationGroup("Array") { + TitleResolver = () => "Array".Localize(), StickySelection = true, Operations = new List() { @@ -914,6 +916,7 @@ namespace MatterHackers.MatterControl }, new OperationGroup("ModifyMesh") { + TitleResolver = () => "Mesh Modifiers".Localize(), StickySelection = true, Operations = new List() { @@ -955,9 +958,9 @@ namespace MatterHackers.MatterControl } } }, - new OperationGroup("Booleans") { + TitleResolver = () => "Booleans".Localize(), StickySelection = true, Operations = new List() { @@ -3278,7 +3281,7 @@ Support and tutorials: VAnchor = VAnchor.Stretch }; - helpDocsTab = new ChromeTab("HelpDocs", "Help".Localize(), tabControl, helpTreePanel, theme, hasClose: false) + helpDocsTab = new ChromeTab("HelpDocs", "Help".Localize(), tabControl, helpTreePanel, theme) { MinimumSize = new Vector2(0, theme.TabButtonHeight), Name = "Library Tab", diff --git a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/OverflowBar.cs b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/OverflowBar.cs index 24c679745..e0f47a140 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/OverflowBar.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/OverflowBar.cs @@ -43,8 +43,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { public class OverflowBar : Toolbar { - private static HashSet ignoredTypes = new HashSet { typeof(HorizontalLine), typeof(SearchInputBox) }; - private static HashSet ignoredInMenuTypes = new HashSet { typeof(VerticalLine), typeof(HorizontalLine), typeof(SearchInputBox), typeof(HorizontalSpacer) }; + protected static HashSet ignoredTypes = new HashSet { typeof(HorizontalLine), typeof(SearchInputBox) }; + protected static HashSet ignoredInMenuTypes = new HashSet { typeof(VerticalLine), typeof(HorizontalLine), typeof(SearchInputBox), typeof(HorizontalSpacer) }; public OverflowBar(ThemeConfig theme) : this(null, theme) diff --git a/MatterControlLib/PartPreviewWindow/ViewControls3D.cs b/MatterControlLib/PartPreviewWindow/ViewControls3D.cs index 5c9edd917..113dee630 100644 --- a/MatterControlLib/PartPreviewWindow/ViewControls3D.cs +++ b/MatterControlLib/PartPreviewWindow/ViewControls3D.cs @@ -92,7 +92,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private ISceneContext sceneContext; private PartWorkspace workspace; private ViewControls3DButtons activeTransformState = ViewControls3DButtons.PartSelect; - private List<(GuiWidget button, SceneSelectionOperation operation)> operationButtons; + private Dictionary operationButtons; private MainViewWidget mainViewWidget = null; private PopupMenuButton bedMenuButton; private ThemeConfig theme; @@ -110,6 +110,43 @@ namespace MatterHackers.MatterControl.PartPreviewWindow view3DWidget.InteractionLayer.Focus(); }; + this.OverflowButton.DynamicPopupContent = () => + { + var menuTheme = AppContext.MenuTheme; + var popupMenu = new PopupMenu(theme); + int i = 0; + + foreach (var widget in this.ActionArea.Children.Where(c => !c.Visible && !ignoredInMenuTypes.Contains(c.GetType()))) + { + if (operationButtons.TryGetValue(widget, out SceneSelectionOperation operation)) + { + if (operation is OperationGroup operationGroup) + { + popupMenu.CreateSubMenu( + operationGroup.Title, + menuTheme, + (subMenu) => + { + foreach (var childOperation in operationGroup.Operations) + { + var menuItem = subMenu.CreateMenuItem(childOperation.Title, childOperation.Icon(menuTheme.InvertIcons)); + menuItem.Click += (s, e) => UiThread.RunOnIdle(() => + { + childOperation.Action?.Invoke(sceneContext); + }); + } + }); + } + else + { + popupMenu.CreateMenuItem(operation.Title, operation.Icon(menuTheme.InvertIcons)); + } + } + } + + return popupMenu; + }; + this.IsPrinterMode = isPrinterType; this.sceneContext = workspace.SceneContext; this.workspace = workspace; @@ -243,7 +280,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow AddChild(partSelectButton); } - operationButtons = new List<(GuiWidget, SceneSelectionOperation)>(); + operationButtons = new Dictionary(); // Add Selected IObject3D -> Operations to toolbar foreach (var namedAction in ApplicationController.Instance.RegisteredSceneOperations) @@ -311,9 +348,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } }); - groupButton.VisibleChanged += (s, e) => Console.WriteLine(); - - button = groupButton; } else if (namedAction.Icon != null) @@ -340,7 +374,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow }; } - operationButtons.Add((button, namedAction)); + operationButtons.Add(button, namedAction); // Only bind Click event if not a SplitButton if (!(button is PopupMenuButton)) @@ -600,13 +634,13 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private void Scene_SelectionChanged(object sender, EventArgs e) { // Set enabled level based on operation rules - foreach (var item in operationButtons) + foreach (var (button, operation) in operationButtons.Select(kvp => (kvp.Key, kvp.Value))) { - item.button.Enabled = item.operation.IsEnabled?.Invoke(sceneContext) ?? false; + button.Enabled = operation.IsEnabled?.Invoke(sceneContext) ?? false; - if (item.operation is OperationGroup operationGroup - && item.button is PopupMenuButton splitButton - && item.button.Descendants().FirstOrDefault() is IconButton iconButton) + if (operation is OperationGroup operationGroup + && button is PopupMenuButton splitButton + && button.Descendants().FirstOrDefault() is IconButton iconButton) { iconButton.Enabled = operationGroup.GetDefaultOperation().IsEnabled(sceneContext); }