View.Attribute.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #nullable enable
  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. // BUGBUG: This should be in Border.cs somehow
  26. if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
  27. {
  28. Border.ColorScheme = _colorScheme;
  29. }
  30. SetNeedsDraw ();
  31. }
  32. }
  33. }
  34. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  35. /// <returns>
  36. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  37. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  38. /// overridden can return other values.
  39. /// </returns>
  40. public virtual Attribute GetFocusColor ()
  41. {
  42. ColorScheme? cs = ColorScheme;
  43. if (cs is null)
  44. {
  45. cs = new ();
  46. }
  47. return Enabled ? GetColor (cs.Focus) : cs.Disabled;
  48. }
  49. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  50. /// <returns>
  51. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  52. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  53. /// overridden can return other values.
  54. /// </returns>
  55. public virtual Attribute GetHotFocusColor ()
  56. {
  57. ColorScheme? cs = ColorScheme ?? new ();
  58. return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
  59. }
  60. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  61. /// <returns>
  62. /// <see cref="ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  63. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  64. /// overridden can return other values.
  65. /// </returns>
  66. public virtual Attribute GetHotNormalColor ()
  67. {
  68. ColorScheme? cs = ColorScheme;
  69. if (cs is null)
  70. {
  71. cs = new ();
  72. }
  73. return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
  74. }
  75. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  76. /// <returns>
  77. /// <see cref="ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  78. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  79. /// overridden can return other values.
  80. /// </returns>
  81. public virtual Attribute GetNormalColor ()
  82. {
  83. ColorScheme? cs = ColorScheme;
  84. if (cs is null)
  85. {
  86. cs = new ();
  87. }
  88. Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
  89. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  90. {
  91. disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
  92. }
  93. return Enabled ? GetColor (cs.Normal) : disabled;
  94. }
  95. private Attribute GetColor (Attribute inputAttribute)
  96. {
  97. Attribute attr = inputAttribute;
  98. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  99. {
  100. attr = new (attr.Foreground.GetDarkerColor (), attr.Background.GetDarkerColor ());
  101. }
  102. return attr;
  103. }
  104. #endregion ColorScheme
  105. #region Attribute
  106. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  107. /// <remarks></remarks>
  108. /// <param name="attribute">THe Attribute to set.</param>
  109. public Attribute SetAttribute (Attribute attribute) { return Driver?.SetAttribute (attribute) ?? Attribute.Default; }
  110. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  111. /// <returns>The current attribute.</returns>
  112. public Attribute GetAttribute () { return Driver?.GetAttribute () ?? Attribute.Default; }
  113. #endregion Attribute
  114. }