ScrollButton.cs 4.1 KB

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