ExpanderButton.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #nullable enable
  2. using System;
  3. using System.Text;
  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="Orientation.Vertical"/>, the button will appear
  15. /// at the top/right.
  16. /// If <see cref="Orientation"/> is set to <see cref="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.Toggle, Toggle);
  38. KeyBindings.Add (Key.F4, Command.Toggle);
  39. Orientation = Orientation.Vertical;
  40. HighlightStates = Terminal.Gui.ViewBase.MouseState.None;
  41. Initialized += ExpanderButton_Initialized;
  42. EnabledChanged += (sender, args) =>
  43. {
  44. ShowHide ();
  45. };
  46. }
  47. private void ShowHide ()
  48. {
  49. if (!Enabled)
  50. {
  51. Visible = false;
  52. }
  53. if (SuperView is Border { } border)
  54. {
  55. switch (Orientation)
  56. {
  57. case Orientation.Vertical:
  58. Visible = border.Thickness.Top > 0;
  59. break;
  60. case Orientation.Horizontal:
  61. Visible = border.Border?.Thickness.Left > 0;
  62. break;
  63. }
  64. }
  65. }
  66. private void ExpanderButton_Initialized (object? sender, EventArgs e)
  67. {
  68. ShadowStyle = ShadowStyle.None;
  69. ExpandOrCollapse (Collapsed);
  70. if (SuperView is Border { } border)
  71. {
  72. border.ThicknessChanged += (o, args) => ShowHide ();
  73. }
  74. }
  75. private Orientation _orientation = Orientation.Horizontal;
  76. /// <summary>Orientation.</summary>
  77. /// <remarks>
  78. /// <para>
  79. /// If <see cref="Orientation"/> is set to <see cref="Orientation.Vertical"/>, the button will appear at the
  80. /// top/right.
  81. /// If <see cref="Orientation"/> is set to <see cref="Orientation.Horizontal"/>, the button will appear at the
  82. /// bottom/left.
  83. /// </para>
  84. /// </remarks>
  85. public Orientation Orientation
  86. {
  87. get => _orientation;
  88. set => OnOrientationChanging (value);
  89. }
  90. /// <summary>Called when the orientation is changing. Invokes the <see cref="OrientationChanging"/> event.</summary>
  91. /// <param name="newOrientation"></param>
  92. /// <returns>True of the event was cancelled.</returns>
  93. protected virtual bool OnOrientationChanging (Orientation newOrientation)
  94. {
  95. CancelEventArgs<Orientation> args = new CancelEventArgs<Orientation> (in _orientation, ref newOrientation);
  96. OrientationChanging?.Invoke (this, args);
  97. if (!args.Cancel)
  98. {
  99. _orientation = newOrientation;
  100. if (Orientation == Orientation.Vertical)
  101. {
  102. X = Pos.AnchorEnd ();
  103. Y = 0;
  104. CollapseGlyph = new ('\u21d1'); // ⇑
  105. ExpandGlyph = new ('\u21d3'); // ⇓
  106. }
  107. else
  108. {
  109. X = 0;
  110. Y = Pos.AnchorEnd ();
  111. CollapseGlyph = new ('\u21d0'); // ⇐
  112. ExpandGlyph = new ('\u21d2'); // ⇒
  113. }
  114. ExpandOrCollapse (Collapsed);
  115. }
  116. return args.Cancel;
  117. }
  118. /// <summary>
  119. /// Fired when the orientation has changed. Can be cancelled.
  120. /// </summary>
  121. public event EventHandler<CancelEventArgs<Orientation>>? OrientationChanging;
  122. /// <summary>
  123. /// The glyph that indicates the button will collapse the view.
  124. /// </summary>
  125. public Rune CollapseGlyph { get; set; }
  126. /// <summary>
  127. /// The glyph that indicates the button will expand the view.
  128. /// </summary>
  129. public Rune ExpandGlyph { get; set; }
  130. private bool _collapsed;
  131. /// <summary>
  132. /// Gets or sets a value indicating whether the view is collapsed.
  133. /// </summary>
  134. public bool Collapsed
  135. {
  136. get => _collapsed;
  137. set => OnCollapsedChanging (value);
  138. }
  139. /// <summary>Called when the orientation is changing. Invokes the <see cref="OrientationChanging"/> event.</summary>
  140. /// <param name="newValue"></param>
  141. /// <returns>True of the event was cancelled.</returns>
  142. protected virtual bool OnCollapsedChanging (bool newValue)
  143. {
  144. CancelEventArgs<bool> args = new (ref _collapsed, ref newValue);
  145. CollapsedChanging?.Invoke (this, args);
  146. if (!args.Cancel)
  147. {
  148. _collapsed = args.NewValue;
  149. ExpandOrCollapse (_collapsed);
  150. }
  151. return args.Cancel;
  152. }
  153. /// <summary>
  154. /// Fired when the orientation has changed. Can be cancelled.
  155. /// </summary>
  156. public event EventHandler<CancelEventArgs<bool>>? CollapsedChanging;
  157. /// <summary>
  158. /// Collapses or Expands the view.
  159. /// </summary>
  160. /// <returns></returns>
  161. public bool? Toggle ()
  162. {
  163. Collapsed = !Collapsed;
  164. return true;
  165. }
  166. private Dim? _previousDim;
  167. private void ExpandOrCollapse (bool collapse)
  168. {
  169. Text = $"{(Collapsed ? ExpandGlyph : CollapseGlyph)}";
  170. View? superView = SuperView;
  171. if (superView is Adornment adornment)
  172. {
  173. superView = adornment.Parent;
  174. }
  175. if (superView is null)
  176. {
  177. return;
  178. }
  179. if (collapse)
  180. {
  181. // Collapse
  182. if (Orientation == Orientation.Vertical)
  183. {
  184. _previousDim = superView!.Height!;
  185. superView.Height = 1;
  186. }
  187. else
  188. {
  189. _previousDim = superView!.Width!;
  190. superView.Width = 1;
  191. }
  192. }
  193. else
  194. {
  195. if (_previousDim is null)
  196. {
  197. return;
  198. }
  199. // Expand
  200. if (Orientation == Orientation.Vertical)
  201. {
  202. superView.Height = _previousDim;
  203. }
  204. else
  205. {
  206. superView.Width = _previousDim;
  207. }
  208. }
  209. foreach (View subview in superView.SubViews)
  210. {
  211. subview.Visible = !Collapsed;
  212. }
  213. }
  214. }