Margin.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>The Margin for a <see cref="View"/>. Accessed via <see cref="View.Margin"/></summary>
  4. /// <remarks>
  5. /// <para>See the <see cref="Adornment"/> class.</para>
  6. /// </remarks>
  7. public class Margin : Adornment
  8. {
  9. /// <inheritdoc/>
  10. public Margin ()
  11. { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  12. }
  13. /// <inheritdoc/>
  14. public Margin (View parent) : base (parent)
  15. {
  16. /* Do nothing; View.CreateAdornment requires a constructor that takes a parent */
  17. // BUGBUG: We should not set HighlightStyle.Pressed here, but wherever it is actually needed
  18. // HighlightStyle |= HighlightStyle.Pressed;
  19. Highlight += Margin_Highlight;
  20. LayoutStarted += Margin_LayoutStarted;
  21. // Margin should not be focusable
  22. CanFocus = false;
  23. }
  24. private bool _pressed;
  25. private ShadowView? _bottomShadow;
  26. private ShadowView? _rightShadow;
  27. /// <inheritdoc/>
  28. public override void BeginInit ()
  29. {
  30. base.BeginInit ();
  31. if (Parent is null)
  32. {
  33. return;
  34. }
  35. ShadowStyle = base.ShadowStyle;
  36. Add (
  37. _rightShadow = new ()
  38. {
  39. X = Pos.AnchorEnd (1),
  40. Y = 0,
  41. Width = 1,
  42. Height = Dim.Fill (),
  43. ShadowStyle = ShadowStyle,
  44. Orientation = Orientation.Vertical
  45. },
  46. _bottomShadow = new ()
  47. {
  48. X = 0,
  49. Y = Pos.AnchorEnd (1),
  50. Width = Dim.Fill (),
  51. Height = 1,
  52. ShadowStyle = ShadowStyle,
  53. Orientation = Orientation.Horizontal
  54. }
  55. );
  56. }
  57. /// <summary>
  58. /// The color scheme for the Margin. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>'s
  59. /// <see cref="View.SuperView"/> scheme. color scheme.
  60. /// </summary>
  61. public override ColorScheme? ColorScheme
  62. {
  63. get
  64. {
  65. if (base.ColorScheme is { })
  66. {
  67. return base.ColorScheme;
  68. }
  69. return (Parent?.SuperView?.ColorScheme ?? Colors.ColorSchemes ["TopLevel"])!;
  70. }
  71. set
  72. {
  73. base.ColorScheme = value;
  74. Parent?.SetNeedsDisplay ();
  75. }
  76. }
  77. /// <inheritdoc/>
  78. public override void OnDrawContent (Rectangle viewport)
  79. {
  80. if (!NeedsDisplay)
  81. {
  82. return;
  83. }
  84. Rectangle screen = ViewportToScreen (viewport);
  85. Attribute normalAttr = GetNormalColor ();
  86. Driver?.SetAttribute (normalAttr);
  87. if (ShadowStyle != ShadowStyle.None)
  88. {
  89. screen = Rectangle.Inflate (screen, -1, -1);
  90. }
  91. // This just draws/clears the thickness, not the insides.
  92. Thickness.Draw (screen, ToString ());
  93. if (Subviews.Count > 0)
  94. {
  95. // Draw subviews
  96. // TODO: Implement OnDrawSubviews (cancelable);
  97. if (Subviews is { } && SubViewNeedsDisplay)
  98. {
  99. IEnumerable<View> subviewsNeedingDraw = Subviews.Where (
  100. view => view.Visible
  101. && (view.NeedsDisplay || view.SubViewNeedsDisplay || view.LayoutNeeded)
  102. );
  103. foreach (View view in subviewsNeedingDraw)
  104. {
  105. if (view.LayoutNeeded)
  106. {
  107. view.LayoutSubviews ();
  108. }
  109. view.Draw ();
  110. }
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Sets whether the Margin includes a shadow effect. The shadow is drawn on the right and bottom sides of the
  116. /// Margin.
  117. /// </summary>
  118. public ShadowStyle SetShadow (ShadowStyle style)
  119. {
  120. if (ShadowStyle == style)
  121. {
  122. // return style;
  123. }
  124. if (ShadowStyle != ShadowStyle.None)
  125. {
  126. // Turn off shadow
  127. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right - 1, Thickness.Bottom - 1);
  128. }
  129. if (style != ShadowStyle.None)
  130. {
  131. // Turn on shadow
  132. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right + 1, Thickness.Bottom + 1);
  133. }
  134. if (_rightShadow is { })
  135. {
  136. _rightShadow.ShadowStyle = style;
  137. }
  138. if (_bottomShadow is { })
  139. {
  140. _bottomShadow.ShadowStyle = style;
  141. }
  142. return style;
  143. }
  144. /// <inheritdoc/>
  145. public override ShadowStyle ShadowStyle
  146. {
  147. get => base.ShadowStyle;
  148. set => base.ShadowStyle = SetShadow (value);
  149. }
  150. private const int PRESS_MOVE_HORIZONTAL = 1;
  151. private const int PRESS_MOVE_VERTICAL = 0;
  152. private void Margin_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
  153. {
  154. if (ShadowStyle != ShadowStyle.None)
  155. {
  156. if (_pressed && e.NewValue == HighlightStyle.None)
  157. {
  158. // If the view is pressed and the highlight is being removed, move the shadow back.
  159. // Note, for visual effects reasons, we only move horizontally.
  160. // TODO: Add a setting or flag that lets the view move vertically as well.
  161. Thickness = new (Thickness.Left - PRESS_MOVE_HORIZONTAL, Thickness.Top - PRESS_MOVE_VERTICAL, Thickness.Right + PRESS_MOVE_HORIZONTAL, Thickness.Bottom + PRESS_MOVE_VERTICAL);
  162. if (_rightShadow is { })
  163. {
  164. _rightShadow.Visible = true;
  165. }
  166. if (_bottomShadow is { })
  167. {
  168. _bottomShadow.Visible = true;
  169. }
  170. _pressed = false;
  171. return;
  172. }
  173. if (!_pressed && e.NewValue.HasFlag (HighlightStyle.Pressed))
  174. {
  175. // If the view is not pressed and we want highlight move the shadow
  176. // Note, for visual effects reasons, we only move horizontally.
  177. // TODO: Add a setting or flag that lets the view move vertically as well.
  178. Thickness = new (Thickness.Left + PRESS_MOVE_HORIZONTAL, Thickness.Top+ PRESS_MOVE_VERTICAL, Thickness.Right - PRESS_MOVE_HORIZONTAL, Thickness.Bottom - PRESS_MOVE_VERTICAL);
  179. _pressed = true;
  180. if (_rightShadow is { })
  181. {
  182. _rightShadow.Visible = false;
  183. }
  184. if (_bottomShadow is { })
  185. {
  186. _bottomShadow.Visible = false;
  187. }
  188. }
  189. }
  190. }
  191. private void Margin_LayoutStarted (object? sender, LayoutEventArgs e)
  192. {
  193. // Adjust the shadow such that it is drawn aligned with the Border
  194. if (_rightShadow is { } && _bottomShadow is { })
  195. {
  196. switch (ShadowStyle)
  197. {
  198. case ShadowStyle.Transparent:
  199. // BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
  200. _rightShadow.Y = Parent!.Border.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
  201. break;
  202. case ShadowStyle.Opaque:
  203. // BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
  204. _rightShadow.Y = Parent!.Border.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
  205. _bottomShadow.X = Parent.Border.Thickness.Left > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).X + 1 : 0;
  206. break;
  207. case ShadowStyle.None:
  208. default:
  209. _rightShadow.Y = 0;
  210. _bottomShadow.X = 0;
  211. break;
  212. }
  213. }
  214. }
  215. }