From d2772904975a077f723ebfe69f67769f40537cff Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 17 Oct 2017 12:35:24 -0700 Subject: [PATCH] Convert RenamePage into reusable InputBoxPage, use for Create Folder - Allow WizardPage objects to control window Width & MinimumSize - Issue MatterHackers/MCCentral#2086 Invalid button styling - custom window type rather than WizardPage --- Library/Widgets/PrintLibraryWidget.cs | 101 ++++++------ .../SystemWindows/CreateFolderWindow.cs | 149 ------------------ MatterControl.csproj | 3 +- PartPreviewWindow/View3D/PrinterActionsBar.cs | 26 +-- .../InputBoxPage.cs | 67 ++++---- SetupWizard/WizardPage.cs | 3 + SetupWizard/WizardWindow.cs | 41 +++-- .../LibraryDownloadsTest.cs | 2 +- .../LocalLibraryTests.cs | 4 +- .../MatterControl/PerformanceTests.cs | 2 +- 10 files changed, 131 insertions(+), 267 deletions(-) delete mode 100644 Library/Widgets/SystemWindows/CreateFolderWindow.cs rename Library/Widgets/SystemWindows/RenameItemPage.cs => SetupWizard/InputBoxPage.cs (58%) diff --git a/Library/Widgets/PrintLibraryWidget.cs b/Library/Widgets/PrintLibraryWidget.cs index 31f4ef5df..f5a8fb35c 100644 --- a/Library/Widgets/PrintLibraryWidget.cs +++ b/Library/Widgets/PrintLibraryWidget.cs @@ -45,8 +45,6 @@ namespace MatterHackers.MatterControl.PrintLibrary { public class PrintLibraryWidget : GuiWidget { - private static CreateFolderWindow createFolderWindow = null; - private Button addToLibraryButton; private Button createFolderButton; private FlowLayoutWidget buttonPanel; @@ -303,31 +301,30 @@ namespace MatterHackers.MatterControl.PrintLibrary // the create folder button createFolderButton = textImageButtonFactory.Generate("Create Folder".Localize()); - createFolderButton.Enabled = false; // The library selector (the first library selected) is protected so we can't add to it. + createFolderButton.Enabled = false; // Disabled until changed by the ActiveContainer createFolderButton.Name = "Create Folder From Library Button"; createFolderButton.Margin = new BorderDouble(0, 0, 3, 0); createFolderButton.Click += (sender, e) => { - if (createFolderWindow == null) - { - createFolderWindow = new CreateFolderWindow((result) => - { - if (!string.IsNullOrEmpty(result.newName) - && this.libraryView.ActiveContainer is ILibraryWritableContainer writableContainer) + WizardWindow.Show( + new InputBoxPage( + "Create Folder".Localize(), + "Folder Name".Localize(), + "", + "Enter New Name Here".Localize(), + "Create".Localize(), + (newName) => { - writableContainer.Add( - new[] + if (!string.IsNullOrEmpty(newName) + && this.libraryView.ActiveContainer is ILibraryWritableContainer writableContainer) + { + writableContainer.Add(new[] { - new CreateFolderItem() { Name = result.newName } + new CreateFolderItem() { Name = newName } }); - } - }); - createFolderWindow.Closed += (sender2, e2) => { createFolderWindow = null; }; - } - else - { - createFolderWindow.BringToFront(); - } + } + })); + }; buttonPanel.AddChild(createFolderButton); @@ -452,7 +449,38 @@ namespace MatterHackers.MatterControl.PrintLibrary AllowMultiple = false, AllowProtected = false, AllowContainers = true, - Action = (selectedLibraryItems, listView) => renameFromLibraryButton_Click(selectedLibraryItems, null), + Action = (selectedLibraryItems, listView) => + { + if (libraryView.SelectedItems.Count == 1) + { + var selectedItem = libraryView.SelectedItems.FirstOrDefault(); + if (selectedItem == null) + { + return; + } + + WizardWindow.Show( + new InputBoxPage( + "Rename Item".Localize(), + "Name".Localize(), + selectedItem.Model.Name, + "Enter New Name Here".Localize(), + "Rename".Localize(), + (newName) => + { + var model = libraryView.SelectedItems.FirstOrDefault()?.Model; + if (model != null) + { + var container = libraryView.ActiveContainer as ILibraryWritableContainer; + if (container != null) + { + container.Rename(model, newName); + libraryView.SelectedItems.Clear(); + } + } + })); + } + }, }); // move menu item @@ -611,37 +639,6 @@ namespace MatterHackers.MatterControl.PrintLibrary }); } - private void renameFromLibraryButton_Click(IEnumerable items, object p) - { - if (libraryView.SelectedItems.Count == 1) - { - var selectedItem = libraryView.SelectedItems.FirstOrDefault(); - if (selectedItem == null) - { - return; - } - - var renameItemPage = new RenameItemPage( - "Rename Item:".Localize(), - selectedItem.Model.Name, - (newName) => - { - var model = libraryView.SelectedItems.FirstOrDefault()?.Model; - if (model != null) - { - var container = libraryView.ActiveContainer as ILibraryWritableContainer; - if (container != null) - { - container.Rename(model, newName); - libraryView.SelectedItems.Clear(); - } - } - }); - - WizardWindow.Show(renameItemPage); - } - } - public override void OnClosed(ClosedEventArgs e) { if (libraryView?.ActiveContainer != null) diff --git a/Library/Widgets/SystemWindows/CreateFolderWindow.cs b/Library/Widgets/SystemWindows/CreateFolderWindow.cs deleted file mode 100644 index 905048831..000000000 --- a/Library/Widgets/SystemWindows/CreateFolderWindow.cs +++ /dev/null @@ -1,149 +0,0 @@ -using MatterHackers.Agg; -using MatterHackers.Agg.UI; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.CustomWidgets; -using MatterHackers.MatterControl.DataStorage; -using System; -using System.IO; - -namespace MatterHackers.MatterControl -{ - public class CreateFolderWindow : SystemWindow - { - private Action functionToCallToCreateNamedFolder; - private TextImageButtonFactory textImageButtonFactory = new TextImageButtonFactory(); - private MHTextEditWidget folderNameWidget; - - public CreateFolderWindow(Action functionToCallToCreateNamedFolder) - : base(480, 180) - { - Title = "MatterControl - Create Folder"; - AlwaysOnTopOfMain = true; - - this.functionToCallToCreateNamedFolder = functionToCallToCreateNamedFolder; - - FlowLayoutWidget topToBottom = new FlowLayoutWidget(FlowDirection.TopToBottom); - topToBottom.AnchorAll(); - topToBottom.Padding = new BorderDouble(3, 0, 3, 5); - - // Creates Header - FlowLayoutWidget headerRow = new FlowLayoutWidget(FlowDirection.LeftToRight); - headerRow.HAnchor = HAnchor.Stretch; - headerRow.Margin = new BorderDouble(0, 3, 0, 0); - headerRow.Padding = new BorderDouble(0, 3, 0, 3); - BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor; - - //Creates Text and adds into header - { - string createFolderLabel = "Create New Folder:".Localize(); - TextWidget elementHeader = new TextWidget(createFolderLabel, pointSize: 14); - elementHeader.TextColor = ActiveTheme.Instance.PrimaryTextColor; - elementHeader.HAnchor = HAnchor.Stretch; - elementHeader.VAnchor = Agg.UI.VAnchor.Bottom; - - headerRow.AddChild(elementHeader); - topToBottom.AddChild(headerRow); - this.AddChild(topToBottom); - } - - //Creates container in the middle of window - FlowLayoutWidget middleRowContainer = new FlowLayoutWidget(FlowDirection.TopToBottom); - { - middleRowContainer.HAnchor = HAnchor.Stretch; - middleRowContainer.VAnchor = VAnchor.Stretch; - middleRowContainer.Padding = new BorderDouble(5); - middleRowContainer.BackgroundColor = ActiveTheme.Instance.SecondaryBackgroundColor; - } - - string fileNameLabel = "Folder Name".Localize(); - TextWidget textBoxHeader = new TextWidget(fileNameLabel, pointSize: 12); - textBoxHeader.TextColor = ActiveTheme.Instance.PrimaryTextColor; - textBoxHeader.Margin = new BorderDouble(5); - textBoxHeader.HAnchor = HAnchor.Left; - - //Adds text box and check box to the above container - folderNameWidget = new MHTextEditWidget("", pixelWidth: 300, messageWhenEmptyAndNotSelected: "Enter a Folder Name Here".Localize()); - folderNameWidget.Name = "Create Folder - Text Input"; - folderNameWidget.HAnchor = HAnchor.Stretch; - folderNameWidget.Margin = new BorderDouble(5); - - middleRowContainer.AddChild(textBoxHeader); - middleRowContainer.AddChild(folderNameWidget); - middleRowContainer.AddChild(new HorizontalSpacer()); - topToBottom.AddChild(middleRowContainer); - - //Creates button container on the bottom of window - FlowLayoutWidget buttonRow = new FlowLayoutWidget(FlowDirection.LeftToRight); - { - BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor; - buttonRow.HAnchor = HAnchor.Stretch; - buttonRow.Padding = new BorderDouble(0, 3); - } - - Button createFolderButton = textImageButtonFactory.Generate("Create".Localize()); - createFolderButton.Name = "Create Folder Button"; - createFolderButton.Visible = true; - createFolderButton.Cursor = Cursors.Hand; - buttonRow.AddChild(createFolderButton); - - createFolderButton.Click += createFolderButton_Click; - folderNameWidget.ActualTextEditWidget.EnterPressed += new KeyEventHandler(ActualTextEditWidget_EnterPressed); - - //Adds Create and Close Button to button container - buttonRow.AddChild(new HorizontalSpacer()); - - Button cancelButton = textImageButtonFactory.Generate("Cancel".Localize()); - cancelButton.Visible = true; - cancelButton.Cursor = Cursors.Hand; - buttonRow.AddChild(cancelButton); - cancelButton.Click += (sender, e) => - { - CloseOnIdle(); - }; - - topToBottom.AddChild(buttonRow); - - ShowAsSystemWindow(); - } - - public override void OnLoad(EventArgs args) - { - UiThread.RunOnIdle(folderNameWidget.Focus); - base.OnLoad(args); - } - - private void ActualTextEditWidget_EnterPressed(object sender, KeyEventArgs keyEvent) - { - SubmitForm(); - } - - private void createFolderButton_Click(object sender, EventArgs mouseEvent) - { - SubmitForm(); - } - - private void SubmitForm() - { - string newName = folderNameWidget.ActualTextEditWidget.Text; - if (newName != "") - { - string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); - string fileNameAndPath = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); - - CreateFolderReturnInfo returnInfo = new CreateFolderReturnInfo(newName); - functionToCallToCreateNamedFolder(returnInfo); - CloseOnIdle(); - } - } - - public class CreateFolderReturnInfo - { - public string newName; - - public CreateFolderReturnInfo(string newName) - { - this.newName = newName; - } - } - } -} \ No newline at end of file diff --git a/MatterControl.csproj b/MatterControl.csproj index 151048283..82e501bc4 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -249,7 +249,7 @@ - + @@ -300,7 +300,6 @@ - diff --git a/PartPreviewWindow/View3D/PrinterActionsBar.cs b/PartPreviewWindow/View3D/PrinterActionsBar.cs index 5a63199e9..41dacf170 100644 --- a/PartPreviewWindow/View3D/PrinterActionsBar.cs +++ b/PartPreviewWindow/View3D/PrinterActionsBar.cs @@ -178,18 +178,20 @@ namespace MatterHackers.MatterControl.PartPreviewWindow Title = "Rename Printer".Localize(), Action = () => { - var renameItemPage = new RenameItemPage( - "Rename Printer".Localize() + ":", - printer.Settings.GetValue(SettingsKey.printer_name), - (newName) => - { - if (!string.IsNullOrEmpty(newName)) - { - printer.Settings.SetValue(SettingsKey.printer_name, newName); - } - }); - - WizardWindow.Show(renameItemPage); + WizardWindow.Show( + new InputBoxPage( + "Rename Printer".Localize(), + "Name".Localize(), + printer.Settings.GetValue(SettingsKey.printer_name), + "Enter New Name Here".Localize(), + "Rename".Localize(), + (newName) => + { + if (!string.IsNullOrEmpty(newName)) + { + printer.Settings.SetValue(SettingsKey.printer_name, newName); + } + })); } }, new NamedAction() { Title = "----" }, diff --git a/Library/Widgets/SystemWindows/RenameItemPage.cs b/SetupWizard/InputBoxPage.cs similarity index 58% rename from Library/Widgets/SystemWindows/RenameItemPage.cs rename to SetupWizard/InputBoxPage.cs index 904a32725..1a0f2112c 100644 --- a/Library/Widgets/SystemWindows/RenameItemPage.cs +++ b/SetupWizard/InputBoxPage.cs @@ -30,71 +30,62 @@ either expressed or implied, of the FreeBSD Project. using System; using MatterHackers.Agg; using MatterHackers.Agg.UI; -using MatterHackers.Localizations; -using MatterHackers.MatterControl.CustomWidgets; +using MatterHackers.VectorMath; namespace MatterHackers.MatterControl { - public class RenameItemPage : WizardPage + public class InputBoxPage : WizardPage { - private Action renameCallback; + private MHTextEditWidget textEditWidget; - private MHTextEditWidget saveAsNameWidget; - - public RenameItemPage(string windowTitle, string currentItemName, Action functionToCallToRenameItem) + public InputBoxPage(string windowTitle, string label, string initialValue, string emptyText, string actionButtonTitle, Action action) { - this.renameCallback = functionToCallToRenameItem; - this.WindowTitle = windowTitle; this.HeaderText = windowTitle; + this.WindowSize = new Vector2(500, 200); - var textBoxHeader = new TextWidget("Name".Localize(), pointSize: 12) + Button actionButton = null; + + contentRow.AddChild(new TextWidget(label, pointSize: 12) { TextColor = ActiveTheme.Instance.PrimaryTextColor, Margin = new BorderDouble(5), HAnchor = HAnchor.Left - }; - contentRow.AddChild(textBoxHeader); + }); //Adds text box and check box to the above container - saveAsNameWidget = new MHTextEditWidget(currentItemName, pixelWidth: 300, messageWhenEmptyAndNotSelected: "Enter New Name Here".Localize()); - saveAsNameWidget.HAnchor = HAnchor.Stretch; - saveAsNameWidget.Margin = new BorderDouble(5); - saveAsNameWidget.ActualTextEditWidget.EnterPressed += (s, e) => + textEditWidget = new MHTextEditWidget(initialValue, pixelWidth: 300, messageWhenEmptyAndNotSelected: emptyText); + textEditWidget.HAnchor = HAnchor.Stretch; + textEditWidget.Margin = new BorderDouble(5); + textEditWidget.ActualTextEditWidget.EnterPressed += (s, e) => { - SubmitForm(); + actionButton.OnClick(null); }; - contentRow.AddChild(saveAsNameWidget); + contentRow.AddChild(textEditWidget); - var renameItemButton = textImageButtonFactory.Generate("Rename".Localize()); - renameItemButton.Name = "Rename Button"; - renameItemButton.Visible = true; - renameItemButton.Cursor = Cursors.Hand; - renameItemButton.Click += (s, e) => + actionButton = textImageButtonFactory.Generate(actionButtonTitle); + actionButton.Name = "InputBoxPage Action Button"; + actionButton.Cursor = Cursors.Hand; + actionButton.Click += (s, e) => { - SubmitForm(); + string newName = textEditWidget.ActualTextEditWidget.Text; + if (!string.IsNullOrEmpty(newName)) + { + action.Invoke(newName); + this.WizardWindow.CloseOnIdle(); + } }; - this.AddPageAction(renameItemButton); + this.AddPageAction(actionButton); } public override void OnLoad(EventArgs args) { UiThread.RunOnIdle(() => { - saveAsNameWidget.Focus(); - saveAsNameWidget.ActualTextEditWidget.InternalTextEditWidget.SelectAll(); + textEditWidget.Focus(); + textEditWidget.ActualTextEditWidget.InternalTextEditWidget.SelectAll(); }); base.OnLoad(args); } - - private void SubmitForm() - { - string newName = saveAsNameWidget.ActualTextEditWidget.Text; - if (newName != "") - { - renameCallback(newName); - this.WizardWindow.CloseOnIdle(); - } - } } -} \ No newline at end of file +} diff --git a/SetupWizard/WizardPage.cs b/SetupWizard/WizardPage.cs index c30aace2d..a5613f162 100644 --- a/SetupWizard/WizardPage.cs +++ b/SetupWizard/WizardPage.cs @@ -32,6 +32,7 @@ using MatterHackers.Agg; using MatterHackers.Agg.UI; using MatterHackers.Localizations; using MatterHackers.MatterControl.CustomWidgets; +using MatterHackers.VectorMath; namespace MatterHackers.MatterControl { @@ -44,6 +45,8 @@ namespace MatterHackers.MatterControl private WrappedTextWidget headerLabel; protected Button cancelButton { get; } + public Vector2 WindowSize { get; set; } + protected TextImageButtonFactory textImageButtonFactory { get; } = ApplicationController.Instance.Theme.WizardButtons; protected TextImageButtonFactory whiteImageButtonFactory { get; } = ApplicationController.Instance.Theme.WhiteButtonFactory; protected LinkButtonFactory linkButtonFactory = ApplicationController.Instance.Theme.LinkButtonFactory; diff --git a/SetupWizard/WizardWindow.cs b/SetupWizard/WizardWindow.cs index a9e504603..1d679a714 100644 --- a/SetupWizard/WizardWindow.cs +++ b/SetupWizard/WizardWindow.cs @@ -1,14 +1,12 @@ -using MatterHackers.Agg; +using System; +using System.Collections.Generic; +using MatterHackers.Agg; using MatterHackers.Agg.Platform; using MatterHackers.Agg.UI; using MatterHackers.Localizations; -using MatterHackers.MatterControl.DataStorage; -using MatterHackers.MatterControl.PrinterCommunication; using MatterHackers.MatterControl.PrinterControls.PrinterConnections; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; -using System; -using System.Collections.Generic; namespace MatterHackers.MatterControl { @@ -25,12 +23,9 @@ namespace MatterHackers.MatterControl : base(500 * GuiWidget.DeviceScale, 500 * GuiWidget.DeviceScale) { this.AlwaysOnTopOfMain = true; - + this.MinimumSize = new Vector2(200, 200); this.BackgroundColor = ActiveTheme.Instance.PrimaryBackgroundColor; this.Padding = new BorderDouble(8); - this.MinimumSize = new Vector2(350 * GuiWidget.DeviceScale, 400 * GuiWidget.DeviceScale); - - this.ShowAsSystemWindow(); } public static void Close(Type type) @@ -47,16 +42,40 @@ namespace MatterHackers.MatterControl { WizardWindow wizardWindow = GetWindow(typeof(PanelType)); var newPanel = wizardWindow.ChangeToPage(); - wizardWindow.Title = newPanel.WindowTitle; + wizardWindow.Title = newPanel.WindowTitle; + + SetSizeAndShow(wizardWindow, newPanel); } public static void Show(WizardPage wizardPage) { WizardWindow wizardWindow = GetWindow(wizardPage.GetType()); wizardWindow.Title = wizardPage.WindowTitle; + + SetSizeAndShow(wizardWindow, wizardPage); + wizardWindow.ChangeToPage(wizardPage); } + WizardPage activePage; + + // Allow the WizardPage MinimumSize to override our MinimumSize + public override Vector2 MinimumSize + { + get => activePage?.MinimumSize ?? base.MinimumSize; + set => base.MinimumSize = value; + } + + public static void SetSizeAndShow(WizardWindow wizardWindow, WizardPage wizardPage) + { + if (wizardPage.WindowSize != Vector2.Zero) + { + wizardWindow.Size = wizardPage.WindowSize; + } + + wizardWindow.ShowAsSystemWindow(); + } + public static void ShowPrinterSetup(bool userRequestedNewPrinter = false) { WizardWindow wizardWindow = GetWindow(typeof(SetupStepComPortOne)); @@ -160,6 +179,8 @@ namespace MatterHackers.MatterControl internal void ChangeToPage(WizardPage pageToChangeTo) { + activePage = pageToChangeTo; + pageToChangeTo.WizardWindow = this; this.CloseAllChildren(); this.AddChild(pageToChangeTo); diff --git a/Tests/MatterControl.AutomationTests/LibraryDownloadsTest.cs b/Tests/MatterControl.AutomationTests/LibraryDownloadsTest.cs index f6989ecfe..7bca0715d 100644 --- a/Tests/MatterControl.AutomationTests/LibraryDownloadsTest.cs +++ b/Tests/MatterControl.AutomationTests/LibraryDownloadsTest.cs @@ -200,7 +200,7 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.ClickByName("Create Folder From Library Button"); testRunner.Delay(2); testRunner.Type(newFolderName); - testRunner.ClickByName("Create Folder Button"); + testRunner.ClickByName("InputBoxPage Action Button"); testRunner.Delay(2); Assert.IsTrue(testRunner.WaitForName(newFolderName + " Row Item Collection", 2), $"{newFolderName} exists"); diff --git a/Tests/MatterControl.AutomationTests/LocalLibraryTests.cs b/Tests/MatterControl.AutomationTests/LocalLibraryTests.cs index 080bd10bd..a6bbe13e2 100644 --- a/Tests/MatterControl.AutomationTests/LocalLibraryTests.cs +++ b/Tests/MatterControl.AutomationTests/LocalLibraryTests.cs @@ -119,7 +119,7 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.Type("New Folder"); testRunner.Delay(.5); - testRunner.ClickByName("Create Folder Button"); + testRunner.ClickByName("InputBoxPage Action Button"); testRunner.Delay(.2); // Confirm newly created folder exists @@ -133,7 +133,7 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.Delay(.5); testRunner.Type("Renamed Library Folder"); - testRunner.ClickByName("Rename Button"); + testRunner.ClickByName("InputBoxPage Action Button"); testRunner.Delay(.2); // Make sure the renamed Library Folder exists diff --git a/Tests/MatterControl.Tests/MatterControl/PerformanceTests.cs b/Tests/MatterControl.Tests/MatterControl/PerformanceTests.cs index b78fe9e6a..09fecc8ff 100644 --- a/Tests/MatterControl.Tests/MatterControl/PerformanceTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/PerformanceTests.cs @@ -206,7 +206,7 @@ namespace MatterHackers.MatterControl testRunner.ClickByName("Create Folder From Library Button"); testRunner.Delay(2); testRunner.Type("New Folder"); - testRunner.ClickByName("Create Folder Button"); + testRunner.ClickByName("InputBoxPage Action Button"); testRunner.ClickByName("Library Edit Button"); testRunner.ClickByName("Row Item New Folder");