Wizard.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Collections.Generic;
  3. using NStack;
  4. namespace Terminal.Gui {
  5. /// <summary>
  6. /// Provides a step-based "wizard" UI. The Wizard supports multiple steps. Each step (<see cref="WizardStep"/>) can host
  7. /// arbitrary <see cref="View"/>s, much like a <see cref="Dialog"/>. Each step also has a pane for help text. Along the
  8. /// bottom of the Wizard view are customizable buttons enabling the user to navigate forward and backward through the Wizard.
  9. /// </summary>
  10. /// <remarks>
  11. /// </remarks>
  12. public class Wizard : Dialog {
  13. /// <summary>
  14. /// One step for the Wizard. The <see cref="WizardStep"/> view hosts two sub-views: 1) add <see cref="View"/>s to <see cref="WizardStep.Controls"/>,
  15. /// 2) use <see cref="WizardStep.HelpText"/> to set the contents of the <see cref="TextView"/> that shows on the
  16. /// right side. Use <see cref="WizardStep.showControls"/> and <see cref="WizardStep.showHelp"/> to
  17. /// control wether the control or help pane are shown.
  18. /// </summary>
  19. /// <remarks>
  20. /// If <see cref="Button"/>s are added, do not set <see cref="Button.IsDefault"/> to true as this will conflict
  21. /// with the Next button of the Wizard.
  22. /// </remarks>
  23. public class WizardStep : View {
  24. /// <summary>
  25. /// The title of the <see cref="WizardStep"/>.
  26. /// </summary>
  27. public ustring Title { get => title; set => title = value; }
  28. // TODO: Update Wizard title when step title is changed if step is current - this will require step to slueth it's parent
  29. private ustring title;
  30. private View controlPane = new FrameView ();
  31. /// <summary>
  32. /// THe pane that holds the controls for the <see cref="WizardStep"/>. Use <see cref="WizardStep.Controls"/> `Add(View`) to add
  33. /// controls. Note that the Controls view is sized to take 70% of the Wizard's width and the <see cref="WizardStep.HelpText"/>
  34. /// takes the other 30%. This can be adjusted by setting `Width` from `Dim.Percent(70)` to
  35. /// another value. If <see cref="WizardStep.ShowHelp"/> is set to `false` the control pane will fill the entire
  36. /// Wizard.
  37. /// </summary>
  38. public View Controls { get => controlPane; }
  39. /// <summary>
  40. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.ShowHelp"/> is set to
  41. /// `false` the control pane will fill the entire wizard.
  42. /// </summary>
  43. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  44. public ustring HelpText { get => helpTextView.Text; set => helpTextView.Text = value; }
  45. private TextView helpTextView = new TextView ();
  46. /// <summary>
  47. /// Sets or gets the text for the back button. The back button will only be visible on
  48. /// steps after the first step.
  49. /// </summary>
  50. /// <remarks>The default text is "Back"</remarks>
  51. public ustring BackButtonText { get; set; } = ustring.Empty;
  52. // TODO: Update button text of Wizard button when step's button text is changed if step is current - this will require step to slueth it's parent
  53. /// <summary>
  54. /// Sets or gets the text for the next/finish button.
  55. /// </summary>
  56. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  57. public ustring NextButtonText { get; set; } = ustring.Empty;
  58. // TODO: Update button text of Wizard button when step's button text is changed if step is current - this will require step to slueth it's parent
  59. /// <summary>
  60. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  61. /// </summary>
  62. /// <param name="title">Title for the Step. Will be appended to the containing Wizard's title as
  63. /// "Wizard Title - Wizard Step Title" when this step is active.</param>
  64. /// <remarks>
  65. /// </remarks>
  66. public WizardStep (ustring title)
  67. {
  68. this.Title = title; // this.Title holds just the "Wizard Title"; base.Title holds "Wizard Title - Step Title"
  69. this.ColorScheme = Colors.Menu;
  70. Y = 0;
  71. Height = Dim.Fill (1); // for button frame
  72. Width = Dim.Fill ();
  73. Controls.ColorScheme = Colors.Dialog;
  74. Controls.Border.BorderStyle = BorderStyle.None;
  75. Controls.Border.Padding = new Thickness (0);
  76. Controls.Border.BorderThickness = new Thickness (0);
  77. this.Add (Controls);
  78. helpTextView.ColorScheme = Colors.Menu;
  79. helpTextView.Y = 0;
  80. helpTextView.ReadOnly = true;
  81. helpTextView.WordWrap = true;
  82. this.Add (helpTextView);
  83. ShowHide ();
  84. var scrollBar = new ScrollBarView (helpTextView, true);
  85. scrollBar.ChangedPosition += () => {
  86. helpTextView.TopRow = scrollBar.Position;
  87. if (helpTextView.TopRow != scrollBar.Position) {
  88. scrollBar.Position = helpTextView.TopRow;
  89. }
  90. helpTextView.SetNeedsDisplay ();
  91. };
  92. scrollBar.VisibleChanged += () => {
  93. if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  94. helpTextView.RightOffset = 1;
  95. } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  96. helpTextView.RightOffset = 0;
  97. }
  98. };
  99. helpTextView.DrawContent += (e) => {
  100. scrollBar.Size = helpTextView.Lines;
  101. scrollBar.Position = helpTextView.TopRow;
  102. if (scrollBar.OtherScrollBarView != null) {
  103. scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  104. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  105. }
  106. scrollBar.LayoutSubviews ();
  107. scrollBar.Refresh ();
  108. };
  109. this.Add (scrollBar);
  110. }
  111. /// <summary>
  112. /// If true (the default) the help will be visible. If false, the help will not be shown and the control pane will
  113. /// fill the wizard step.
  114. /// </summary>
  115. public bool ShowHelp {
  116. get => showHelp;
  117. set {
  118. showHelp = value;
  119. ShowHide ();
  120. }
  121. }
  122. private bool showHelp = true;
  123. /// <summary>
  124. /// If true (the default) the <see cref="Controls"/> View will be visible. If false, the controls will not be shown and the help will
  125. /// fill the wizard step.
  126. /// </summary>
  127. public bool ShowControls {
  128. get => showControls;
  129. set {
  130. showControls = value;
  131. ShowHide ();
  132. }
  133. }
  134. private bool showControls = true;
  135. /// <summary>
  136. /// Does the work to show and hide the controls, help, and buttons as appropriate
  137. /// </summary>
  138. private void ShowHide ()
  139. {
  140. Controls.Height = Dim.Fill (1);
  141. helpTextView.Height = Dim.Fill (1);
  142. helpTextView.Width = Dim.Fill ();
  143. if (showControls) {
  144. if (showHelp) {
  145. Controls.Width = Dim.Percent (70);
  146. helpTextView.X = Pos.Right (Controls) ;
  147. helpTextView.Width = Dim.Fill ();
  148. } else {
  149. Controls.Width = Dim.Percent (100);
  150. }
  151. } else {
  152. if (showHelp) {
  153. helpTextView.X = 0;
  154. } else {
  155. // Error - no pane shown
  156. }
  157. }
  158. Controls.Visible = showControls;
  159. helpTextView.Visible = showHelp;
  160. }
  161. }
  162. /// <summary>
  163. /// If the <see cref="CurrentStep"/> is not the first step in the wizard, this button causes
  164. /// the <see cref="MovingBack"/> event to be fired and the wizard moves to the previous step.
  165. /// </summary>
  166. /// <remarks>
  167. /// Use the <see cref="MovingBack"></see> event to be notified when the user attempts to go back.
  168. /// </remarks>
  169. public Button BackButton { get => backBtn; }
  170. private Button backBtn;
  171. /// <summary>
  172. /// If the <see cref="CurrentStep"/> is the last step in the wizard, this button causes
  173. /// the <see cref="Finished"/> event to be fired and the wizard to close. If the step is not the last step,
  174. /// the <see cref="MovingNext"/> event will be fired and the wizard will move next step.
  175. /// </summary>
  176. /// <remarks>
  177. /// Use the <see cref="MovingNext"></see> and <see cref="Finished"></see> events to be notified
  178. /// when the user attempts go to the next step or finish the wizard.
  179. /// </remarks>
  180. public Button NextFinishButton { get => nextfinishBtn; }
  181. private Button nextfinishBtn;
  182. /// <summary>
  183. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  184. /// </summary>
  185. /// <remarks>
  186. /// The Wizard will be vertically and horizontally centered in the container.
  187. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  188. /// </remarks>
  189. public Wizard () : this (ustring.Empty)
  190. {
  191. }
  192. /// <summary>
  193. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  194. /// </summary>
  195. /// <param name="title">Title for the Wizard.</param>
  196. /// <remarks>
  197. /// The Wizard will be vertically and horizontally centered in the container.
  198. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  199. /// </remarks>
  200. public Wizard (ustring title) : base (title)
  201. {
  202. wizardTitle = title;
  203. // Using Justify causes the Back and Next buttons to be hard justified against
  204. // the left and right edge
  205. ButtonAlignment = ButtonAlignments.Justify;
  206. this.Border.BorderStyle = BorderStyle.Double;
  207. // Add a horiz separator
  208. var separator = new LineView (Graphs.Orientation.Horizontal) {
  209. Y = Pos.AnchorEnd (2)
  210. };
  211. Add (separator);
  212. backBtn = new Button ("_Back") { AutoSize = true };
  213. AddButton (backBtn);
  214. nextfinishBtn = new Button ("_Next...") { AutoSize = true };
  215. nextfinishBtn.IsDefault = true;
  216. AddButton (nextfinishBtn);
  217. backBtn.Clicked += () => {
  218. var args = new WizardStepEventArgs ();
  219. MovingBack?.Invoke (args);
  220. if (!args.Cancel) {
  221. if (currentStep > 0) {
  222. CurrentStep--;
  223. }
  224. }
  225. };
  226. nextfinishBtn.Clicked += () => {
  227. if (currentStep == steps.Count - 1) {
  228. var args = new WizardStepEventArgs ();
  229. Finished?.Invoke (args);
  230. if (!args.Cancel) {
  231. Application.RequestStop (this);
  232. }
  233. } else {
  234. var args = new WizardStepEventArgs ();
  235. MovingNext?.Invoke (args);
  236. if (!args.Cancel) {
  237. CurrentStep++;
  238. }
  239. }
  240. };
  241. Loaded += () => {
  242. foreach (var step in steps) {
  243. step.Y = 0;
  244. }
  245. if (steps.Count > 0) {
  246. CurrentStep = 0;
  247. }
  248. };
  249. }
  250. private List<WizardStep> steps = new List<WizardStep> ();
  251. private int currentStep = 0;
  252. /// <summary>
  253. /// Adds a step to the wizard. The Next and Back buttons navigate through the added steps in the
  254. /// order they were added.
  255. /// </summary>
  256. /// <param name="newStep"></param>
  257. /// <remarks>The "Next..." button of the last step added will read "Finish" (unless changed from default).</remarks>
  258. public void AddStep (WizardStep newStep)
  259. {
  260. steps.Add (newStep);
  261. this.Add (newStep);
  262. }
  263. /// <summary>
  264. /// The title of the Wizard, shown at the top of the Wizard with " - currentStep.Title" appended.
  265. /// </summary>
  266. public new ustring Title {
  267. get {
  268. // The base (Dialog) Title holds the full title ("Wizard Title - Step Title")
  269. return base.Title;
  270. }
  271. set {
  272. wizardTitle = value;
  273. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + steps [currentStep].Title : string.Empty)}";
  274. }
  275. }
  276. private ustring wizardTitle = ustring.Empty;
  277. /// <summary>
  278. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> transition events.
  279. /// </summary>
  280. public class WizardStepEventArgs : EventArgs {
  281. /// <summary>
  282. /// Set to true to cancel the transition to the next step.
  283. /// </summary>
  284. public bool Cancel { get; set; }
  285. /// <summary>
  286. /// Initializes a new instance of <see cref="WizardStepEventArgs"/>
  287. /// </summary>
  288. public WizardStepEventArgs ()
  289. {
  290. Cancel = false;
  291. }
  292. }
  293. /// <summary>
  294. /// This event is raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always
  295. /// the first button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
  296. /// </summary>
  297. public event Action<WizardStepEventArgs> MovingBack;
  298. /// <summary>
  299. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  300. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  301. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  302. /// (otherwise the <see cref="Finished"/> event is raised).
  303. /// </summary>
  304. public event Action<WizardStepEventArgs> MovingNext;
  305. /// <summary>
  306. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  307. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  308. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  309. /// (otherwise the <see cref="Finished"/> event is raised).
  310. /// </summary>
  311. public event Action<WizardStepEventArgs> Finished;
  312. /// <summary>
  313. /// This event is raised when the current step )<see cref="CurrentStep"/>) in the <see cref="Wizard"/> changes.
  314. /// </summary>
  315. public event Action<CurrentStepChangedEventArgs> CurrentStepChanged;
  316. /// <summary>
  317. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> events.
  318. /// </summary>
  319. public class CurrentStepChangedEventArgs : EventArgs {
  320. /// <summary>
  321. /// The new current <see cref="WizardStep"/>.
  322. /// </summary>
  323. public int CurrentStepIndex { get; }
  324. /// <summary>
  325. /// Initializes a new instance of <see cref="CurrentStepChangedEventArgs"/>
  326. /// </summary>
  327. /// <param name="currentStepIndex">The new current <see cref="WizardStep"/>.</param>
  328. public CurrentStepChangedEventArgs (int currentStepIndex)
  329. {
  330. CurrentStepIndex = currentStepIndex;
  331. }
  332. }
  333. /// <summary>
  334. /// Gets or sets the currently active <see cref="WizardStep"/>.
  335. /// </summary>
  336. public int CurrentStep {
  337. get => currentStep;
  338. set {
  339. currentStep = value;
  340. OnCurrentStepChanged ();
  341. }
  342. }
  343. /// <summary>
  344. /// Called when the current <see cref="WizardStep"/> has changed (<see cref="CurrentStep"/>).
  345. /// </summary>
  346. public virtual void OnCurrentStepChanged ()
  347. {
  348. CurrentStepChanged?.Invoke (new CurrentStepChangedEventArgs (currentStep));
  349. // Hide all but the first step
  350. foreach (WizardStep step in steps) {
  351. step.Visible = (steps [currentStep] == step);
  352. }
  353. // TODO: Add support for "Wizard Title - Step Title"
  354. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + steps [currentStep].Title : string.Empty)}";
  355. backBtn.Text = steps [currentStep].BackButtonText != ustring.Empty ? steps [currentStep].BackButtonText : "_Back";
  356. if (currentStep == 0) {
  357. backBtn.Visible = false;
  358. } else {
  359. backBtn.Visible = true;
  360. }
  361. if (currentStep == steps.Count - 1) {
  362. nextfinishBtn.Text = steps [currentStep].NextButtonText != ustring.Empty ? steps [currentStep].NextButtonText : "Fi_nish";
  363. } else {
  364. nextfinishBtn.Text = steps [currentStep].NextButtonText != ustring.Empty ? steps [currentStep].NextButtonText : "_Next...";
  365. }
  366. }
  367. }
  368. }