View.Attribute.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. namespace Terminal.Gui;
  3. public partial class View
  4. {
  5. // TODO: Rename "Color"->"Attribute" given we'll soon have non-color information in Attributes?
  6. // TODO: See https://github.com/gui-cs/Terminal.Gui/issues/457
  7. #region ColorScheme
  8. private ColorScheme _colorScheme;
  9. /// <summary>The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s color scheme.</summary>
  10. public virtual ColorScheme ColorScheme
  11. {
  12. get
  13. {
  14. if (_colorScheme is null)
  15. {
  16. return SuperView?.ColorScheme;
  17. }
  18. return _colorScheme;
  19. }
  20. set
  21. {
  22. if (_colorScheme != value)
  23. {
  24. _colorScheme = value;
  25. if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
  26. {
  27. Border.ColorScheme = _colorScheme;
  28. }
  29. SetNeedsDisplay ();
  30. }
  31. }
  32. }
  33. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  34. /// <returns>
  35. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  36. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  37. /// overridden can return other values.
  38. /// </returns>
  39. public virtual Attribute GetFocusColor ()
  40. {
  41. ColorScheme cs = ColorScheme;
  42. if (cs is null)
  43. {
  44. cs = new ();
  45. }
  46. return Enabled ? GetColor (cs.Focus) : cs.Disabled;
  47. }
  48. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  49. /// <returns>
  50. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  51. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  52. /// overridden can return other values.
  53. /// </returns>
  54. public virtual Attribute GetHotFocusColor ()
  55. {
  56. ColorScheme cs = ColorScheme ?? new ();
  57. return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
  58. }
  59. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  60. /// <returns>
  61. /// <see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  62. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  63. /// overridden can return other values.
  64. /// </returns>
  65. public virtual Attribute GetHotNormalColor ()
  66. {
  67. ColorScheme cs = ColorScheme;
  68. if (cs is null)
  69. {
  70. cs = new ();
  71. }
  72. return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
  73. }
  74. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  75. /// <returns>
  76. /// <see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  77. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  78. /// overridden can return other values.
  79. /// </returns>
  80. public virtual Attribute GetNormalColor ()
  81. {
  82. ColorScheme cs = ColorScheme;
  83. if (cs is null)
  84. {
  85. cs = new ();
  86. }
  87. Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
  88. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  89. {
  90. disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
  91. }
  92. return Enabled ? GetColor (cs.Normal) : disabled;
  93. }
  94. private Attribute GetColor (Attribute inputAttribute)
  95. {
  96. Attribute attr = inputAttribute;
  97. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  98. {
  99. attr = new (attr.Foreground.GetDarkerColor (), attr.Background.GetDarkerColor ());
  100. }
  101. return attr;
  102. }
  103. #endregion ColorScheme
  104. #region Attribute
  105. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  106. /// <remarks></remarks>
  107. /// <param name="attribute">THe Attribute to set.</param>
  108. public Attribute SetAttribute (Attribute attribute)
  109. {
  110. return Driver?.SetAttribute (attribute) ?? Attribute.Default;
  111. }
  112. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  113. /// <returns>The current attribute.</returns>
  114. public Attribute GetAttribute ()
  115. {
  116. return Driver?.GetAttribute () ?? Attribute.Default;
  117. }
  118. #endregion Attribute
  119. }