View.Attribute.cs 4.4 KB

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