From 14e2e3cbb630a12d99720c403fa5b719900b6ec0 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 4 Sep 2024 23:06:17 +0100 Subject: [PATCH] Fix some KeepContentInAllViewport bugs. --- Terminal.Gui/Views/Scroll/Scroll.cs | 21 +++++++++++++-------- Terminal.Gui/Views/Scroll/ScrollSlider.cs | 11 ++++++++--- UICatalog/Scenarios/ScrollBarDemo.cs | 21 +++++++++++++++++---- UICatalog/Scenarios/ScrollDemo.cs | 10 +++++++++- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/Terminal.Gui/Views/Scroll/Scroll.cs b/Terminal.Gui/Views/Scroll/Scroll.cs index d8867c656..46f524a41 100644 --- a/Terminal.Gui/Views/Scroll/Scroll.cs +++ b/Terminal.Gui/Views/Scroll/Scroll.cs @@ -54,22 +54,27 @@ public class Scroll : View _keepContentInAllViewport = value; var pos = 0; - if (value && Orientation == Orientation.Horizontal && _position + SuperViewAsScrollBar!.Viewport.Width > Size) + if (value + && Orientation == Orientation.Horizontal + && _position + (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.Viewport.Width : Viewport.Width) > Size) { - pos = Size - SuperViewAsScrollBar.Viewport.Width; + pos = Size - (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.Viewport.Width : Viewport.Width); } - if (value && Orientation == Orientation.Vertical && _position + SuperViewAsScrollBar!.Viewport.Height > Size) + if (value + && Orientation == Orientation.Vertical + && _position + (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.Viewport.Height : Viewport.Height) > Size) { - pos = _size - SuperViewAsScrollBar.Viewport.Height; + pos = _size - (SuperViewAsScrollBar is { } ? SuperViewAsScrollBar.Viewport.Height : Viewport.Height); } if (pos != 0) { Position = pos; - SetNeedsDisplay (); - AdjustScroll (); } + + SetNeedsDisplay (); + AdjustScroll (); } } } @@ -175,12 +180,12 @@ public class Scroll : View } else if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed) && location > sliderPos.end) { - Position = Math.Min (Position + barSize, Size - barSize); + Position = Math.Min (Position + barSize, Size - barSize + (KeepContentInAllViewport ? 0 : barSize)); } else if ((mouseEvent.Flags == MouseFlags.WheeledDown && Orientation == Orientation.Vertical) || (mouseEvent.Flags == MouseFlags.WheeledRight && Orientation == Orientation.Horizontal)) { - Position = Math.Min (Position + 1, Size - barSize); + Position = Math.Min (Position + 1, Size - barSize + (KeepContentInAllViewport ? 0 : barSize)); } else if ((mouseEvent.Flags == MouseFlags.WheeledUp && Orientation == Orientation.Vertical) || (mouseEvent.Flags == MouseFlags.WheeledLeft && Orientation == Orientation.Horizontal)) diff --git a/Terminal.Gui/Views/Scroll/ScrollSlider.cs b/Terminal.Gui/Views/Scroll/ScrollSlider.cs index 89a1998c7..ad37e58bd 100644 --- a/Terminal.Gui/Views/Scroll/ScrollSlider.cs +++ b/Terminal.Gui/Views/Scroll/ScrollSlider.cs @@ -89,8 +89,13 @@ internal class ScrollSlider : View { if (SuperViewAsScroll.Orientation == Orientation.Vertical) { - Y = Frame.Y + offset < 0 ? 0 : - Frame.Y + offset + Frame.Height > barSize ? Math.Max (barSize - Frame.Height, 0) : Frame.Y + offset; + Y = Frame.Y + offset < 0 + ? 0 + : + Frame.Y + offset + Frame.Height > barSize + (SuperViewAsScroll.KeepContentInAllViewport ? 0 : barSize) + ? + Math.Max (barSize - Frame.Height, 0) + : Frame.Y + offset; SuperViewAsScroll.Position = GetPositionFromSliderLocation (Frame.Y); } @@ -157,7 +162,7 @@ internal class ScrollSlider : View if ((SuperViewAsScroll.Orientation == Orientation.Vertical && location + Frame.Height >= scrollSize) || (SuperViewAsScroll.Orientation == Orientation.Horizontal && location + Frame.Width >= scrollSize)) { - return SuperViewAsScroll.Size - scrollSize; + return SuperViewAsScroll.Size - scrollSize + (SuperViewAsScroll.KeepContentInAllViewport ? 0 : scrollSize); } return (int)Math.Min (Math.Round ((double)(location * SuperViewAsScroll.Size + location) / scrollSize), SuperViewAsScroll.Size - scrollSize); diff --git a/UICatalog/Scenarios/ScrollBarDemo.cs b/UICatalog/Scenarios/ScrollBarDemo.cs index c0b2cd4ae..373e4c5a9 100644 --- a/UICatalog/Scenarios/ScrollBarDemo.cs +++ b/UICatalog/Scenarios/ScrollBarDemo.cs @@ -182,14 +182,27 @@ public class ScrollBarDemo : Scenario } }; - var ckbAutoHideScrollBar = new CheckBox { Y = Pos.Bottom (scrollPosition), Text = "AutoHideScrollBar" }; - ckbAutoHideScrollBar.CheckedStateChanging += (s, e) => scrollBar.AutoHide = e.NewValue == CheckState.Checked; - view.Add (ckbAutoHideScrollBar); + var ckbAutoHide = new CheckBox + { Y = Pos.Bottom (scrollPosition), Text = "AutoHideScrollBar", CheckedState = scrollBar.AutoHide ? CheckState.Checked : CheckState.UnChecked }; + ckbAutoHide.CheckedStateChanging += (s, e) => scrollBar.AutoHide = e.NewValue == CheckState.Checked; + view.Add (ckbAutoHide); - var ckbShowScrollIndicator = new CheckBox { X = Pos.Right (ckbAutoHideScrollBar) + 1, Y = Pos.Bottom (scrollPosition), Text = "ShowScrollIndicator" }; + var ckbShowScrollIndicator = new CheckBox + { + X = Pos.Right (ckbAutoHide) + 1, Y = Pos.Bottom (scrollPosition), Text = "ShowScrollIndicator", + CheckedState = scrollBar.ShowScrollIndicator ? CheckState.Checked : CheckState.UnChecked + }; ckbShowScrollIndicator.CheckedStateChanging += (s, e) => scrollBar.ShowScrollIndicator = e.NewValue == CheckState.Checked; view.Add (ckbShowScrollIndicator); + var ckbKeepContentInAllViewport = new CheckBox + { + X = Pos.Right (ckbShowScrollIndicator) + 1, Y = Pos.Bottom (scrollPosition), Text = "KeepContentInAllViewport", + CheckedState = scrollBar.KeepContentInAllViewport ? CheckState.Checked : CheckState.UnChecked + }; + ckbKeepContentInAllViewport.CheckedStateChanging += (s, e) => scrollBar.KeepContentInAllViewport = e.NewValue == CheckState.Checked; + view.Add (ckbKeepContentInAllViewport); + var lblSizeChanged = new Label { Y = Pos.Bottom (ckbShowScrollIndicator) + 1 diff --git a/UICatalog/Scenarios/ScrollDemo.cs b/UICatalog/Scenarios/ScrollDemo.cs index 9fdb0226d..a0cd41fd7 100644 --- a/UICatalog/Scenarios/ScrollDemo.cs +++ b/UICatalog/Scenarios/ScrollDemo.cs @@ -182,9 +182,17 @@ public class ScrollDemo : Scenario } }; + var ckbKeepContentInAllViewport = new CheckBox + { + Y = Pos.Bottom (scrollPosition), Text = "KeepContentInAllViewport", + CheckedState = scroll.KeepContentInAllViewport ? CheckState.Checked : CheckState.UnChecked + }; + ckbKeepContentInAllViewport.CheckedStateChanging += (s, e) => scroll.KeepContentInAllViewport = e.NewValue == CheckState.Checked; + view.Add (ckbKeepContentInAllViewport); + var lblSizeChanged = new Label { - Y = Pos.Bottom (lblPosition) + 1 + Y = Pos.Bottom (ckbKeepContentInAllViewport) + 1 }; view.Add (lblSizeChanged);