ShadowView.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #nullable enable
  2. using Microsoft.VisualBasic;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. /// <summary>
  6. /// Draws a shadow on the right or bottom of the view. Used internally by <see cref="Margin"/>.
  7. /// </summary>
  8. internal class ShadowView : View
  9. {
  10. private ShadowStyle _shadowStyle;
  11. /// <inheritdoc/>
  12. public override Attribute GetNormalColor ()
  13. {
  14. if (SuperView is Adornment adornment)
  15. {
  16. var attr = Attribute.Default;
  17. if (adornment.Parent?.SuperView is { })
  18. {
  19. attr = adornment.Parent.SuperView.GetNormalColor ();
  20. }
  21. else if (Application.Top is { })
  22. {
  23. attr = Application.Top.GetNormalColor ();
  24. }
  25. return new (
  26. new Attribute (
  27. ShadowStyle == ShadowStyle.Opaque ? Color.Black : attr.Foreground.GetDarkerColor (),
  28. ShadowStyle == ShadowStyle.Opaque ? attr.Background : attr.Background.GetDarkerColor ()));
  29. }
  30. return base.GetNormalColor ();
  31. }
  32. /// <inheritdoc/>
  33. public override void OnDrawContent (Rectangle viewport)
  34. {
  35. //base.OnDrawContent (viewport);
  36. switch (ShadowStyle)
  37. {
  38. case ShadowStyle.Opaque:
  39. if (Orientation == Orientation.Vertical)
  40. {
  41. DrawVerticalShadowOpaque (viewport);
  42. }
  43. else
  44. {
  45. DrawHorizontalShadowOpaque (viewport);
  46. }
  47. break;
  48. case ShadowStyle.Transparent:
  49. //Attribute prevAttr = Driver.GetAttribute ();
  50. //var attr = new Attribute (prevAttr.Foreground, prevAttr.Background);
  51. //Driver.SetAttribute (attr);
  52. if (Orientation == Orientation.Vertical)
  53. {
  54. DrawVerticalShadowTransparent (viewport);
  55. }
  56. else
  57. {
  58. DrawHorizontalShadowTransparent (viewport);
  59. }
  60. //Driver.SetAttribute (prevAttr);
  61. break;
  62. }
  63. }
  64. /// <summary>
  65. /// Gets or sets the orientation of the shadow.
  66. /// </summary>
  67. public Orientation Orientation { get; set; }
  68. public override ShadowStyle ShadowStyle
  69. {
  70. get => _shadowStyle;
  71. set
  72. {
  73. Visible = value != ShadowStyle.None;
  74. _shadowStyle = value;
  75. }
  76. }
  77. private void DrawHorizontalShadowOpaque (Rectangle rectangle)
  78. {
  79. // Draw the start glyph
  80. AddRune (0, 0, Glyphs.ShadowHorizontalStart);
  81. // Fill the rest of the rectangle with the glyph - note we skip the last since vertical will draw it
  82. for (var i = 1; i < rectangle.Width - 1; i++)
  83. {
  84. AddRune (i, 0, Glyphs.ShadowHorizontal);
  85. }
  86. // Last is special
  87. AddRune (rectangle.Width - 1, 0, Glyphs.ShadowHorizontalEnd);
  88. }
  89. private void DrawHorizontalShadowTransparent (Rectangle viewport)
  90. {
  91. Rectangle screen = ViewportToScreen (viewport);
  92. // Fill the rest of the rectangle - note we skip the last since vertical will draw it
  93. for (int i = screen.X + 1; i < screen.X + screen.Width - 1; i++)
  94. {
  95. Driver.Move (i, screen.Y);
  96. if (i < Driver.Contents!.GetLength (1) && screen.Y < Driver.Contents.GetLength (0))
  97. {
  98. Driver.AddRune (Driver.Contents [screen.Y, i].Rune);
  99. }
  100. }
  101. }
  102. private void DrawVerticalShadowOpaque (Rectangle viewport)
  103. {
  104. // Draw the start glyph
  105. AddRune (0, 0, Glyphs.ShadowVerticalStart);
  106. // Fill the rest of the rectangle with the glyph
  107. for (var i = 1; i < viewport.Height; i++)
  108. {
  109. AddRune (0, i, Glyphs.ShadowVertical);
  110. }
  111. }
  112. private void DrawVerticalShadowTransparent (Rectangle viewport)
  113. {
  114. Rectangle screen = ViewportToScreen (viewport);
  115. // Fill the rest of the rectangle
  116. for (int i = Math.Max (0, screen.Y); i < screen.Y + viewport.Height; i++)
  117. {
  118. Driver.Move (screen.X, i);
  119. if (Driver.Contents is { } && screen.X < Driver.Contents.GetLength (1) && i < Driver.Contents.GetLength (0))
  120. {
  121. Driver.AddRune (Driver.Contents [i, screen.X].Rune);
  122. }
  123. }
  124. }
  125. }