Margin.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. Rectangle screen = ViewportToScreen (viewport);
  81. Attribute normalAttr = GetNormalColor ();
  82. Driver?.SetAttribute (normalAttr);
  83. // This just draws/clears the thickness, not the insides.
  84. if (ShadowStyle != ShadowStyle.None)
  85. {
  86. screen = Rectangle.Inflate (screen, -1, -1);
  87. }
  88. Thickness.Draw (screen, ToString ());
  89. if (Subviews.Count > 0)
  90. {
  91. // Draw subviews
  92. // TODO: Implement OnDrawSubviews (cancelable);
  93. if (Subviews is { } && SubViewNeedsDisplay)
  94. {
  95. IEnumerable<View> subviewsNeedingDraw = Subviews.Where (
  96. view => view.Visible
  97. && (view.NeedsDisplay || view.SubViewNeedsDisplay || view.LayoutNeeded)
  98. );
  99. foreach (View view in subviewsNeedingDraw)
  100. {
  101. if (view.LayoutNeeded)
  102. {
  103. view.LayoutSubviews ();
  104. }
  105. view.Draw ();
  106. }
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Sets whether the Margin includes a shadow effect. The shadow is drawn on the right and bottom sides of the
  112. /// Margin.
  113. /// </summary>
  114. public ShadowStyle SetShadow (ShadowStyle style)
  115. {
  116. if (ShadowStyle == style)
  117. {
  118. // return style;
  119. }
  120. if (ShadowStyle != ShadowStyle.None)
  121. {
  122. // Turn off shadow
  123. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right - 1, Thickness.Bottom - 1);
  124. }
  125. if (style != ShadowStyle.None)
  126. {
  127. // Turn on shadow
  128. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right + 1, Thickness.Bottom + 1);
  129. }
  130. if (_rightShadow is { })
  131. {
  132. _rightShadow.ShadowStyle = style;
  133. }
  134. if (_bottomShadow is { })
  135. {
  136. _bottomShadow.ShadowStyle = style;
  137. }
  138. return style;
  139. }
  140. /// <inheritdoc/>
  141. public override ShadowStyle ShadowStyle
  142. {
  143. get => base.ShadowStyle;
  144. set => base.ShadowStyle = SetShadow (value);
  145. }
  146. private const int PRESS_MOVE_HORIZONTAL = 1;
  147. private const int PRESS_MOVE_VERTICAL = 0;
  148. private void Margin_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
  149. {
  150. if (ShadowStyle != ShadowStyle.None)
  151. {
  152. if (_pressed && e.NewValue == HighlightStyle.None)
  153. {
  154. // If the view is pressed and the highlight is being removed, move the shadow back.
  155. // Note, for visual effects reasons, we only move horizontally.
  156. // TODO: Add a setting or flag that lets the view move vertically as well.
  157. Thickness = new (Thickness.Left - PRESS_MOVE_HORIZONTAL, Thickness.Top - PRESS_MOVE_VERTICAL, Thickness.Right + PRESS_MOVE_HORIZONTAL, Thickness.Bottom + PRESS_MOVE_VERTICAL);
  158. if (_rightShadow is { })
  159. {
  160. _rightShadow.Visible = true;
  161. }
  162. if (_bottomShadow is { })
  163. {
  164. _bottomShadow.Visible = true;
  165. }
  166. _pressed = false;
  167. return;
  168. }
  169. if (!_pressed && e.NewValue.HasFlag (HighlightStyle.Pressed))
  170. {
  171. // If the view is not pressed and we want highlight move the shadow
  172. // Note, for visual effects reasons, we only move horizontally.
  173. // TODO: Add a setting or flag that lets the view move vertically as well.
  174. Thickness = new (Thickness.Left + PRESS_MOVE_HORIZONTAL, Thickness.Top+ PRESS_MOVE_VERTICAL, Thickness.Right - PRESS_MOVE_HORIZONTAL, Thickness.Bottom - PRESS_MOVE_VERTICAL);
  175. _pressed = true;
  176. if (_rightShadow is { })
  177. {
  178. _rightShadow.Visible = false;
  179. }
  180. if (_bottomShadow is { })
  181. {
  182. _bottomShadow.Visible = false;
  183. }
  184. }
  185. }
  186. }
  187. private void Margin_LayoutStarted (object? sender, LayoutEventArgs e)
  188. {
  189. // Adjust the shadow such that it is drawn aligned with the Border
  190. if (_rightShadow is { } && _bottomShadow is { })
  191. {
  192. switch (ShadowStyle)
  193. {
  194. case ShadowStyle.Transparent:
  195. // BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
  196. _rightShadow.Y = Parent!.Border.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
  197. break;
  198. case ShadowStyle.Opaque:
  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. _bottomShadow.X = Parent.Border.Thickness.Left > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).X + 1 : 0;
  202. break;
  203. case ShadowStyle.None:
  204. default:
  205. _rightShadow.Y = 0;
  206. _bottomShadow.X = 0;
  207. break;
  208. }
  209. }
  210. }
  211. }