WizardStep.cs 6.9 KB

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