Margin.cs 7.7 KB

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