From 2884b8e94ddd7e1fe4a415d6aa1cbfbadccab36d Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 24 Jan 2021 10:35:03 +0000 Subject: [PATCH] Added horizontal scrollbar, fixed tests --- Terminal.Gui/Views/TreeView.cs | 40 ++++++++++++++++++++++- UICatalog/Scenarios/TreeViewFileSystem.cs | 16 ++++----- UnitTests/TreeViewTests.cs | 10 +++--- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Terminal.Gui/Views/TreeView.cs b/Terminal.Gui/Views/TreeView.cs index 10f443833..4a865ecf4 100644 --- a/Terminal.Gui/Views/TreeView.cs +++ b/Terminal.Gui/Views/TreeView.cs @@ -346,7 +346,6 @@ namespace Terminal.Gui { /// public int ContentHeight => BuildLineMap().Count(); - /// /// Returns the string representation of model objects hosted in the tree. Default implementation is to call /// @@ -523,6 +522,32 @@ namespace Terminal.Gui { return -1; } + /// + /// Returns the maximum width line in the tree including prefix and expansion symbols + /// + /// True to consider only rows currently visible (based on window bounds and . False to calculate the width of every exposed branch in the tree + /// + public int GetContentWidth(bool visible){ + + var map = BuildLineMap(); + + if(map.Length == 0) + return 0; + + if(visible){ + + //Somehow we managed to scroll off the end of the control + if(ScrollOffsetVertical > map.Length) + return 0; + + return map.Skip(ScrollOffsetVertical).Take(Bounds.Height).Max(b=>b.GetWidth(Driver)); + } + else{ + + return map.Max(b=>b.GetWidth(Driver)); + } + } + /// /// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen /// @@ -898,6 +923,18 @@ namespace Terminal.Gui { this.ChildBranches = children.ToDictionary(k=>k,val=>new Branch(tree,this,val)); } + /// + /// Returns the width of the line including prefix and the results of (the line body). + /// + /// + public virtual int GetWidth (ConsoleDriver driver) + { + return + GetLinePrefix(driver).Sum(Rune.ColumnWidth) + + Rune.ColumnWidth(GetExpandableSymbol(driver)) + + (tree.AspectGetter(Model) ?? "").Length; + } + /// /// Renders the current on the specified line /// @@ -1210,6 +1247,7 @@ namespace Terminal.Gui { return false; } + } /// diff --git a/UICatalog/Scenarios/TreeViewFileSystem.cs b/UICatalog/Scenarios/TreeViewFileSystem.cs index 1d4efa53b..b9ac80a5c 100644 --- a/UICatalog/Scenarios/TreeViewFileSystem.cs +++ b/UICatalog/Scenarios/TreeViewFileSystem.cs @@ -92,20 +92,20 @@ namespace UICatalog.Scenarios { } treeViewFiles.SetNeedsDisplay (); }; - /* + _scrollBar.OtherScrollBarView.ChangedPosition += () => { - _listView.LeftItem = _scrollBar.OtherScrollBarView.Position; - if (_listView.LeftItem != _scrollBar.OtherScrollBarView.Position) { - _scrollBar.OtherScrollBarView.Position = _listView.LeftItem; + treeViewFiles.ScrollOffsetHorizontal = _scrollBar.OtherScrollBarView.Position; + if (treeViewFiles.ScrollOffsetHorizontal != _scrollBar.OtherScrollBarView.Position) { + _scrollBar.OtherScrollBarView.Position = treeViewFiles.ScrollOffsetHorizontal; } - _listView.SetNeedsDisplay (); - };*/ + treeViewFiles.SetNeedsDisplay (); + }; treeViewFiles.DrawContent += (e) => { _scrollBar.Size = treeViewFiles.ContentHeight; _scrollBar.Position = treeViewFiles.ScrollOffsetVertical; - // _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1; - // _scrollBar.OtherScrollBarView.Position = _listView.LeftItem; + _scrollBar.OtherScrollBarView.Size = treeViewFiles.GetContentWidth(true); + _scrollBar.OtherScrollBarView.Position = treeViewFiles.ScrollOffsetHorizontal; _scrollBar.Refresh (); }; } diff --git a/UnitTests/TreeViewTests.cs b/UnitTests/TreeViewTests.cs index b8fe3d72e..c103908ba 100644 --- a/UnitTests/TreeViewTests.cs +++ b/UnitTests/TreeViewTests.cs @@ -89,13 +89,13 @@ namespace UnitTests { { var tree = CreateTree(); - Assert.Equal(0,tree.ScrollOffset); + Assert.Equal(0,tree.ScrollOffsetVertical); - tree.ScrollOffset = -100; - Assert.Equal(0,tree.ScrollOffset); + tree.ScrollOffsetVertical = -100; + Assert.Equal(0,tree.ScrollOffsetVertical); - tree.ScrollOffset = 10; - Assert.Equal(10,tree.ScrollOffset); + tree.ScrollOffsetVertical = 10; + Assert.Equal(10,tree.ScrollOffsetVertical); }