ExpanderButton.cs 6.5 KB

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