private void Printer_SettingChanged(object s, EventArgs e)
        {
            string settingsName = (e as StringEventArgs)?.Data;

            if (settingsName != null && settingsName == SettingsKey.printer_name)
            {
                // Allow enough time for ProfileManager to respond and refresh its data
                UiThread.RunOnIdle(() =>
                {
                    HardwareTreeView.CreatePrinterProfilesTree(printersNode, theme);
                }, .2);

                this.Invalidate();
            }
        }
        public HardwareTreeView(ThemeConfig theme)
            : base(theme)
        {
            rootColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.Stretch,
                VAnchor = VAnchor.Fit
            };
            this.AddChild(rootColumn);

            // Printers
            printersNode = new TreeNode(theme)
            {
                Text             = "Printers".Localize(),
                HAnchor          = HAnchor.Stretch,
                AlwaysExpandable = true,
                Image            = AggContext.StaticData.LoadIcon("printer.png", 16, 16, theme.InvertIcons)
            };
            printersNode.TreeView = this;

            var forcedHeight = 20;
            var mainRow      = printersNode.Children.FirstOrDefault();

            mainRow.HAnchor = HAnchor.Stretch;
            mainRow.AddChild(new HorizontalSpacer());

            // add in the create printer button
            var createPrinter = new IconButton(AggContext.StaticData.LoadIcon("md-add-circle_18.png", 18, 18, theme.InvertIcons), theme)
            {
                Name        = "Create Printer",
                VAnchor     = VAnchor.Center,
                Margin      = theme.ButtonSpacing.Clone(left: theme.ButtonSpacing.Right),
                ToolTipText = "Create Printer".Localize(),
                Height      = forcedHeight,
                Width       = forcedHeight
            };

            createPrinter.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                if (ApplicationController.Instance.AnyPrintTaskRunning)
                {
                    StyledMessageBox.ShowMessageBox("Please wait until the print has finished and try again.".Localize(), "Can't add printers while printing".Localize());
                }
                else
                {
                    DialogWindow.Show(PrinterSetup.GetBestStartPage(PrinterSetup.StartPageOptions.ShowMakeModel));
                }
            });
            mainRow.AddChild(createPrinter);

            // add in the import printer button
            var importPrinter = new IconButton(AggContext.StaticData.LoadIcon("md-import_18.png", 18, 18, theme.InvertIcons), theme)
            {
                VAnchor     = VAnchor.Center,
                Margin      = theme.ButtonSpacing,
                ToolTipText = "Import Printer".Localize(),
                Height      = forcedHeight,
                Width       = forcedHeight
            };

            importPrinter.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                AggContext.FileDialogs.OpenFileDialog(
                    new OpenFileDialogParams(
                        "settings files|*.ini;*.printer;*.slice"),
                    (result) =>
                {
                    if (!string.IsNullOrEmpty(result.FileName) &&
                        File.Exists(result.FileName))
                    {
                        //simpleTabs.RemoveTab(simpleTabs.ActiveTab);
                        if (ProfileManager.ImportFromExisting(result.FileName))
                        {
                            string importPrinterSuccessMessage = "You have successfully imported a new printer profile. You can find '{0}' in your list of available printers.".Localize();
                            DialogWindow.Show(
                                new ImportSucceeded(
                                    importPrinterSuccessMessage.FormatWith(Path.GetFileNameWithoutExtension(result.FileName))));
                        }
                        else
                        {
                            StyledMessageBox.ShowMessageBox("Oops! Settings file '{0}' did not contain any settings we could import.".Localize().FormatWith(Path.GetFileName(result.FileName)), "Unable to Import".Localize());
                        }
                    }
                });
            });
            mainRow.AddChild(importPrinter);

            rootColumn.AddChild(printersNode);

            HardwareTreeView.CreatePrinterProfilesTree(printersNode, theme);
            this.Invalidate();

            // Filament
            var materialsNode = new TreeNode(theme)
            {
                Text             = "Materials".Localize(),
                AlwaysExpandable = true,
                Image            = AggContext.StaticData.LoadIcon("filament.png", 16, 16, theme.InvertIcons)
            };

            materialsNode.TreeView = this;

            rootColumn.AddChild(materialsNode);

            // Register listeners
            PrinterSettings.AnyPrinterSettingChanged += Printer_SettingChanged;

            // Rebuild the treeview anytime the Profiles list changes
            ProfileManager.ProfilesListChanged.RegisterEvent((s, e) =>
            {
                HardwareTreeView.CreatePrinterProfilesTree(printersNode, theme);
                this.Invalidate();
            }, ref unregisterEvents);
        }
        public HardwareTreeView(ThemeConfig theme)
            : base(theme)
        {
            rootColumn = new FlowLayoutWidget(FlowDirection.TopToBottom)
            {
                HAnchor = HAnchor.Stretch,
                VAnchor = VAnchor.Fit
            };
            this.AddChild(rootColumn);

            // Printers
            printersNode = new TreeNode(theme)
            {
                Text             = "Printers".Localize(),
                HAnchor          = HAnchor.Stretch,
                AlwaysExpandable = true,
                Image            = StaticData.Instance.LoadIcon("printer.png", 16, 16, theme.InvertIcons)
            };
            printersNode.TreeView = this;

            var forcedHeight = 20 * GuiWidget.DeviceScale;
            var mainRow      = printersNode.Children.FirstOrDefault();

            mainRow.HAnchor = HAnchor.Stretch;
            mainRow.AddChild(new HorizontalSpacer());

            // add in the create printer button
            var createPrinter = new IconButton(StaticData.Instance.LoadIcon("md-add-circle_18.png", 18, 18, theme.InvertIcons), theme)
            {
                Name        = "Create Printer",
                VAnchor     = VAnchor.Center,
                Margin      = theme.ButtonSpacing.Clone(left: theme.ButtonSpacing.Right),
                ToolTipText = "Create Printer".Localize(),
                Height      = forcedHeight,
                Width       = forcedHeight
            };

            createPrinter.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.Show(PrinterSetup.GetBestStartPage(PrinterSetup.StartPageOptions.ShowMakeModel));
            });
            mainRow.AddChild(createPrinter);

            // add in the import printer button
            var importPrinter = new IconButton(StaticData.Instance.LoadIcon("md-import_18.png", 18, 18, theme.InvertIcons), theme)
            {
                VAnchor     = VAnchor.Center,
                Margin      = theme.ButtonSpacing,
                ToolTipText = "Import Printer".Localize(),
                Height      = forcedHeight,
                Width       = forcedHeight,
                Name        = "Import Printer Button"
            };

            importPrinter.Click += (s, e) => UiThread.RunOnIdle(() =>
            {
                DialogWindow.Show(new CloneSettingsPage());
            });
            mainRow.AddChild(importPrinter);

            rootColumn.AddChild(printersNode);

            HardwareTreeView.CreatePrinterProfilesTree(printersNode, theme);
            this.Invalidate();

            // Filament
            var materialsNode = new TreeNode(theme)
            {
                Text             = "Materials".Localize(),
                AlwaysExpandable = true,
                Image            = StaticData.Instance.LoadIcon("filament.png", 16, 16, theme.InvertIcons)
            };

            materialsNode.TreeView = this;

            rootColumn.AddChild(materialsNode);

            // Register listeners
            PrinterSettings.AnyPrinterSettingChanged += Printer_SettingChanged;

            // Rebuild the treeview anytime the Profiles list changes
            ProfileManager.ProfilesListChanged.RegisterEvent((s, e) =>
            {
                HardwareTreeView.CreatePrinterProfilesTree(printersNode, theme);
                this.Invalidate();
            }, ref unregisterEvents);
        }