ExpanderButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Text;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. /// <summary>
  6. /// A Button that can expand or collapse a view.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// Add this button to a view's Border to allow the user to expand or collapse the view via either the keyboard
  11. /// (F4) or mouse.
  12. /// </para>
  13. /// <para>
  14. /// If <see cref="Orientation"/> is set to <see cref="Terminal.Gui.Orientation.Vertical"/>, the button will appear
  15. /// at the top/right.
  16. /// If <see cref="Orientation"/> is set to <see cref="Terminal.Gui.Orientation.Horizontal"/>, the button will
  17. /// appear at the
  18. /// bottom/left.
  19. /// </para>
  20. /// </remarks>
  21. /// <example>
  22. /// private void MyView_Initialized (object sender, EventArgs e)
  23. /// {
  24. /// Border.Add(new ExpanderButton ());
  25. /// ...
  26. /// </example>
  27. public class ExpanderButton : Button
  28. {
  29. public ExpanderButton ()
  30. {
  31. CanFocus = false;
  32. Width = 1;
  33. Height = 1;
  34. NoDecorations = true;
  35. NoPadding = true;
  36. AddCommand (Command.HotKey, Toggle);
  37. AddCommand (Command.ToggleExpandCollapse, Toggle);
  38. KeyBindings.Add (Key.F4, Command.ToggleExpandCollapse);
  39. Orientation = Orientation.Vertical;
  40. Initialized += ExpanderButton_Initialized;
  41. }
  42. private void ExpanderButton_Initialized (object sender, EventArgs e)
  43. {
  44. ExpandOrCollapse (Collapsed);
  45. }
  46. private Orientation _orientation = Orientation.Horizontal;
  47. /// <summary>Orientation.</summary>
  48. /// <remarks>
  49. /// <para>
  50. /// If <see cref="Orientation"/> is set to <see cref="Orientation.Vertical"/>, the button will appear at the
  51. /// top/right.
  52. /// If <see cref="Orientation"/> is set to <see cref="Orientation.Horizontal"/>, the button will appear at the
  53. /// bottom/left.
  54. /// </para>
  55. /// </remarks>
  56. public Orientation Orientation
  57. {
  58. get => _orientation;
  59. set => OnOrientationChanging (value);
  60. }
  61. /// <summary>Called when the orientation is changing. Invokes the <see cref="OrientationChanging"/> event.</summary>
  62. /// <param name="newOrientation"></param>
  63. /// <returns>True of the event was cancelled.</returns>
  64. protected virtual bool OnOrientationChanging (Orientation newOrientation)
  65. {
  66. var args = new OrientationEventArgs (newOrientation);
  67. OrientationChanging?.Invoke (this, args);
  68. if (!args.Cancel)
  69. {
  70. _orientation = newOrientation;
  71. if (Orientation == Orientation.Vertical)
  72. {
  73. X = Pos.AnchorEnd ();
  74. Y = 0;
  75. CollapsedGlyph = new ('\u21d1'); // ⇑
  76. ExpandedGlyph = new ('\u21d3'); // ⇓
  77. }
  78. else
  79. {
  80. X = 0;
  81. Y = Pos.AnchorEnd ();
  82. CollapsedGlyph = new ('\u21d0'); // ⇐
  83. ExpandedGlyph = new ('\u21d2'); // ⇒
  84. }
  85. Text = $"{(Collapsed ? CollapsedGlyph : ExpandedGlyph)}";
  86. ExpandOrCollapse (Collapsed);
  87. }
  88. return args.Cancel;
  89. }
  90. /// <summary>
  91. /// Fired when the orientation has changed. Can be cancelled by setting
  92. /// <see cref="OrientationEventArgs.Cancel"/> to true.
  93. /// </summary>
  94. public event EventHandler<OrientationEventArgs> OrientationChanging;
  95. /// <summary>
  96. /// The glyph to display when the view is collapsed.
  97. /// </summary>
  98. public Rune CollapsedGlyph { get; set; }
  99. /// <summary>
  100. /// The glyph to display when the view is expanded.
  101. /// </summary>
  102. public Rune ExpandedGlyph { get; set; }
  103. private bool _collapsed;
  104. /// <summary>
  105. /// Gets or sets a value indicating whether the view is collapsed.
  106. /// </summary>
  107. public bool Collapsed
  108. {
  109. get => _collapsed;
  110. set => OnCollapsedChanging (value);
  111. }
  112. /// <summary>Called when the orientation is changing. Invokes the <see cref="OrientationChanging"/> event.</summary>
  113. /// <param name="newOrientation"></param>
  114. /// <returns>True of the event was cancelled.</returns>
  115. protected virtual bool OnCollapsedChanging (bool newValue)
  116. {
  117. StateEventArgs<bool> args = new (Collapsed, newValue);
  118. CollapsedChanging?.Invoke (this, args);
  119. if (!args.Cancel)
  120. {
  121. _collapsed = newValue;
  122. ExpandOrCollapse (_collapsed);
  123. View superView = SuperView;
  124. if (superView is Adornment adornment)
  125. {
  126. superView = adornment.Parent;
  127. }
  128. foreach (View subview in superView.Subviews)
  129. {
  130. subview.Visible = !Collapsed;
  131. subview.Enabled = !Collapsed;
  132. }
  133. // BUGBUG: This should not be needed. There's some bug in the layout system that doesn't update the layout.
  134. superView.SuperView?.LayoutSubviews ();
  135. }
  136. return args.Cancel;
  137. }
  138. /// <summary>
  139. /// Fired when the orientation has changed. Can be cancelled by setting
  140. /// <see cref="OrientationEventArgs.Cancel"/> to true.
  141. /// </summary>
  142. public event EventHandler<StateEventArgs<bool>> CollapsedChanging;
  143. /// <summary>
  144. /// Collapses or Expands the view.
  145. /// </summary>
  146. /// <returns></returns>
  147. public bool? Toggle ()
  148. {
  149. Collapsed = !Collapsed;
  150. return true;
  151. }
  152. private Dim _previousDim;
  153. private void ExpandOrCollapse (bool collapse)
  154. {
  155. View superView = SuperView;
  156. if (superView is Adornment adornment)
  157. {
  158. superView = adornment.Parent;
  159. }
  160. if (superView is null)
  161. {
  162. return;
  163. }
  164. if (collapse)
  165. {
  166. // Collapse
  167. if (Orientation == Orientation.Vertical)
  168. {
  169. _previousDim = superView.Height;
  170. superView.Height = 1;
  171. }
  172. else
  173. {
  174. _previousDim = superView.Width;
  175. superView.Width = 1;
  176. }
  177. }
  178. else
  179. {
  180. if (_previousDim is null)
  181. {
  182. return;
  183. }
  184. // Expand
  185. if (Orientation == Orientation.Vertical)
  186. {
  187. superView.Height = _previousDim;
  188. }
  189. else
  190. {
  191. superView.Width = _previousDim;
  192. }
  193. }
  194. }
  195. }