Files
Terminal.Gui/UICatalog/Scenarios/TabViewExample.cs
BDisp 56943f17d2 Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame (#2980)
* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame

* Trying fix this unit test that sometimes fail.

* Fixes #2983. View need a alternative DrawFrame for the v2.

* Use new DrawFrame method.

* Change _lines field to Lines property.

* Add TabWindow unit test.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Revert "Change _lines field to Lines property."

This reverts commit ab6c5f3094.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Force render immediately instead of join.

* I will never rely on zero-location-based unit test again.

* Fix TestTreeViewColor unit test fail.

* Using location of 3 to avoid be divisible by 2 and so avoiding bugs.

* Revert "Using location of 3 to avoid be divisible by 2 and so avoiding bugs."

This reverts commit dd3df135d8.

* Revert "I will never rely on zero-location-based unit test again."

This reverts commit 62adf6f285.

* Revert "Fix newline conflict errors."

This reverts commit 4acf72612d.

* Revert "Add more unit tests to LineCanvas."

This reverts commit 66bc6f514e.

* Revert "Add DrawIncompleteFrame method and unit tests."

This reverts commit 680ba264e1.

* Resolving merge conflicts.

* Revert "Use new DrawFrame method."

This reverts commit 69a7f17f19.

* Revert "Fixes #2983. View need a alternative DrawFrame for the v2."

This reverts commit dade9fd767.

* Reverting this changes to start a new one.

* Add horizontal and vertical support for combining glyphs.

* Fix text and auto size behavior.

* Add TabWidth property.

* Add unit test for WordWrap.

* Fixes #3017. View TextDirection returns incorrect size on a vertical direction instance with AutoSize as false.

* Using Frame to force read from the get method.

* Fix some issues with AutoSize and ForceValidatePosDim.

* Fixing broken unit tests.

* Restoring code I've broken.

* Removing forgotten code.

* Only LayoutStyle.Computed can change the Frame.

* DateField and TimeField depends on LayoutStyle.Computed.

* Fix unit tests related with LayoutStyle.

* Implements tabs, left and right arrows as View.

* Draws a minimum full border.

* Adds missing XML parameter.

* Adds assert tests for Frame.

* Removes duplicates InlineData.

* Adds more unit tests for minimum full border without Left and Right thickness.

* Trying to fix the TestTreeViewColor unit test fail.

* Prevents a user to set TextDirection to -1.

* Prevents any invalid TextDirection value.

* Removes (TextDirection)(-1).

* Removes unnecessary TextDirection initialization.

* Removes LayoutStyle.

* Fixing unit tests with border.

* Trying to fix TestTreeViewColor again.

* Revert "Trying to fix TestTreeViewColor again."

This reverts commit c2efa8e42e.

* Trying to fix TestTreeViewColor again.

* Fix merge errors.

* Fix merge errors.

* Restoring unit test.

* Restores the right XML comment.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Only call OnResizeNeeded if it's LayoutStyle.Computed.

* Fix merge errors.

* Fix merge errors.

* Fix unit tests fail.

* Reformat.

* Again.

* Rename to OnDrawAdornments.

* Fix failing unit tests.

* Reduces indentation and cleanup code.

* Cleanup code.

* Fix bug done when cleanup.

* Replace FrameHandledMouseEvent to AdornmentHandledMouseEvent.

* Removes Tab constructor parameters.

---------

Co-authored-by: Tig <tig@users.noreply.github.com>
2024-01-16 10:05:06 -07:00

208 lines
5.7 KiB
C#

using System.Linq;
using Terminal.Gui;
namespace UICatalog.Scenarios;
[ScenarioMetadata (Name: "Tab View", Description: "Demos TabView control with limited screen space in Absolute layout.")]
[ScenarioCategory ("Controls"), ScenarioCategory ("TabView")]
public class TabViewExample : Scenario {
TabView _tabView;
MenuItem _miShowTopLine;
MenuItem _miShowBorder;
MenuItem _miTabsOnBottom;
MenuItem _miShowTabViewBorder;
public override void Setup ()
{
Win.Title = this.GetName ();
Win.Y = 1; // menu
Win.Height = Dim.Fill (1); // status bar
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_Add Blank Tab", "", () => AddBlankTab()),
new MenuItem ("_Clear SelectedTab", "", () => _tabView.SelectedTab=null),
new MenuItem ("_Quit", "", () => Quit()),
}),
new MenuBarItem ("_View", new MenuItem [] {
_miShowTopLine = new MenuItem ("_Show Top Line", "", () => ShowTopLine()){
Checked = true,
CheckType = MenuItemCheckStyle.Checked
},
_miShowBorder = new MenuItem ("_Show Border", "", () => ShowBorder()){
Checked = true,
CheckType = MenuItemCheckStyle.Checked
},
_miTabsOnBottom = new MenuItem ("_Tabs On Bottom", "", () => SetTabsOnBottom()){
Checked = false,
CheckType = MenuItemCheckStyle.Checked
},
_miShowTabViewBorder = new MenuItem ("_Show TabView Border", "", () => ShowTabViewBorder()){
Checked = true,
CheckType = MenuItemCheckStyle.Checked
}
})
});
Application.Top.Add (menu);
_tabView = new TabView () {
X = 0,
Y = 0,
Width = 60,
Height = 20,
BorderStyle = LineStyle.Single
};
_tabView.AddTab (new Tab () { DisplayText = "Tab1", View = new Label ("hodor!") }, false);
_tabView.AddTab (new Tab () { DisplayText = "Tab2", View = new TextField ("durdur") }, false);
_tabView.AddTab (new Tab () { DisplayText = "Interactive Tab", View = GetInteractiveTab () }, false);
_tabView.AddTab (new Tab () { DisplayText = "Big Text", View = GetBigTextFileTab () }, false);
_tabView.AddTab (new Tab () {
DisplayText = "Long name Tab, I mean seriously long. Like you would not believe how long this tab's name is its just too much really woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooowwww thats long",
View = new Label ("This tab has a very long name which should be truncated. See TabView.MaxTabTextWidth")
}, false);
_tabView.AddTab (new Tab () { DisplayText = "Les Mise" + '\u0301' + "rables", View = new Label ("This tab name is unicode") }, false);
_tabView.AddTab (new Tab () { DisplayText = "Les Mise" + '\u0328' + '\u0301' + "rables", View = new Label ("This tab name has two combining marks. Only one will show due to Issue #2616.") }, false);
for (int i = 0; i < 100; i++) {
_tabView.AddTab (new Tab () { DisplayText = $"Tab{i}", View = new Label($"Welcome to tab {i}") }, false);
}
_tabView.SelectedTab = _tabView.Tabs.First ();
Win.Add (_tabView);
var frameRight = new FrameView ("About") {
X = Pos.Right (_tabView),
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
};
frameRight.Add (new TextView () {
Text = "This demos the tabs control\nSwitch between tabs using cursor keys",
Width = Dim.Fill (),
Height = Dim.Fill ()
});
Win.Add (frameRight);
var frameBelow = new FrameView ("Bottom Frame") {
X = 0,
Y = Pos.Bottom (_tabView),
Width = _tabView.Width,
Height = Dim.Fill (),
};
frameBelow.Add (new TextView () {
Text = "This frame exists to check you can still tab here\nand that the tab control doesn't overspill it's bounds",
Width = Dim.Fill (),
Height = Dim.Fill ()
});
Win.Add (frameBelow);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit()),
});
Application.Top.Add (statusBar);
}
private void AddBlankTab ()
{
_tabView.AddTab (new Tab (), false);
}
private View GetInteractiveTab ()
{
var interactiveTab = new View () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var lblName = new Label ("Name:");
interactiveTab.Add (lblName);
var tbName = new TextField () {
X = Pos.Right (lblName),
Width = 10
};
interactiveTab.Add (tbName);
var lblAddr = new Label ("Address:") {
Y = 1
};
interactiveTab.Add (lblAddr);
var tbAddr = new TextField () {
X = Pos.Right (lblAddr),
Y = 1,
Width = 10
};
interactiveTab.Add (tbAddr);
return interactiveTab;
}
private View GetBigTextFileTab ()
{
var text = new TextView () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var sb = new System.Text.StringBuilder ();
for (int y = 0; y < 300; y++) {
for (int x = 0; x < 500; x++) {
sb.Append ((x + y) % 2 == 0 ? '1' : '0');
}
sb.AppendLine ();
}
text.Text = sb.ToString ();
return text;
}
private void ShowTopLine ()
{
_miShowTopLine.Checked = !_miShowTopLine.Checked;
_tabView.Style.ShowTopLine = (bool)_miShowTopLine.Checked;
_tabView.ApplyStyleChanges ();
}
private void ShowBorder ()
{
_miShowBorder.Checked = !_miShowBorder.Checked;
_tabView.Style.ShowBorder = (bool)_miShowBorder.Checked;
_tabView.ApplyStyleChanges ();
}
private void SetTabsOnBottom ()
{
_miTabsOnBottom.Checked = !_miTabsOnBottom.Checked;
_tabView.Style.TabsOnBottom = (bool)_miTabsOnBottom.Checked;
_tabView.ApplyStyleChanges ();
}
private void ShowTabViewBorder ()
{
_miShowTabViewBorder.Checked = !_miShowTabViewBorder.Checked;
_tabView.BorderStyle = _miShowTabViewBorder.Checked == true ? _tabView.BorderStyle = LineStyle.Single
: LineStyle.None;
_tabView.ApplyStyleChanges ();
}
private void Quit ()
{
Application.RequestStop ();
}
}