Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Terminal.Gui/Views/ScrollView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ namespace Terminal.Gui {
/// </para>
/// </remarks>
public class ScrollView : View {
View contentView = null;
private class ContentView : View {
public ContentView (Rect frame) : base (frame)
{
}
}

ContentView contentView;
ScrollBarView vertical, horizontal;

/// <summary>
Expand All @@ -52,7 +58,7 @@ public ScrollView () : base ()

void Initialize (Rect frame)
{
contentView = new View (frame);
contentView = new ContentView (frame);
vertical = new ScrollBarView (1, 0, isVertical: true) {
X = Pos.AnchorEnd (1),
Y = 0,
Expand Down Expand Up @@ -177,6 +183,12 @@ public bool AutoHideScrollBars {
set {
if (autoHideScrollBars != value) {
autoHideScrollBars = value;
if (Subviews.Contains (vertical)) {
vertical.AutoHideScrollBars = value;
}
if (Subviews.Contains (horizontal)) {
horizontal.AutoHideScrollBars = value;
}
SetNeedsDisplay ();
}
}
Expand Down Expand Up @@ -251,6 +263,8 @@ public bool ShowHorizontalScrollIndicator {
SetNeedsLayout ();
if (value) {
base.Add (horizontal);
horizontal.ShowScrollIndicator = value;
horizontal.AutoHideScrollBars = autoHideScrollBars;
horizontal.OtherScrollBarView = vertical;
horizontal.OtherScrollBarView.ShowScrollIndicator = value;
horizontal.MouseEnter += View_MouseEnter;
Expand Down Expand Up @@ -290,6 +304,8 @@ public bool ShowVerticalScrollIndicator {
SetNeedsLayout ();
if (value) {
base.Add (vertical);
vertical.ShowScrollIndicator = value;
vertical.AutoHideScrollBars = autoHideScrollBars;
vertical.OtherScrollBarView = horizontal;
vertical.OtherScrollBarView.ShowScrollIndicator = value;
vertical.MouseEnter += View_MouseEnter;
Expand Down Expand Up @@ -322,10 +338,12 @@ public override void Redraw (Rect region)
ShowHideScrollBars ();
} else {
if (ShowVerticalScrollIndicator) {
vertical.SetRelativeLayout (Bounds);
vertical.Redraw (vertical.Bounds);
}

if (ShowHorizontalScrollIndicator) {
horizontal.SetRelativeLayout (Bounds);
horizontal.Redraw (horizontal.Bounds);
}
}
Expand Down
109 changes: 108 additions & 1 deletion UnitTests/ScrollViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Terminal.Gui.Views {
public class ScrollViewTests {
readonly ITestOutputHelper output;

public ScrollViewTests (ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void Constructors_Defaults ()
{
Expand Down Expand Up @@ -173,5 +181,104 @@ public void KeyBindings_Command ()
Assert.False (sv.ProcessKey (new KeyEvent (Key.End | Key.CtrlMask, new KeyModifiers ())));
Assert.Equal (new Point (-39, -19), sv.ContentOffset);
}

[Fact, AutoInitShutdown]
public void AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
{
var sv = new ScrollView {
Width = 10,
Height = 10
};

Application.Top.Add (sv);
Application.Begin (Application.Top);

Assert.True (sv.AutoHideScrollBars);
Assert.False (sv.ShowHorizontalScrollIndicator);
Assert.False (sv.ShowVerticalScrollIndicator);
GraphViewTests.AssertDriverContentsWithFrameAre ("", output);

sv.AutoHideScrollBars = false;
sv.ShowHorizontalScrollIndicator = true;
sv.ShowVerticalScrollIndicator = true;
sv.Redraw (sv.Bounds);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
◄├─────┤►
", output);
}

[Fact, AutoInitShutdown]
public void ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
{
var sv = new ScrollView {
Width = 10,
Height = 10,
ContentSize = new Size (50, 50)
};

Application.Top.Add (sv);
Application.Begin (Application.Top);

Assert.Equal (50, sv.ContentSize.Width);
Assert.Equal (50, sv.ContentSize.Height);
Assert.True (sv.AutoHideScrollBars);
Assert.True (sv.ShowHorizontalScrollIndicator);
Assert.True (sv.ShowVerticalScrollIndicator);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
◄├┤░░░░░►
", output);
}

[Fact, AutoInitShutdown]
public void ContentOffset_ContentSize_AutoHideScrollBars_ShowHorizontalScrollIndicator_ShowVerticalScrollIndicator ()
{
var sv = new ScrollView {
Width = 10,
Height = 10,
ContentSize = new Size (50, 50),
ContentOffset = new Point (25, 25)
};

Application.Top.Add (sv);
Application.Begin (Application.Top);

Assert.Equal (-25, sv.ContentOffset.X);
Assert.Equal (-25, sv.ContentOffset.Y);
Assert.Equal (50, sv.ContentSize.Width);
Assert.Equal (50, sv.ContentSize.Height);
Assert.True (sv.AutoHideScrollBars);
Assert.True (sv.ShowHorizontalScrollIndicator);
Assert.True (sv.ShowVerticalScrollIndicator);
GraphViewTests.AssertDriverContentsWithFrameAre (@"
◄░░░├─┤░►
", output);
}
}
}
}