ContextMenus.cs 11 KB

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