From a4704515551e047999e71b5c4fe662d1ec58f0cc Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 15 Jan 2023 09:21:32 +0000 Subject: [PATCH] TrySplitPanel now migrates PanelTitle to children --- Terminal.Gui/Views/SplitContainer.cs | 50 +++++++++++++++----- UICatalog/Scenarios/SplitContainerNesting.cs | 15 +++--- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/Terminal.Gui/Views/SplitContainer.cs b/Terminal.Gui/Views/SplitContainer.cs index c30bd122e..732af026f 100644 --- a/Terminal.Gui/Views/SplitContainer.cs +++ b/Terminal.Gui/Views/SplitContainer.cs @@ -248,7 +248,8 @@ namespace Terminal.Gui { /// is already a then returns false. /// /// After successful splitting, the returned container's - /// will contain the original content (if any) while will be empty and available + /// will contain the original content and (if any) while + /// will be empty and available for adding to. /// for adding to. /// The new now showing in /// or the existing one if it was already been converted before. @@ -257,10 +258,22 @@ namespace Terminal.Gui { /// public bool TrySplitPanel1(out SplitContainer result) { - return TrySplit ( - () => this.Panel1, - (n) => this.Panel1 = n, + // when splitting a panel into 2 sub panels we will need to migrate + // the title too + var title = Panel1Title; + + bool returnValue = TrySplit ( + this.Panel1, + (newSplitContainer) => { + this.Panel1 = newSplitContainer; + + // Move title to new container + Panel1Title = string.Empty; + newSplitContainer.Panel1Title = title; + }, out result); + + return returnValue; } /// @@ -269,7 +282,8 @@ namespace Terminal.Gui { /// is already a then returns false. /// /// After successful splitting, the returned container's - /// will contain the original content (if any) while will be empty and available + /// will contain the original content and (if any) while + /// will be empty and available for adding to. /// for adding to. /// The new now showing in /// or the existing one if it was already been converted before. @@ -278,19 +292,31 @@ namespace Terminal.Gui { /// public bool TrySplitPanel2 (out SplitContainer result) { - return TrySplit ( - () => this.Panel2, - (n) => this.Panel2 = n, + // when splitting a panel into 2 sub panels we will need to migrate + // the title too + var title = Panel2Title; + + bool returnValue = TrySplit ( + this.Panel2, + (newSplitContainer) => { + this.Panel2 = newSplitContainer; + + // Move title to new container + Panel2Title = string.Empty; + + // Content always goes into Panel1 of the new container + // so that is where the title goes too + newSplitContainer.Panel1Title = title; + }, out result); + + return returnValue; } private bool TrySplit( - Func getter, + View toMove, Action newSplitContainerSetter, out SplitContainer result) { - // Get the current panel contents (Panel1 or Panel2) - var toMove = getter(); - if (toMove is SplitContainer existing) { result = existing; return false; diff --git a/UICatalog/Scenarios/SplitContainerNesting.cs b/UICatalog/Scenarios/SplitContainerNesting.cs index d0dae060f..dee36eac5 100644 --- a/UICatalog/Scenarios/SplitContainerNesting.cs +++ b/UICatalog/Scenarios/SplitContainerNesting.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Linq; using System.Threading; +using System.Xml.Linq; using Terminal.Gui; using Terminal.Gui.Graphs; @@ -174,8 +175,9 @@ namespace UICatalog.Scenarios { panelsCreated++; - // we can split Panel1 - SetPanelTitles (newContainer, panelsCreated); + // During splitting the old Title will have been migrated to Panel1 so we only need + // to set the Title on Panel2 (the one that gets our new TextView) + newContainer.Panel2Title = cbTitles.Checked ? $"Panel {panelsCreated}" : string.Empty; // Flip orientation newContainer.Orientation = newContainer.Orientation == Orientation.Vertical ? @@ -185,12 +187,6 @@ namespace UICatalog.Scenarios { newContainer.Panel2.Add (CreateTextView (panelsCreated)); } - private void SetPanelTitles (SplitContainer container, int containerNumber) - { - container.Panel1Title = cbTitles.Checked ? $"Panel {containerNumber}" : string.Empty; - container.Panel2Title = cbTitles.Checked ? $"Panel {containerNumber + 1}" : string.Empty; - } - private SplitContainer CreateSplitContainer (int titleNumber, Orientation orientation) { var toReturn = new SplitContainer { @@ -200,7 +196,8 @@ namespace UICatalog.Scenarios { Orientation = orientation }; - SetPanelTitles (toReturn, titleNumber); + toReturn.Panel1Title = cbTitles.Checked ? $"Panel {titleNumber}" : string.Empty; + toReturn.Panel2Title = cbTitles.Checked ? $"Panel {titleNumber + 1}" : string.Empty; return toReturn; }