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