From 8626c4e2d768a19ab92fc265dd1750e24af99dd4 Mon Sep 17 00:00:00 2001 From: tznind Date: Sat, 14 Jan 2023 19:20:11 +0000 Subject: [PATCH] Enforce minimum panel sizes --- Terminal.Gui/Views/SplitContainer.cs | 32 +++++++++++++++++----------- UnitTests/SplitContainerTests.cs | 4 ++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Terminal.Gui/Views/SplitContainer.cs b/Terminal.Gui/Views/SplitContainer.cs index bee06ce7f..294d291e8 100644 --- a/Terminal.Gui/Views/SplitContainer.cs +++ b/Terminal.Gui/Views/SplitContainer.cs @@ -33,8 +33,8 @@ namespace Terminal.Gui { /// public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this - - public int Panel1MinSize { get; set; } + + public int Panel1MinSize { get; set; } = 1; public ustring Panel1Title { get; set; } = string.Empty; /// @@ -46,7 +46,7 @@ namespace Terminal.Gui { /// public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this - public int Panel2MinSize { get; set; } + public int Panel2MinSize { get; set; } = 1; public ustring Panel2Title { get; set; } = string.Empty; private Pos splitterDistance = Pos.Percent (50); @@ -348,31 +348,37 @@ namespace Terminal.Gui { if (!IsInitialized) { return pos; } + + var panel1MinSize = Panel1MinSize; + var panel2MinSize = Panel2MinSize; + + + // if there is a border then there is less space + // for the panels so we need to make size restrictions + // tighter. + if(HasBorder()) { + panel1MinSize++; + panel2MinSize++; + } var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width; var idealPosition = pos.Anchor (availableSpace); // bad position because not enough space for Panel1 - if (idealPosition < Panel1MinSize) { + if (idealPosition < panel1MinSize) { // TODO: we should preserve Absolute/Percent status here not just force it to absolute - return (Pos)Math.Min (Panel1MinSize, availableSpace); - } - - // if there is a border then 2 screen units are taken occupied - // by the border around the edge (one on left, one on right). - if (HasBorder ()) { - availableSpace -= 2; + return (Pos)Math.Min (panel1MinSize, availableSpace); } // bad position because not enough space for Panel2 - if (availableSpace - idealPosition <= Panel2MinSize) { + if (availableSpace - idealPosition <= panel2MinSize) { // TODO: we should preserve Absolute/Percent status here not just force it to absolute // +1 is to allow space for the splitter - return (Pos)Math.Max (availableSpace - (Panel2MinSize + 1), 0); + return (Pos)Math.Max (availableSpace - (panel2MinSize + 1), 0); } // this splitter position is fine, there is enough space for everyone diff --git a/UnitTests/SplitContainerTests.cs b/UnitTests/SplitContainerTests.cs index e700d334b..feab80984 100644 --- a/UnitTests/SplitContainerTests.cs +++ b/UnitTests/SplitContainerTests.cs @@ -260,7 +260,7 @@ namespace UnitTests { splitContainer.SplitterDistance = 2; // Should bound the value to the minimum distance - Assert.Equal (5, splitContainer.SplitterDistance); + Assert.Equal (6, splitContainer.SplitterDistance); splitContainer.Redraw (splitContainer.Bounds); @@ -348,7 +348,7 @@ namespace UnitTests { splitContainer.SplitterDistance = 8; // Should bound the value to the minimum distance - Assert.Equal (3, splitContainer.SplitterDistance); + Assert.Equal (4, splitContainer.SplitterDistance); splitContainer.Redraw (splitContainer.Bounds);