ContextMenus.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Threading;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("ContextMenus", "Context Menu Sample.")]
  7. [ScenarioCategory ("Menus")]
  8. public class ContextMenus : Scenario
  9. {
  10. private readonly List<CultureInfo> _cultureInfos = Application.SupportedCultures;
  11. private ContextMenu _contextMenu = new ();
  12. private bool _forceMinimumPosToZero = true;
  13. private MenuItem _miForceMinimumPosToZero;
  14. private MenuItem _miUseSubMenusSingleFrame;
  15. private TextField _tfTopLeft, _tfTopRight, _tfMiddle, _tfBottomLeft, _tfBottomRight;
  16. private bool _useSubMenusSingleFrame;
  17. public override void Main ()
  18. {
  19. // Init
  20. Application.Init ();
  21. // Setup - Create a top-level application window and configure it.
  22. Window appWindow = new ()
  23. {
  24. Title = GetQuitKeyAndName ()
  25. };
  26. var text = "Context Menu";
  27. var width = 20;
  28. var winContextMenuKey = (KeyCode)Key.Space.WithCtrl;
  29. var label = new Label
  30. {
  31. X = Pos.Center (), Y = 1, Text = $"Press '{winContextMenuKey}' to open the Window context menu."
  32. };
  33. appWindow.Add (label);
  34. label = new()
  35. {
  36. X = Pos.Center (),
  37. Y = Pos.Bottom (label),
  38. Text = $"Press '{ContextMenu.DefaultKey}' to open the TextField context menu."
  39. };
  40. appWindow.Add (label);
  41. _tfTopLeft = new() { Width = width, Text = text };
  42. appWindow.Add (_tfTopLeft);
  43. _tfTopRight = new() { X = Pos.AnchorEnd (width), Width = width, Text = text };
  44. appWindow.Add (_tfTopRight);
  45. _tfMiddle = new() { X = Pos.Center (), Y = Pos.Center (), Width = width, Text = text };
  46. appWindow.Add (_tfMiddle);
  47. _tfBottomLeft = new() { Y = Pos.AnchorEnd (1), Width = width, Text = text };
  48. appWindow.Add (_tfBottomLeft);
  49. _tfBottomRight = new() { X = Pos.AnchorEnd (width), Y = Pos.AnchorEnd (1), Width = width, Text = text };
  50. appWindow.Add (_tfBottomRight);
  51. Point mousePos = default;
  52. appWindow.KeyDown += (s, e) =>
  53. {
  54. if (e.KeyCode == winContextMenuKey)
  55. {
  56. ShowContextMenu (mousePos.X, mousePos.Y);
  57. e.Handled = true;
  58. }
  59. };
  60. appWindow.MouseClick += (s, e) =>
  61. {
  62. if (e.MouseEvent.Flags == _contextMenu.MouseFlags)
  63. {
  64. ShowContextMenu (e.MouseEvent.Position.X, e.MouseEvent.Position.Y);
  65. e.Handled = true;
  66. }
  67. };
  68. Application.MouseEvent += ApplicationMouseEvent;
  69. void ApplicationMouseEvent (object sender, MouseEvent a) { mousePos = a.Position; }
  70. appWindow.WantMousePositionReports = true;
  71. appWindow.Closed += (s, e) =>
  72. {
  73. Thread.CurrentThread.CurrentUICulture = new ("en-US");
  74. Application.MouseEvent -= ApplicationMouseEvent;
  75. };
  76. // Run - Start the application.
  77. Application.Run (appWindow);
  78. appWindow.Dispose ();
  79. // Shutdown - Calling Application.Shutdown is required.
  80. Application.Shutdown ();
  81. }
  82. private MenuItem [] GetSupportedCultures ()
  83. {
  84. List<MenuItem> supportedCultures = new ();
  85. int index = -1;
  86. foreach (CultureInfo c in _cultureInfos)
  87. {
  88. var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked };
  89. if (index == -1)
  90. {
  91. culture.Title = "_English";
  92. culture.Help = "en-US";
  93. culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US";
  94. CreateAction (supportedCultures, culture);
  95. supportedCultures.Add (culture);
  96. index++;
  97. culture = new() { CheckType = MenuItemCheckStyle.Checked };
  98. }
  99. culture.Title = $"_{c.Parent.EnglishName}";
  100. culture.Help = c.Name;
  101. culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name;
  102. CreateAction (supportedCultures, culture);
  103. supportedCultures.Add (culture);
  104. }
  105. return supportedCultures.ToArray ();
  106. void CreateAction (List<MenuItem> supportedCultures, MenuItem culture)
  107. {
  108. culture.Action += () =>
  109. {
  110. Thread.CurrentThread.CurrentUICulture = new (culture.Help);
  111. culture.Checked = true;
  112. foreach (MenuItem item in supportedCultures)
  113. {
  114. item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name;
  115. }
  116. };
  117. }
  118. }
  119. private void ShowContextMenu (int x, int y)
  120. {
  121. _contextMenu = new()
  122. {
  123. Position = new (x, y),
  124. MenuItems = new (
  125. new []
  126. {
  127. new (
  128. "_Configuration",
  129. "Show configuration",
  130. () => MessageBox.Query (
  131. 50,
  132. 5,
  133. "Info",
  134. "This would open settings dialog",
  135. "Ok"
  136. )
  137. ),
  138. new MenuBarItem (
  139. "More options",
  140. new MenuItem []
  141. {
  142. new (
  143. "_Setup",
  144. "Change settings",
  145. () => MessageBox
  146. .Query (
  147. 50,
  148. 5,
  149. "Info",
  150. "This would open setup dialog",
  151. "Ok"
  152. ),
  153. shortcut: KeyCode.T
  154. | KeyCode
  155. .CtrlMask
  156. ),
  157. new (
  158. "_Maintenance",
  159. "Maintenance mode",
  160. () => MessageBox
  161. .Query (
  162. 50,
  163. 5,
  164. "Info",
  165. "This would open maintenance dialog",
  166. "Ok"
  167. )
  168. )
  169. }
  170. ),
  171. new MenuBarItem (
  172. "_Languages",
  173. GetSupportedCultures ()
  174. ),
  175. _miForceMinimumPosToZero =
  176. new (
  177. "ForceMinimumPosToZero",
  178. "",
  179. () =>
  180. {
  181. _miForceMinimumPosToZero
  182. .Checked =
  183. _forceMinimumPosToZero =
  184. !_forceMinimumPosToZero;
  185. _tfTopLeft.ContextMenu
  186. .ForceMinimumPosToZero =
  187. _forceMinimumPosToZero;
  188. _tfTopRight.ContextMenu
  189. .ForceMinimumPosToZero =
  190. _forceMinimumPosToZero;
  191. _tfMiddle.ContextMenu
  192. .ForceMinimumPosToZero =
  193. _forceMinimumPosToZero;
  194. _tfBottomLeft.ContextMenu
  195. .ForceMinimumPosToZero =
  196. _forceMinimumPosToZero;
  197. _tfBottomRight
  198. .ContextMenu
  199. .ForceMinimumPosToZero =
  200. _forceMinimumPosToZero;
  201. }
  202. )
  203. {
  204. CheckType =
  205. MenuItemCheckStyle
  206. .Checked,
  207. Checked =
  208. _forceMinimumPosToZero
  209. },
  210. _miUseSubMenusSingleFrame =
  211. new (
  212. "Use_SubMenusSingleFrame",
  213. "",
  214. () => _contextMenu
  215. .UseSubMenusSingleFrame =
  216. (bool)
  217. (_miUseSubMenusSingleFrame
  218. .Checked =
  219. _useSubMenusSingleFrame =
  220. !_useSubMenusSingleFrame)
  221. )
  222. {
  223. CheckType = MenuItemCheckStyle
  224. .Checked,
  225. Checked =
  226. _useSubMenusSingleFrame
  227. },
  228. null,
  229. new (
  230. "_Quit",
  231. "",
  232. () => Application.RequestStop ()
  233. )
  234. }
  235. ),
  236. ForceMinimumPosToZero = _forceMinimumPosToZero,
  237. UseSubMenusSingleFrame = _useSubMenusSingleFrame
  238. };
  239. _tfTopLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  240. _tfTopRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  241. _tfMiddle.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  242. _tfBottomLeft.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  243. _tfBottomRight.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  244. _contextMenu.Show ();
  245. }
  246. }