diff --git a/Terminal.Gui/Views/Wizard/Wizard.cs b/Terminal.Gui/Views/Wizard/Wizard.cs index 254aece7a..2155c7cc1 100644 --- a/Terminal.Gui/Views/Wizard/Wizard.cs +++ b/Terminal.Gui/Views/Wizard/Wizard.cs @@ -1,4 +1,5 @@ -using Terminal.Gui.Resources; +#nullable enable +using Terminal.Gui.Resources; namespace Terminal.Gui; @@ -52,7 +53,7 @@ namespace Terminal.Gui; public class Wizard : Dialog { private readonly LinkedList _steps = new (); - private WizardStep _currentStep; + private WizardStep? _currentStep; private bool _finishedPressed; private string _wizardTitle = string.Empty; @@ -81,12 +82,12 @@ public class Wizard : Dialog //// Add a horiz separator var separator = new LineView (Orientation.Horizontal) { Y = Pos.Top (BackButton) - 1 }; - Add (separator); + base.Add (separator); AddButton (BackButton); AddButton (NextFinishButton); - BackButton.Accepting += BackBtn_Clicked; - NextFinishButton.Accepting += NextfinishBtn_Clicked; + BackButton.Accepting += BackBtn_Accepting; + NextFinishButton.Accepting += NextFinishBtn_Accepting; Loaded += Wizard_Loaded; Closing += Wizard_Closing; @@ -103,7 +104,7 @@ public class Wizard : Dialog public Button BackButton { get; } /// Gets or sets the currently active . - public WizardStep CurrentStep + public WizardStep? CurrentStep { get => _currentStep; set => GoToStep (value); @@ -197,7 +198,7 @@ public class Wizard : Dialog /// is true) Wizard from closing, cancel the event by setting /// to true before returning from the event handler. /// - public event EventHandler Cancelled; + public event EventHandler? Cancelled; /// /// Raised when the Next/Finish button in the is clicked. The Next/Finish button is always @@ -205,15 +206,15 @@ public class Wizard : Dialog /// raised if the is the last Step in the Wizard flow (otherwise the /// event is raised). /// - public event EventHandler Finished; + public event EventHandler? Finished; /// Returns the first enabled step in the Wizard /// The last enabled step - public WizardStep GetFirstStep () { return _steps.FirstOrDefault (s => s.Enabled); } + public WizardStep? GetFirstStep () { return _steps.FirstOrDefault (s => s.Enabled); } /// Returns the last enabled step in the Wizard /// The last enabled step - public WizardStep GetLastStep () { return _steps.LastOrDefault (s => s.Enabled); } + public WizardStep? GetLastStep () { return _steps.LastOrDefault (s => s.Enabled); } /// /// Returns the next enabled after the current step. Takes into account steps which are @@ -223,9 +224,9 @@ public class Wizard : Dialog /// The next step after the current step, if there is one; otherwise returns null, which indicates either /// there are no enabled steps or the current step is the last enabled step. /// - public WizardStep GetNextStep () + public WizardStep? GetNextStep () { - LinkedListNode step = null; + LinkedListNode? step = null; if (CurrentStep is null) { @@ -265,9 +266,9 @@ public class Wizard : Dialog /// The first step ahead of the current step, if there is one; otherwise returns null, which indicates /// either there are no enabled steps or the current step is the first enabled step. /// - public WizardStep GetPreviousStep () + public WizardStep? GetPreviousStep () { - LinkedListNode step = null; + LinkedListNode? step = null; if (CurrentStep is null) { @@ -303,36 +304,42 @@ public class Wizard : Dialog /// Causes the wizard to move to the previous enabled step (or first step if is not set). /// If there is no previous step, does nothing. /// - public void GoBack () + /// if the transition to the step succeeded. if the step was not found or the operation was cancelled. + public bool GoBack () { - WizardStep previous = GetPreviousStep (); + WizardStep? previous = GetPreviousStep (); if (previous is { }) { - GoToStep (previous); + return GoToStep (previous); } + + return false; } /// /// Causes the wizard to move to the next enabled step (or last step if is not set). If /// there is no previous step, does nothing. /// - public void GoNext () + /// if the transition to the step succeeded. if the step was not found or the operation was cancelled. + public bool GoNext () { - WizardStep nextStep = GetNextStep (); + WizardStep? nextStep = GetNextStep (); if (nextStep is { }) { - GoToStep (nextStep); + return GoToStep (nextStep); } + + return false; } /// Changes to the specified . /// The step to go to. - /// True if the transition to the step succeeded. False if the step was not found or the operation was cancelled. - public bool GoToStep (WizardStep newStep) + /// if the transition to the step succeeded. if the step was not found or the operation was cancelled. + public bool GoToStep (WizardStep? newStep) { - if (OnStepChanging (_currentStep, newStep) || (newStep is { } && !newStep.Enabled)) + if (OnStepChanging (_currentStep, newStep) || newStep is { Enabled: false }) { return false; } @@ -344,20 +351,17 @@ public class Wizard : Dialog step.ShowHide (); } - WizardStep oldStep = _currentStep; + WizardStep? oldStep = _currentStep; _currentStep = newStep; UpdateButtonsAndTitle (); // Set focus on the contentview - if (newStep is { }) - { - newStep.Subviews.ToArray () [0].SetFocus (); - } + newStep?.Subviews.ToArray () [0].SetFocus (); if (OnStepChanged (oldStep, _currentStep)) { - // For correctness we do this, but it's meaningless because there's nothing to cancel + // For correctness, we do this, but it's meaningless because there's nothing to cancel return false; } @@ -368,7 +372,7 @@ public class Wizard : Dialog /// Raised when the Back button in the is clicked. The Back button is always the first button /// in the array of Buttons passed to the constructor, if any. /// - public event EventHandler MovingBack; + public event EventHandler? MovingBack; /// /// Raised when the Next/Finish button in the is clicked (or the user presses Enter). The @@ -376,7 +380,7 @@ public class Wizard : Dialog /// constructor, if any. This event is only raised if the is the last Step in the Wizard flow /// (otherwise the event is raised). /// - public event EventHandler MovingNext; + public event EventHandler? MovingNext; /// /// is derived from and Dialog causes Esc to call @@ -410,7 +414,7 @@ public class Wizard : Dialog /// The step the Wizard changed from /// The step the Wizard has changed to /// True if the change is to be cancelled. - public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep) + public virtual bool OnStepChanged (WizardStep? oldStep, WizardStep? newStep) { var args = new StepChangeEventArgs (oldStep, newStep); StepChanged?.Invoke (this, args); @@ -425,7 +429,7 @@ public class Wizard : Dialog /// The step the Wizard is about to change from /// The step the Wizard is about to change to /// True if the change is to be cancelled. - public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep) + public virtual bool OnStepChanging (WizardStep? oldStep, WizardStep? newStep) { var args = new StepChangeEventArgs (oldStep, newStep); StepChanging?.Invoke (this, args); @@ -434,26 +438,26 @@ public class Wizard : Dialog } /// This event is raised after the has changed the . - public event EventHandler StepChanged; + public event EventHandler? StepChanged; /// /// This event is raised when the current ) is about to change. Use /// to abort the transition. /// - public event EventHandler StepChanging; + public event EventHandler? StepChanging; - private void BackBtn_Clicked (object sender, EventArgs e) + private void BackBtn_Accepting (object? sender, CommandEventArgs e) { var args = new WizardButtonEventArgs (); MovingBack?.Invoke (this, args); if (!args.Cancel) { - GoBack (); + e.Cancel = GoBack (); } } - private void NextfinishBtn_Clicked (object sender, EventArgs e) + private void NextFinishBtn_Accepting (object? sender, CommandEventArgs e) { if (CurrentStep == GetLastStep ()) { @@ -467,6 +471,7 @@ public class Wizard : Dialog if (IsCurrentTop) { Application.RequestStop (this); + e.Cancel = true; } // Wizard was created as a non-modal (just added to another View). @@ -480,7 +485,7 @@ public class Wizard : Dialog if (!args.Cancel) { - GoNext (); + e.Cancel = GoNext (); } } } @@ -548,7 +553,7 @@ public class Wizard : Dialog SetNeedsLayout (); } - private void Wizard_Closing (object sender, ToplevelClosingEventArgs obj) + private void Wizard_Closing (object? sender, ToplevelClosingEventArgs obj) { if (!_finishedPressed) { @@ -557,14 +562,14 @@ public class Wizard : Dialog } } - private void Wizard_Loaded (object sender, EventArgs args) + private void Wizard_Loaded (object? sender, EventArgs args) { CurrentStep = GetFirstStep (); // gets the first step if CurrentStep == null } - private void Wizard_TitleChanged (object sender, EventArgs e) + private void Wizard_TitleChanged (object? sender, EventArgs e) { if (string.IsNullOrEmpty (_wizardTitle)) { diff --git a/Terminal.Gui/Views/Wizard/WizardStep.cs b/Terminal.Gui/Views/Wizard/WizardStep.cs index e3d94f6b3..8d9be18b0 100644 --- a/Terminal.Gui/Views/Wizard/WizardStep.cs +++ b/Terminal.Gui/Views/Wizard/WizardStep.cs @@ -1,4 +1,5 @@ -namespace Terminal.Gui; +#nullable enable +namespace Terminal.Gui; /// /// Represents a basic step that is displayed in a . The view is @@ -15,26 +16,6 @@ /// public class WizardStep : View { - ///// - ///// The title of the . - ///// - ///// The Title is only displayed when the is used as a modal pop-up (see . - //public new string Title { - // // BUGBUG: v2 - No need for this as View now has Title w/ notifications. - // get => title; - // set { - // if (!OnTitleChanging (title, value)) { - // var old = title; - // title = value; - // OnTitleChanged (old, title); - // } - // base.Title = value; - // SetNeedsDraw (); - // } - //} - - //private string title = string.Empty; - // The contentView works like the ContentView in FrameView. private readonly View _contentView = new () { @@ -140,7 +121,7 @@ public class WizardStep : View /// Add the specified to the . /// to add to this container - public override View Add (View view) + public override View Add (View? view) { _contentView.Add (view); @@ -156,10 +137,10 @@ public class WizardStep : View /// Removes a from . /// - public override View Remove (View view) + public override View? Remove (View? view) { SetNeedsDraw (); - View container = view?.SuperView; + View? container = view?.SuperView; if (container == this) { diff --git a/UICatalog/Scenarios/Wizards.cs b/UICatalog/Scenarios/Wizards.cs index 750785249..024991904 100644 --- a/UICatalog/Scenarios/Wizards.cs +++ b/UICatalog/Scenarios/Wizards.cs @@ -263,7 +263,7 @@ public class Wizards : Scenario }; thirdStep.Add (progLbl, progressBar); thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; - thirdStepEnabledCeckBox.CheckedStateChanging += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; }; + thirdStepEnabledCeckBox.CheckedStateChanged += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; }; // Add 4th step var fourthStep = new WizardStep { Title = "Step Four" }; @@ -357,7 +357,7 @@ public class Wizards : Scenario "This step only shows if it was enabled on the other last step."; finalFinalStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; - finalFinalStepEnabledCeckBox.CheckedStateChanging += (s, e) => + finalFinalStepEnabledCeckBox.CheckedStateChanged += (s, e) => { finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.CheckedState == CheckState.Checked; };