ContextMenus.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.MouseClick -= OnAppWindowOnMouseClick;
  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.MouseClick += OnAppWindowOnMouseClick;
  65. CultureInfo originalCulture = Thread.CurrentThread.CurrentUICulture;
  66. _appWindow.IsRunningChanged += (s, e) => {
  67. if (!e.Value)
  68. {
  69. Thread.CurrentThread.CurrentUICulture = originalCulture;
  70. } };
  71. }
  72. void OnAppWindowOnMouseClick (object? s, MouseEventArgs e)
  73. {
  74. if (e.Flags == MouseFlags.Button3Clicked)
  75. {
  76. // ReSharper disable once AccessToDisposedClosure
  77. _winContextMenu?.MakeVisible (e.ScreenPosition);
  78. e.Handled = true;
  79. }
  80. }
  81. void OnAppWindowOnKeyDown (object? s, Key e)
  82. {
  83. if (e == _winContextMenuKey)
  84. {
  85. // ReSharper disable once AccessToDisposedClosure
  86. _winContextMenu?.MakeVisible ();
  87. e.Handled = true;
  88. }
  89. }
  90. }
  91. private void CreateWinContextMenu (IApplication? app)
  92. {
  93. _winContextMenu = new (
  94. [
  95. new MenuItem
  96. {
  97. Title = "C_ultures",
  98. SubMenu = GetSupportedCultureMenu (),
  99. },
  100. new Line (),
  101. new MenuItem
  102. {
  103. Title = "_Configuration...",
  104. HelpText = "Show configuration",
  105. Action = () => MessageBox.Query (app,
  106. 50,
  107. 10,
  108. "Configuration",
  109. "This would be a configuration dialog",
  110. "Ok"
  111. )
  112. },
  113. new MenuItem
  114. {
  115. Title = "M_ore options",
  116. SubMenu = new (
  117. [
  118. new MenuItem
  119. {
  120. Title = "_Setup...",
  121. HelpText = "Perform setup",
  122. Action = () => MessageBox
  123. .Query (app,
  124. 50,
  125. 10,
  126. "Setup",
  127. "This would be a setup dialog",
  128. "Ok"
  129. ),
  130. Key = Key.T.WithCtrl
  131. },
  132. new MenuItem
  133. {
  134. Title = "_Maintenance...",
  135. HelpText = "Maintenance mode",
  136. Action = () => MessageBox
  137. .Query (app,
  138. 50,
  139. 10,
  140. "Maintenance",
  141. "This would be a maintenance dialog",
  142. "Ok"
  143. )
  144. }
  145. ])
  146. },
  147. new Line (),
  148. new MenuItem
  149. {
  150. Title = "_Quit",
  151. Action = () => Application.RequestStop ()
  152. }
  153. ])
  154. {
  155. Key = _winContextMenuKey
  156. };
  157. Application.Popover?.Register (_winContextMenu);
  158. }
  159. private Menu GetSupportedCultureMenu ()
  160. {
  161. List<MenuItem> supportedCultures = [];
  162. int index = -1;
  163. foreach (CultureInfo c in _cultureInfos!)
  164. {
  165. MenuItem culture = new ();
  166. culture.CommandView = new CheckBox { CanFocus = false };
  167. if (index == -1)
  168. {
  169. // Create English because GetSupportedCutures doesn't include it
  170. culture.Id = "_English";
  171. culture.Title = "_English";
  172. culture.HelpText = "en-US";
  173. ((CheckBox)culture.CommandView).CheckedState =
  174. Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? CheckState.Checked : CheckState.UnChecked;
  175. CreateAction (supportedCultures, culture);
  176. supportedCultures.Add (culture);
  177. index++;
  178. culture = new ();
  179. culture.CommandView = new CheckBox { CanFocus = false };
  180. }
  181. culture.Id = $"_{c.Parent.EnglishName}";
  182. culture.Title = $"_{c.Parent.EnglishName}";
  183. culture.HelpText = c.Name;
  184. ((CheckBox)culture.CommandView).CheckedState =
  185. Thread.CurrentThread.CurrentUICulture.Name == culture.HelpText ? CheckState.Checked : CheckState.UnChecked;
  186. CreateAction (supportedCultures, culture);
  187. supportedCultures.Add (culture);
  188. }
  189. Menu menu = new (supportedCultures.ToArray ());
  190. return menu;
  191. void CreateAction (List<MenuItem> cultures, MenuItem culture)
  192. {
  193. culture.Action += () =>
  194. {
  195. Thread.CurrentThread.CurrentUICulture = new (culture.HelpText);
  196. foreach (MenuItem item in cultures)
  197. {
  198. ((CheckBox)item.CommandView).CheckedState =
  199. Thread.CurrentThread.CurrentUICulture.Name == item.HelpText ? CheckState.Checked : CheckState.UnChecked;
  200. }
  201. };
  202. }
  203. }
  204. public override List<Key> GetDemoKeyStrokes ()
  205. {
  206. List<Key> keys = new ();
  207. keys.Add (Key.F10.WithShift);
  208. keys.Add (Key.Esc);
  209. keys.Add (Key.Space.WithCtrl);
  210. keys.Add (Key.CursorDown);
  211. keys.Add (Key.Enter);
  212. keys.Add (Key.F10.WithShift);
  213. keys.Add (Key.Esc);
  214. keys.Add (Key.Tab);
  215. keys.Add (Key.Space.WithCtrl);
  216. keys.Add (Key.CursorDown);
  217. keys.Add (Key.CursorDown);
  218. keys.Add (Key.Enter);
  219. keys.Add (Key.F10.WithShift);
  220. keys.Add (Key.Esc);
  221. keys.Add (Key.Tab);
  222. keys.Add (Key.Space.WithCtrl);
  223. keys.Add (Key.CursorDown);
  224. keys.Add (Key.CursorDown);
  225. keys.Add (Key.CursorDown);
  226. keys.Add (Key.Enter);
  227. keys.Add (Key.F10.WithShift);
  228. keys.Add (Key.Esc);
  229. return keys;
  230. }
  231. }