Wizard.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.OtherScrollBarView.ChangedPosition += () => {
  93. helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  94. if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  95. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  96. }
  97. helpTextView.SetNeedsDisplay ();
  98. };
  99. scrollBar.VisibleChanged += () => {
  100. if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  101. helpTextView.RightOffset = 1;
  102. } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  103. helpTextView.RightOffset = 0;
  104. }
  105. };
  106. scrollBar.OtherScrollBarView.VisibleChanged += () => {
  107. if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  108. helpTextView.BottomOffset = 1;
  109. } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  110. helpTextView.BottomOffset = 0;
  111. }
  112. };
  113. helpTextView.DrawContent += (e) => {
  114. scrollBar.Size = helpTextView.Lines;
  115. scrollBar.Position = helpTextView.TopRow;
  116. if (scrollBar.OtherScrollBarView != null) {
  117. scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  118. scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  119. }
  120. scrollBar.LayoutSubviews ();
  121. scrollBar.Refresh ();
  122. };
  123. this.Add (scrollBar);
  124. }
  125. /// <summary>
  126. /// If true (the default) the help will be visible. If false, the help will not be shown and the control pane will
  127. /// fill the wizard step.
  128. /// </summary>
  129. public bool ShowHelp {
  130. get => showHelp;
  131. set {
  132. showHelp = value;
  133. ShowHide ();
  134. }
  135. }
  136. private bool showHelp = true;
  137. /// <summary>
  138. /// If true (the default) the <see cref="Controls"/> View will be visible. If false, the controls will not be shown and the help will
  139. /// fill the wizard step.
  140. /// </summary>
  141. public bool ShowControls {
  142. get => showControls;
  143. set {
  144. showControls = value;
  145. ShowHide ();
  146. }
  147. }
  148. private bool showControls = true;
  149. /// <summary>
  150. /// Does the work to show and hide the controls, help, and buttons as appropriate
  151. /// </summary>
  152. private void ShowHide ()
  153. {
  154. Controls.Height = Dim.Fill (1);
  155. helpTextView.Height = Dim.Fill (1);
  156. helpTextView.Width = Dim.Fill ();
  157. if (showControls) {
  158. if (showHelp) {
  159. Controls.Width = Dim.Percent (70);
  160. helpTextView.X = Pos.Right (Controls) ;
  161. helpTextView.Width = Dim.Fill ();
  162. } else {
  163. Controls.Width = Dim.Percent (100);
  164. }
  165. } else {
  166. if (showHelp) {
  167. helpTextView.X = 0;
  168. } else {
  169. // Error - no pane shown
  170. }
  171. }
  172. Controls.Visible = showControls;
  173. helpTextView.Visible = showHelp;
  174. }
  175. }
  176. /// <summary>
  177. /// If the <see cref="CurrentStep"/> is not the first step in the wizard, this button causes
  178. /// the <see cref="MovingBack"/> event to be fired and the wizard moves to the previous step.
  179. /// </summary>
  180. /// <remarks>
  181. /// Use the <see cref="MovingBack"></see> event to be notified when the user attempts to go back.
  182. /// </remarks>
  183. public Button BackButton { get => backBtn; }
  184. private Button backBtn;
  185. /// <summary>
  186. /// If the <see cref="CurrentStep"/> is the last step in the wizard, this button causes
  187. /// the <see cref="Finished"/> event to be fired and the wizard to close. If the step is not the last step,
  188. /// the <see cref="MovingNext"/> event will be fired and the wizard will move next step.
  189. /// </summary>
  190. /// <remarks>
  191. /// Use the <see cref="MovingNext"></see> and <see cref="Finished"></see> events to be notified
  192. /// when the user attempts go to the next step or finish the wizard.
  193. /// </remarks>
  194. public Button NextFinishButton { get => nextfinishBtn; }
  195. private Button nextfinishBtn;
  196. /// <summary>
  197. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  198. /// </summary>
  199. /// <remarks>
  200. /// The Wizard will be vertically and horizontally centered in the container.
  201. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  202. /// </remarks>
  203. public Wizard () : this (ustring.Empty)
  204. {
  205. }
  206. /// <summary>
  207. /// Initializes a new instance of the <see cref="Wizard"/> class using <see cref="LayoutStyle.Computed"/> positioning.
  208. /// </summary>
  209. /// <param name="title">Title for the Wizard.</param>
  210. /// <remarks>
  211. /// The Wizard will be vertically and horizontally centered in the container.
  212. /// After initialization use <c>X</c>, <c>Y</c>, <c>Width</c>, and <c>Height</c> change size and position.
  213. /// </remarks>
  214. public Wizard (ustring title) : base (title)
  215. {
  216. wizardTitle = title;
  217. // Using Justify causes the Back and Next buttons to be hard justified against
  218. // the left and right edge
  219. ButtonAlignment = ButtonAlignments.Justify;
  220. this.Border.BorderStyle = BorderStyle.Double;
  221. // Add a horiz separator
  222. var separator = new LineView (Graphs.Orientation.Horizontal) {
  223. Y = Pos.AnchorEnd (2)
  224. };
  225. Add (separator);
  226. backBtn = new Button ("_Back") { AutoSize = true };
  227. AddButton (backBtn);
  228. nextfinishBtn = new Button ("_Next...") { AutoSize = true };
  229. nextfinishBtn.IsDefault = true;
  230. AddButton (nextfinishBtn);
  231. backBtn.Clicked += () => {
  232. var args = new WizardStepEventArgs ();
  233. MovingBack?.Invoke (args);
  234. if (!args.Cancel) {
  235. if (currentStep > 0) {
  236. CurrentStep--;
  237. }
  238. }
  239. };
  240. nextfinishBtn.Clicked += () => {
  241. if (currentStep == steps.Count - 1) {
  242. var args = new WizardStepEventArgs ();
  243. Finished?.Invoke (args);
  244. if (!args.Cancel) {
  245. Application.RequestStop (this);
  246. }
  247. } else {
  248. var args = new WizardStepEventArgs ();
  249. MovingNext?.Invoke (args);
  250. if (!args.Cancel) {
  251. CurrentStep++;
  252. }
  253. }
  254. };
  255. Loaded += () => {
  256. foreach (var step in steps) {
  257. step.Y = 0;
  258. }
  259. if (steps.Count > 0) {
  260. CurrentStep = 0;
  261. }
  262. };
  263. }
  264. private List<WizardStep> steps = new List<WizardStep> ();
  265. private int currentStep = 0;
  266. /// <summary>
  267. /// Adds a step to the wizard. The Next and Back buttons navigate through the added steps in the
  268. /// order they were added.
  269. /// </summary>
  270. /// <param name="newStep"></param>
  271. /// <remarks>The "Next..." button of the last step added will read "Finish" (unless changed from default).</remarks>
  272. public void AddStep (WizardStep newStep)
  273. {
  274. steps.Add (newStep);
  275. this.Add (newStep);
  276. }
  277. /// <summary>
  278. /// The title of the Wizard, shown at the top of the Wizard with " - currentStep.Title" appended.
  279. /// </summary>
  280. public new ustring Title {
  281. get {
  282. // The base (Dialog) Title holds the full title ("Wizard Title - Step Title")
  283. return base.Title;
  284. }
  285. set {
  286. wizardTitle = value;
  287. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + steps [currentStep].Title : string.Empty)}";
  288. }
  289. }
  290. private ustring wizardTitle = ustring.Empty;
  291. /// <summary>
  292. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> transition events.
  293. /// </summary>
  294. public class WizardStepEventArgs : EventArgs {
  295. /// <summary>
  296. /// Set to true to cancel the transition to the next step.
  297. /// </summary>
  298. public bool Cancel { get; set; }
  299. /// <summary>
  300. /// Initializes a new instance of <see cref="WizardStepEventArgs"/>
  301. /// </summary>
  302. public WizardStepEventArgs ()
  303. {
  304. Cancel = false;
  305. }
  306. }
  307. /// <summary>
  308. /// This event is raised when the Back button in the <see cref="Wizard"/> is clicked. The Back button is always
  309. /// the first button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any.
  310. /// </summary>
  311. public event Action<WizardStepEventArgs> MovingBack;
  312. /// <summary>
  313. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  314. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  315. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  316. /// (otherwise the <see cref="Finished"/> event is raised).
  317. /// </summary>
  318. public event Action<WizardStepEventArgs> MovingNext;
  319. /// <summary>
  320. /// This event is raised when the Next/Finish button in the <see cref="Wizard"/> is clicked. The Next/Finish button is always
  321. /// the last button in the array of Buttons passed to the <see cref="Wizard"/> constructor, if any. This event is only
  322. /// raised if the <see cref="CurrentStep"/> is the last Step in the Wizard flow
  323. /// (otherwise the <see cref="Finished"/> event is raised).
  324. /// </summary>
  325. public event Action<WizardStepEventArgs> Finished;
  326. /// <summary>
  327. /// This event is raised when the current step )<see cref="CurrentStep"/>) in the <see cref="Wizard"/> changes.
  328. /// </summary>
  329. public event Action<CurrentStepChangedEventArgs> CurrentStepChanged;
  330. /// <summary>
  331. /// <see cref="EventArgs"/> for <see cref="WizardStep"/> events.
  332. /// </summary>
  333. public class CurrentStepChangedEventArgs : EventArgs {
  334. /// <summary>
  335. /// The new current <see cref="WizardStep"/>.
  336. /// </summary>
  337. public int CurrentStepIndex { get; }
  338. /// <summary>
  339. /// Initializes a new instance of <see cref="CurrentStepChangedEventArgs"/>
  340. /// </summary>
  341. /// <param name="currentStepIndex">The new current <see cref="WizardStep"/>.</param>
  342. public CurrentStepChangedEventArgs (int currentStepIndex)
  343. {
  344. CurrentStepIndex = currentStepIndex;
  345. }
  346. }
  347. /// <summary>
  348. /// Gets or sets the currently active <see cref="WizardStep"/>.
  349. /// </summary>
  350. public int CurrentStep {
  351. get => currentStep;
  352. set {
  353. currentStep = value;
  354. OnCurrentStepChanged ();
  355. }
  356. }
  357. /// <summary>
  358. /// Called when the current <see cref="WizardStep"/> has changed (<see cref="CurrentStep"/>).
  359. /// </summary>
  360. public virtual void OnCurrentStepChanged ()
  361. {
  362. CurrentStepChanged?.Invoke (new CurrentStepChangedEventArgs (currentStep));
  363. // Hide all but the first step
  364. foreach (WizardStep step in steps) {
  365. step.Visible = (steps [currentStep] == step);
  366. }
  367. // TODO: Add support for "Wizard Title - Step Title"
  368. base.Title = $"{wizardTitle}{(steps.Count > 0 ? " - " + steps [currentStep].Title : string.Empty)}";
  369. backBtn.Text = steps [currentStep].BackButtonText != ustring.Empty ? steps [currentStep].BackButtonText : "_Back";
  370. if (currentStep == 0) {
  371. backBtn.Visible = false;
  372. } else {
  373. backBtn.Visible = true;
  374. }
  375. if (currentStep == steps.Count - 1) {
  376. nextfinishBtn.Text = steps [currentStep].NextButtonText != ustring.Empty ? steps [currentStep].NextButtonText : "Fi_nish";
  377. } else {
  378. nextfinishBtn.Text = steps [currentStep].NextButtonText != ustring.Empty ? steps [currentStep].NextButtonText : "_Next...";
  379. }
  380. }
  381. }
  382. }