Example #1
0
        private void RebuildList(PrinterSettings printerSettings)
        {
            this.contentRow.CloseAllChildren();

            if (printerSettings?.Macros != null)
            {
                foreach (GCodeMacro macro in printerSettings.Macros)
                {
                    var macroRow = new FlowLayoutWidget
                    {
                        Margin  = new BorderDouble(3, 0, 3, 3),
                        HAnchor = HAnchor.Stretch,
                        Padding = new BorderDouble(3),
                    };

                    macroRow.AddChild(new TextWidget(GCodeMacro.FixMacroName(macro.Name), textColor: theme.TextColor));

                    macroRow.AddChild(new HorizontalSpacer());

                    // You can't use the foreach variable inside the lambda functions directly or it will always be the last item.
                    // We make a local variable to create a closure around it to ensure we get the correct instance
                    var localMacroReference = macro;

                    var editLink = new LinkLabel("edit".Localize(), theme)
                    {
                        Margin    = new BorderDouble(right: 5),
                        TextColor = theme.TextColor
                    };
                    editLink.Click += (s, e) =>
                    {
                        this.DialogWindow.ChangeToPage(
                            new MacroDetailPage(localMacroReference, printerSettings));
                    };
                    macroRow.AddChild(editLink);

                    var removeLink = new LinkLabel("remove".Localize(), theme)
                    {
                        TextColor = theme.TextColor
                    };
                    removeLink.Click += (sender, e) =>
                    {
                        printerSettings.Macros.Remove(localMacroReference);
                        printerSettings.Save();
                        printerSettings.NotifyMacrosChanged();
                        this.RebuildList(printerSettings);
                    };
                    macroRow.AddChild(removeLink);

                    contentRow.AddChild(macroRow);
                }
            }
        }
Example #2
0
        public MacroDetailPage(GCodeMacro gcodeMacro, PrinterSettings printerSettings)
        {
            // Form validation fields
            MHTextEditWidget macroNameInput;
            MHTextEditWidget macroCommandInput;
            TextWidget       macroCommandError;
            TextWidget       macroNameError;

            this.HeaderText      = "Edit Macro".Localize();
            this.printerSettings = printerSettings;

            var elementMargin = new BorderDouble(top: 3);

            contentRow.Padding += 3;

            contentRow.AddChild(new TextWidget("Macro Name".Localize() + ":", 0, 0, 12)
            {
                TextColor = theme.Colors.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = new BorderDouble(0, 0, 0, 1)
            });

            contentRow.AddChild(macroNameInput = new MHTextEditWidget(GCodeMacro.FixMacroName(gcodeMacro.Name))
            {
                HAnchor = HAnchor.Stretch
            });

            contentRow.AddChild(macroNameError = new TextWidget("Give the macro a name".Localize() + ".", 0, 0, 10)
            {
                TextColor = theme.Colors.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            });

            contentRow.AddChild(new TextWidget("Macro Commands".Localize() + ":", 0, 0, 12)
            {
                TextColor = theme.Colors.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = new BorderDouble(0, 0, 0, 1)
            });

            macroCommandInput = new MHTextEditWidget(gcodeMacro.GCode, pixelHeight: 120, multiLine: true, typeFace: ApplicationController.GetTypeFace(NamedTypeFace.Liberation_Mono))
            {
                HAnchor = HAnchor.Stretch,
                VAnchor = VAnchor.Stretch
            };
            macroCommandInput.ActualTextEditWidget.VAnchor = VAnchor.Stretch;
            macroCommandInput.DrawFromHintedCache();
            contentRow.AddChild(macroCommandInput);

            contentRow.AddChild(macroCommandError = new TextWidget("This should be in 'G-Code'".Localize() + ".", 0, 0, 10)
            {
                TextColor = theme.Colors.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            });

            var container = new FlowLayoutWidget
            {
                Margin  = new BorderDouble(0, 5),
                HAnchor = HAnchor.Stretch
            };

            contentRow.AddChild(container);

            var saveMacroButton = theme.CreateDialogButton("Save".Localize());

            saveMacroButton.Click += (s, e) =>
            {
                UiThread.RunOnIdle(() =>
                {
                    if (ValidateMacroForm())
                    {
                        // SaveActiveMacro
                        gcodeMacro.Name  = macroNameInput.Text;
                        gcodeMacro.GCode = macroCommandInput.Text;

                        if (!printerSettings.Macros.Contains(gcodeMacro))
                        {
                            printerSettings.Macros.Add(gcodeMacro);
                        }

                        printerSettings.NotifyMacrosChanged();
                        printerSettings.Save();

                        this.DialogWindow.ChangeToPage(new MacroListPage(printerSettings));
                    }
                });
            };

            this.AddPageAction(saveMacroButton);

            // Define field validation
            var validationMethods        = new ValidationMethods();
            var stringValidationHandlers = new FormField.ValidationHandler[] { validationMethods.StringIsNotEmpty };

            formFields = new List <FormField>
            {
                new FormField(macroNameInput, macroNameError, stringValidationHandlers),
                new FormField(macroCommandInput, macroCommandError, stringValidationHandlers)
            };
        }