From 5f9899d2f73bf5d717f6583b41f4cf9d2937827f Mon Sep 17 00:00:00 2001 From: John Lewin Date: Mon, 9 May 2016 14:20:13 -0700 Subject: [PATCH] Fix Macro null reference errors - Make the Add->Save feature work for Macros --- .../ControlWidgets/MacroControls.cs | 5 ++ PrinterControls/EditMacrosWindow.cs | 70 +++++++++++-------- .../Settings/LayeredProfile.cs | 2 +- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/PrinterControls/ControlWidgets/MacroControls.cs b/PrinterControls/ControlWidgets/MacroControls.cs index 7c45da898..c2f31d625 100644 --- a/PrinterControls/ControlWidgets/MacroControls.cs +++ b/PrinterControls/ControlWidgets/MacroControls.cs @@ -129,6 +129,11 @@ namespace MatterHackers.MatterControl.PrinterControls macroButtonContainer.Margin = new BorderDouble(3, 0); macroButtonContainer.Padding = new BorderDouble(3); + if (ActiveSliceSettings.Instance?.Macros == null) + { + return macroButtonContainer; + } + int buttonCount = 0; foreach (GCodeMacro macro in ActiveSliceSettings.Instance.Macros) { diff --git a/PrinterControls/EditMacrosWindow.cs b/PrinterControls/EditMacrosWindow.cs index 65b22c131..2be268077 100644 --- a/PrinterControls/EditMacrosWindow.cs +++ b/PrinterControls/EditMacrosWindow.cs @@ -224,6 +224,11 @@ namespace MatterHackers.MatterControl { windowController.ActiveMacro.Name = macroNameInput.Text; windowController.ActiveMacro.GCode = macroCommandInput.Text; + + if (!ActiveSliceSettings.Instance.Macros.Contains(windowController.ActiveMacro)) + { + ActiveSliceSettings.Instance.Macros.Add(windowController.ActiveMacro); + } } } @@ -269,41 +274,44 @@ namespace MatterHackers.MatterControl topToBottom.AddChild(presetsFormContainer); - foreach (GCodeMacro macro in ActiveSliceSettings.Instance.Macros) + if (ActiveSliceSettings.Instance?.Macros != null) { - FlowLayoutWidget macroRow = new FlowLayoutWidget(); - macroRow.Margin = new BorderDouble(3, 0, 3, 3); - macroRow.HAnchor = Agg.UI.HAnchor.ParentLeftRight; - macroRow.Padding = new BorderDouble(3); - macroRow.BackgroundColor = RGBA_Bytes.White; - - TextWidget buttonLabel = new TextWidget(macro.Name); - macroRow.AddChild(buttonLabel); - - macroRow.AddChild(new HorizontalSpacer()); - - // You can't pass a foreach variable into a link function or it wall always be the last item. - // So we make a local variable copy of it and pass that. This will get the right one. - var localMacroReference = macro; - - Button editLink = linkButtonFactory.Generate("edit".Localize()); - editLink.Margin = new BorderDouble(right: 5); - editLink.Click += (sender, e) => + foreach (GCodeMacro macro in ActiveSliceSettings.Instance.Macros) { - windowController.ChangeToMacroDetail(localMacroReference); - }; - macroRow.AddChild(editLink); + FlowLayoutWidget macroRow = new FlowLayoutWidget(); + macroRow.Margin = new BorderDouble(3, 0, 3, 3); + macroRow.HAnchor = Agg.UI.HAnchor.ParentLeftRight; + macroRow.Padding = new BorderDouble(3); + macroRow.BackgroundColor = RGBA_Bytes.White; - Button removeLink = linkButtonFactory.Generate("remove".Localize()); - removeLink.Click += (sender, e) => - { - ActiveSliceSettings.Instance.Macros.Remove(localMacroReference); - windowController.functionToCallOnSave(this, null); - windowController.ChangeToMacroList(); - }; - macroRow.AddChild(removeLink); + TextWidget buttonLabel = new TextWidget(macro.Name); + macroRow.AddChild(buttonLabel); - presetsFormContainer.AddChild(macroRow); + macroRow.AddChild(new HorizontalSpacer()); + + // You can't pass a foreach variable into a link function or it wall always be the last item. + // So we make a local variable copy of it and pass that. This will get the right one. + var localMacroReference = macro; + + Button editLink = linkButtonFactory.Generate("edit".Localize()); + editLink.Margin = new BorderDouble(right: 5); + editLink.Click += (sender, e) => + { + windowController.ChangeToMacroDetail(localMacroReference); + }; + macroRow.AddChild(editLink); + + Button removeLink = linkButtonFactory.Generate("remove".Localize()); + removeLink.Click += (sender, e) => + { + ActiveSliceSettings.Instance.Macros.Remove(localMacroReference); + windowController.functionToCallOnSave(this, null); + windowController.ChangeToMacroList(); + }; + macroRow.AddChild(removeLink); + + presetsFormContainer.AddChild(macroRow); + } } Button addMacroButton = textImageButtonFactory.Generate("Add".Localize(), "icon_circle_plus.png"); diff --git a/SlicerConfiguration/Settings/LayeredProfile.cs b/SlicerConfiguration/Settings/LayeredProfile.cs index 2ba79f157..dcf2ec2f8 100644 --- a/SlicerConfiguration/Settings/LayeredProfile.cs +++ b/SlicerConfiguration/Settings/LayeredProfile.cs @@ -57,7 +57,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration this.BaseLayer = baseConfig; } - public List Macros { get; set; } + public List Macros { get; set; } = new List(); [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context)