WizardStep.cs 7.4 KB

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