Browse Source

Replace private _barSize with local variables barSize and improving performance in the Position property.

BDisp 1 year ago
parent
commit
5d2120eac6
1 changed files with 16 additions and 9 deletions
  1. 16 9
      Terminal.Gui/Views/Scroll.cs

+ 16 - 9
Terminal.Gui/Views/Scroll.cs

@@ -38,8 +38,6 @@ public class Scroll : View
 
     private bool _wasSliderMouse;
 
-    private int _barSize => Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
-
     private Orientation _orientation;
     /// <summary>
     ///     Gets or sets if the Scroll is oriented vertically or horizontally.
@@ -63,7 +61,14 @@ public class Scroll : View
         get => _position;
         set
         {
-            if (value == _position || value < 0 || value + _barSize > Size)
+            if (value == _position || value < 0)
+            {
+                return;
+            }
+
+            int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
+
+            if (value + barSize > Size)
             {
                 return;
             }
@@ -229,6 +234,7 @@ public class Scroll : View
     {
         MouseEvent me = e.MouseEvent;
         int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
+        int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
 
         (int topLeft, int bottomRight) sliderPos = _orientation == Orientation.Vertical
                                                        ? new (_slider.Frame.Y, _slider.Frame.Bottom - 1)
@@ -236,16 +242,16 @@ public class Scroll : View
 
         if (me.Flags == MouseFlags.Button1Pressed && location < sliderPos.topLeft)
         {
-            Position = Math.Max (Position - _barSize, 0);
+            Position = Math.Max (Position - barSize, 0);
         }
         else if (me.Flags == MouseFlags.Button1Pressed && location > sliderPos.bottomRight)
         {
-            Position = Math.Min (Position + _barSize, Size - _barSize);
+            Position = Math.Min (Position + barSize, Size - barSize);
         }
         else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
                  || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
         {
-            Position = Math.Min (Position + 1, Size - _barSize);
+            Position = Math.Min (Position + 1, Size - barSize);
         }
         else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
                  || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))
@@ -317,6 +323,7 @@ public class Scroll : View
         MouseEvent me = e.MouseEvent;
         int location = Orientation == Orientation.Vertical ? me.Position.Y : me.Position.X;
         int offset = _lastLocation > -1 ? location - _lastLocation : 0;
+        int barSize = Orientation == Orientation.Vertical ? GetContentSize ().Height : GetContentSize ().Width;
 
         if (me.Flags == MouseFlags.Button1Pressed)
         {
@@ -330,7 +337,7 @@ public class Scroll : View
         {
             if (Orientation == Orientation.Vertical)
             {
-                if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= _barSize)
+                if (_slider.Frame.Y + offset >= 0 && _slider.Frame.Y + offset + _slider.Frame.Height <= barSize)
                 {
                     _wasSliderMouse = true;
                     _slider.Y = _slider.Frame.Y + offset;
@@ -339,7 +346,7 @@ public class Scroll : View
             }
             else
             {
-                if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= _barSize)
+                if (_slider.Frame.X + offset >= 0 && _slider.Frame.X + offset + _slider.Frame.Width <= barSize)
                 {
                     _wasSliderMouse = true;
                     _slider.X = _slider.Frame.X + offset;
@@ -359,7 +366,7 @@ public class Scroll : View
         else if ((me.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical)
                  || (me.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal))
         {
-            Position = Math.Min (Position + 1, Size - _barSize);
+            Position = Math.Min (Position + 1, Size - barSize);
         }
         else if ((me.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical)
                  || (me.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal))