View.Color.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. private ColorScheme _colorScheme;
  5. /// <summary>The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s color scheme.</summary>
  6. public virtual ColorScheme ColorScheme
  7. {
  8. get
  9. {
  10. if (_colorScheme is null)
  11. {
  12. return SuperView?.ColorScheme;
  13. }
  14. return _colorScheme;
  15. }
  16. set
  17. {
  18. if (_colorScheme != value)
  19. {
  20. _colorScheme = value;
  21. if (Border is { } && Border.LineStyle != LineStyle.None && Border.ColorScheme is { })
  22. {
  23. Border.ColorScheme = _colorScheme;
  24. }
  25. SetNeedsDisplay ();
  26. }
  27. }
  28. }
  29. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  30. /// <returns>
  31. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  32. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  33. /// overridden can return other values.
  34. /// </returns>
  35. public virtual Attribute GetFocusColor ()
  36. {
  37. ColorScheme cs = ColorScheme;
  38. if (cs is null)
  39. {
  40. cs = new ();
  41. }
  42. return Enabled ? GetColor (cs.Focus) : cs.Disabled;
  43. }
  44. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  45. /// <returns>
  46. /// <see cref="ColorScheme.Focus"/> if <see cref="Enabled"/> is <see langword="true"/> or
  47. /// <see cref="ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  48. /// overridden can return other values.
  49. /// </returns>
  50. public virtual Attribute GetHotFocusColor ()
  51. {
  52. ColorScheme cs = ColorScheme ?? new ();
  53. return Enabled ? GetColor (cs.HotFocus) : cs.Disabled;
  54. }
  55. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  56. /// <returns>
  57. /// <see cref="Terminal.Gui.ColorScheme.HotNormal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  58. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  59. /// overridden can return other values.
  60. /// </returns>
  61. public virtual Attribute GetHotNormalColor ()
  62. {
  63. ColorScheme cs = ColorScheme;
  64. if (cs is null)
  65. {
  66. cs = new ();
  67. }
  68. return Enabled ? GetColor (cs.HotNormal) : cs.Disabled;
  69. }
  70. /// <summary>Determines the current <see cref="ColorScheme"/> based on the <see cref="Enabled"/> value.</summary>
  71. /// <returns>
  72. /// <see cref="Terminal.Gui.ColorScheme.Normal"/> if <see cref="Enabled"/> is <see langword="true"/> or
  73. /// <see cref="Terminal.Gui.ColorScheme.Disabled"/> if <see cref="Enabled"/> is <see langword="false"/>. If it's
  74. /// overridden can return other values.
  75. /// </returns>
  76. public virtual Attribute GetNormalColor ()
  77. {
  78. ColorScheme cs = ColorScheme;
  79. if (cs is null)
  80. {
  81. cs = new ();
  82. }
  83. Attribute disabled = new (cs.Disabled.Foreground, cs.Disabled.Background);
  84. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  85. {
  86. disabled = new (disabled.Foreground.GetDarkerColor (), disabled.Background.GetDarkerColor ());
  87. }
  88. return Enabled ? GetColor (cs.Normal) : disabled;
  89. }
  90. private Attribute GetColor (Attribute inputAttribute)
  91. {
  92. Attribute attr = inputAttribute;
  93. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Hover) && _hovering)
  94. {
  95. attr = new (attr.Foreground.GetDarkerColor (), attr.Background.GetDarkerColor ());
  96. }
  97. return attr;
  98. }
  99. }