ScrollButton.cs 4.2 KB

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