Wizard.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NStack;
  5. using Terminal.Gui.Resources;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Provides navigation and a user interface (UI) to collect related data across multiple steps. Each step (<see cref="WizardStep"/>) can host
  9. /// arbitrary <see cref="View"/>s, much like a <see cref="Dialog"/>. Each step also has a pane for help text. Along the
  10. /// bottom of the Wizard view are customizable buttons enabling the user to navigate forward and backward through the Wizard.
  11. /// </summary>
  12. /// <remarks>
  13. /// The Wizard can be displayed either as a modal (pop-up) <see cref="Window"/> (like <see cref="Dialog"/>) or as an embedded <see cref="View"/>.
  14. ///
  15. /// By default, <see cref="Modal"/> is true; launch the Wizard using `Application.Run(wizard)`.
  16. ///
  17. /// Set <see cref="Modal"/> to `false` to use Wizard as an embedded View, and add the Wizard to a containing view with <see cref="View.Add(View)"/>.
  18. ///
  19. /// When used as a modal pop-up window, the Esc key will cause the <see cref="Cancelled"/> event to fire and (if the event is not cancelled),
  20. /// will cause <see cref="Application.RequestStop(Toplevel)"/> to be called, closing the Wizard.
  21. ///
  22. /// When used as an embedded View, no frame is drawn around the Wizard. To detect if the user wants to cancel
  23. /// the Wizard, subscrie to the <see cref="Cancelled"/> event.
  24. ///
  25. /// </remarks>
  26. public class Wizard : Dialog {
  27. /// <summary>
  28. /// Represents a basic step that is displayed in a <see cref="Wizard"/>. The <see cref="WizardStep"/> view is divided horizontally in two. On the left is the
  29. /// content view where <see cref="View"/>s can be added, On the right is the help for the step.
  30. /// Set <see cref="WizardStep.HelpText"/> to set the help text. If the help text is empty the help pane will not
  31. /// be shown.
  32. ///
  33. /// If there are no Views added to the WizardStep the <see cref="HelpText"/> (if not empty) will fill the wizard step.
  34. /// </summary>
  35. /// <remarks>
  36. /// If <see cref="Button"/>s are added, do not set <see cref="Button.IsDefault"/> to true as this will conflict
  37. /// with the Next button of the Wizard.
  38. ///
  39. /// Subscribe to the <see cref="View.VisibleChanged"/> event to be notified when the step is active; see also: <see cref="Wizard.StepChanged"/>.
  40. ///
  41. /// To enable or disable a step from being shown to the user, set <see cref="View.Enabled"/>.
  42. ///
  43. /// </remarks>
  44. public class WizardStep : FrameView {
  45. /// <summary>
  46. /// The title of the <see cref="WizardStep"/>.
  47. /// </summary>
  48. /// <remarks>The Title is only displayed when the <see cref="Wizard"/> is used as a modal pop-up.</remarks>
  49. public new ustring Title {
  50. get => title;
  51. set {
  52. if (!OnTitleChanging (title, value)) {
  53. var old = title;
  54. title = value;
  55. OnTitleChanged (old, title);
  56. }
  57. base.Title = value;
  58. SetNeedsDisplay ();
  59. }
  60. }
  61. private ustring title = ustring.Empty;
  62. /// <summary>
  63. /// An <see cref="EventArgs"/> which allows passing a cancelable new <see cref="Title"/> value event.
  64. /// </summary>
  65. public class TitleEventArgs : EventArgs {
  66. /// <summary>
  67. /// The new Window Title.
  68. /// </summary>
  69. public ustring NewTitle { get; set; }
  70. /// <summary>
  71. /// The old Window Title.
  72. /// </summary>
  73. public ustring OldTitle { get; set; }
  74. /// <summary>
  75. /// Flag which allows cancelling the Title change.
  76. /// </summary>
  77. public bool Cancel { get; set; }
  78. /// <summary>
  79. /// Initializes a new instance of <see cref="TitleEventArgs"/>
  80. /// </summary>
  81. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  82. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  83. public TitleEventArgs (ustring oldTitle, ustring newTitle)
  84. {
  85. OldTitle = oldTitle;
  86. NewTitle = newTitle;
  87. }
  88. }
  89. /// <summary>
  90. /// Called before the <see cref="Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
  91. /// </summary>
  92. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  93. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  94. /// <returns>`true` if an event handler cancelled the Title change.</returns>
  95. public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
  96. {
  97. var args = new TitleEventArgs (oldTitle, newTitle);
  98. TitleChanging?.Invoke (args);
  99. return args.Cancel;
  100. }
  101. /// <summary>
  102. /// Event fired when the <see cref="Title"/> is changing. Set <see cref="TitleEventArgs.Cancel"/> to
  103. /// `true` to cancel the Title change.
  104. /// </summary>
  105. public event Action<TitleEventArgs> TitleChanging;
  106. /// <summary>
  107. /// Called when the <see cref="Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
  108. /// </summary>
  109. /// <param name="oldTitle">The <see cref="Title"/> that is/has been replaced.</param>
  110. /// <param name="newTitle">The new <see cref="Title"/> to be replaced.</param>
  111. public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
  112. {
  113. var args = new TitleEventArgs (oldTitle, newTitle);
  114. TitleChanged?.Invoke (args);
  115. }
  116. /// <summary>
  117. /// Event fired after the <see cref="Title"/> has been changed.
  118. /// </summary>
  119. public event Action<TitleEventArgs> TitleChanged;
  120. // The contentView works like the ContentView in FrameView.
  121. private View contentView = new View ();
  122. /// <summary>
  123. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.HelpText"/> is empty
  124. /// the help pane will not be visible and the content will fill the entire WizardStep.
  125. /// </summary>
  126. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  127. public ustring HelpText {
  128. get => helpTextView.Text;
  129. set {
  130. helpTextView.Text = value;
  131. ShowHide ();
  132. SetNeedsDisplay ();
  133. }
  134. }
  135. private TextView helpTextView = new TextView ();
  136. /// <summary>
  137. /// Sets or gets the text for the back button. The back button will only be visible on
  138. /// steps after the first step.
  139. /// </summary>
  140. /// <remarks>The default text is "Back"</remarks>
  141. public ustring BackButtonText { get; set; } = ustring.Empty;
  142. /// <summary>
  143. /// Sets or gets the text for the next/finish button.
  144. /// </summary>
  145. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  146. public ustring NextButtonText { get; set; } = ustring.Empty;
  147. /// <summary>
  148. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  149. /// </summary>
  150. /// <param name="title">Title for the Step. Will be appended to the containing Wizard's title as
  151. /// "Wizard Title - Wizard Step Title" when this step is active.</param>
  152. /// <remarks>
  153. /// </remarks>
  154. public WizardStep (ustring title)
  155. {
  156. this.Title = title; // this.Title holds just the "Wizard Title"; base.Title holds "Wizard Title - Step Title"
  157. this.Border.BorderStyle = BorderStyle.Rounded;
  158. base.Add (contentView);
  159. helpTextView.ColorScheme = Colors.TopLevel;
  160. helpTextView.ReadOnly = true;
  161. helpTextView.WordWrap = true;
  162. base.Add (helpTextView);
  163. ShowHide ();
  164. var scrollBar = new ScrollBarView (helpTextView, true);
  165. scrollBar.ChangedPosition += () => {
  166. helpTextView.TopRow = scrollBar.Position;
  167. if (helpTextView.TopRow != scrollBar.Position) {
  168. scrollBar.Position = helpTextView.TopRow;
  169. }
  170. helpTextView.SetNeedsDisplay ();
  171. };
  172. scrollBar.OtherScrollBarView.ChangedPosition += () => {
  173. helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  174. if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  175. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  176. }
  177. helpTextView.SetNeedsDisplay ();
  178. };
  179. scrollBar.VisibleChanged += () => {
  180. if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  181. helpTextView.RightOffset = 1;
  182. } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  183. helpTextView.RightOffset = 0;
  184. }
  185. };
  186. scrollBar.OtherScrollBarView.VisibleChanged += () => {
  187. if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  188. helpTextView.BottomOffset = 1;
  189. } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  190. helpTextView.BottomOffset = 0;
  191. }
  192. };
  193. helpTextView.DrawContent += (e) => {
  194. scrollBar.Size = helpTextView.Lines;
  195. scrollBar.Position = helpTextView.TopRow;
  196. if (scrollBar.OtherScrollBarView != null) {
  197. scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  198. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  199. }
  200. scrollBar.LayoutSubviews ();
  201. scrollBar.Refresh ();
  202. };
  203. base.Add (scrollBar);
  204. }
  205. /// <summary>
  206. /// Does the work to show and hide the contentView and helpView as appropriate
  207. /// </summary>
  208. internal void ShowHide ()
  209. {
  210. contentView.Height = Dim.Fill ();
  211. helpTextView.Height = Dim.Fill ();
  212. helpTextView.Width = Dim.Fill ();
  213. if (contentView.InternalSubviews?.Count > 0) {
  214. if (helpTextView.Text.Length > 0) {
  215. contentView.Width = Dim.Percent (70);
  216. helpTextView.X = Pos.Right (contentView);
  217. helpTextView.Width = Dim.Fill ();
  218. } else {
  219. contentView.Width = Dim.Percent (100);
  220. }
  221. } else {
  222. if (helpTextView.Text.Length > 0) {
  223. helpTextView.X = 0;
  224. } else {
  225. // Error - no pane shown
  226. }
  227. }
  228. contentView.Visible = contentView.InternalSubviews?.Count > 0;
  229. helpTextView.Visible = helpTextView.Text.Length > 0;
  230. }
  231. /// <summary>
  232. /// Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.
  233. /// </summary>
  234. /// <param name="view"><see cref="View"/> to add to this container</param>
  235. public override void Add (View view)
  236. {
  237. contentView.Add (view);
  238. if (view.CanFocus)
  239. CanFocus = true;
  240. ShowHide ();
  241. }
  242. /// <summary>
  243. /// Removes a <see cref="View"/> from <see cref="WizardStep"/>.
  244. /// </summary>
  245. /// <remarks>
  246. /// </remarks>
  247. public override void Remove (View view)
  248. {
  249. if (view == null)
  250. return;
  251. SetNeedsDisplay ();
  252. var touched = view.Frame;
  253. contentView.Remove (view);
  254. if (contentView.InternalSubviews.Count < 1)
  255. this.CanFocus = false;
  256. ShowHide ();
  257. }
  258. /// <summary>
  259. /// Removes all <see cref="View"/>s from the <see cref="WizardStep"/>.
  260. /// </summary>
  261. /// <remarks>
  262. /// </remarks>
  263. public override void RemoveAll ()
  264. {
  265. contentView.RemoveAll ();
  266. ShowHide ();
  267. }
  268. } // end of WizardStep class
  269. /// <summary>
  270. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  271. /// </summary>
  272. /// <remarks>
  273. /// The Wizard will be vertically and horizontally centered in the container.
  274. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  275. /// </remarks>
  276. public Wizard () : this (ustring.Empty)
  277. {
  278. }
  279. /// <summary>
  280. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  281. /// </summary>
  282. /// <param name="title">Sets the <see cref="Title"/> for the Wizard.</param>
  283. /// <remarks>
  284. /// The Wizard will be vertically and horizontally centered in the container.
  285. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  286. /// </remarks>
  287. public Wizard (ustring title) : base (title)
  288. {
  289. wizardTitle = title;
  290. // Using Justify causes the Back and Next buttons to be hard justified against
  291. // the left and right edge
  292. ButtonAlignment = ButtonAlignments.Justify;
  293. this.Border.BorderStyle = BorderStyle.Double;
  294. this.Border.Padding = new Thickness (0);
  295. //// Add a horiz separator
  296. //var separator = new LineView (Graphs.Orientation.Horizontal) {
  297. // Y = Pos.AnchorEnd (2)
  298. //};
  299. //Add (separator);
  300. // BUGBUG: Space is to work around https://github.com/migueldeicaza/gui.cs/issues/1812
  301. backBtn = new Button (Strings.wzBack) { AutoSize = true };
  302. AddButton (backBtn);
  303. nextfinishBtn = new Button (Strings.wzFinish) { AutoSize = true };
  304. nextfinishBtn.IsDefault = true;
  305. AddButton (nextfinishBtn);
  306. backBtn.Clicked += BackBtn_Clicked;
  307. nextfinishBtn.Clicked += NextfinishBtn_Clicked;
  308. Loaded += Wizard_Loaded;
  309. Closing += Wizard_Closing;
  310. if (Modal) {
  311. ClearKeybinding (Command.QuitToplevel);
  312. AddKeyBinding (Key.Esc, Command.QuitToplevel);
  313. }
  314. }
  315. private void Wizard_Loaded ()
  316. {
  317. CurrentStep = GetFirstStep (); // gets the first step if CurrentStep == null
  318. }
  319. private bool finishedPressed = false;
  320. private void Wizard_Closing (ToplevelClosingEventArgs obj)
  321. {
  322. if (!finishedPressed) {
  323. var args = new WizardButtonEventArgs ();
  324. Cancelled?.Invoke (args);
  325. }
  326. }
  327. private void NextfinishBtn_Clicked ()
  328. {
  329. if (CurrentStep == GetLastStep ()) {
  330. var args = new WizardButtonEventArgs ();
  331. Finished?.Invoke (args);
  332. if (!args.Cancel) {
  333. finishedPressed = true;
  334. if (IsCurrentTop) {
  335. Application.RequestStop (this);
  336. } else {
  337. // Wizard was created as a non-modal (just added to another View).
  338. // Do nothing
  339. }
  340. }
  341. } else {
  342. var args = new WizardButtonEventArgs ();
  343. MovingNext?.Invoke (args);
  344. if (!args.Cancel) {
  345. GoNext ();
  346. }
  347. }
  348. }
  349. ///<inheritdoc/>
  350. public override bool ProcessKey (KeyEvent kb)
  351. {
  352. if (!Modal) {
  353. switch (kb.Key) {
  354. case Key.Esc:
  355. // Dialog causes ESC to RequestStop; we dont want that with a non-modal wizard
  356. // Instead, we fire the Cancelled event.
  357. var args = new WizardButtonEventArgs ();
  358. Cancelled?.Invoke (args);
  359. return false;
  360. }
  361. }
  362. return base.ProcessKey (kb);
  363. }
  364. /// <summary>
  365. /// Causes the wizad to move to the next enabled step (or last step if <see cref="CurrentStep"/> is not set).
  366. /// If there is no previous step, does nothing.
  367. /// </summary>
  368. public void GoNext ()
  369. {
  370. var nextStep = GetNextStep ();
  371. if (nextStep != null) {
  372. GoToStep (nextStep);
  373. }
  374. }
  375. /// <summary>
  376. /// Returns the next enabled <see cref="WizardStep"/> after the current step. Takes into account steps which
  377. /// are disabled. If <see cref="CurrentStep"/> is `null` returns the first enabled step.
  378. /// </summary>
  379. /// <returns>The next step after the current step, if there is one; otherwise returns `null`, which
  380. /// indicates either there are no enabled steps or the current step is the last enabled step.</returns>
  381. public WizardStep GetNextStep ()
  382. {
  383. LinkedListNode<WizardStep> step = null;
  384. if (CurrentStep == null) {
  385. // Get first step, assume it is next
  386. step = steps.First;
  387. } else {
  388. // Get the step after current
  389. step = steps.Find (CurrentStep);
  390. if (step != null) {
  391. step = step.Next;
  392. }
  393. }
  394. // step now points to the potential next step
  395. while (step != null) {
  396. if (step.Value.Enabled) {
  397. return step.Value;
  398. }
  399. step = step.Next;
  400. }
  401. return null;
  402. }
  403. private void BackBtn_Clicked ()
  404. {
  405. var args = new WizardButtonEventArgs ();
  406. MovingBack?.Invoke (args);
  407. if (!args.Cancel) {
  408. GoBack ();
  409. }
  410. }
  411. /// <summary>
  412. /// Causes the wizad to move to the previous enabled step (or first step if <see cref="CurrentStep"/> is not set).
  413. /// If there is no previous step, does nothing.
  414. /// </summary>
  415. public void GoBack ()
  416. {
  417. var previous = GetPreviousStep ();
  418. if (previous != null) {
  419. GoToStep (previous);
  420. }
  421. }
  422. /// <summary>
  423. /// Returns the first enabled <see cref="WizardStep"/> before the current step. Takes into account steps which
  424. /// are disabled. If <see cref="CurrentStep"/> is `null` returns the last enabled step.
  425. /// </summary>
  426. /// <returns>The first step ahead of the current step, if there is one; otherwise returns `null`, which
  427. /// indicates either there are no enabled steps or the current step is the first enabled step.</returns>
  428. public WizardStep GetPreviousStep ()
  429. {
  430. LinkedListNode<WizardStep> step = null;
  431. if (CurrentStep == null) {
  432. // Get last step, assume it is previous
  433. step = steps.Last;
  434. } else {
  435. // Get the step before current
  436. step = steps.Find (CurrentStep);
  437. if (step != null) {
  438. step = step.Previous;
  439. }
  440. }
  441. // step now points to the potential previous step
  442. while (step != null) {
  443. if (step.Value.Enabled) {
  444. return step.Value;
  445. }
  446. step = step.Previous;
  447. }
  448. return null;
  449. }
  450. /// <summary>
  451. /// Returns the first enabled step in the Wizard
  452. /// </summary>
  453. /// <returns>The last enabled step</returns>
  454. public WizardStep GetFirstStep ()
  455. {
  456. return steps.FirstOrDefault (s => s.Enabled);
  457. }
  458. /// <summary>
  459. /// Returns the last enabled step in the Wizard
  460. /// </summary>
  461. /// <returns>The last enabled step</returns>
  462. public WizardStep GetLastStep ()
  463. {
  464. return steps.LastOrDefault (s => s.Enabled);
  465. }
  466. private LinkedList<WizardStep> steps = new LinkedList<WizardStep> ();
  467. private WizardStep currentStep = null;
  468. /// <summary>
  469. /// If the <see cref="CurrentStep"/> is not the first step in the wizard, this button causes
  470. /// the <see cref="MovingBack"/> event to be fired and the wizard moves to the previous step.
  471. /// </summary>
  472. /// <remarks>
  473. /// Use the <see cref="MovingBack"></see> event to be notified when the user attempts to go back.
  474. /// </remarks>
  475. public Button BackButton { get => backBtn; }
  476. private Button backBtn;
  477. /// <summary>
  478. /// If the <see cref="CurrentStep"/> is the last step in the wizard, this button causes
  479. /// the <see cref="Finished"/> event to be fired and the wizard to close. If the step is not the last step,
  480. /// the <see cref="MovingNext"/> event will be fired and the wizard will move next step.
  481. /// </summary>
  482. /// <remarks>
  483. /// Use the <see cref="MovingNext"></see> and <see cref="Finished"></see> events to be notified
  484. /// when the user attempts go to the next step or finish the wizard.
  485. /// </remarks>
  486. public Button NextFinishButton { get => nextfinishBtn; }
  487. private Button nextfinishBtn;
  488. /// <summary>
  489. /// Adds a step to the wizard. The Next and Back buttons navigate through the added steps in the
  490. /// order they were added.
  491. /// </summary>
  492. /// <param name="newStep"></param>
  493. /// <remarks>The "Next..." button of the last step added will read "Finish" (unless changed from default).</remarks>
  494. public void AddStep (WizardStep newStep)
  495. {
  496. SizeStep (newStep);
  497. newStep.EnabledChanged += UpdateButtonsAndTitle;
  498. newStep.TitleChanged += (args) => UpdateButtonsAndTitle ();
  499. steps.AddLast (newStep);
  500. this.Add (newStep);
  501. UpdateButtonsAndTitle ();
  502. }
  503. /// <summary>
  504. /// The title of the Wizard, shown at the top of the Wizard with " - currentStep.Title" appended.
  505. /// </summary>
  506. /// <remarks>The Title is only displayed when the <see cref="Wizard"/> is used as a modal pop-up.</remarks>
  507. public new ustring Title {
  508. get {
  509. // The base (Dialog) Title holds the full title ("Wizard Title - Step Title")
  510. return base.Title;
  511. }
  512. set {
  513. wizardTitle = value;
  514. base.Title = $"{wizardTitle}{(steps.Count > 0 && currentStep != null ? " - " + currentStep.Title : string.Empty)}";
  515. }
  516. }
  517. private ustring wizardTitle = ustring.Empty;
  518. /// <summary>
  519. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> transition events.
  520. /// </summary>
  521. public class WizardButtonEventArgs : EventArgs {
  522. /// <summary>
  523. /// Set to true to cancel the transition to the next step.
  524. /// </summary>
  525. public bool Cancel { get; set; }
  526. /// <summary>
  527. /// Initializes a new instance of <see cref="WizardButtonEventArgs"/>
  528. /// </summary>
  529. public WizardButtonEventArgs ()
  530. {
  531. Cancel = false;
  532. }
  533. }
  534. /// <summary>
  535. /// This event is raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always
  536. /// the first button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
  537. /// </summary>
  538. public event Action<WizardButtonEventArgs> MovingBack;
  539. /// <summary>
  540. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked (or the user presses Enter).
  541. /// The Next/Finish button is always the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor,
  542. /// if any. This event is only raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  543. /// (otherwise the <see cref="Finished"/> event is raised).
  544. /// </summary>
  545. public event Action<WizardButtonEventArgs> MovingNext;
  546. /// <summary>
  547. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  548. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  549. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  550. /// (otherwise the <see cref="Finished"/> event is raised).
  551. /// </summary>
  552. public event Action<WizardButtonEventArgs> Finished;
  553. /// <summary>
  554. /// This event is raised when the user has cancelled the <see cref="Wizard"/> by pressin the Esc key.
  555. /// To prevent a <see cref="Modal"/> Wizard from
  556. /// closing, cancel the event by setting <see cref="WizardButtonEventArgs.Cancel"/> to `true` before returning from the event handler.
  557. /// </summary>
  558. public event Action<WizardButtonEventArgs> Cancelled;
  559. /// <summary>
  560. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> events.
  561. /// </summary>
  562. public class StepChangeEventArgs : EventArgs {
  563. /// <summary>
  564. /// The current (or previous) <see cref="WizardStep"/>.
  565. /// </summary>
  566. public WizardStep OldStep { get; }
  567. /// <summary>
  568. /// The <see cref="WizardStep"/> the <see cref="Wizard"/> is changing to or has changed to.
  569. /// </summary>
  570. public WizardStep NewStep { get; }
  571. /// <summary>
  572. /// Event handlers can set to true before returning to cancel the step transition.
  573. /// </summary>
  574. public bool Cancel { get; set; }
  575. /// <summary>
  576. /// Initializes a new instance of <see cref="StepChangeEventArgs"/>
  577. /// </summary>
  578. /// <param name="oldStep">The current <see cref="WizardStep"/>.</param>
  579. /// <param name="newStep">The new <see cref="WizardStep"/>.</param>
  580. public StepChangeEventArgs (WizardStep oldStep, WizardStep newStep)
  581. {
  582. OldStep = oldStep;
  583. NewStep = newStep;
  584. Cancel = false;
  585. }
  586. }
  587. /// <summary>
  588. /// This event is raised when the current <see cref="CurrentStep"/>) is about to change. Use <see cref="StepChangeEventArgs.Cancel"/>
  589. /// to abort the transition.
  590. /// </summary>
  591. public event Action<StepChangeEventArgs> StepChanging;
  592. /// <summary>
  593. /// This event is raised after the <see cref="Wizard"/> has changed the <see cref="CurrentStep"/>.
  594. /// </summary>
  595. public event Action<StepChangeEventArgs> StepChanged;
  596. /// <summary>
  597. /// Gets or sets the currently active <see cref="WizardStep"/>.
  598. /// </summary>
  599. public WizardStep CurrentStep {
  600. get => currentStep;
  601. set {
  602. GoToStep (value);
  603. }
  604. }
  605. /// <summary>
  606. /// Called when the <see cref="Wizard"/> is about to transition to another <see cref="WizardStep"/>. Fires the <see cref="StepChanging"/> event.
  607. /// </summary>
  608. /// <param name="oldStep">The step the Wizard is about to change from</param>
  609. /// <param name="newStep">The step the Wizard is about to change to</param>
  610. /// <returns>True if the change is to be cancelled.</returns>
  611. public virtual bool OnStepChanging (WizardStep oldStep, WizardStep newStep)
  612. {
  613. var args = new StepChangeEventArgs (oldStep, newStep);
  614. StepChanging?.Invoke (args);
  615. return args.Cancel;
  616. }
  617. /// <summary>
  618. /// Called when the <see cref="Wizard"/> has completed transition to a new <see cref="WizardStep"/>. Fires the <see cref="StepChanged"/> event.
  619. /// </summary>
  620. /// <param name="oldStep">The step the Wizard changed from</param>
  621. /// <param name="newStep">The step the Wizard has changed to</param>
  622. /// <returns>True if the change is to be cancelled.</returns>
  623. public virtual bool OnStepChanged (WizardStep oldStep, WizardStep newStep)
  624. {
  625. var args = new StepChangeEventArgs (oldStep, newStep);
  626. StepChanged?.Invoke (args);
  627. return args.Cancel;
  628. }
  629. /// <summary>
  630. /// Changes to the specified <see cref="WizardStep"/>.
  631. /// </summary>
  632. /// <param name="newStep">The step to go to.</param>
  633. /// <returns>True if the transition to the step succeeded. False if the step was not found or the operation was cancelled.</returns>
  634. public bool GoToStep (WizardStep newStep)
  635. {
  636. if (OnStepChanging (currentStep, newStep) || (newStep != null && !newStep.Enabled)) {
  637. return false;
  638. }
  639. // Hide all but the new step
  640. foreach (WizardStep step in steps) {
  641. step.Visible = (step == newStep);
  642. step.ShowHide ();
  643. }
  644. var oldStep = currentStep;
  645. currentStep = newStep;
  646. UpdateButtonsAndTitle ();
  647. // Set focus to the nav buttons
  648. if (backBtn.HasFocus) {
  649. backBtn.SetFocus ();
  650. } else {
  651. nextfinishBtn.SetFocus ();
  652. }
  653. if (OnStepChanged (oldStep, currentStep)) {
  654. // For correctness we do this, but it's meaningless because there's nothing to cancel
  655. return false;
  656. }
  657. return true;
  658. }
  659. private void UpdateButtonsAndTitle ()
  660. {
  661. if (CurrentStep == null) return;
  662. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + CurrentStep.Title : string.Empty)}";
  663. // Configure the Back button
  664. backBtn.Text = CurrentStep.BackButtonText != ustring.Empty ? CurrentStep.BackButtonText : Strings.wzBack; // "_Back";
  665. backBtn.Visible = (CurrentStep != GetFirstStep ());
  666. // Configure the Next/Finished button
  667. if (CurrentStep == GetLastStep ()) {
  668. nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzFinish; // "Fi_nish";
  669. } else {
  670. nextfinishBtn.Text = CurrentStep.NextButtonText != ustring.Empty ? CurrentStep.NextButtonText : Strings.wzNext; // "_Next...";
  671. }
  672. SizeStep (CurrentStep);
  673. SetNeedsLayout ();
  674. LayoutSubviews ();
  675. Redraw (Bounds);
  676. }
  677. private void SizeStep (WizardStep step)
  678. {
  679. if (Modal) {
  680. // If we're modal, then we expand the WizardStep so that the top and side
  681. // borders and not visible. The bottom border is the separator above the buttons.
  682. step.X = step.Y = -1;
  683. step.Height = Dim.Fill (1); // for button frame
  684. step.Width = Dim.Fill (-1);
  685. } else {
  686. // If we're not a modal, then we show the border around the WizardStep
  687. step.X = step.Y = 0;
  688. step.Height = Dim.Fill (1); // for button frame
  689. step.Width = Dim.Fill (0);
  690. }
  691. }
  692. /// <summary>
  693. /// Determines whether the <see cref="Wizard"/> is displayed as modal pop-up or not.
  694. ///
  695. /// The default is `true`. The Wizard will be shown with a frame with <see cref="Title"/> and will behave like
  696. /// any <see cref="Toplevel"/> window.
  697. ///
  698. /// If set to `false` the Wizard will have no frame and will behave like any embedded <see cref="View"/>.
  699. ///
  700. /// </summary>
  701. public new bool Modal {
  702. get => base.Modal;
  703. set {
  704. base.Modal = value;
  705. foreach (var step in steps) {
  706. SizeStep (step);
  707. }
  708. if (base.Modal) {
  709. ColorScheme = Colors.Dialog;
  710. Border.BorderStyle = BorderStyle.Rounded;
  711. Border.Effect3D = true;
  712. Border.DrawMarginFrame = true;
  713. } else {
  714. if (SuperView != null) {
  715. ColorScheme = SuperView.ColorScheme;
  716. } else {
  717. ColorScheme = Colors.Base;
  718. }
  719. CanFocus = true;
  720. Border.Effect3D = false;
  721. Border.BorderStyle = BorderStyle.None;
  722. Border.DrawMarginFrame = false;
  723. }
  724. }
  725. }
  726. }
  727. }