WizardStep.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 : FrameView
  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 () { 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. BorderStyle = LineStyle.None;
  44. base.Add (_contentView);
  45. _helpTextView.ReadOnly = true;
  46. _helpTextView.WordWrap = true;
  47. base.Add (_helpTextView);
  48. // BUGBUG: v2 - Disabling scrolling for now
  49. //var scrollBar = new ScrollBarView (helpTextView, true);
  50. //scrollBar.ChangedPosition += (s,e) => {
  51. // helpTextView.TopRow = scrollBar.Position;
  52. // if (helpTextView.TopRow != scrollBar.Position) {
  53. // scrollBar.Position = helpTextView.TopRow;
  54. // }
  55. // helpTextView.SetNeedsDisplay ();
  56. //};
  57. //scrollBar.OtherScrollBarView.ChangedPosition += (s,e) => {
  58. // helpTextView.LeftColumn = scrollBar.OtherScrollBarView.Position;
  59. // if (helpTextView.LeftColumn != scrollBar.OtherScrollBarView.Position) {
  60. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  61. // }
  62. // helpTextView.SetNeedsDisplay ();
  63. //};
  64. //scrollBar.VisibleChanged += (s,e) => {
  65. // if (scrollBar.Visible && helpTextView.RightOffset == 0) {
  66. // helpTextView.RightOffset = 1;
  67. // } else if (!scrollBar.Visible && helpTextView.RightOffset == 1) {
  68. // helpTextView.RightOffset = 0;
  69. // }
  70. //};
  71. //scrollBar.OtherScrollBarView.VisibleChanged += (s,e) => {
  72. // if (scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 0) {
  73. // helpTextView.BottomOffset = 1;
  74. // } else if (!scrollBar.OtherScrollBarView.Visible && helpTextView.BottomOffset == 1) {
  75. // helpTextView.BottomOffset = 0;
  76. // }
  77. //};
  78. //helpTextView.DrawContent += (s,e) => {
  79. // scrollBar.Size = helpTextView.Lines;
  80. // scrollBar.Position = helpTextView.TopRow;
  81. // if (scrollBar.OtherScrollBarView is { }) {
  82. // scrollBar.OtherScrollBarView.Size = helpTextView.Maxlength;
  83. // scrollBar.OtherScrollBarView.Position = helpTextView.LeftColumn;
  84. // }
  85. // scrollBar.LayoutSubviews ();
  86. // scrollBar.Refresh ();
  87. //};
  88. //base.Add (scrollBar);
  89. ShowHide ();
  90. }
  91. /// <summary>Sets or gets the text for the back button. The back button will only be visible on steps after the first step.</summary>
  92. /// <remarks>The default text is "Back"</remarks>
  93. public string BackButtonText { get; set; } = string.Empty;
  94. /// <summary>
  95. /// Sets or gets help text for the <see cref="WizardStep"/>.If <see cref="WizardStep.HelpText"/> is empty the help
  96. /// pane will not be visible and the content will fill the entire WizardStep.
  97. /// </summary>
  98. /// <remarks>The help text is displayed using a read-only <see cref="TextView"/>.</remarks>
  99. public string HelpText
  100. {
  101. get => _helpTextView.Text;
  102. set
  103. {
  104. _helpTextView.Text = value;
  105. ShowHide ();
  106. SetNeedsDisplay ();
  107. }
  108. }
  109. /// <summary>Sets or gets the text for the next/finish button.</summary>
  110. /// <remarks>The default text is "Next..." if the Pane is not the last pane. Otherwise it is "Finish"</remarks>
  111. public string NextButtonText { get; set; } = string.Empty;
  112. /// <summary>Add the specified <see cref="View"/> to the <see cref="WizardStep"/>.</summary>
  113. /// <param name="view"><see cref="View"/> to add to this container</param>
  114. public override View Add (View view)
  115. {
  116. _contentView.Add (view);
  117. if (view.CanFocus)
  118. {
  119. CanFocus = true;
  120. }
  121. ShowHide ();
  122. return view;
  123. }
  124. /// <summary>Removes a <see cref="View"/> from <see cref="WizardStep"/>.</summary>
  125. /// <remarks></remarks>
  126. public override View Remove (View view)
  127. {
  128. if (view is null)
  129. {
  130. return view;
  131. }
  132. SetNeedsDisplay ();
  133. View container = view?.SuperView;
  134. if (container == this)
  135. {
  136. base.Remove (view);
  137. }
  138. else
  139. {
  140. container?.Remove (view);
  141. }
  142. if (_contentView.InternalSubviews.Count < 1)
  143. {
  144. CanFocus = false;
  145. }
  146. ShowHide ();
  147. return view;
  148. }
  149. /// <summary>Removes all <see cref="View"/>s from the <see cref="WizardStep"/>.</summary>
  150. /// <remarks></remarks>
  151. public override void RemoveAll ()
  152. {
  153. _contentView.RemoveAll ();
  154. ShowHide ();
  155. }
  156. /// <summary>Does the work to show and hide the contentView and helpView as appropriate</summary>
  157. internal void ShowHide ()
  158. {
  159. _contentView.Height = Dim.Fill ();
  160. _helpTextView.Height = Dim.Fill ();
  161. _helpTextView.Width = Dim.Fill ();
  162. if (_contentView.InternalSubviews?.Count > 0)
  163. {
  164. if (_helpTextView.Text.Length > 0)
  165. {
  166. _contentView.Width = Dim.Percent (70);
  167. _helpTextView.X = Pos.Right (_contentView);
  168. _helpTextView.Width = Dim.Fill ();
  169. }
  170. else
  171. {
  172. _contentView.Width = Dim.Fill ();
  173. }
  174. }
  175. else
  176. {
  177. if (_helpTextView.Text.Length > 0)
  178. {
  179. _helpTextView.X = 0;
  180. }
  181. // Error - no pane shown
  182. }
  183. _contentView.Visible = _contentView.InternalSubviews?.Count > 0;
  184. _helpTextView.Visible = _helpTextView.Text.Length > 0;
  185. }
  186. } // end of WizardStep class