Exemplo n.º 1
0
        public SetupStepComPortManual(PrinterConfig printer)
        {
            this.printer = printer;

            FlowLayoutWidget printerComPortContainer = CreateComPortContainer();

            contentRow.AddChild(printerComPortContainer);

            // Construct buttons
            nextButton         = theme.CreateDialogButton("Done".Localize());
            nextButton.Click  += (s, e) => Parent.Close();
            nextButton.Visible = false;

            connectButton        = theme.CreateDialogButton("Connect".Localize());
            connectButton.Click += (s, e) =>
            {
                try
                {
                    printerComPortHelpLink.Visible = false;
                    printerComPortError.TextColor  = theme.TextColor;

                    printerComPortError.Text      = "Attempting to connect".Localize() + "...";
                    printerComPortError.TextColor = theme.TextColor;

                    printer.Connection.ConnectionFailed    += Connection_CommunicationStateChanged;
                    printer.Connection.ConnectionSucceeded += Connection_CommunicationStateChanged;

                    printer.Settings.Helpers.SetComPort(GetSelectedSerialPort());
                    printer.Connection.Connect();

                    connectButton.Visible = false;
                    refreshButton.Visible = false;
                }
                catch
                {
                    printerComPortHelpLink.Visible = false;
                    printerComPortError.TextColor  = Color.Red;
                    printerComPortError.Text       = "Oops! Please select a serial port.".Localize();
                }
            };

            refreshButton        = theme.CreateDialogButton("Refresh".Localize());
            refreshButton.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortManual(printer));
            });

            this.AddPageAction(nextButton);
            this.AddPageAction(connectButton);
            this.AddPageAction(refreshButton);

            // Register listeners
            printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
        }
Exemplo n.º 2
0
        protected override void OnCancel(out bool abortCancel)
        {
            if (!ProfileManager.Instance.ActiveProfiles.Any())
            {
                abortCancel = true;

                UiThread.RunOnIdle(() =>
                {
                    DialogWindow.ChangeToPage <SetupStepMakeModelName>();
                });
            }

            abortCancel = false;
        }
Exemplo n.º 3
0
        public SetupStepComPortTwo(PrinterConfig printer)
        {
            this.printer = printer;

            startingPortNames = FrostedSerialPort.GetPortNames();
            contentRow.AddChild(createPrinterConnectionMessageContainer());

            //Construct buttons
            nextButton         = theme.CreateDialogButton("Done".Localize());
            nextButton.Click  += (s, e) => Parent.Close();
            nextButton.Visible = false;

            connectButton        = theme.CreateDialogButton("Connect".Localize());
            connectButton.Click += (s, e) =>
            {
                // Select the first port that's in GetPortNames() but not in startingPortNames
                string candidatePort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();
                if (candidatePort == null)
                {
                    printerErrorMessage.TextColor = Color.Red;
                    printerErrorMessage.Text      = "Oops! Printer could not be detected ".Localize();
                }
                else
                {
                    printerErrorMessage.TextColor = theme.TextColor;
                    printerErrorMessage.Text      = "Attempting to connect".Localize() + "...";

                    printer.Settings.Helpers.SetComPort(candidatePort);
                    printer.Connection.Connect();
                    connectButton.Visible = false;
                }
            };

            var backButton = theme.CreateDialogButton("<< Back".Localize());

            backButton.Click += (s, e) =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
            };

            this.AddPageAction(nextButton);
            this.AddPageAction(backButton);
            this.AddPageAction(connectButton);

            // Register listeners
            printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
        }
        public FlowLayoutWidget createPrinterConnectionMessageContainer()
        {
            FlowLayoutWidget container = new FlowLayoutWidget(FlowDirection.TopToBottom);

            container.VAnchor = VAnchor.Stretch;
            container.Margin  = new BorderDouble(5);
            BorderDouble elementMargin = new BorderDouble(top: 5);

            string     printerMessageOneText = "MatterControl will now attempt to auto-detect printer.".Localize();
            TextWidget printerMessageOne     = new TextWidget(printerMessageOneText, 0, 0, 10);

            printerMessageOne.Margin    = new BorderDouble(0, 10, 0, 5);
            printerMessageOne.TextColor = ActiveTheme.Instance.PrimaryTextColor;
            printerMessageOne.HAnchor   = HAnchor.Stretch;
            printerMessageOne.Margin    = elementMargin;

            string     printerMessageFourBeg  = "Connect printer and power on".Localize();
            string     printerMessageFourFull = string.Format("1.) {0}.", printerMessageFourBeg);
            TextWidget printerMessageFour     = new TextWidget(printerMessageFourFull, 0, 0, 12);

            printerMessageFour.TextColor = ActiveTheme.Instance.PrimaryTextColor;
            printerMessageFour.HAnchor   = HAnchor.Stretch;
            printerMessageFour.Margin    = elementMargin;

            string     printerMessageFiveTxtBeg  = "Press".Localize();
            string     printerMessageFiveTxtEnd  = "Connect".Localize();
            string     printerMessageFiveTxtFull = string.Format("2.) {0} '{1}'.", printerMessageFiveTxtBeg, printerMessageFiveTxtEnd);
            TextWidget printerMessageFive        = new TextWidget(printerMessageFiveTxtFull, 0, 0, 12);

            printerMessageFive.TextColor = ActiveTheme.Instance.PrimaryTextColor;
            printerMessageFive.HAnchor   = HAnchor.Stretch;
            printerMessageFive.Margin    = elementMargin;

            GuiWidget vSpacer = new GuiWidget();

            vSpacer.VAnchor = VAnchor.Stretch;

            var manualLink = new LinkLabel("Manual Configuration".Localize(), theme)
            {
                Margin    = new BorderDouble(0, 5),
                TextColor = theme.Colors.PrimaryTextColor
            };

            manualLink.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortManual(printer));
            });

            printerErrorMessage = new TextWidget("", 0, 0, 10)
            {
                AutoExpandBoundsToText = true,
                TextColor = Color.Red,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageOne);
            container.AddChild(printerMessageFour);
            container.AddChild(printerErrorMessage);
            container.AddChild(vSpacer);
            container.AddChild(manualLink);

            container.HAnchor = HAnchor.Stretch;
            return(container);
        }
Exemplo n.º 5
0
        public SetupStepMakeModelName()
        {
            bool userIsLoggedIn = !ApplicationController.GuestUserActive?.Invoke() ?? false;

            this.HeaderText = this.WindowTitle = "Printer Setup".Localize();
            this.WindowSize = new VectorMath.Vector2(800 * GuiWidget.DeviceScale, 600 * GuiWidget.DeviceScale);

            contentRow.BackgroundColor = theme.SectionBackgroundColor;
            nextButton = theme.CreateDialogButton("Next".Localize());

            printerPanel = new AddPrinterWidget(theme, (enabled) =>
            {
                nextButton.Enabled = enabled;
            })
            {
                HAnchor = HAnchor.Stretch,
                VAnchor = VAnchor.Stretch
            };

            if (userIsLoggedIn)
            {
                contentRow.AddChild(printerPanel);
                contentRow.Padding = 0;
            }
            else
            {
                contentRow.Padding  = theme.DefaultContainerPadding;
                printerPanel.Margin = new BorderDouble(left: 15, top: theme.DefaultContainerPadding);

                var commonMargin = new BorderDouble(4, 2);

                // Create export button for each plugin
                signInRadioButton = new RadioButton(new RadioButtonViewText("Sign in to access your existing printers".Localize(), theme.TextColor))
                {
                    HAnchor = HAnchor.Left,
                    Margin  = commonMargin.Clone(bottom: 10),
                    Cursor  = Cursors.Hand,
                    Name    = "Sign In Radio Button",
                };
                contentRow.AddChild(signInRadioButton);

                createPrinterRadioButton = new RadioButton(new RadioButtonViewText("Create a new printer", theme.TextColor))
                {
                    HAnchor = HAnchor.Left,
                    Margin  = commonMargin,
                    Cursor  = Cursors.Hand,
                    Name    = "Create Printer Radio Button",
                    Checked = true
                };
                contentRow.AddChild(createPrinterRadioButton);

                createPrinterRadioButton.CheckedStateChanged += (s, e) =>
                {
                    printerPanel.Enabled = createPrinterRadioButton.Checked;
                    this.SetElementVisibility();
                };

                contentRow.AddChild(printerPanel);
            }

            nextButton.Name   = "Next Button";
            nextButton.Click += (s, e) => UiThread.RunOnIdle(async() =>
            {
                if (signInRadioButton?.Checked == true)
                {
                    var authContext             = new AuthenticationContext();
                    authContext.SignInComplete += (s2, e2) =>
                    {
                        this.DialogWindow.ChangeToPage(new OpenPrinterPage("Finish".Localize()));
                    };

                    this.DialogWindow.ChangeToPage(ApplicationController.GetAuthPage(authContext));
                }
                else
                {
                    bool controlsValid = printerPanel.ValidateControls();
                    if (controlsValid &&
                        printerPanel.SelectedPrinter is AddPrinterWidget.MakeModelInfo selectedPrinter)
                    {
                        var printer = await ProfileManager.CreatePrinterAsync(selectedPrinter.Make, selectedPrinter.Model, printerPanel.NewPrinterName);
                        if (printer == null)
                        {
                            printerPanel.SetError("Error creating profile".Localize());
                            return;
                        }

                        UiThread.RunOnIdle(() =>
                        {
                            DialogWindow.ChangeToPage(AppContext.Platform.GetConnectDevicePage(printer) as DialogPage);
                        });
                    }
                }
            });

            var printerNotListedButton = theme.CreateDialogButton("Define New".Localize());

            printerNotListedButton.ToolTipText = "Select this option only if your printer does not appear in the list".Localize();

            printerNotListedButton.Click += async(s, e) =>
            {
                var printer = await ProfileManager.CreatePrinterAsync("Other", "Other", "Custom Printer");

                if (printer == null)
                {
                    printerPanel.SetError("Error creating profile".Localize());
                    return;
                }

                UiThread.RunOnIdle(() =>
                {
                    DialogWindow.ChangeToPage(new SetupCustomPrinter(printer) as DialogPage);
                });
            };

            this.AddPageAction(printerNotListedButton, false);
            this.AddPageAction(nextButton);

            SetElementVisibility();
        }
Exemplo n.º 6
0
        public SetupStepComPortOne(PrinterConfig printer)
        {
            this.WindowTitle = "Setup Wizard".Localize();

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

            var elementMargin = new BorderDouble(top: 5);

            var printerMessageOne = new TextWidget("MatterControl will now attempt to auto-detect your printer.".Localize(), 0, 0, 10)
            {
                TextColor = theme.TextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageOne);

            var printerMessageTwo = new WrappedTextWidget(string.Format("1.) {0} ({1}).", "Unplug printer USB cable from computer".Localize(), "if connected".Localize()), 12)
            {
                TextColor = theme.TextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageTwo);

            var printerMessageThree = new TextWidget(string.Format("2.) {0} '{1}'.", "Press".Localize(), "Continue".Localize()), 0, 0, 12)
            {
                TextColor = theme.TextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageThree);

            var removeImage = StaticData.Instance.LoadImage(Path.Combine("Images", "remove usb.png"));

            removeImage.SetRecieveBlender(new BlenderPreMultBGRA());
            container.AddChild(new ImageWidget(removeImage)
            {
                HAnchor = HAnchor.Center,
                Margin  = new BorderDouble(0, 10),
            });

            GuiWidget vSpacer = new GuiWidget();

            vSpacer.VAnchor = VAnchor.Stretch;
            container.AddChild(vSpacer);

            var setupManualConfigurationOrSkipConnectionWidget = new TextWidget("You can also".Localize() + ":", 0, 0, 10)
            {
                TextColor = theme.TextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(setupManualConfigurationOrSkipConnectionWidget);

            var manualLink = new LinkLabel("Manually Configure Connection".Localize(), theme)
            {
                Margin    = new BorderDouble(0, 5),
                TextColor = theme.TextColor
            };

            manualLink.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortManual(printer));
            });
            container.AddChild(manualLink);

            var printerMessageFour = new TextWidget("or".Localize(), 0, 0, 10)
            {
                TextColor = theme.TextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageFour);

            var skipConnectionLink = new LinkLabel("Skip Connection Setup".Localize(), theme)
            {
                Margin    = new BorderDouble(0, 8),
                TextColor = theme.TextColor
            };

            skipConnectionLink.Click += (s, e) =>
            {
                printer.Connection.HaltConnectionThread();
                Parent.Close();
            };
            container.AddChild(skipConnectionLink);

            contentRow.AddChild(container);

            // Construct buttons
            var nextButton = theme.CreateDialogButton("Continue".Localize());

            nextButton.Click += (s, e) =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortTwo(printer));
            };

            this.AddPageAction(nextButton);
        }
Exemplo n.º 7
0
        public SetupStepComPortOne(PrinterConfig printer)
        {
            this.WindowTitle = "Setup Wizard".Localize();

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

            BorderDouble elementMargin = new BorderDouble(top: 5);

            var printerMessageOne = new TextWidget("MatterControl will now attempt to auto-detect printer.".Localize(), 0, 0, 10)
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageOne);

            var printerMessageTwo = new TextWidget(string.Format("1.) {0} ({1}).", "Disconnect printer".Localize(), "if currently connected".Localize()), 0, 0, 12)
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageTwo);

            var printerMessageThree = new TextWidget(string.Format("2.) {0} '{1}'.", "Press".Localize(), "Continue".Localize()), 0, 0, 12)
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageThree);

            GuiWidget vSpacer = new GuiWidget();

            vSpacer.VAnchor = VAnchor.Stretch;
            container.AddChild(vSpacer);

            var setupManualConfigurationOrSkipConnectionWidget = new TextWidget("You can also".Localize() + ":", 0, 0, 10)
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(setupManualConfigurationOrSkipConnectionWidget);

            Button manualLink = linkButtonFactory.Generate("Manually Configure Connection".Localize());

            manualLink.Margin = new BorderDouble(0, 5);
            manualLink.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortManual(printer));
            });
            container.AddChild(manualLink);

            var printerMessageFour = new TextWidget("or".Localize(), 0, 0, 10)
            {
                TextColor = ActiveTheme.Instance.PrimaryTextColor,
                HAnchor   = HAnchor.Stretch,
                Margin    = elementMargin
            };

            container.AddChild(printerMessageFour);

            Button skipConnectionLink = linkButtonFactory.Generate("Skip Connection Setup".Localize());

            skipConnectionLink.Margin = new BorderDouble(0, 8);
            skipConnectionLink.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                printer.Connection.HaltConnectionThread();
                Parent.Close();
            });
            container.AddChild(skipConnectionLink);

            contentRow.AddChild(container);

            //Construct buttons
            var nextButton = theme.CreateDialogButton("Continue".Localize());

            nextButton.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortTwo(printer));
            });

            this.AddPageAction(nextButton);
        }
Exemplo n.º 8
0
        public SetupStepMakeModelName()
        {
            bool userIsLoggedIn = !ApplicationController.GuestUserActive?.Invoke() ?? false;

            this.HeaderText = this.WindowTitle = "Printer Setup".Localize();

            var addPrinterColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.Stretch,
                VAnchor = VAnchor.Fit,
            };

            printerManufacturerSelector = new BoundDropList(string.Format("- {0} -", "Select Make".Localize()), theme, maxHeight: 200)
            {
                HAnchor    = HAnchor.Stretch,
                Margin     = elementMargin,
                Name       = "Select Make",
                ListSource = OemSettings.Instance.AllOems,
                TabStop    = true
            };

            printerManufacturerSelector.SelectionChanged += ManufacturerDropList_SelectionChanged;

            printerMakeContainer = CreateSelectionContainer(
                "Make".Localize() + ":",
                "Select the printer manufacturer".Localize(),
                printerManufacturerSelector);

            printerModelSelector = new BoundDropList(string.Format("- {0} -", "Select Model".Localize()), theme, maxHeight: 200)
            {
                Name    = "Select Model",
                HAnchor = HAnchor.Stretch,
                Margin  = elementMargin,
                TabStop = true
            };
            printerModelSelector.SelectionChanged += ModelDropList_SelectionChanged;

            printerModelContainer = CreateSelectionContainer(
                "Model".Localize() + ":",
                "Select the printer model".Localize(),
                printerModelSelector);

            //Add inputs to main container
            addPrinterColumn.AddChild(printerMakeContainer);
            addPrinterColumn.AddChild(printerModelContainer);
            addPrinterColumn.AddChild(createPrinterNameContainer());

            RadioButton signInRadioButton = null;

            if (userIsLoggedIn)
            {
                contentRow.AddChild(addPrinterColumn);
            }
            else
            {
                contentRow.Padding      = 10;
                addPrinterColumn.Margin = new BorderDouble(28, 15, 15, 5);

                var commonMargin = new BorderDouble(4, 2);

                // Create export button for each plugin
                signInRadioButton = new RadioButton(new RadioButtonViewText("Sign in to access your existing printers".Localize(), theme.Colors.PrimaryTextColor))
                {
                    HAnchor = HAnchor.Left,
                    Margin  = commonMargin.Clone(bottom: 10),
                    Cursor  = Cursors.Hand,
                    Name    = "Sign In Radio Button",
                };
                contentRow.AddChild(signInRadioButton);

                createPrinterRadioButton = new RadioButton(new RadioButtonViewText("Create a new printer", theme.Colors.PrimaryTextColor))
                {
                    HAnchor = HAnchor.Left,
                    Margin  = commonMargin,
                    Cursor  = Cursors.Hand,
                    Name    = "Create Printer Radio Button",
                    Checked = true
                };
                contentRow.AddChild(createPrinterRadioButton);

                createPrinterRadioButton.CheckedStateChanged += (s, e) =>
                {
                    addPrinterColumn.Enabled = createPrinterRadioButton.Checked;
                    this.SetElementVisibility();
                };

                contentRow.AddChild(addPrinterColumn);
            }

            nextButton        = theme.CreateDialogButton("Next".Localize());
            nextButton.Name   = "Next Button";
            nextButton.Click += (s, e) => UiThread.RunOnIdle(async() =>
            {
                if (signInRadioButton?.Checked == true)
                {
                    var authContext             = new AuthenticationContext();
                    authContext.SignInComplete += (s2, e2) =>
                    {
                        this.DialogWindow.ChangeToPage(new SelectPrinterPage("Finish".Localize()));
                    };

                    this.DialogWindow.ChangeToPage(ApplicationController.GetAuthPage(authContext));
                }
                else
                {
                    bool controlsValid = this.ValidateControls();
                    if (controlsValid)
                    {
                        var printer = await ProfileManager.CreateProfileAsync(activeMake, activeModel, activeName);
                        if (printer == null)
                        {
                            this.printerNameError.Text    = "Error creating profile".Localize();
                            this.printerNameError.Visible = true;
                            return;
                        }

                        this.LoadCalibrationPrints();

#if __ANDROID__
                        UiThread.RunOnIdle(() => DialogWindow.ChangeToPage <AndroidConnectDevicePage>());
#else
                        UiThread.RunOnIdle(() =>
                        {
                            DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
                        });
#endif
                    }
                }
            });

            this.AddPageAction(nextButton);

            usingDefaultName = true;

            if (printerManufacturerSelector.MenuItems.Count == 1)
            {
                printerManufacturerSelector.SelectedIndex = 0;
            }

            SetElementVisibility();
        }
Exemplo n.º 9
0
        public SetupStepMakeModelName()
        {
            this.WindowTitle = "Setup Wizard".Localize();

            printerManufacturerSelector = new BoundDropList(string.Format("- {0} -", "Select Make".Localize()), theme, maxHeight: 200)
            {
                HAnchor    = HAnchor.Stretch,
                Margin     = elementMargin,
                Name       = "Select Make",
                ListSource = OemSettings.Instance.AllOems,
                TabStop    = true
            };

            printerManufacturerSelector.SelectionChanged += ManufacturerDropList_SelectionChanged;

            printerMakeContainer = CreateSelectionContainer(
                "Make".Localize() + ":",
                "Select the printer manufacturer".Localize(),
                printerManufacturerSelector);

            printerModelSelector = new BoundDropList(string.Format("- {0} -", "Select Model".Localize()), theme, maxHeight: 200)
            {
                Name    = "Select Model",
                HAnchor = HAnchor.Stretch,
                Margin  = elementMargin,
                TabStop = true
            };
            printerModelSelector.SelectionChanged += ModelDropList_SelectionChanged;

            printerModelContainer = CreateSelectionContainer("Model".Localize() + ":", "Select the printer model".Localize(), printerModelSelector);

            //Add inputs to main container
            contentRow.AddChild(printerMakeContainer);
            contentRow.AddChild(printerModelContainer);
            contentRow.AddChild(createPrinterNameContainer());

            //Construct buttons
            nextButton        = theme.CreateDialogButton("Next".Localize());
            nextButton.Name   = "Save & Continue Button";
            nextButton.Click += async(s, e) =>
            {
                bool controlsValid = this.ValidateControls();
                if (controlsValid)
                {
                    var printer = await ProfileManager.CreateProfileAsync(activeMake, activeModel, activeName);

                    if (printer == null)
                    {
                        this.printerNameError.Text    = "Error creating profile".Localize();
                        this.printerNameError.Visible = true;
                        return;
                    }

                    LoadCalibrationPrints();

#if __ANDROID__
                    UiThread.RunOnIdle(() => DialogWindow.ChangeToPage <AndroidConnectDevicePage>());
#else
                    UiThread.RunOnIdle(() =>
                    {
                        DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
                    });
#endif
                }
            };

            this.AddPageAction(nextButton);

            usingDefaultName = true;

            if (printerManufacturerSelector.MenuItems.Count == 1)
            {
                printerManufacturerSelector.SelectedIndex = 0;
            }

            SetElementVisibility();
        }
Exemplo n.º 10
0
        public SetupStepComPortTwo(PrinterConfig printer)
        {
            this.printer = printer;

            startingPortNames = FrostedSerialPort.GetPortNames();
            contentRow.AddChild(createPrinterConnectionMessageContainer());

            //Construct buttons
            nextButton         = theme.CreateDialogButton("Done".Localize());
            nextButton.Click  += (s, e) => Parent.Close();
            nextButton.Visible = false;

            var connectButtonHasBeenClicked = false;

            void CheckOnPorts()
            {
                string candidatePort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();

                if (candidatePort != null)
                {
                    // we found a new added port click the connect button for the user
                    connectButton.InvokeClick();
                }
                else if (!connectButtonHasBeenClicked && this.ActuallyVisibleOnScreen())
                {
                    // keep checking as long as this is open
                    UiThread.RunOnIdle(CheckOnPorts, .2);
                }
            }

            UiThread.RunOnIdle(CheckOnPorts, .2);

            connectButton        = theme.CreateDialogButton("Connect".Localize());
            connectButton.Click += (s, e) =>
            {
                connectButtonHasBeenClicked = true;
                // Select the first port that's in GetPortNames() but not in startingPortNames
                foundPort = FrostedSerialPort.GetPortNames().Except(startingPortNames).FirstOrDefault();
                if (foundPort == null)
                {
                    foundPort = FrostedSerialPort.GetPortNames(includeEmulator: false).LastOrDefault();
                    if (foundPort != null)
                    {
                        // try to connect to the last port found
                        printerConnectionMessage.TextColor = theme.TextColor;
                        printerConnectionMessage.Text      = "Attempting to connect to {0}".Localize().FormatWith(foundPort) + "...";

                        printer.Settings.Helpers.SetComPort(foundPort);
                        printer.Connection.Connect();
                        connectButton.Visible = false;
                    }
                    else
                    {
                        // no com port was found, attempt to connect to a com port if there is any
                        printerConnectionMessage.TextColor = Color.Red;
                        printerConnectionMessage.Text      = "Oops! Printer could not be detected ".Localize();
                    }
                }
                else
                {
                    printerConnectionMessage.TextColor = theme.TextColor;
                    printerConnectionMessage.Text      = "Attempting to connect to {0}".Localize().FormatWith(foundPort) + "...";

                    printer.Settings.Helpers.SetComPort(foundPort);
                    printer.Connection.Connect();
                    connectButton.Visible = false;
                }
            };

            var backButton = theme.CreateDialogButton("<< Back".Localize());

            backButton.Click += (s, e) =>
            {
                DialogWindow.ChangeToPage(new SetupStepComPortOne(printer));
            };

            this.AddPageAction(nextButton);
            this.AddPageAction(backButton);
            this.AddPageAction(connectButton);

            // Register listeners
            printer.Connection.CommunicationStateChanged += Connection_CommunicationStateChanged;
        }