MenuBarScenario.cs 5.0 KB

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