ContextMenus.cs 11 KB

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