diff --git a/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs b/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs index 408f82a7d..25a4d8591 100644 --- a/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs +++ b/ShareX.ScreenCaptureLib/RegionHelpers/ImageEditorScrollBar.cs @@ -25,7 +25,6 @@ You should have received a copy of the GNU General Public License using ShareX.HelpersLib; using System; -using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; @@ -38,77 +37,51 @@ internal class ImageEditorScrollbar : DrawableObject public int Thickness { get; set; } = 10; public int Margin { get; set; } = 15; public int Padding { get; set; } = 2; - public Rectangle TrackRectangle { get; set; } + public float Opacity { get; set; } public Rectangle ThumbRectangle { get; set; } - public int FadeInTime { get; set; } = 150; - public int FadeOutDelay { get; set; } = 500; - public int FadeOutTime { get; set; } = 150; - - private bool shouldDraw = true; - private bool shouldDrawBefore = true; - private Stopwatch changeTime; - private int opacityLast; - private int opacityCurrent; private RegionCaptureForm form; public ImageEditorScrollbar(Orientation orientation, RegionCaptureForm form) { Orientation = orientation; this.form = form; - - changeTime = Stopwatch.StartNew(); - - if (!form.ClientArea.Contains(form.CanvasRectangle)) - { - opacityLast = 255; - opacityCurrent = 255; - } } public void Update() { - Rectangle imageRectangleVisible = form.CanvasRectangle; - imageRectangleVisible.Intersect(form.ClientArea); + UpdateOpacity(); - if (Orientation == Orientation.Horizontal) + if (Visible) { - shouldDraw = form.ShapeManager.IsPanning && (form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right); + Rectangle imageRectangleVisible = form.CanvasRectangle; + imageRectangleVisible.Intersect(form.ClientArea); - UpdateOpacity(); - - if (Visible) + if (Orientation == Orientation.Horizontal) { int horizontalTrackLength = form.ClientArea.Width - Margin * 2 - Thickness - Padding * 2; int horizontalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Width / form.CanvasRectangle.Width * horizontalTrackLength)); - TrackRectangle = new Rectangle(new Point(Margin - Padding, form.ClientArea.Bottom - (Thickness + Margin) - Padding), + Rectangle = new Rectangle(new Point(Margin - Padding, form.ClientArea.Bottom - (Thickness + Margin) - Padding), new Size(horizontalTrackLength + Padding * 2, Thickness + Padding * 2)); double limitHorizontal = (horizontalTrackLength - horizontalThumbLength) / 2.0f; - double thumbHorizontalPositionX = Math.Round(TrackRectangle.X + TrackRectangle.Width / 2.0f - (horizontalThumbLength / 2.0f) - + double thumbHorizontalPositionX = Math.Round(Rectangle.X + Rectangle.Width / 2.0f - (horizontalThumbLength / 2.0f) - Math.Min(limitHorizontal, Math.Max(-limitHorizontal, form.CanvasCenterOffset.X / form.CanvasRectangle.Width * horizontalTrackLength))); ThumbRectangle = new Rectangle(new Point((int)thumbHorizontalPositionX, form.ClientArea.Bottom - (Thickness + Margin)), new Size(horizontalThumbLength, Thickness)); } - } - else - { - shouldDraw = form.ShapeManager.IsPanning && (form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom); - - UpdateOpacity(); - - if (Visible) + else { int verticalTrackLength = form.ClientArea.Height - Margin * 2 - Thickness - Padding * 2; int verticalThumbLength = Math.Max(Thickness, (int)Math.Round((float)imageRectangleVisible.Height / form.CanvasRectangle.Height * verticalTrackLength)); - TrackRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin) - Padding, Margin - Padding), + Rectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin) - Padding, Margin - Padding), new Size(Thickness + Padding * 2, verticalTrackLength + Padding * 2)); double limitVertical = (verticalTrackLength - verticalThumbLength) / 2.0f; - double thumbVerticalPositionY = Math.Round(TrackRectangle.Y + TrackRectangle.Height / 2.0f - (verticalThumbLength / 2.0f) - + double thumbVerticalPositionY = Math.Round(Rectangle.Y + Rectangle.Height / 2.0f - (verticalThumbLength / 2.0f) - Math.Min(limitVertical, Math.Max(-limitVertical, form.CanvasCenterOffset.Y / form.CanvasRectangle.Height * verticalTrackLength))); ThumbRectangle = new Rectangle(new Point(form.ClientArea.Right - (Thickness + Margin), (int)thumbVerticalPositionY), @@ -117,53 +90,47 @@ public void Update() } } + private void UpdateOpacity() + { + bool isScrollbarNeeded; + + if (Orientation == Orientation.Horizontal) + { + isScrollbarNeeded = form.CanvasRectangle.Left < form.ClientArea.Left || form.CanvasRectangle.Right > form.ClientArea.Right; + } + else + { + isScrollbarNeeded = form.CanvasRectangle.Top < form.ClientArea.Top || form.CanvasRectangle.Bottom > form.ClientArea.Bottom; + } + + if (!isScrollbarNeeded) + { + Opacity = 0f; + } + else if (form.ShapeManager.IsPanning || IsCursorHover) + { + Opacity = 1f; + } + else + { + Opacity = 0.7f; + } + + Visible = Opacity > 0; + } + public override void OnDraw(Graphics g) { if (!Visible) return; - using (Brush trackBrush = new SolidBrush(Color.FromArgb(opacityCurrent, 60, 60, 60))) - using (Brush thumbBrush = new SolidBrush(Color.FromArgb(opacityCurrent, 130, 130, 130))) + using (Brush trackBrush = new SolidBrush(Color.FromArgb((int)(255 * Opacity), 60, 60, 60))) + using (Brush thumbBrush = new SolidBrush(Color.FromArgb((int)(255 * Opacity), 130, 130, 130))) { g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawCapsule(trackBrush, TrackRectangle); + g.DrawCapsule(trackBrush, Rectangle); g.DrawCapsule(thumbBrush, ThumbRectangle); g.SmoothingMode = SmoothingMode.None; } } - - private void UpdateOpacity() - { - if (shouldDraw != shouldDrawBefore) - { - changeTime = Stopwatch.StartNew(); - opacityLast = opacityCurrent; - } - - shouldDrawBefore = shouldDraw; - - if (shouldDraw) - { - opacityCurrent = opacityLast + OpacityGain(changeTime); - } - else - { - opacityCurrent = opacityLast - OpacityLoss(changeTime); - } - - opacityCurrent = opacityCurrent.Between(0, 255); - - Visible = opacityCurrent > 0; - } - - private int OpacityGain(Stopwatch changeTime) - { - return (int)Math.Min(255.0f, 255.0f * changeTime.ElapsedMilliseconds / Math.Max(0, (float)FadeInTime)); - } - - private int OpacityLoss(Stopwatch changeTime) - { - int deltaTime = Math.Max(0, (int)changeTime.ElapsedMilliseconds - FadeOutDelay); - return (int)Math.Min(255.0f, 255.0f * deltaTime / Math.Max(0, (float)FadeOutTime)); - } } } \ No newline at end of file