Margin.cs 7.6 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. SubviewLayout += 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. }
  37. /// <summary>
  38. /// The color scheme for the Margin. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>'s
  39. /// <see cref="View.SuperView"/> scheme. color scheme.
  40. /// </summary>
  41. public override ColorScheme? ColorScheme
  42. {
  43. get
  44. {
  45. if (base.ColorScheme is { })
  46. {
  47. return base.ColorScheme;
  48. }
  49. return (Parent?.SuperView?.ColorScheme ?? Colors.ColorSchemes ["TopLevel"])!;
  50. }
  51. set
  52. {
  53. base.ColorScheme = value;
  54. Parent?.SetNeedsDraw ();
  55. }
  56. }
  57. /// <inheritdoc />
  58. protected override bool OnClearingViewport (Rectangle viewport)
  59. {
  60. if (Thickness == Thickness.Empty)
  61. {
  62. return true;
  63. }
  64. Rectangle screen = ViewportToScreen (viewport);
  65. if (ShadowStyle != ShadowStyle.None)
  66. {
  67. // Don't clear where the shadow goes
  68. screen = Rectangle.Inflate (screen, -1, -1);
  69. }
  70. // This just draws/clears the thickness, not the insides.
  71. Thickness.Draw (screen, Diagnostics, ToString ());
  72. return true;
  73. }
  74. ///// <inheritdoc />
  75. ////protected override bool OnDrawSubviews (Rectangle viewport) { return true; }
  76. //protected override bool OnDrawComplete (Rectangle viewport)
  77. //{
  78. // DoDrawSubviews (viewport);
  79. // return true;
  80. //}
  81. /// <summary>
  82. /// Sets whether the Margin includes a shadow effect. The shadow is drawn on the right and bottom sides of the
  83. /// Margin.
  84. /// </summary>
  85. public ShadowStyle SetShadow (ShadowStyle style)
  86. {
  87. if (_rightShadow is { })
  88. {
  89. Remove (_rightShadow);
  90. _rightShadow.Dispose ();
  91. _rightShadow = null;
  92. }
  93. if (_bottomShadow is { })
  94. {
  95. Remove (_bottomShadow);
  96. _bottomShadow.Dispose ();
  97. _bottomShadow = null;
  98. }
  99. if (ShadowStyle != ShadowStyle.None)
  100. {
  101. // Turn off shadow
  102. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right - 1, Thickness.Bottom - 1);
  103. }
  104. if (style != ShadowStyle.None)
  105. {
  106. // Turn on shadow
  107. Thickness = new (Thickness.Left, Thickness.Top, Thickness.Right + 1, Thickness.Bottom + 1);
  108. }
  109. if (style != ShadowStyle.None)
  110. {
  111. _rightShadow = new ()
  112. {
  113. X = Pos.AnchorEnd (1),
  114. Y = 0,
  115. Width = 1,
  116. Height = Dim.Fill (),
  117. ShadowStyle = style,
  118. Orientation = Orientation.Vertical
  119. };
  120. _bottomShadow = new ()
  121. {
  122. X = 0,
  123. Y = Pos.AnchorEnd (1),
  124. Width = Dim.Fill (),
  125. Height = 1,
  126. ShadowStyle = style,
  127. Orientation = Orientation.Horizontal
  128. };
  129. Add (_rightShadow, _bottomShadow);
  130. }
  131. return style;
  132. }
  133. /// <inheritdoc/>
  134. public override ShadowStyle ShadowStyle
  135. {
  136. get => base.ShadowStyle;
  137. set
  138. {
  139. base.ShadowStyle = SetShadow (value);
  140. }
  141. }
  142. private const int PRESS_MOVE_HORIZONTAL = 1;
  143. private const int PRESS_MOVE_VERTICAL = 0;
  144. private void Margin_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
  145. {
  146. if (Thickness == Thickness.Empty || ShadowStyle == ShadowStyle.None)
  147. {
  148. return;
  149. }
  150. if (_pressed && e.NewValue == HighlightStyle.None)
  151. {
  152. // If the view is pressed and the highlight is being removed, move the shadow back.
  153. // Note, for visual effects reasons, we only move horizontally.
  154. // TODO: Add a setting or flag that lets the view move vertically as well.
  155. Thickness = new (Thickness.Left - PRESS_MOVE_HORIZONTAL, Thickness.Top - PRESS_MOVE_VERTICAL, Thickness.Right + PRESS_MOVE_HORIZONTAL, Thickness.Bottom + PRESS_MOVE_VERTICAL);
  156. if (_rightShadow is { })
  157. {
  158. _rightShadow.Visible = true;
  159. }
  160. if (_bottomShadow is { })
  161. {
  162. _bottomShadow.Visible = true;
  163. }
  164. _pressed = false;
  165. return;
  166. }
  167. if (!_pressed && e.NewValue.HasFlag (HighlightStyle.Pressed))
  168. {
  169. // If the view is not pressed and we want highlight move the shadow
  170. // Note, for visual effects reasons, we only move horizontally.
  171. // TODO: Add a setting or flag that lets the view move vertically as well.
  172. Thickness = new (Thickness.Left + PRESS_MOVE_HORIZONTAL, Thickness.Top + PRESS_MOVE_VERTICAL, Thickness.Right - PRESS_MOVE_HORIZONTAL, Thickness.Bottom - PRESS_MOVE_VERTICAL);
  173. _pressed = true;
  174. if (_rightShadow is { })
  175. {
  176. _rightShadow.Visible = false;
  177. }
  178. if (_bottomShadow is { })
  179. {
  180. _bottomShadow.Visible = false;
  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. }