ExpanderButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. ShadowStyle = ShadowStyle.None;
  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. 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. /// <param name="newValue"></param>
  115. /// <returns>True of the event was cancelled.</returns>
  116. protected virtual bool OnCollapsedChanging (bool newValue)
  117. {
  118. CancelEventArgs<bool> args = new (ref _collapsed, ref newValue);
  119. CollapsedChanging?.Invoke (this, args);
  120. if (!args.Cancel)
  121. {
  122. _collapsed = args.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. }
  135. return args.Cancel;
  136. }
  137. /// <summary>
  138. /// Fired when the orientation has changed. Can be cancelled by setting
  139. /// <see cref="OrientationEventArgs.Cancel"/> to true.
  140. /// </summary>
  141. public event EventHandler<CancelEventArgs<bool>> CollapsedChanging;
  142. /// <summary>
  143. /// Collapses or Expands the view.
  144. /// </summary>
  145. /// <returns></returns>
  146. public bool? Toggle ()
  147. {
  148. Collapsed = !Collapsed;
  149. return true;
  150. }
  151. private Dim _previousDim;
  152. private void ExpandOrCollapse (bool collapse)
  153. {
  154. Text = $"{(Collapsed ? CollapsedGlyph : ExpandedGlyph)}";
  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. }