Skip to content

Commit aff7e2c

Browse files
authored
Fixed nav (gui-cs#3926)
1 parent f3697a9 commit aff7e2c

File tree

3 files changed

+54
-68
lines changed

3 files changed

+54
-68
lines changed

Terminal.Gui/Views/Wizard/Wizard.cs

+47-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Terminal.Gui.Resources;
1+
#nullable enable
2+
using Terminal.Gui.Resources;
23

34
namespace Terminal.Gui;
45

@@ -52,7 +53,7 @@ namespace Terminal.Gui;
5253
public class Wizard : Dialog
5354
{
5455
private readonly LinkedList<WizardStep> _steps = new ();
55-
private WizardStep _currentStep;
56+
private WizardStep? _currentStep;
5657
private bool _finishedPressed;
5758
private string _wizardTitle = string.Empty;
5859

@@ -81,12 +82,12 @@ public Wizard ()
8182
//// Add a horiz separator
8283
var separator = new LineView (Orientation.Horizontal) { Y = Pos.Top (BackButton) - 1 };
8384

84-
Add (separator);
85+
base.Add (separator);
8586
AddButton (BackButton);
8687
AddButton (NextFinishButton);
8788

88-
BackButton.Accepting += BackBtn_Clicked;
89-
NextFinishButton.Accepting += NextfinishBtn_Clicked;
89+
BackButton.Accepting += BackBtn_Accepting;
90+
NextFinishButton.Accepting += NextFinishBtn_Accepting;
9091

9192
Loaded += Wizard_Loaded;
9293
Closing += Wizard_Closing;
@@ -103,7 +104,7 @@ public Wizard ()
103104
public Button BackButton { get; }
104105

105106
/// <summary>Gets or sets the currently active <see cref="WizardStep"/>.</summary>
106-
public WizardStep CurrentStep
107+
public WizardStep? CurrentStep
107108
{
108109
get => _currentStep;
109110
set => GoToStep (value);
@@ -197,23 +198,23 @@ public void AddStep (WizardStep newStep)
197198
/// <see cref="Wizard.Modal"/> is <c>true</c>) Wizard from closing, cancel the event by setting
198199
/// <see cref="WizardButtonEventArgs.Cancel"/> to <c>true</c> before returning from the event handler.
199200
/// </summary>
200-
public event EventHandler<WizardButtonEventArgs> Cancelled;
201+
public event EventHandler<WizardButtonEventArgs>? Cancelled;
201202

202203
/// <summary>
203204
/// Raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
204205
/// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
205206
/// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow (otherwise the <see cref="Finished"/>
206207
/// event is raised).
207208
/// </summary>
208-
public event EventHandler<WizardButtonEventArgs> Finished;
209+
public event EventHandler<WizardButtonEventArgs>? Finished;
209210

210211
/// <summary>Returns the first enabled step in the Wizard</summary>
211212
/// <returns>The last enabled step</returns>
212-
public WizardStep GetFirstStep () { return _steps.FirstOrDefault (s => s.Enabled); }
213+
public WizardStep? GetFirstStep () { return _steps.FirstOrDefault (s => s.Enabled); }
213214

214215
/// <summary>Returns the last enabled step in the Wizard</summary>
215216
/// <returns>The last enabled step</returns>
216-
public WizardStep GetLastStep () { return _steps.LastOrDefault (s => s.Enabled); }
217+
public WizardStep? GetLastStep () { return _steps.LastOrDefault (s => s.Enabled); }
217218

218219
/// <summary>
219220
/// Returns the next enabled <see cref="WizardStep"/> after the current step. Takes into account steps which are
@@ -223,9 +224,9 @@ public void AddStep (WizardStep newStep)
223224
/// The next step after the current step, if there is one; otherwise returns <c>null</c>, which indicates either
224225
/// there are no enabled steps or the current step is the last enabled step.
225226
/// </returns>
226-
public WizardStep GetNextStep ()
227+
public WizardStep? GetNextStep ()
227228
{
228-
LinkedListNode<WizardStep> step = null;
229+
LinkedListNode<WizardStep>? step = null;
229230

230231
if (CurrentStep is null)
231232
{
@@ -265,9 +266,9 @@ public WizardStep GetNextStep ()
265266
/// The first step ahead of the current step, if there is one; otherwise returns <c>null</c>, which indicates
266267
/// either there are no enabled steps or the current step is the first enabled step.
267268
/// </returns>
268-
public WizardStep GetPreviousStep ()
269+
public WizardStep? GetPreviousStep ()
269270
{
270-
LinkedListNode<WizardStep> step = null;
271+
LinkedListNode<WizardStep>? step = null;
271272

272273
if (CurrentStep is null)
273274
{
@@ -303,36 +304,42 @@ public WizardStep GetPreviousStep ()
303304
/// Causes the wizard to move to the previous enabled step (or first step if <see cref="CurrentStep"/> is not set).
304305
/// If there is no previous step, does nothing.
305306
/// </summary>
306-
public void GoBack ()
307+
/// <returns><see langword="true"/> if the transition to the step succeeded. <see langword="false"/> if the step was not found or the operation was cancelled.</returns>
308+
public bool GoBack ()
307309
{
308-
WizardStep previous = GetPreviousStep ();
310+
WizardStep? previous = GetPreviousStep ();
309311

310312
if (previous is { })
311313
{
312-
GoToStep (previous);
314+
return GoToStep (previous);
313315
}
316+
317+
return false;
314318
}
315319

316320
/// <summary>
317321
/// Causes the wizard to move to the next enabled step (or last step if <see cref="CurrentStep"/> is not set). If
318322
/// there is no previous step, does nothing.
319323
/// </summary>
320-
public void GoNext ()
324+
/// <returns><see langword="true"/> if the transition to the step succeeded. <see langword="false"/> if the step was not found or the operation was cancelled.</returns>
325+
public bool GoNext ()
321326
{
322-
WizardStep nextStep = GetNextStep ();
327+
WizardStep? nextStep = GetNextStep ();
323328

324329
if (nextStep is { })
325330
{
326-
GoToStep (nextStep);
331+
return GoToStep (nextStep);
327332
}
333+
334+
return false;
328335
}
329336

330337
/// <summary>Changes to the specified <see cref="WizardStep"/>.</summary>
331338
/// <param name="newStep">The step to go to.</param>
332-
/// <returns>True if the transition to the step succeeded. False if the step was not found or the operation was cancelled.</returns>
333-
public bool GoToStep (WizardStep newStep)
339+
/// <returns><see langword="true"/> if the transition to the step succeeded. <see langword="false"/> if the step was not found or the operation was cancelled.</returns>
340+
public bool GoToStep (WizardStep? newStep)
334341
{
335-
if (OnStepChanging (_currentStep, newStep) || (newStep is { } && !newStep.Enabled))
342+
if (OnStepChanging (_currentStep, newStep) || newStep is { Enabled: false })
336343
{
337344
return false;
338345
}
@@ -344,20 +351,17 @@ public bool GoToStep (WizardStep newStep)
344351
step.ShowHide ();
345352
}
346353

347-
WizardStep oldStep = _currentStep;
354+
WizardStep? oldStep = _currentStep;
348355
_currentStep = newStep;
349356

350357
UpdateButtonsAndTitle ();
351358

352359
// Set focus on the contentview
353-
if (newStep is { })
354-
{
355-
newStep.Subviews.ToArray () [0].SetFocus ();
356-
}
360+
newStep?.Subviews.ToArray () [0].SetFocus ();
357361

358362
if (OnStepChanged (oldStep, _currentStep))
359363
{
360-
// For correctness we do this, but it's meaningless because there's nothing to cancel
364+
// For correctness, we do this, but it's meaningless because there's nothing to cancel
361365
return false;
362366
}
363367

@@ -368,15 +372,15 @@ public bool GoToStep (WizardStep newStep)
368372
/// Raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always the first button
369373
/// in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
370374
/// </summary>
371-
public event EventHandler<WizardButtonEventArgs> MovingBack;
375+
public event EventHandler<WizardButtonEventArgs>? MovingBack;
372376

373377
/// <summary>
374378
/// Raised when the Next/Finish button in the <see cref="Wizard"/> is clicked (or the user presses Enter). The
375379
/// Next/Finish button is always the last button in the array of Buttons passed to the <see cref="Wizard"/>
376380
/// constructor, if any. This event is only raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
377381
/// (otherwise the <see cref="Finished"/> event is raised).
378382
/// </summary>
379-
public event EventHandler<WizardButtonEventArgs> MovingNext;
383+
public event EventHandler<WizardButtonEventArgs>? MovingNext;
380384

381385
/// <summary>
382386
/// <see cref="Wizard"/> is derived from <see cref="Dialog"/> and Dialog causes <c>Esc</c> to call
@@ -410,7 +414,7 @@ protected override bool OnKeyDownNotHandled (Key key)
410414
/// <param name="oldStep">The step the Wizard changed from</param>
411415
/// <param name="newStep">The step the Wizard has changed to</param>
412416
/// <returns>True if the change is to be cancelled.</returns>
413-
public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep)
417+
public virtual bool OnStepChanged (WizardStep? oldStep, WizardStep? newStep)
414418
{
415419
var args = new StepChangeEventArgs (oldStep, newStep);
416420
StepChanged?.Invoke (this, args);
@@ -425,7 +429,7 @@ public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep)
425429
/// <param name="oldStep">The step the Wizard is about to change from</param>
426430
/// <param name="newStep">The step the Wizard is about to change to</param>
427431
/// <returns>True if the change is to be cancelled.</returns>
428-
public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep)
432+
public virtual bool OnStepChanging (WizardStep? oldStep, WizardStep? newStep)
429433
{
430434
var args = new StepChangeEventArgs (oldStep, newStep);
431435
StepChanging?.Invoke (this, args);
@@ -434,26 +438,26 @@ public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep)
434438
}
435439

436440
/// <summary>This event is raised after the <see cref="Wizard"/> has changed the <see cref="CurrentStep"/>.</summary>
437-
public event EventHandler<StepChangeEventArgs> StepChanged;
441+
public event EventHandler<StepChangeEventArgs>? StepChanged;
438442

439443
/// <summary>
440444
/// This event is raised when the current <see cref="CurrentStep"/>) is about to change. Use
441445
/// <see cref="StepChangeEventArgs.Cancel"/> to abort the transition.
442446
/// </summary>
443-
public event EventHandler<StepChangeEventArgs> StepChanging;
447+
public event EventHandler<StepChangeEventArgs>? StepChanging;
444448

445-
private void BackBtn_Clicked (object sender, EventArgs e)
449+
private void BackBtn_Accepting (object? sender, CommandEventArgs e)
446450
{
447451
var args = new WizardButtonEventArgs ();
448452
MovingBack?.Invoke (this, args);
449453

450454
if (!args.Cancel)
451455
{
452-
GoBack ();
456+
e.Cancel = GoBack ();
453457
}
454458
}
455459

456-
private void NextfinishBtn_Clicked (object sender, EventArgs e)
460+
private void NextFinishBtn_Accepting (object? sender, CommandEventArgs e)
457461
{
458462
if (CurrentStep == GetLastStep ())
459463
{
@@ -467,6 +471,7 @@ private void NextfinishBtn_Clicked (object sender, EventArgs e)
467471
if (IsCurrentTop)
468472
{
469473
Application.RequestStop (this);
474+
e.Cancel = true;
470475
}
471476

472477
// Wizard was created as a non-modal (just added to another View).
@@ -480,7 +485,7 @@ private void NextfinishBtn_Clicked (object sender, EventArgs e)
480485

481486
if (!args.Cancel)
482487
{
483-
GoNext ();
488+
e.Cancel = GoNext ();
484489
}
485490
}
486491
}
@@ -548,7 +553,7 @@ private void UpdateButtonsAndTitle ()
548553
SetNeedsLayout ();
549554
}
550555

551-
private void Wizard_Closing (object sender, ToplevelClosingEventArgs obj)
556+
private void Wizard_Closing (object? sender, ToplevelClosingEventArgs obj)
552557
{
553558
if (!_finishedPressed)
554559
{
@@ -557,14 +562,14 @@ private void Wizard_Closing (object sender, ToplevelClosingEventArgs obj)
557562
}
558563
}
559564

560-
private void Wizard_Loaded (object sender, EventArgs args)
565+
private void Wizard_Loaded (object? sender, EventArgs args)
561566
{
562567
CurrentStep = GetFirstStep ();
563568

564569
// gets the first step if CurrentStep == null
565570
}
566571

567-
private void Wizard_TitleChanged (object sender, EventArgs<string> e)
572+
private void Wizard_TitleChanged (object? sender, EventArgs<string> e)
568573
{
569574
if (string.IsNullOrEmpty (_wizardTitle))
570575
{

Terminal.Gui/Views/Wizard/WizardStep.cs

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Terminal.Gui;
1+
#nullable enable
2+
namespace Terminal.Gui;
23

34
/// <summary>
45
/// Represents a basic step that is displayed in a <see cref="Wizard"/>. The <see cref="WizardStep"/> view is
@@ -15,26 +16,6 @@
1516
/// </remarks>
1617
public class WizardStep : View
1718
{
18-
///// <summary>
19-
///// The title of the <see cref="WizardStep"/>.
20-
///// </summary>
21-
///// <remarks>The Title is only displayed when the <see cref="Wizard"/> is used as a modal pop-up (see <see cref="Wizard.Modal"/>.</remarks>
22-
//public new string Title {
23-
// // BUGBUG: v2 - No need for this as View now has Title w/ notifications.
24-
// get => title;
25-
// set {
26-
// if (!OnTitleChanging (title, value)) {
27-
// var old = title;
28-
// title = value;
29-
// OnTitleChanged (old, title);
30-
// }
31-
// base.Title = value;
32-
// SetNeedsDraw ();
33-
// }
34-
//}
35-
36-
//private string title = string.Empty;
37-
3819
// The contentView works like the ContentView in FrameView.
3920
private readonly View _contentView = new ()
4021
{
@@ -140,7 +121,7 @@ public string HelpText
140121

141122
/// <summary>Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.</summary>
142123
/// <param name="view"><see cref="View"/> to add to this container</param>
143-
public override View Add (View view)
124+
public override View Add (View? view)
144125
{
145126
_contentView.Add (view);
146127

@@ -156,10 +137,10 @@ public override View Add (View view)
156137

157138
/// <summary>Removes a <see cref="View"/> from <see cref="WizardStep"/>.</summary>
158139
/// <remarks></remarks>
159-
public override View Remove (View view)
140+
public override View? Remove (View? view)
160141
{
161142
SetNeedsDraw ();
162-
View container = view?.SuperView;
143+
View? container = view?.SuperView;
163144

164145
if (container == this)
165146
{

UICatalog/Scenarios/Wizards.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void Win_Loaded (object sender, EventArgs args)
263263
};
264264
thirdStep.Add (progLbl, progressBar);
265265
thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
266-
thirdStepEnabledCeckBox.CheckedStateChanging += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; };
266+
thirdStepEnabledCeckBox.CheckedStateChanged += (s, e) => { thirdStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked; };
267267

268268
// Add 4th step
269269
var fourthStep = new WizardStep { Title = "Step Four" };
@@ -357,7 +357,7 @@ void Win_Loaded (object sender, EventArgs args)
357357
"This step only shows if it was enabled on the other last step.";
358358
finalFinalStep.Enabled = thirdStepEnabledCeckBox.CheckedState == CheckState.Checked;
359359

360-
finalFinalStepEnabledCeckBox.CheckedStateChanging += (s, e) =>
360+
finalFinalStepEnabledCeckBox.CheckedStateChanged += (s, e) =>
361361
{
362362
finalFinalStep.Enabled = finalFinalStepEnabledCeckBox.CheckedState == CheckState.Checked;
363363
};

0 commit comments

Comments
 (0)