Browse Source

propogated enable to Wizard

Charlie Kindel 3 years ago
parent
commit
56e78d4b92
3 changed files with 139 additions and 44 deletions
  1. 48 22
      Terminal.Gui/Windows/Wizard.cs
  2. 11 2
      UICatalog/Scenarios/Wizards.cs
  3. 80 20
      UnitTests/WizardTests.cs

+ 48 - 22
Terminal.Gui/Windows/Wizard.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using NStack;
 using Terminal.Gui.Resources;
 
@@ -211,6 +212,7 @@ namespace Terminal.Gui {
 				Controls.Visible = showControls;
 				helpTextView.Visible = showHelp;
 			}
+
 		} // WizardStep
 
 		/// <summary>
@@ -390,6 +392,24 @@ namespace Terminal.Gui {
 			return null;
 		}
 
+		/// <summary>
+		/// Returns the first enabled step in the Wizard
+		/// </summary>
+		/// <returns>The last enabled step</returns>
+		public WizardStep GetFirstStep ()
+		{
+			return steps.FirstOrDefault (s => s.Enabled);
+		}
+
+		/// <summary>
+		/// Returns the last enabled step in the Wizard
+		/// </summary>
+		/// <returns>The last enabled step</returns>
+		public WizardStep GetLastStep ()
+		{
+			return steps.LastOrDefault (s => s.Enabled);
+		}
+
 		private LinkedList<WizardStep> steps = new LinkedList<WizardStep> ();
 		private WizardStep currentStep = null;
 
@@ -425,7 +445,9 @@ namespace Terminal.Gui {
 		{
 			steps.AddLast (newStep);
 			this.Add (newStep);
-			SetNeedsLayout ();
+			newStep.EnabledChanged += UpdateButtonsAndTitle;
+			//newStep.TitleChanged += UpdateButtonsAndTitle;
+			UpdateButtonsAndTitle ();
 		}
 
 		/// <summary>
@@ -555,7 +577,6 @@ namespace Terminal.Gui {
 			return args.Cancel;
 		}
 
-
 		/// <summary>
 		/// Called when the <see cref="Wizard"/> has completed transition to a new <see cref="WizardStep"/>. Fires the <see cref="StepChanged"/> event. 
 		/// </summary>
@@ -568,6 +589,7 @@ namespace Terminal.Gui {
 			StepChanged?.Invoke (args);
 			return args.Cancel;
 		}
+
 		/// <summary>
 		/// Changes to the specified <see cref="WizardStep"/>.
 		/// </summary>
@@ -584,21 +606,10 @@ namespace Terminal.Gui {
 				step.Visible = (step == newStep);
 			}
 
-			if (newStep != null) {
-
-				base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + newStep.Title : string.Empty)}";
-
-				// Configure the Back button
-				backBtn.Text = newStep.BackButtonText != ustring.Empty ? newStep.BackButtonText : Strings.wzBack; // "_Back";
-				backBtn.Visible = (newStep != steps.First.Value);
+			var oldStep = currentStep;
+			currentStep = newStep;
 
-				// Configure the Next/Finished button
-				if (newStep == steps.Last.Value) {
-					nextfinishBtn.Text = newStep.NextButtonText != ustring.Empty ? newStep.NextButtonText : Strings.wzFinish; // "Fi_nish";
-				} else {
-					nextfinishBtn.Text = newStep.NextButtonText != ustring.Empty ? newStep.NextButtonText : Strings.wzNext; // "_Next...";
-				}
-			}
+			UpdateButtonsAndTitle ();
 
 			// Set focus to the nav buttons
 			if (backBtn.HasFocus) {
@@ -607,12 +618,6 @@ namespace Terminal.Gui {
 				nextfinishBtn.SetFocus ();
 			}
 
-			var oldStep = currentStep;
-			currentStep = newStep;
-
-			LayoutSubviews ();
-			Redraw (this.Bounds);
-
 			if (OnStepChanged (oldStep, currentStep)) {
 				// For correctness we do this, but it's meaningless because there's nothing to cancel
 				return false;
@@ -620,5 +625,26 @@ namespace Terminal.Gui {
 
 			return true;
 		}
+
+		private void UpdateButtonsAndTitle ()
+		{
+			if (CurrentStep == null) return;
+
+			base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + CurrentStep.Title : string.Empty)}";
+
+			// Configure the Back button
+			backBtn.Text = CurrentStep.BackButtonText != ustring.Empty ? CurrentStep.BackButtonText : Strings.wzBack; // "_Back";
+			backBtn.Visible = (CurrentStep != GetFirstStep ());
+
+			// Configure the Next/Finished button
+			if (CurrentStep == GetLastStep ()) {
+				nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzFinish; // "Fi_nish";
+			} else {
+				nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzNext; // "_Next...";
+			}
+			SetNeedsLayout ();
+			LayoutSubviews ();
+			Redraw (Bounds);
+		}
 	}
 }

+ 11 - 2
UICatalog/Scenarios/Wizards.cs

@@ -269,9 +269,18 @@ namespace UICatalog.Scenarios {
 					// Add last step
 					var lastStep = new Wizard.WizardStep ("The last step");
 					wizard.AddStep (lastStep);
-					lastStep.HelpText = "The wizard is complete! Press the Finish button to continue. Pressing ESC will cancel the wizard.";
+					lastStep.HelpText = "The wizard is complete!\n\nPress the Finish button to continue.\n\nPressing ESC will cancel the wizard.";
+					var finalFinalStepEnabledCeckBox = new CheckBox () { Text = "Enable _Final Final Step", Checked = false, X = 0, Y = 1 };
+					lastStep.Add (finalFinalStepEnabledCeckBox);
 
-					// TODO: Demo setting initial Pane
+					// Add an optional FINAL last step
+					var finalFinalStep = new Wizard.WizardStep ("The VERY last step");
+					wizard.AddStep (finalFinalStep);
+					finalFinalStep.HelpText = "This step only shows if it was enabled on the other last step.";
+					finalFinalStep.Enabled = thirdStepEnabledCeckBox.Checked;
+					finalFinalStepEnabledCeckBox.Toggled += (args) => {
+						finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.Checked;
+					};
 
 					Application.Run (wizard);
 

+ 80 - 20
UnitTests/WizardTests.cs

@@ -204,7 +204,7 @@ namespace Terminal.Gui.Views {
 			wizard.AddStep (step1);
 
 			// If no current step, should be last step
-			Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			wizard.CurrentStep = step1;
 			// If there is 1 step it's current step should be null
@@ -219,7 +219,7 @@ namespace Terminal.Gui.Views {
 			wizard.AddStep (step2);
 			wizard.CurrentStep = step2;
 			step1.Enabled = true;
-			Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			// If two steps and at 2 and step 1 is `Enabled = false` should be null
 			step1.Enabled = false;
@@ -237,9 +237,9 @@ namespace Terminal.Gui.Views {
 			step3.Enabled = true;
 			Assert.Null (wizard.GetPreviousStep ());
 			wizard.CurrentStep = step2;
-			Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 			wizard.CurrentStep = step3;
-			Assert.Equal (step2.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			// If three steps with Step2.Enabled = false
 			//   At step 1 should be null
@@ -250,7 +250,7 @@ namespace Terminal.Gui.Views {
 			wizard.CurrentStep = step1;
 			Assert.Null (wizard.GetPreviousStep ());
 			wizard.CurrentStep = step3;
-			Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			// If three steps with Step1.Enabled = false & Step2.Enabled = false
 			//   At step 3 should be null
@@ -260,27 +260,27 @@ namespace Terminal.Gui.Views {
 			step1.Enabled = true;
 			step2.Enabled = true;
 			step3.Enabled = true;
-			Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = true;
 			step3.Enabled = true;
-			Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = false;
 			step3.Enabled = true;
-			Assert.Equal (step3.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = true;
 			step3.Enabled = false;
-			Assert.Equal (step2.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 
 			step1.Enabled = true;
 			step2.Enabled = false;
 			step3.Enabled = false;
-			Assert.Equal (step1.Title.ToString(), wizard.GetPreviousStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetPreviousStep ().Title.ToString ());
 		}
 
 		[Fact, AutoInitShutdown]
@@ -295,7 +295,7 @@ namespace Terminal.Gui.Views {
 			wizard.AddStep (step1);
 
 			// If no current step, should be first step
-			Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			wizard.CurrentStep = step1;
 			// If there is 1 step it's current step should be null
@@ -308,7 +308,7 @@ namespace Terminal.Gui.Views {
 			// If two steps and at 1 and step 2 is `Enabled = true`should be step 2
 			var step2 = new Wizard.WizardStep ("step2");
 			wizard.AddStep (step2);
-			Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			// If two steps and at 1 and step 2 is `Enabled = false` should be null
 			step1.Enabled = true;
@@ -326,9 +326,9 @@ namespace Terminal.Gui.Views {
 			wizard.CurrentStep = step1;
 			step2.Enabled = true;
 			step3.Enabled = true;
-			Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 			wizard.CurrentStep = step2;
-			Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 			wizard.CurrentStep = step3;
 			Assert.Null (wizard.GetNextStep ());
 
@@ -339,7 +339,7 @@ namespace Terminal.Gui.Views {
 			wizard.CurrentStep = step1;
 			step2.Enabled = false;
 			step3.Enabled = true;
-			Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 			wizard.CurrentStep = step3;
 			Assert.Null (wizard.GetNextStep ());
 
@@ -356,27 +356,27 @@ namespace Terminal.Gui.Views {
 			step1.Enabled = true;
 			step2.Enabled = true;
 			step3.Enabled = true;
-			Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = true;
 			step3.Enabled = true;
-			Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = false;
 			step3.Enabled = true;
-			Assert.Equal (step3.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step3.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			step1.Enabled = false;
 			step2.Enabled = true;
 			step3.Enabled = false;
-			Assert.Equal (step2.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step2.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 
 			step1.Enabled = true;
 			step2.Enabled = false;
 			step3.Enabled = false;
-			Assert.Equal (step1.Title.ToString(), wizard.GetNextStep ().Title.ToString());
+			Assert.Equal (step1.Title.ToString (), wizard.GetNextStep ().Title.ToString ());
 		}
 
 		[Fact, AutoInitShutdown]
@@ -406,5 +406,65 @@ namespace Terminal.Gui.Views {
 			//        If 1 is enabled 2 becomes current
 			//        If 1 is disabled 1 stays current
 		}
+
+		[Fact, AutoInitShutdown]
+		public void Navigate_GetFirstStep_Works ()
+		{
+			var wizard = new Wizard ();
+
+			Assert.Null (wizard.GetFirstStep ());
+
+			var step1 = new Wizard.WizardStep ("step1");
+			wizard.AddStep (step1);
+			Assert.Equal (step1.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+
+			var step2 = new Wizard.WizardStep ("step2");
+			wizard.AddStep (step2);
+			Assert.Equal (step1.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+
+			var step3 = new Wizard.WizardStep ("step3");
+			wizard.AddStep (step3);
+			Assert.Equal (step1.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+
+			step1.Enabled = false;
+			Assert.Equal (step2.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+
+			step1.Enabled = true;
+			Assert.Equal (step1.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+
+			step1.Enabled = false;
+			step2.Enabled = false;
+			Assert.Equal (step3.Title.ToString (), wizard.GetFirstStep ().Title.ToString ());
+		}
+
+		[Fact, AutoInitShutdown]
+		public void Navigate_GetLastStep_Works ()
+		{
+			var wizard = new Wizard ();
+
+			Assert.Null (wizard.GetLastStep ());
+
+			var step1 = new Wizard.WizardStep ("step1");
+			wizard.AddStep (step1);
+			Assert.Equal (step1.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+
+			var step2 = new Wizard.WizardStep ("step2");
+			wizard.AddStep (step2);
+			Assert.Equal (step2.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+
+			var step3 = new Wizard.WizardStep ("step3");
+			wizard.AddStep (step3);
+			Assert.Equal (step3.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+
+			step3.Enabled = false;
+			Assert.Equal (step2.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+
+			step3.Enabled = true;
+			Assert.Equal (step3.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+
+			step3.Enabled = false;
+			step2.Enabled = false;
+			Assert.Equal (step1.Title.ToString (), wizard.GetLastStep ().Title.ToString ());
+		}
 	}
 }