ShadowView.cs 4.1 KB

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