WizardStep.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Represents a basic step that is displayed in a <see cref="Wizard"/>. The <see cref="WizardStep"/> view is
  4. /// divided horizontally in two. On the left is the content view where <see cref="View"/>s can be added, On the right
  5. /// is the help for the step. Set <see cref="WizardStep.HelpText"/> to set the help text. If the help text is empty the
  6. /// help pane will not be shown. If there are no Views added to the WizardStep the <see cref="HelpText"/> (if not
  7. /// empty) will fill the wizard step.
  8. /// </summary>
  9. /// <remarks>
  10. /// If <see cref="Button"/>s are added, do not set <see cref="Button.IsDefault"/> to true as this will conflict
  11. /// with the Next button of the Wizard. Subscribe to the <see cref="View.VisibleChanged"/> event to be notified when
  12. /// the step is active; see also: <see cref="Wizard.StepChanged"/>. To enable or disable a step from being shown to the
  13. /// user, set <see cref="View.Enabled"/>.
  14. /// </remarks>
  15. public class WizardStep : View
  16. {
  17. ///// <summary>
  18. ///// The title of the <see cref="WizardStep"/>.
  19. ///// </summary>
  20. ///// <remarks>The Title is only displayed when the <see cref="Wizard"/> is used as a modal pop-up (see <see cref="Wizard.Modal"/>.</remarks>
  21. //public new string Title {
  22. // // BUGBUG: v2 - No need for this as View now has Title w/ notifications.
  23. // get => title;
  24. // set {
  25. // if (!OnTitleChanging (title, value)) {
  26. // var old = title;
  27. // title = value;
  28. // OnTitleChanged (old, title);
  29. // }
  30. // base.Title = value;
  31. // SetNeedsDisplay ();
  32. // }
  33. //}
  34. //private string title = string.Empty;
  35. // The contentView works like the ContentView in FrameView.
  36. private readonly View _contentView = new () { CanFocus = true, TabStop = TabBehavior.TabStop, Id = "WizardContentView" };
  37. private readonly TextView _helpTextView = new ();
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="Wizard"/> class.
  40. /// </summary>
  41. public WizardStep ()
  42. {
  43. TabStop = TabBehavior.TabStop;
  44. CanFocus = true;
  45. BorderStyle = LineStyle.None;
  46. base.Add (_contentView);
  47. _helpTextView.CanFocus = true;
  48. _helpTextView.TabStop = TabBehavior.NoStop;
  49. _helpTextView.ReadOnly = true;
  50. _helpTextView.WordWrap = true;
  51. base.Add (_helpTextView);
  52. // BUGBUG: v2 - Disabling scrolling for now
  53. //var scrollBar = new ScrollBarView (helpTextView, true);
  54. //scrollBar.ChangedPosition += (s,e) => {
  55. // helpTextView.TopRow = scrollBar.Position;
  56. // if (helpTextView.TopRow != scrollBar.Position) {
  57. // scrollBar.Position = helpTextView.TopRow;
  58. // }
  59. // helpTextView.SetNeedsDisplay ();
  60. //};
  61. //scrollBar.OtherScrollBarView.ChangedPosition += (s,e) => {
  62. // helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  63. // if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  64. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  65. // }
  66. // helpTextView.SetNeedsDisplay ();
  67. //};
  68. //scrollBar.VisibleChanged += (s,e) => {
  69. // if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  70. // helpTextView.RightOffset = 1;
  71. // } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  72. // helpTextView.RightOffset = 0;
  73. // }
  74. //};
  75. //scrollBar.OtherScrollBarView.VisibleChanged += (s,e) => {
  76. // if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  77. // helpTextView.BottomOffset = 1;
  78. // } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  79. // helpTextView.BottomOffset = 0;
  80. // }
  81. //};
  82. //helpTextView.DrawContent += (s,e) => {
  83. // scrollBar.Size = helpTextView.Lines;
  84. // scrollBar.Position = helpTextView.TopRow;
  85. // if (scrollBar.OtherScrollBarView is { }) {
  86. // scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  87. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  88. // }
  89. // scrollBar.LayoutSubviews ();
  90. // scrollBar.Refresh ();
  91. //};
  92. //base.Add (scrollBar);
  93. ShowHide ();
  94. }
  95. /// <summary>Sets or gets the text for the back button. The back button will only be visible on steps after the first step.</summary>
  96. /// <remarks>The default text is "Back"</remarks>
  97. public string BackButtonText { get; set; } = string.Empty;
  98. /// <summary>
  99. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.HelpText"/> is empty the help
  100. /// pane will not be visible and the content will fill the entire WizardStep.
  101. /// </summary>
  102. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  103. public string HelpText
  104. {
  105. get => _helpTextView.Text;
  106. set
  107. {
  108. _helpTextView.Text = value;
  109. ShowHide ();
  110. SetNeedsDisplay ();
  111. }
  112. }
  113. /// <summary>Sets or gets the text for the next/finish button.</summary>
  114. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  115. public string NextButtonText { get; set; } = string.Empty;
  116. /// <summary>Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.</summary>
  117. /// <param name="view"><see cref="View"/> to add to this container</param>
  118. public override View Add (View view)
  119. {
  120. _contentView.Add (view);
  121. if (view.CanFocus)
  122. {
  123. CanFocus = true;
  124. }
  125. ShowHide ();
  126. return view;
  127. }
  128. /// <summary>Removes a <see cref="View"/> from <see cref="WizardStep"/>.</summary>
  129. /// <remarks></remarks>
  130. public override View Remove (View view)
  131. {
  132. if (view is null)
  133. {
  134. return view;
  135. }
  136. SetNeedsDisplay ();
  137. View container = view?.SuperView;
  138. if (container == this)
  139. {
  140. base.Remove (view);
  141. }
  142. else
  143. {
  144. container?.Remove (view);
  145. }
  146. if (_contentView.InternalSubviews.Count < 1)
  147. {
  148. CanFocus = false;
  149. }
  150. ShowHide ();
  151. return view;
  152. }
  153. /// <summary>Removes all <see cref="View"/>s from the <see cref="WizardStep"/>.</summary>
  154. /// <remarks></remarks>
  155. public override void RemoveAll ()
  156. {
  157. _contentView.RemoveAll ();
  158. ShowHide ();
  159. }
  160. /// <summary>Does the work to show and hide the contentView and helpView as appropriate</summary>
  161. internal void ShowHide ()
  162. {
  163. _contentView.Height = Dim.Fill ();
  164. _helpTextView.Height = Dim.Fill ();
  165. _helpTextView.Width = Dim.Fill ();
  166. if (_contentView.InternalSubviews?.Count > 0)
  167. {
  168. if (_helpTextView.Text.Length > 0)
  169. {
  170. _contentView.Width = Dim.Percent (70);
  171. _helpTextView.X = Pos.Right (_contentView);
  172. _helpTextView.Width = Dim.Fill ();
  173. }
  174. else
  175. {
  176. _contentView.Width = Dim.Fill ();
  177. }
  178. }
  179. else
  180. {
  181. if (_helpTextView.Text.Length > 0)
  182. {
  183. _helpTextView.X = 0;
  184. }
  185. // Error - no pane shown
  186. }
  187. _contentView.Visible = _contentView.InternalSubviews?.Count > 0;
  188. _helpTextView.Visible = _helpTextView.Text.Length > 0;
  189. }
  190. } // end of WizardStep class