ScrollButton.cs 4.1 KB

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