ScrollButton.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. internal enum VariationMode
  4. {
  5. Decrease,
  6. Increase
  7. }
  8. internal class ScrollButton : View
  9. {
  10. public ScrollButton (ScrollBar host, VariationMode variation = VariationMode.Decrease)
  11. {
  12. _host = host;
  13. VariationMode = variation;
  14. TextAlignment = Alignment.Center;
  15. VerticalTextAlignment = Alignment.Center;
  16. Id = "scrollButton";
  17. //Width = Dim.Auto (DimAutoStyle.Content, 1);
  18. //Height = Dim.Auto (DimAutoStyle.Content, 1);
  19. WantContinuousButtonPressed = true;
  20. }
  21. private readonly ScrollBar _host;
  22. private ColorScheme? _savedColorScheme;
  23. public void AdjustButton ()
  24. {
  25. if (!IsInitialized)
  26. {
  27. return;
  28. }
  29. Width = _host.Orientation == Orientation.Vertical ? Dim.Fill () : 1;
  30. Height = _host.Orientation == Orientation.Vertical ? 1 : Dim.Fill ();
  31. switch (VariationMode)
  32. {
  33. case VariationMode.Decrease:
  34. X = 0;
  35. Y = 0;
  36. break;
  37. case VariationMode.Increase:
  38. X = _host.Orientation == Orientation.Vertical ? 0 : Pos.AnchorEnd (1);
  39. Y = _host.Orientation == Orientation.Vertical ? Pos.AnchorEnd (1) : 0;
  40. break;
  41. default:
  42. throw new ArgumentOutOfRangeException ();
  43. }
  44. SetButtonText ();
  45. }
  46. /// <inheritdoc/>
  47. public override Attribute GetNormalColor ()
  48. {
  49. if (_savedColorScheme is null)
  50. {
  51. ColorScheme = new () { Normal = new (_host.ColorScheme.HotNormal.Foreground, _host.ColorScheme.HotNormal.Background) };
  52. }
  53. else
  54. {
  55. ColorScheme = new () { Normal = new (_host.ColorScheme.Normal.Background, _host.ColorScheme.Normal.Foreground) };
  56. }
  57. return base.GetNormalColor ();
  58. }
  59. public VariationMode VariationMode { get; }
  60. /// <inheritdoc/>
  61. protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
  62. {
  63. _savedColorScheme ??= _host.ColorScheme;
  64. ColorScheme = new ()
  65. {
  66. Normal = new (_savedColorScheme.HotNormal.Foreground, _savedColorScheme.HotNormal.Foreground),
  67. Focus = new (_savedColorScheme.Focus.Foreground, _savedColorScheme.Focus.Foreground),
  68. HotNormal = new (_savedColorScheme.Normal.Foreground, _savedColorScheme.Normal.Foreground),
  69. HotFocus = new (_savedColorScheme.HotFocus.Foreground, _savedColorScheme.HotFocus.Foreground),
  70. Disabled = new (_savedColorScheme.Disabled.Foreground, _savedColorScheme.Disabled.Foreground)
  71. };
  72. return base.OnMouseEnter (mouseEvent);
  73. }
  74. /// <inheritdoc/>
  75. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  76. {
  77. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  78. {
  79. switch (VariationMode)
  80. {
  81. case VariationMode.Decrease:
  82. _host.Position--;
  83. return true;
  84. case VariationMode.Increase:
  85. _host.Position++;
  86. return true;
  87. default:
  88. throw new ArgumentOutOfRangeException ();
  89. }
  90. }
  91. return base.OnMouseEvent (mouseEvent);
  92. }
  93. /// <inheritdoc/>
  94. protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
  95. {
  96. if (_savedColorScheme is { } && !mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  97. {
  98. ColorScheme = _savedColorScheme;
  99. _savedColorScheme = null;
  100. }
  101. return base.OnMouseLeave (mouseEvent);
  102. }
  103. private void SetButtonText ()
  104. {
  105. switch (VariationMode)
  106. {
  107. case VariationMode.Decrease:
  108. Text = _host.Orientation == Orientation.Vertical ? Glyphs.UpArrow.ToString () : Glyphs.LeftArrow.ToString ();
  109. break;
  110. case VariationMode.Increase:
  111. Text = _host.Orientation == Orientation.Vertical ? Glyphs.DownArrow.ToString () : Glyphs.RightArrow.ToString ();
  112. break;
  113. default:
  114. throw new ArgumentOutOfRangeException ();
  115. }
  116. }
  117. }