Make split container nesting scenario better and scrap old example

This commit is contained in:
tznind
2023-01-14 16:55:00 +00:00
parent 7abf2d7966
commit da14ccd3fe
2 changed files with 51 additions and 218 deletions

View File

@@ -1,170 +0,0 @@
using Terminal.Gui;
using Terminal.Gui.Graphs;
using NStack;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "Split Container", Description: "Demonstrates the SplitContainer functionality")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("LineView")]
public class SplitContainerExample : Scenario {
private SplitContainer splitContainer;
private SplitContainer nestedSplitContainer;
private MenuItem miVertical;
private MenuItem miShowBoth;
private MenuItem miShowPanel1;
private MenuItem miShowPanel2;
private MenuItem miShowNeither;
private MenuItem miSplitContainer1Border;
private MenuItem minestedSplitContainerBorder;
/// <summary>
/// Setup the scenario.
/// </summary>
public override void Setup ()
{
// Scenario Windows.
Win.Title = this.GetName ();
Win.Y = 1;
Win.Add (new Label ("This is a SplitContainer with a minimum panel size of 4. Drag the splitter to resize:"));
splitContainer = new SplitContainer {
Y = 2,
X = 2,
Width = Dim.Fill () - 2,
Height = Dim.Fill () - 1,
SplitterDistance = Pos.Percent (50),
};
nestedSplitContainer = new SplitContainer(){
Width = Dim.Fill(),
Height = Dim.Fill(),
Orientation = Orientation.Horizontal
};
splitContainer.Panel1MinSize = 4;
splitContainer.Panel2MinSize = 4;
Label lbl1;
splitContainer.Panel1Title = "Hello";
splitContainer.Panel1.Add (lbl1 = new Label ("Type Something:") { Y = 0 });
splitContainer.Panel1.Add (new TextField () { Width = Dim.Fill (), Y = 0, X = Pos.Right (lbl1) + 1 });
Label lbl2;
splitContainer.Panel2Title = "World";
splitContainer.Panel2.Add(nestedSplitContainer);
nestedSplitContainer.Panel1.Add (new TextView ()
{
Width = Dim.Fill(),
Height = Dim.Fill(),
Text = GenerateLotsOfText(),
AllowsTab = false,
WordWrap = true,
});
nestedSplitContainer.IntegratedBorder = BorderStyle.None;
nestedSplitContainer.Panel2.Add (lbl2 = new Label ("Type Here Too:") { Y = 0 });
nestedSplitContainer.Panel2.Add (new TextField () { Width = Dim.Fill (), Y = 0, X = Pos.Right (lbl2) + 1 });
nestedSplitContainer.Panel2.Add (new Label ("Here is a Text box:") { Y = 1 });
nestedSplitContainer.Panel2.Add (new TextView () { Y = 2, Width = Dim.Fill (), Height = Dim.Fill (), AllowsTab = false });
Win.Add (splitContainer);
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_Quit", "", () => Quit()),
}),
new MenuBarItem ("_Options", new MenuItem [] {
miVertical = new MenuItem ("_Vertical", "", () => ToggleOrientation())
{
Checked = splitContainer.Orientation == Orientation.Vertical,
CheckType = MenuItemCheckStyle.Checked
},
miSplitContainer1Border = new MenuItem ("_Outer Panel Border", "", () => ToggleBorder(miSplitContainer1Border, splitContainer))
{
Checked = splitContainer.IntegratedBorder == BorderStyle.Single,
CheckType = MenuItemCheckStyle.Checked
},
minestedSplitContainerBorder = new MenuItem ("_Inner Panel Border", "", () => ToggleBorder(minestedSplitContainerBorder,nestedSplitContainer))
{
Checked = nestedSplitContainer.IntegratedBorder == BorderStyle.Single,
CheckType = MenuItemCheckStyle.Checked
},
new MenuBarItem ("_Show", new MenuItem [] {
miShowBoth = new MenuItem ("Both", "",()=>{
splitContainer.Panel1.Visible = true;
splitContainer.Panel2.Visible = true;
UpdateShowMenuCheckedStates();
}),
miShowPanel1 = new MenuItem ("Panel 1", "", () => {
splitContainer.Panel1.Visible = true;
splitContainer.Panel2.Visible = false;
UpdateShowMenuCheckedStates();
}),
miShowPanel2 = new MenuItem ("Panel 2", "", () => {
splitContainer.Panel1.Visible = false;
splitContainer.Panel2.Visible = true;
UpdateShowMenuCheckedStates();
}),
miShowNeither = new MenuItem ("Neither", "",()=>{
splitContainer.Panel1.Visible = false;
splitContainer.Panel2.Visible = false;
UpdateShowMenuCheckedStates();
}),
})
}),
});
UpdateShowMenuCheckedStates ();
Application.Top.Add (menu);
}
private ustring GenerateLotsOfText ()
{
return "Hello There ".Repeat(100);
}
private void UpdateShowMenuCheckedStates ()
{
miShowBoth.Checked = (splitContainer.Panel1.Visible) && (splitContainer.Panel2.Visible);
miShowBoth.CheckType = MenuItemCheckStyle.Checked;
miShowPanel1.Checked = splitContainer.Panel1.Visible && !splitContainer.Panel2.Visible;
miShowPanel1.CheckType = MenuItemCheckStyle.Checked;
miShowPanel2.Checked = !splitContainer.Panel1.Visible && splitContainer.Panel2.Visible;
miShowPanel2.CheckType = MenuItemCheckStyle.Checked;
miShowNeither.Checked = (!splitContainer.Panel1.Visible) && (!splitContainer.Panel2.Visible);
miShowNeither.CheckType = MenuItemCheckStyle.Checked;
}
public void ToggleOrientation ()
{
miVertical.Checked = !miVertical.Checked;
splitContainer.Orientation = miVertical.Checked ? Orientation.Vertical : Orientation.Horizontal;
nestedSplitContainer.Orientation = miVertical.Checked ? Orientation.Horizontal : Orientation.Vertical;
}
private void ToggleBorder (MenuItem menuItem, SplitContainer splitContainer)
{
menuItem.Checked = !menuItem.Checked;
if(menuItem.Checked) {
splitContainer.IntegratedBorder = BorderStyle.Single;
} else {
splitContainer.IntegratedBorder = BorderStyle.None;
}
}
private void Quit ()
{
Application.RequestStop ();
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using Terminal.Gui;
using Terminal.Gui.Graphs;
@@ -17,6 +18,8 @@ namespace UICatalog.Scenarios {
private CheckBox cbTitles;
bool loaded = false;
int panelsCreated;
int panelsToCreate;
/// <summary>
/// Setup the scenario.
@@ -87,7 +90,7 @@ namespace UICatalog.Scenarios {
bool startHorizontal = cbHorizontal.Checked;
workArea.RemoveAll ();
if (numberOfPanels <= 0) {
return;
}
@@ -113,7 +116,9 @@ namespace UICatalog.Scenarios {
if (numberOfPanels > 2) {
AddMorePanels (root, 2, numberOfPanels);
panelsCreated = 2;
panelsToCreate = numberOfPanels;
AddMorePanels (root);
}
if (loaded) {
@@ -128,59 +133,57 @@ namespace UICatalog.Scenarios {
Height = Dim.Fill (),
Text = number.ToString ().Repeat (1000),
AllowsTab = false,
WordWrap = true,
//WordWrap = true, // TODO: This is very slow (like 10s to render with 45 panels)
};
}
private void AddMorePanels (SplitContainer to, int done, int numberOfPanels)
private void AddMorePanels (SplitContainer to)
{
if (done == numberOfPanels) {
return;
bool canSplitLeft = !(to.Panel1 is SplitContainer);
bool canSplitRight = !(to.Panel2 is SplitContainer);
if(canSplitRight) {
SplitRight (to);
}
View toSplit;
if (!(to.Panel1 is SplitContainer)) {
// we can split Panel1
var tv = (TextView)to.Panel1.Subviews.Single ();
var newContainer = CreateSplitContainer (to.Orientation, true);
to.Remove (to.Panel1);
to.Add (newContainer);
to.Panel1 = newContainer;
newContainer.Panel1.Add (tv);
newContainer.Panel2.Add (CreateTextView (++done));
AddMorePanels (to, done, numberOfPanels);
} else
if (!(to.Panel2 is SplitContainer)) {
// we can split Panel2
var tv = (TextView)to.Panel2.Subviews.Single ();
var newContainer = CreateSplitContainer (to.Orientation, true);
to.Remove (to.Panel2);
to.Add (newContainer);
to.Panel2 = newContainer;
newContainer.Panel1.Add (tv);
newContainer.Panel2.Add (CreateTextView (++done));
AddMorePanels (to, done, numberOfPanels);
} else {
// Both Panel1 and Panel2 are already SplitContainer
// So split one of the children
if(done % 2 == 0) {
AddMorePanels ((SplitContainer)to.Panel1, done, numberOfPanels);
} else {
AddMorePanels ((SplitContainer)to.Panel2, done, numberOfPanels);
}
if (canSplitLeft && panelsCreated < panelsToCreate) {
SplitLeft(to);
}
if (to.Panel1 is SplitContainer && to.Panel2 is SplitContainer) {
AddMorePanels ((SplitContainer)to.Panel1);
AddMorePanels ((SplitContainer)to.Panel2);
}
}
private void SplitLeft(SplitContainer to)
{
// we can split Panel1
var tv = (TextView)to.Panel1.Subviews.Single ();
var newContainer = CreateSplitContainer (to.Orientation, true);
to.Remove (to.Panel1);
to.Add (newContainer);
to.Panel1 = newContainer;
newContainer.Panel1.Add (tv);
newContainer.Panel2.Add (CreateTextView (++panelsCreated));
}
private void SplitRight(SplitContainer to)
{
// we can split Panel2
var tv = (TextView)to.Panel2.Subviews.Single ();
var newContainer = CreateSplitContainer (to.Orientation, true);
to.Remove (to.Panel2);
to.Add (newContainer);
to.Panel2 = newContainer;
newContainer.Panel2.Add (tv);
newContainer.Panel1.Add (CreateTextView (++panelsCreated));
}
private SplitContainer CreateSplitContainer (Orientation orientation, bool flip)