ContextMenus.cs 14 KB

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