MenuBarScenario.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using Terminal.Gui;
  3. using static System.Runtime.InteropServices.JavaScript.JSType;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("MenuBar", "Demonstrates the MenuBar using the demo menu.")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Menus")]
  8. public class MenuBarScenario : Scenario
  9. {
  10. private Label _currentMenuBarItem;
  11. private Label _currentMenuItem;
  12. private Label _focusedView;
  13. private Label _lastAction;
  14. private Label _lastKey;
  15. public override void Main ()
  16. {
  17. // Init
  18. Application.Init ();
  19. // Setup - Create a top-level application window and configure it.
  20. Window appWindow = new ()
  21. {
  22. Title = GetQuitKeyAndName (),
  23. BorderStyle = LineStyle.None
  24. };
  25. MenuItem mbiCurrent = null;
  26. MenuItem miCurrent = null;
  27. var label = new Label { X = 0, Y = 10, Text = "Last Key: " };
  28. appWindow.Add (label);
  29. _lastKey = new Label { X = Pos.Right (label), Y = Pos.Top (label), Text = "" };
  30. appWindow.Add (_lastKey);
  31. label = new Label { X = 0, Y = Pos.Bottom (label), Text = "Current MenuBarItem: " };
  32. appWindow.Add (label);
  33. _currentMenuBarItem = new Label { X = Pos.Right (label), Y = Pos.Top (label), Text = "" };
  34. appWindow.Add (_currentMenuBarItem);
  35. label = new Label { X = 0, Y = Pos.Bottom (label), Text = "Current MenuItem: " };
  36. appWindow.Add (label);
  37. _currentMenuItem = new Label { X = Pos.Right (label), Y = Pos.Top (label), Text = "" };
  38. appWindow.Add (_currentMenuItem);
  39. label = new Label { X = 0, Y = Pos.Bottom (label), Text = "Last Action: " };
  40. appWindow.Add (label);
  41. _lastAction = new Label { X = Pos.Right (label), Y = Pos.Top (label), Text = "" };
  42. appWindow.Add (_lastAction);
  43. label = new Label { X = 0, Y = Pos.Bottom (label), Text = "Focused View: " };
  44. appWindow.Add (label);
  45. _focusedView = new Label { X = Pos.Right (label), Y = Pos.Top (label), Text = "" };
  46. appWindow.Add (_focusedView);
  47. MenuBar menuBar = new MenuBar ();
  48. menuBar.UseKeysUpDownAsKeysLeftRight = true;
  49. menuBar.Key = KeyCode.F9;
  50. menuBar.Title = "TestMenuBar";
  51. bool FnAction (string s)
  52. {
  53. _lastAction.Text = s;
  54. return true;
  55. }
  56. // Declare a variable for the function
  57. Func<string, bool> fnActionVariable = FnAction;
  58. menuBar.EnableForDesign (ref fnActionVariable);
  59. menuBar.MenuOpening += (s, e) =>
  60. {
  61. mbiCurrent = e.CurrentMenu;
  62. SetCurrentMenuBarItem (mbiCurrent);
  63. SetCurrentMenuItem (miCurrent);
  64. _lastAction.Text = string.Empty;
  65. };
  66. menuBar.MenuOpened += (s, e) =>
  67. {
  68. miCurrent = e.MenuItem;
  69. SetCurrentMenuBarItem (mbiCurrent);
  70. SetCurrentMenuItem (miCurrent);
  71. };
  72. menuBar.MenuClosing += (s, e) =>
  73. {
  74. mbiCurrent = null;
  75. miCurrent = null;
  76. SetCurrentMenuBarItem (mbiCurrent);
  77. SetCurrentMenuItem (miCurrent);
  78. };
  79. Application.KeyDown += (s, e) =>
  80. {
  81. _lastAction.Text = string.Empty;
  82. _lastKey.Text = e.ToString ();
  83. };
  84. // There's no focus change event, so this is a bit of a hack.
  85. menuBar.SubviewsLaidOut += (s, e) => { _focusedView.Text = appWindow.MostFocused?.ToString () ?? "None"; };
  86. var openBtn = new Button { X = Pos.Center (), Y = 4, Text = "_Open Menu", IsDefault = true };
  87. openBtn.Accepting += (s, e) => { menuBar.OpenMenu (); };
  88. appWindow.Add (openBtn);
  89. var hideBtn = new Button { X = Pos.Center (), Y = Pos.Bottom (openBtn), Text = "Toggle Menu._Visible" };
  90. hideBtn.Accepting += (s, e) => { menuBar.Visible = !menuBar.Visible; };
  91. appWindow.Add (hideBtn);
  92. var enableBtn = new Button { X = Pos.Center (), Y = Pos.Bottom (hideBtn), Text = "_Toggle Menu.Enable" };
  93. enableBtn.Accepting += (s, e) => { menuBar.Enabled = !menuBar.Enabled; };
  94. appWindow.Add (enableBtn);
  95. appWindow.Add (menuBar);
  96. // Run - Start the application.
  97. Application.Run (appWindow);
  98. appWindow.Dispose ();
  99. // Shutdown - Calling Application.Shutdown is required.
  100. Application.Shutdown ();
  101. }
  102. private void SetCurrentMenuBarItem (MenuItem mbi) { _currentMenuBarItem.Text = mbi != null ? mbi.Title : "Closed"; }
  103. private void SetCurrentMenuItem (MenuItem mi) { _currentMenuItem.Text = mi != null ? mi.Title : "None"; }
  104. }