123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System.Linq;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Tab View", "Demos TabView control with limited screen space in Absolute layout.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("TabView")]
- public class TabViewExample : Scenario
- {
- private MenuItem _miShowBorder;
- private MenuItem _miShowTabViewBorder;
- private MenuItem _miShowTopLine;
- private MenuItem _miTabsOnBottom;
- private TabView _tabView;
- public override void Main ()
- {
- // Init
- Application.Init ();
- // Setup - Create a top-level application window and configure it.
- Toplevel appWindow = new ();
- var menu = new MenuBar
- {
- Menus =
- [
- new (
- "_File",
- new MenuItem []
- {
- new ("_Add Blank Tab", "", AddBlankTab),
- new (
- "_Clear SelectedTab",
- "",
- () => _tabView.SelectedTab = null
- ),
- new ("_Quit", "", Quit)
- }
- ),
- new (
- "_View",
- new []
- {
- _miShowTopLine =
- new ("_Show Top Line", "", ShowTopLine)
- {
- Checked = true, CheckType = MenuItemCheckStyle.Checked
- },
- _miShowBorder =
- new ("_Show Border", "", ShowBorder)
- {
- Checked = true, CheckType = MenuItemCheckStyle.Checked
- },
- _miTabsOnBottom =
- new ("_Tabs On Bottom", "", SetTabsOnBottom)
- {
- Checked = false, CheckType = MenuItemCheckStyle.Checked
- },
- _miShowTabViewBorder =
- new (
- "_Show TabView Border",
- "",
- ShowTabViewBorder
- ) { Checked = true, CheckType = MenuItemCheckStyle.Checked }
- }
- )
- ]
- };
- appWindow.Add (menu);
- _tabView = new()
- {
- X = 0,
- Y = 1,
- Width = 60,
- Height = 20,
- BorderStyle = LineStyle.Single
- };
- _tabView.AddTab (new() { DisplayText = "Tab1", View = new Label { Text = "hodor!" } }, false);
- _tabView.AddTab (new() { DisplayText = "Tab2", View = new TextField { Text = "durdur" } }, false);
- _tabView.AddTab (new() { DisplayText = "Interactive Tab", View = GetInteractiveTab () }, false);
- _tabView.AddTab (new() { DisplayText = "Big Text", View = GetBigTextFileTab () }, false);
- _tabView.AddTab (
- new()
- {
- 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
- {
- Text =
- "This tab has a very long name which should be truncated. See TabView.MaxTabTextWidth"
- }
- },
- false
- );
- _tabView.AddTab (
- new()
- {
- DisplayText = "Les Mise" + '\u0301' + "rables", View = new Label { Text = "This tab name is unicode" }
- },
- false
- );
- _tabView.AddTab (
- new()
- {
- DisplayText = "Les Mise" + '\u0328' + '\u0301' + "rables",
- View = new Label
- {
- Text =
- "This tab name has two combining marks. Only one will show due to Issue #2616."
- }
- },
- false
- );
- for (var i = 0; i < 100; i++)
- {
- _tabView.AddTab (
- new() { DisplayText = $"Tab{i}", View = new Label { Text = $"Welcome to tab {i}" } },
- false
- );
- }
- _tabView.SelectedTab = _tabView.Tabs.First ();
- appWindow.Add (_tabView);
- var frameRight = new View
- {
- X = Pos.Right (_tabView),
- Y = 1,
- Width = Dim.Fill (),
- Height = Dim.Fill (1),
- Title = "About",
- BorderStyle = LineStyle.Single,
- TabStop = TabBehavior.TabStop
- };
- frameRight.Add (
- new TextView
- {
- Text = "This demos the tabs control\nSwitch between tabs using cursor keys.\nThis TextView has AllowsTab = false, so tab should nav too.",
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- AllowsTab = false,
- }
- );
- appWindow.Add (frameRight);
- var frameBelow = new View
- {
- X = 0,
- Y = Pos.Bottom (_tabView),
- Width = _tabView.Width,
- Height = Dim.Fill (1),
- Title = "Bottom Frame",
- BorderStyle = LineStyle.Single,
- TabStop = TabBehavior.TabStop
- };
- frameBelow.Add (
- new TextView
- {
- Text =
- "This frame exists to check that you can still tab here\nand that the tab control doesn't overspill it's bounds\nAllowsTab is true.",
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- }
- );
- appWindow.Add (frameBelow);
- var statusBar = new StatusBar ([new (Application.QuitKey, "Quit", Quit)]);
- appWindow.Add (statusBar);
- // Run - Start the application.
- Application.Run (appWindow);
- appWindow.Dispose ();
- // Shutdown - Calling Application.Shutdown is required.
- Application.Shutdown ();
- }
- private void AddBlankTab () { _tabView.AddTab (new (), false); }
- private View GetBigTextFileTab ()
- {
- var text = new TextView { Width = Dim.Fill (), Height = Dim.Fill () };
- var sb = new StringBuilder ();
- for (var y = 0; y < 300; y++)
- {
- for (var x = 0; x < 500; x++)
- {
- sb.Append ((x + y) % 2 == 0 ? '1' : '0');
- }
- sb.AppendLine ();
- }
- text.Text = sb.ToString ();
- return text;
- }
- private View GetInteractiveTab ()
- {
- var interactiveTab = new View { Width = Dim.Fill (), Height = Dim.Fill () };
- var lblName = new Label { Text = "Name:" };
- interactiveTab.Add (lblName);
- var tbName = new TextField { X = Pos.Right (lblName), Width = 10 };
- interactiveTab.Add (tbName);
- var lblAddr = new Label { Y = 1, Text = "Address:" };
- interactiveTab.Add (lblAddr);
- var tbAddr = new TextField { X = Pos.Right (lblAddr), Y = 1, Width = 10 };
- interactiveTab.Add (tbAddr);
- return interactiveTab;
- }
- private void Quit () { Application.RequestStop (); }
- private void SetTabsOnBottom ()
- {
- _miTabsOnBottom.Checked = !_miTabsOnBottom.Checked;
- _tabView.Style.TabsOnBottom = (bool)_miTabsOnBottom.Checked;
- _tabView.ApplyStyleChanges ();
- }
- private void ShowBorder ()
- {
- _miShowBorder.Checked = !_miShowBorder.Checked;
- _tabView.Style.ShowBorder = (bool)_miShowBorder.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 ShowTopLine ()
- {
- _miShowTopLine.Checked = !_miShowTopLine.Checked;
- _tabView.Style.ShowTopLine = (bool)_miShowTopLine.Checked;
- _tabView.ApplyStyleChanges ();
- }
- }
|