ContextMenus.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.Globalization;
  2. using JetBrains.Annotations;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("ContextMenus", "Context Menu Sample.")]
  6. [ScenarioCategory ("Menus")]
  7. public class ContextMenus : Scenario
  8. {
  9. [CanBeNull]
  10. private PopoverMenu _winContextMenu;
  11. private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight;
  12. private readonly List<CultureInfo> _cultureInfos = Application.SupportedCultures;
  13. private readonly Key _winContextMenuKey = Key.Space.WithCtrl;
  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. Arrangement = ViewArrangement.Fixed,
  23. SchemeName = "Toplevel"
  24. };
  25. var text = "Context Menu";
  26. var width = 20;
  27. CreateWinContextMenu ();
  28. var label = new Label
  29. {
  30. X = Pos.Center (), Y = 1, Text = $"Press '{_winContextMenuKey}' to open the Window context menu."
  31. };
  32. appWindow.Add (label);
  33. label = new ()
  34. {
  35. X = Pos.Center (),
  36. Y = Pos.Bottom (label),
  37. Text = $"Press '{PopoverMenu.DefaultKey}' to open the TextField context menu."
  38. };
  39. appWindow.Add (label);
  40. _tfTopLeft = new () { Id = "_tfTopLeft", Width = width, Text = text };
  41. appWindow.Add (_tfTopLeft);
  42. _tfTopRight = new () { Id = "_tfTopRight", X = Pos.AnchorEnd (width), Width = width, Text = text };
  43. appWindow.Add (_tfTopRight);
  44. _tfMiddle = new () { Id = "_tfMiddle", X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text };
  45. appWindow.Add (_tfMiddle);
  46. _tfBottomLeft = new () { Id = "_tfBottomLeft", Y = Pos.AnchorEnd (1), Width = width, Text = text };
  47. appWindow.Add (_tfBottomLeft);
  48. _tfBottomRight = new () { Id = "_tfBottomRight", X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text };
  49. appWindow.Add (_tfBottomRight);
  50. appWindow.KeyDown += OnAppWindowOnKeyDown;
  51. appWindow.MouseClick += OnAppWindowOnMouseClick;
  52. CultureInfo originalCulture = Thread.CurrentThread.CurrentUICulture;
  53. appWindow.Closed += (s, e) => { Thread.CurrentThread.CurrentUICulture = originalCulture; };
  54. // Run - Start the application.
  55. Application.Run (appWindow);
  56. appWindow.Dispose ();
  57. appWindow.KeyDown -= OnAppWindowOnKeyDown;
  58. appWindow.MouseClick -= OnAppWindowOnMouseClick;
  59. _winContextMenu?.Dispose ();
  60. // Shutdown - Calling Application.Shutdown is required.
  61. Application.Shutdown ();
  62. return;
  63. void OnAppWindowOnMouseClick (object s, MouseEventArgs e)
  64. {
  65. if (e.Flags == MouseFlags.Button3Clicked)
  66. {
  67. // ReSharper disable once AccessToDisposedClosure
  68. _winContextMenu?.MakeVisible (e.ScreenPosition);
  69. e.Handled = true;
  70. }
  71. }
  72. void OnAppWindowOnKeyDown (object s, Key e)
  73. {
  74. if (e == _winContextMenuKey)
  75. {
  76. // ReSharper disable once AccessToDisposedClosure
  77. _winContextMenu?.MakeVisible ();
  78. e.Handled = true;
  79. }
  80. }
  81. }
  82. private void CreateWinContextMenu ()
  83. {
  84. if (_winContextMenu is { })
  85. {
  86. _winContextMenu.Dispose ();
  87. _winContextMenu = null;
  88. }
  89. _winContextMenu = new (
  90. [
  91. new MenuItemv2
  92. {
  93. Title = "C_ultures",
  94. SubMenu = GetSupportedCultureMenu (),
  95. },
  96. new Line (),
  97. new MenuItemv2
  98. {
  99. Title = "_Configuration...",
  100. HelpText = "Show configuration",
  101. Action = () => MessageBox.Query (
  102. 50,
  103. 10,
  104. "Configuration",
  105. "This would be a configuration dialog",
  106. "Ok"
  107. )
  108. },
  109. new MenuItemv2
  110. {
  111. Title = "M_ore options",
  112. SubMenu = new (
  113. [
  114. new MenuItemv2
  115. {
  116. Title = "_Setup...",
  117. HelpText = "Perform setup",
  118. Action = () => MessageBox
  119. .Query (
  120. 50,
  121. 10,
  122. "Setup",
  123. "This would be a setup dialog",
  124. "Ok"
  125. ),
  126. Key = Key.T.WithCtrl
  127. },
  128. new MenuItemv2
  129. {
  130. Title = "_Maintenance...",
  131. HelpText = "Maintenance mode",
  132. Action = () => MessageBox
  133. .Query (
  134. 50,
  135. 10,
  136. "Maintenance",
  137. "This would be a maintenance dialog",
  138. "Ok"
  139. )
  140. }
  141. ])
  142. },
  143. new Line (),
  144. new MenuItemv2
  145. {
  146. Title = "_Quit",
  147. Action = () => Application.RequestStop ()
  148. }
  149. ])
  150. {
  151. Key = _winContextMenuKey
  152. };
  153. }
  154. private Menuv2 GetSupportedCultureMenu ()
  155. {
  156. List<MenuItemv2> supportedCultures = [];
  157. int index = -1;
  158. foreach (CultureInfo c in _cultureInfos)
  159. {
  160. MenuItemv2 culture = new ();
  161. culture.CommandView = new CheckBox { CanFocus = false };
  162. if (index == -1)
  163. {
  164. // Create English because GetSupportedCutures doesn't include it
  165. culture.Id = "_English";
  166. culture.Title = "_English";
  167. culture.HelpText = "en-US";
  168. ((CheckBox)culture.CommandView).CheckedState =
  169. Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? CheckState.Checked : CheckState.UnChecked;
  170. CreateAction (supportedCultures, culture);
  171. supportedCultures.Add (culture);
  172. index++;
  173. culture = new ();
  174. culture.CommandView = new CheckBox { CanFocus = false };
  175. }
  176. culture.Id = $"_{c.Parent.EnglishName}";
  177. culture.Title = $"_{c.Parent.EnglishName}";
  178. culture.HelpText = c.Name;
  179. ((CheckBox)culture.CommandView).CheckedState =
  180. Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked;
  181. CreateAction (supportedCultures, culture);
  182. supportedCultures.Add (culture);
  183. }
  184. Menuv2 menu = new (supportedCultures.ToArray ());
  185. return menu;
  186. void CreateAction (List<MenuItemv2> cultures, MenuItemv2 culture)
  187. {
  188. culture.Action += () =>
  189. {
  190. Thread.CurrentThread.CurrentUICulture = new (culture.HelpText);
  191. foreach (MenuItemv2 item in cultures)
  192. {
  193. ((CheckBox)item.CommandView).CheckedState =
  194. Thread.CurrentThread.CurrentUICulture.Name == item.HelpText ? CheckState.Checked : CheckState.UnChecked;
  195. }
  196. };
  197. }
  198. }
  199. public override List<Key> GetDemoKeyStrokes ()
  200. {
  201. List<Key> keys = new ();
  202. keys.Add (Key.F10.WithShift);
  203. keys.Add (Key.Esc);
  204. keys.Add (Key.Space.WithCtrl);
  205. keys.Add (Key.CursorDown);
  206. keys.Add (Key.Enter);
  207. keys.Add (Key.F10.WithShift);
  208. keys.Add (Key.Esc);
  209. keys.Add (Key.Tab);
  210. keys.Add (Key.Space.WithCtrl);
  211. keys.Add (Key.CursorDown);
  212. keys.Add (Key.CursorDown);
  213. keys.Add (Key.Enter);
  214. keys.Add (Key.F10.WithShift);
  215. keys.Add (Key.Esc);
  216. keys.Add (Key.Tab);
  217. keys.Add (Key.Space.WithCtrl);
  218. keys.Add (Key.CursorDown);
  219. keys.Add (Key.CursorDown);
  220. keys.Add (Key.CursorDown);
  221. keys.Add (Key.Enter);
  222. keys.Add (Key.F10.WithShift);
  223. keys.Add (Key.Esc);
  224. return keys;
  225. }
  226. }