Margin.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>The Margin for a <see cref="View"/>. Accessed via <see cref="View.Margin"/></summary>
  3. /// <remarks>
  4. /// <para>
  5. /// The Margin is transparent by default. This can be overriden by explicitly setting <see cref="Scheme"/>.
  6. /// </para>
  7. /// <para>
  8. /// Margins are drawn after all other Views in the application View hierarchy are drawn.
  9. /// </para>
  10. /// <para>
  11. /// Margins have <see cref="ViewportSettingsFlags.TransparentMouse"/> enabled by default and are thus
  12. /// transparent to the mouse. This can be overridden by explicitly setting <see cref="ViewportSettingsFlags"/>.
  13. /// </para>
  14. /// <para>See the <see cref="Adornment"/> class.</para>
  15. /// </remarks>
  16. public class Margin : Adornment
  17. {
  18. private const int PRESS_MOVE_HORIZONTAL = 1;
  19. private const int PRESS_MOVE_VERTICAL = 0;
  20. /// <inheritdoc/>
  21. public Margin ()
  22. { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  23. }
  24. /// <inheritdoc/>
  25. public Margin (View parent) : base (parent)
  26. {
  27. SubViewLayout += Margin_LayoutStarted;
  28. ThicknessChanged += OnThicknessChanged;
  29. // Margin should not be focusable
  30. CanFocus = false;
  31. // Margins are transparent by default
  32. ViewportSettings |= ViewportSettingsFlags.Transparent;
  33. // Margins are transparent to mouse by default
  34. ViewportSettings |= ViewportSettingsFlags.TransparentMouse;
  35. }
  36. private void OnThicknessChanged (object? sender, EventArgs e)
  37. {
  38. if (!_isThicknessChanging)
  39. {
  40. _originalThickness = new (Thickness.Left, Thickness.Top, Thickness.Right, Thickness.Bottom);
  41. SetShadow (ShadowStyle);
  42. }
  43. }
  44. // When the Parent is drawn, we cache the clip region so we can draw the Margin after all other Views
  45. // QUESTION: Why can't this just be the NeedsDisplay region?
  46. private Region? _cachedClip;
  47. internal Region? GetCachedClip () { return _cachedClip; }
  48. internal void ClearCachedClip () { _cachedClip = null; }
  49. internal void CacheClip ()
  50. {
  51. if (Thickness != Thickness.Empty && ShadowStyle != ShadowStyle.None)
  52. {
  53. // PERFORMANCE: How expensive are these clones?
  54. _cachedClip = GetClip ()?.Clone ();
  55. }
  56. }
  57. /// <summary>
  58. /// INTERNAL API - Draws the transparent margins for the specified views. This is called from <see cref="View.Draw"/> on each
  59. /// iteration of the main loop after all Views have been drawn.
  60. /// </summary>
  61. /// <remarks>
  62. /// Non-transparent margins are drawn as-normal in <see cref="View.DrawAdornments"/>.
  63. /// </remarks>
  64. /// <param name="views"></param>
  65. /// <returns><see langword="true"/></returns>
  66. internal static bool DrawTransparentMargins (IEnumerable<View> views)
  67. {
  68. Stack<View> stack = new (views);
  69. while (stack.Count > 0)
  70. {
  71. View view = stack.Pop ();
  72. if (view.Margin is { } margin
  73. && margin.Thickness != Thickness.Empty
  74. && margin.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent)
  75. && margin.GetCachedClip () != null)
  76. {
  77. margin.SetNeedsDraw ();
  78. Region? saved = view.GetClip ();
  79. view.SetClip (margin.GetCachedClip ());
  80. margin.Draw ();
  81. view.SetClip (saved);
  82. margin.ClearCachedClip ();
  83. }
  84. foreach (View subview in view.SubViews.OrderBy (v => v.HasFocus && v.ShadowStyle != ShadowStyle.None).Reverse ())
  85. {
  86. stack.Push (subview);
  87. }
  88. }
  89. return true;
  90. }
  91. /// <inheritdoc/>
  92. public override void BeginInit ()
  93. {
  94. base.BeginInit ();
  95. if (Parent is null)
  96. {
  97. return;
  98. }
  99. ShadowStyle = base.ShadowStyle;
  100. Parent.MouseStateChanged += OnParentOnMouseStateChanged;
  101. }
  102. /// <inheritdoc/>
  103. protected override bool OnClearingViewport ()
  104. {
  105. if (Thickness == Thickness.Empty)
  106. {
  107. return true;
  108. }
  109. Rectangle screen = ViewportToScreen (Viewport);
  110. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Thickness) || HasScheme)
  111. {
  112. // This just draws/clears the thickness, not the insides.
  113. // TODO: This is a hack. See https://github.com/gui-cs/Terminal.Gui/issues/4016
  114. //SetAttribute (GetAttributeForRole (VisualRole.Normal));
  115. Thickness.Draw (Driver, screen, Diagnostics, ToString ());
  116. }
  117. if (ShadowStyle != ShadowStyle.None)
  118. {
  119. // Don't clear where the shadow goes
  120. screen = Rectangle.Inflate (screen, -ShadowSize.Width, -ShadowSize.Height);
  121. }
  122. return true;
  123. }
  124. /// <inheritdoc />
  125. protected override bool OnDrawingText ()
  126. {
  127. return ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent);
  128. }
  129. #region Shadow
  130. // private bool _pressed;
  131. private ShadowView? _bottomShadow;
  132. private ShadowView? _rightShadow;
  133. private bool _isThicknessChanging;
  134. private Thickness? _originalThickness;
  135. /// <summary>
  136. /// Sets whether the Margin includes a shadow effect. The shadow is drawn on the right and bottom sides of the
  137. /// Margin.
  138. /// </summary>
  139. public ShadowStyle SetShadow (ShadowStyle style)
  140. {
  141. if (_rightShadow is { })
  142. {
  143. Remove (_rightShadow);
  144. _rightShadow.Dispose ();
  145. _rightShadow = null;
  146. }
  147. if (_bottomShadow is { })
  148. {
  149. Remove (_bottomShadow);
  150. _bottomShadow.Dispose ();
  151. _bottomShadow = null;
  152. }
  153. _originalThickness ??= Thickness;
  154. if (ShadowStyle != ShadowStyle.None)
  155. {
  156. // Turn off shadow
  157. _originalThickness = new (Thickness.Left, Thickness.Top, Math.Max (Thickness.Right - ShadowSize.Width, 0), Math.Max (Thickness.Bottom - ShadowSize.Height, 0));
  158. }
  159. if (style != ShadowStyle.None)
  160. {
  161. // Turn on shadow
  162. _isThicknessChanging = true;
  163. Thickness = new (_originalThickness.Value.Left, _originalThickness.Value.Top, _originalThickness.Value.Right + ShadowSize.Width, _originalThickness.Value.Bottom + ShadowSize.Height);
  164. _isThicknessChanging = false;
  165. }
  166. if (style != ShadowStyle.None)
  167. {
  168. _rightShadow = new ()
  169. {
  170. X = Pos.AnchorEnd (ShadowSize.Width),
  171. Y = 0,
  172. Width = ShadowSize.Width,
  173. Height = Dim.Fill (),
  174. ShadowStyle = style,
  175. Orientation = Orientation.Vertical
  176. };
  177. _bottomShadow = new ()
  178. {
  179. X = 0,
  180. Y = Pos.AnchorEnd (ShadowSize.Height),
  181. Width = Dim.Fill (),
  182. Height = ShadowSize.Height,
  183. ShadowStyle = style,
  184. Orientation = Orientation.Horizontal
  185. };
  186. Add (_rightShadow, _bottomShadow);
  187. }
  188. else if (Thickness != _originalThickness)
  189. {
  190. _isThicknessChanging = true;
  191. Thickness = new (_originalThickness.Value.Left, _originalThickness.Value.Top, _originalThickness.Value.Right, _originalThickness.Value.Bottom);
  192. _isThicknessChanging = false;
  193. }
  194. return style;
  195. }
  196. /// <inheritdoc/>
  197. public override ShadowStyle ShadowStyle
  198. {
  199. get => base.ShadowStyle;
  200. set
  201. {
  202. if (value == ShadowStyle.Opaque || (value == ShadowStyle.Transparent && (ShadowSize.Width == 0 || ShadowSize.Height == 0)))
  203. {
  204. if (ShadowSize.Width != 1)
  205. {
  206. ShadowSize = ShadowSize with { Width = 1 };
  207. }
  208. if (ShadowSize.Height != 1)
  209. {
  210. ShadowSize = ShadowSize with { Height = 1 };
  211. }
  212. }
  213. base.ShadowStyle = SetShadow (value);
  214. }
  215. }
  216. private Size _shadowSize;
  217. /// <summary>
  218. /// Gets or sets the size of the shadow effect.
  219. /// </summary>
  220. public Size ShadowSize
  221. {
  222. get => _shadowSize;
  223. set
  224. {
  225. if (TryValidateShadowSize (_shadowSize, value, out Size result))
  226. {
  227. _shadowSize = value;
  228. SetShadow (ShadowStyle);
  229. }
  230. else
  231. {
  232. _shadowSize = result;
  233. }
  234. }
  235. }
  236. private bool TryValidateShadowSize (Size originalValue, in Size newValue, out Size result)
  237. {
  238. result = newValue;
  239. bool wasValid = true;
  240. if (newValue.Width < 0)
  241. {
  242. result = ShadowStyle is ShadowStyle.Opaque or ShadowStyle.Transparent ? result with { Width = 1 } : originalValue;
  243. wasValid = false;
  244. }
  245. if (newValue.Height < 0)
  246. {
  247. result = ShadowStyle is ShadowStyle.Opaque or ShadowStyle.Transparent ? result with { Height = 1 } : originalValue;
  248. wasValid = false;
  249. }
  250. if (!wasValid)
  251. {
  252. return false;
  253. }
  254. bool wasUpdated = false;
  255. if ((ShadowStyle == ShadowStyle.Opaque && newValue.Width != 1) || (ShadowStyle == ShadowStyle.Transparent && newValue.Width < 1))
  256. {
  257. result = result with { Width = 1 };
  258. wasUpdated = true;
  259. }
  260. if ((ShadowStyle == ShadowStyle.Opaque && newValue.Height != 1) || (ShadowStyle == ShadowStyle.Transparent && newValue.Height < 1))
  261. {
  262. result = result with { Height = 1 };
  263. wasUpdated = true;
  264. }
  265. return !wasUpdated;
  266. }
  267. private void OnParentOnMouseStateChanged (object? sender, EventArgs<MouseState> args)
  268. {
  269. if (sender is not View parent || Thickness == Thickness.Empty || ShadowStyle == ShadowStyle.None)
  270. {
  271. return;
  272. }
  273. bool pressed = args.Value.HasFlag (MouseState.Pressed) && parent.HighlightStates.HasFlag (MouseState.Pressed);
  274. bool pressedOutside = args.Value.HasFlag (MouseState.PressedOutside) && parent.HighlightStates.HasFlag (MouseState.PressedOutside);
  275. if (pressedOutside)
  276. {
  277. pressed = false;
  278. }
  279. if (MouseState.HasFlag (MouseState.Pressed) && !pressed)
  280. {
  281. // If the view is pressed and the highlight is being removed, move the shadow back.
  282. // Note, for visual effects reasons, we only move horizontally.
  283. // TODO: Add a setting or flag that lets the view move vertically as well.
  284. _isThicknessChanging = true;
  285. Thickness = new (
  286. Thickness.Left - PRESS_MOVE_HORIZONTAL,
  287. Thickness.Top - PRESS_MOVE_VERTICAL,
  288. Thickness.Right + PRESS_MOVE_HORIZONTAL,
  289. Thickness.Bottom + PRESS_MOVE_VERTICAL);
  290. _isThicknessChanging = false;
  291. if (_rightShadow is { })
  292. {
  293. _rightShadow.Visible = true;
  294. }
  295. if (_bottomShadow is { })
  296. {
  297. _bottomShadow.Visible = true;
  298. }
  299. MouseState &= ~MouseState.Pressed;
  300. return;
  301. }
  302. if (!MouseState.HasFlag (MouseState.Pressed) && pressed)
  303. {
  304. // If the view is not pressed, and we want highlight move the shadow
  305. // Note, for visual effects reasons, we only move horizontally.
  306. // TODO: Add a setting or flag that lets the view move vertically as well.
  307. _isThicknessChanging = true;
  308. Thickness = new (
  309. Thickness.Left + PRESS_MOVE_HORIZONTAL,
  310. Thickness.Top + PRESS_MOVE_VERTICAL,
  311. Thickness.Right - PRESS_MOVE_HORIZONTAL,
  312. Thickness.Bottom - PRESS_MOVE_VERTICAL);
  313. _isThicknessChanging = false;
  314. MouseState |= MouseState.Pressed;
  315. if (_rightShadow is { })
  316. {
  317. _rightShadow.Visible = false;
  318. }
  319. if (_bottomShadow is { })
  320. {
  321. _bottomShadow.Visible = false;
  322. }
  323. }
  324. }
  325. private void Margin_LayoutStarted (object? sender, LayoutEventArgs e)
  326. {
  327. // Adjust the shadow such that it is drawn aligned with the Border
  328. if (_rightShadow is { } && _bottomShadow is { })
  329. {
  330. switch (ShadowStyle)
  331. {
  332. case ShadowStyle.Transparent:
  333. // BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
  334. _rightShadow.Y = Parent!.Border!.Thickness.Top > 0 ? ScreenToViewport (Parent.Border!.GetBorderRectangle ().Location).Y + 1 : 0;
  335. break;
  336. case ShadowStyle.Opaque:
  337. // BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
  338. _rightShadow.Y = Parent!.Border!.Thickness.Top > 0 ? ScreenToViewport (Parent.Border!.GetBorderRectangle ().Location).Y + 1 : 0;
  339. _bottomShadow.X = Parent.Border!.Thickness.Left > 0 ? ScreenToViewport (Parent.Border!.GetBorderRectangle ().Location).X + 1 : 0;
  340. break;
  341. case ShadowStyle.None:
  342. default:
  343. _rightShadow.Y = 0;
  344. _bottomShadow.X = 0;
  345. break;
  346. }
  347. }
  348. }
  349. #endregion Shadow
  350. }