Menu.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. //
  2. // Menu.cs: application menus and submenus
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // TODO:
  8. // Add accelerator support, but should also support chords (Shortcut in MenuItem)
  9. // Allow menus inside menus
  10. using System;
  11. using NStack;
  12. using System.Linq;
  13. using System.Collections.Generic;
  14. namespace Terminal.Gui {
  15. /// <summary>
  16. /// Specifies how a <see cref="MenuItem"/> shows selection state.
  17. /// </summary>
  18. [Flags]
  19. public enum MenuItemCheckStyle {
  20. /// <summary>
  21. /// The menu item will be shown normally, with no check indicator.
  22. /// </summary>
  23. NoCheck = 0b_0000_0000,
  24. /// <summary>
  25. /// The menu item will indicate checked/un-checked state (see <see cref="Checked"/>.
  26. /// </summary>
  27. Checked = 0b_0000_0001,
  28. /// <summary>
  29. /// The menu item is part of a menu radio group (see <see cref="Checked"/> and will indicate selected state.
  30. /// </summary>
  31. Radio = 0b_0000_0010,
  32. };
  33. /// <summary>
  34. /// A <see cref="MenuItem"/> has a title, an associated help text, and an action to execute on activation.
  35. /// </summary>
  36. public class MenuItem {
  37. ustring title;
  38. ShortcutHelper shortcutHelper;
  39. /// <summary>
  40. /// Initializes a new instance of <see cref="MenuItem"/>
  41. /// </summary>
  42. public MenuItem (Key shortcut = Key.Null) : this ("", "", null, null, null, shortcut) { }
  43. /// <summary>
  44. /// Initializes a new instance of <see cref="MenuItem"/>.
  45. /// </summary>
  46. /// <param name="title">Title for the menu item.</param>
  47. /// <param name="help">Help text to display.</param>
  48. /// <param name="action">Action to invoke when the menu item is activated.</param>
  49. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  50. /// <param name="parent">The <see cref="Parent"/> of this menu item.</param>
  51. /// <param name="shortcut">The <see cref="Shortcut"/> keystroke combination.</param>
  52. public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null, Key shortcut = Key.Null)
  53. {
  54. Title = title ?? "";
  55. Help = help ?? "";
  56. Action = action;
  57. CanExecute = canExecute;
  58. Parent = parent;
  59. shortcutHelper = new ShortcutHelper ();
  60. if (shortcut != Key.Null) {
  61. shortcutHelper.Shortcut = shortcut;
  62. }
  63. }
  64. /// <summary>
  65. /// The HotKey is used when the menu is active, the shortcut can be triggered when the menu is not active.
  66. /// For example HotKey would be "N" when the File Menu is open (assuming there is a "_New" entry
  67. /// if the Shortcut is set to "Control-N", this would be a global hotkey that would trigger as well
  68. /// </summary>
  69. public Rune HotKey;
  70. /// <summary>
  71. /// This is the global setting that can be used as a global <see cref="ShortcutHelper.Shortcut"/> to invoke the action on the menu.
  72. /// </summary>
  73. public Key Shortcut {
  74. get => shortcutHelper.Shortcut;
  75. set {
  76. if (shortcutHelper.Shortcut != value && (ShortcutHelper.PostShortcutValidation (value) || value == Key.Null)) {
  77. shortcutHelper.Shortcut = value;
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// The keystroke combination used in the <see cref="ShortcutHelper.ShortcutTag"/> as string.
  83. /// </summary>
  84. public ustring ShortcutTag => ShortcutHelper.GetShortcutTag (shortcutHelper.Shortcut);
  85. /// <summary>
  86. /// Gets or sets the title.
  87. /// </summary>
  88. /// <value>The title.</value>
  89. public ustring Title {
  90. get { return title; }
  91. set {
  92. if (title != value) {
  93. title = value;
  94. GetHotKey ();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets the help text for the menu item.
  100. /// </summary>
  101. /// <value>The help text.</value>
  102. public ustring Help { get; set; }
  103. /// <summary>
  104. /// Gets or sets the action to be invoked when the menu is triggered
  105. /// </summary>
  106. /// <value>Method to invoke.</value>
  107. public Action Action { get; set; }
  108. /// <summary>
  109. /// Gets or sets the action to be invoked if the menu can be triggered
  110. /// </summary>
  111. /// <value>Function to determine if action is ready to be executed.</value>
  112. public Func<bool> CanExecute { get; set; }
  113. /// <summary>
  114. /// Shortcut to check if the menu item is enabled
  115. /// </summary>
  116. public bool IsEnabled ()
  117. {
  118. return CanExecute == null ? true : CanExecute ();
  119. }
  120. internal int Width => Title.RuneCount + Help.RuneCount + 1 + 2 +
  121. (Checked || CheckType.HasFlag (MenuItemCheckStyle.Checked) || CheckType.HasFlag (MenuItemCheckStyle.Radio) ? 2 : 0) +
  122. (ShortcutTag.RuneCount > 0 ? ShortcutTag.RuneCount + 2 : 0);
  123. /// <summary>
  124. /// Sets or gets whether the <see cref="MenuItem"/> shows a check indicator or not. See <see cref="MenuItemCheckStyle"/>.
  125. /// </summary>
  126. public bool Checked { set; get; }
  127. /// <summary>
  128. /// Sets or gets the type selection indicator the menu item will be displayed with.
  129. /// </summary>
  130. public MenuItemCheckStyle CheckType { get; set; }
  131. /// <summary>
  132. /// Gets or sets the parent for this <see cref="MenuItem"/>.
  133. /// </summary>
  134. /// <value>The parent.</value>
  135. public MenuItem Parent { get; internal set; }
  136. /// <summary>
  137. /// Gets if this <see cref="MenuItem"/> is from a sub-menu.
  138. /// </summary>
  139. internal bool IsFromSubMenu { get { return Parent != null; } }
  140. /// <summary>
  141. /// Merely a debugging aid to see the interaction with main
  142. /// </summary>
  143. public MenuItem GetMenuItem ()
  144. {
  145. return this;
  146. }
  147. /// <summary>
  148. /// Merely a debugging aid to see the interaction with main
  149. /// </summary>
  150. public bool GetMenuBarItem ()
  151. {
  152. return IsFromSubMenu;
  153. }
  154. void GetHotKey ()
  155. {
  156. bool nextIsHot = false;
  157. foreach (var x in title) {
  158. if (x == '_') {
  159. nextIsHot = true;
  160. } else {
  161. if (nextIsHot) {
  162. HotKey = Char.ToUpper ((char)x);
  163. break;
  164. }
  165. nextIsHot = false;
  166. HotKey = default;
  167. }
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// A <see cref="MenuBarItem"/> contains <see cref="MenuBarItem"/>s or <see cref="MenuItem"/>s.
  173. /// </summary>
  174. public class MenuBarItem : MenuItem {
  175. /// <summary>
  176. /// Initializes a new <see cref="MenuBarItem"/> as a <see cref="MenuItem"/>.
  177. /// </summary>
  178. /// <param name="title">Title for the menu item.</param>
  179. /// <param name="help">Help text to display.</param>
  180. /// <param name="action">Action to invoke when the menu item is activated.</param>
  181. /// <param name="canExecute">Function to determine if the action can currently be executed.</param>
  182. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  183. public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
  184. {
  185. Initialize (title, null, null, true);
  186. }
  187. /// <summary>
  188. /// Initializes a new <see cref="MenuBarItem"/>.
  189. /// </summary>
  190. /// <param name="title">Title for the menu item.</param>
  191. /// <param name="children">The items in the current menu.</param>
  192. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  193. public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
  194. {
  195. Initialize (title, children, parent);
  196. }
  197. /// <summary>
  198. /// Initializes a new <see cref="MenuBarItem"/> with separate list of items.
  199. /// </summary>
  200. /// <param name="title">Title for the menu item.</param>
  201. /// <param name="children">The list of items in the current menu.</param>
  202. /// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
  203. public MenuBarItem (ustring title, List<MenuItem []> children, MenuItem parent = null)
  204. {
  205. Initialize (title, children, parent);
  206. }
  207. /// <summary>
  208. /// Initializes a new <see cref="MenuBarItem"/>.
  209. /// </summary>
  210. /// <param name="children">The items in the current menu.</param>
  211. public MenuBarItem (MenuItem [] children) : this ("", children) { }
  212. /// <summary>
  213. /// Initializes a new <see cref="MenuBarItem"/>.
  214. /// </summary>
  215. public MenuBarItem () : this (children: new MenuItem [] { }) { }
  216. void Initialize (ustring title, object children, MenuItem parent = null, bool isTopLevel = false)
  217. {
  218. if (!isTopLevel && children == null) {
  219. throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
  220. }
  221. SetTitle (title ?? "");
  222. if (parent != null) {
  223. Parent = parent;
  224. }
  225. if (children is List<MenuItem []>) {
  226. MenuItem [] childrens = new MenuItem [] { };
  227. foreach (var item in (List<MenuItem []>)children) {
  228. for (int i = 0; i < item.Length; i++) {
  229. SetChildrensParent (item);
  230. Array.Resize (ref childrens, childrens.Length + 1);
  231. childrens [childrens.Length - 1] = item [i];
  232. }
  233. }
  234. Children = childrens;
  235. } else if (children is MenuItem []) {
  236. SetChildrensParent ((MenuItem [])children);
  237. Children = (MenuItem [])children;
  238. } else {
  239. Children = null;
  240. }
  241. }
  242. //static int GetMaxTitleLength (MenuItem [] children)
  243. //{
  244. // int maxLength = 0;
  245. // foreach (var item in children) {
  246. // int len = GetMenuBarItemLength (item.Title);
  247. // if (len > maxLength)
  248. // maxLength = len;
  249. // item.IsFromSubMenu = true;
  250. // }
  251. // return maxLength;
  252. //}
  253. void SetChildrensParent (MenuItem [] childrens)
  254. {
  255. foreach (var child in childrens) {
  256. if (child != null && child.Parent == null) {
  257. child.Parent = this;
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// Check if the children parameter is a <see cref="MenuBarItem"/>.
  263. /// </summary>
  264. /// <param name="children"></param>
  265. /// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
  266. public MenuBarItem SubMenu (MenuItem children)
  267. {
  268. return children as MenuBarItem;
  269. }
  270. /// <summary>
  271. /// Check if the <see cref="MenuItem"/> parameter is a child of this.
  272. /// </summary>
  273. /// <param name="menuItem"></param>
  274. /// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
  275. public bool IsSubMenuOf (MenuItem menuItem)
  276. {
  277. foreach (var child in Children) {
  278. if (child == menuItem && child.Parent == menuItem.Parent) {
  279. return true;
  280. }
  281. }
  282. return false;
  283. }
  284. /// <summary>
  285. /// Get the index of the <see cref="MenuItem"/> parameter.
  286. /// </summary>
  287. /// <param name="children"></param>
  288. /// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
  289. public int GetChildrenIndex (MenuItem children)
  290. {
  291. if (Children?.Length == 0) {
  292. return -1;
  293. }
  294. int i = 0;
  295. foreach (var child in Children) {
  296. if (child == children) {
  297. return i;
  298. }
  299. i++;
  300. }
  301. return -1;
  302. }
  303. void SetTitle (ustring title)
  304. {
  305. if (title == null)
  306. title = "";
  307. Title = title;
  308. }
  309. int GetMenuBarItemLength (ustring title)
  310. {
  311. int len = 0;
  312. foreach (var ch in title) {
  313. if (ch == '_')
  314. continue;
  315. len++;
  316. }
  317. return len;
  318. }
  319. ///// <summary>
  320. ///// Gets or sets the title to display.
  321. ///// </summary>
  322. ///// <value>The title.</value>
  323. //public ustring Title { get; set; }
  324. /// <summary>
  325. /// Gets or sets an array of <see cref="MenuItem"/> objects that are the children of this <see cref="MenuBarItem"/>
  326. /// </summary>
  327. /// <value>The children.</value>
  328. public MenuItem [] Children { get; set; }
  329. internal int TitleLength => GetMenuBarItemLength (Title);
  330. internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0) && Action != null; }
  331. }
  332. class Menu : View {
  333. internal MenuBarItem barItems;
  334. internal MenuBar host;
  335. internal int current;
  336. internal View previousSubFocused;
  337. internal static Rect MakeFrame (int x, int y, MenuItem [] items)
  338. {
  339. if (items == null || items.Length == 0) {
  340. return new Rect ();
  341. }
  342. int maxW = items.Max (z => z?.Width) ?? 0;
  343. return new Rect (x, y, maxW + 2, items.Length + 2);
  344. }
  345. public Menu (MenuBar host, int x, int y, MenuBarItem barItems) : base (MakeFrame (x, y, barItems.Children))
  346. {
  347. this.barItems = barItems;
  348. this.host = host;
  349. if (barItems.IsTopLevel) {
  350. // This is a standalone MenuItem on a MenuBar
  351. ColorScheme = host.ColorScheme;
  352. CanFocus = true;
  353. } else {
  354. current = -1;
  355. for (int i = 0; i < barItems.Children?.Length; i++) {
  356. if (barItems.Children [i] != null) {
  357. current = i;
  358. break;
  359. }
  360. }
  361. ColorScheme = host.ColorScheme;
  362. CanFocus = true;
  363. WantMousePositionReports = host.WantMousePositionReports;
  364. }
  365. // Things this view knows how to do
  366. AddCommand (Command.LineUp, () => MoveUp ());
  367. AddCommand (Command.LineDown, () => MoveDown ());
  368. AddCommand (Command.Left, () => { this.host.PreviousMenu (true); return true; });
  369. AddCommand (Command.Right, () => {
  370. this.host.NextMenu (this.barItems.IsTopLevel || (this.barItems.Children != null
  371. && current > -1 && current < this.barItems.Children.Length && this.barItems.Children [current].IsFromSubMenu)
  372. ? true : false); return true;
  373. });
  374. AddCommand (Command.Cancel, () => { CloseAllMenus (); return true; });
  375. AddCommand (Command.Accept, () => { RunSelected (); return true; });
  376. // Default keybindings for this view
  377. AddKeyBinding (Key.CursorUp, Command.LineUp);
  378. AddKeyBinding (Key.CursorDown, Command.LineDown);
  379. AddKeyBinding (Key.CursorLeft, Command.Left);
  380. AddKeyBinding (Key.CursorRight, Command.Right);
  381. AddKeyBinding (Key.Esc, Command.Cancel);
  382. AddKeyBinding (Key.Enter, Command.Accept);
  383. }
  384. internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
  385. {
  386. if (item != null) {
  387. if (index == current) return ColorScheme.Focus;
  388. if (!item.IsEnabled ()) return ColorScheme.Disabled;
  389. }
  390. return GetNormalColor ();
  391. }
  392. public override void Redraw (Rect bounds)
  393. {
  394. Driver.SetAttribute (GetNormalColor ());
  395. DrawFrame (Bounds, padding: 0, fill: true);
  396. for (int i = Bounds.Y; i < barItems.Children.Length; i++) {
  397. if (i < 0)
  398. continue;
  399. var item = barItems.Children [i];
  400. Driver.SetAttribute (item == null ? GetNormalColor ()
  401. : i == current ? ColorScheme.Focus : GetNormalColor ());
  402. if (item == null) {
  403. Move (0, i + 1);
  404. Driver.AddRune (Driver.LeftTee);
  405. } else if (Frame.X + 1 < Driver.Cols)
  406. Move (1, i + 1);
  407. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  408. for (int p = Bounds.X; p < Frame.Width - 2; p++) {
  409. if (p < 0)
  410. continue;
  411. if (item == null)
  412. Driver.AddRune (Driver.HLine);
  413. else if (p == Frame.Width - 3 && barItems.SubMenu (barItems.Children [i]) != null)
  414. Driver.AddRune (Driver.RightArrow);
  415. else
  416. Driver.AddRune (' ');
  417. }
  418. if (item == null) {
  419. if (SuperView?.Frame.Right - Frame.X > Frame.Width - 1) {
  420. Move (Frame.Width - 1, i + 1);
  421. Driver.AddRune (Driver.RightTee);
  422. }
  423. continue;
  424. }
  425. ustring textToDraw;
  426. var checkChar = Driver.Selected;
  427. var uncheckedChar = Driver.UnSelected;
  428. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked)) {
  429. checkChar = Driver.Checked;
  430. uncheckedChar = Driver.UnChecked;
  431. }
  432. // Support Checked even though CheckType wasn't set
  433. if (item.Checked) {
  434. textToDraw = ustring.Make (new Rune [] { checkChar, ' ' }) + item.Title;
  435. } else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio)) {
  436. textToDraw = ustring.Make (new Rune [] { uncheckedChar, ' ' }) + item.Title;
  437. } else {
  438. textToDraw = item.Title;
  439. }
  440. ViewToScreen (2, i + 1, out int vtsCol, out _, false);
  441. if (vtsCol < Driver.Cols) {
  442. Move (2, i + 1);
  443. if (!item.IsEnabled ())
  444. DrawHotString (textToDraw, ColorScheme.Disabled, ColorScheme.Disabled);
  445. else
  446. DrawHotString (textToDraw,
  447. i == current ? ColorScheme.HotFocus : ColorScheme.HotNormal,
  448. i == current ? ColorScheme.Focus : GetNormalColor ());
  449. // The help string
  450. var l = item.ShortcutTag.RuneCount == 0 ? item.Help.RuneCount : item.Help.RuneCount + item.ShortcutTag.RuneCount + 2;
  451. var col = Frame.Width - l - 2;
  452. ViewToScreen (col, i + 1, out vtsCol, out _, false);
  453. if (vtsCol < Driver.Cols) {
  454. Move (col, 1 + i);
  455. Driver.AddStr (item.Help);
  456. // The shortcut tag string
  457. if (!item.ShortcutTag.IsEmpty) {
  458. l = item.ShortcutTag.RuneCount;
  459. Move (Frame.Width - l - 2, 1 + i);
  460. Driver.AddStr (item.ShortcutTag);
  461. }
  462. }
  463. }
  464. }
  465. PositionCursor ();
  466. }
  467. public override void PositionCursor ()
  468. {
  469. if (host == null || host.IsMenuOpen)
  470. if (barItems.IsTopLevel) {
  471. host.PositionCursor ();
  472. } else
  473. Move (2, 1 + current);
  474. else
  475. host.PositionCursor ();
  476. }
  477. public void Run (Action action)
  478. {
  479. if (action == null)
  480. return;
  481. Application.UngrabMouse ();
  482. host.CloseAllMenus ();
  483. Application.Refresh ();
  484. Application.MainLoop.AddIdle (() => {
  485. action ();
  486. return false;
  487. });
  488. }
  489. public override bool OnLeave (View view)
  490. {
  491. return host.OnLeave (view);
  492. }
  493. public override bool OnKeyDown (KeyEvent keyEvent)
  494. {
  495. if (keyEvent.IsAlt) {
  496. host.CloseAllMenus ();
  497. return true;
  498. }
  499. return false;
  500. }
  501. public override bool ProcessHotKey (KeyEvent keyEvent)
  502. {
  503. // To ncurses simulate a AltMask key pressing Alt+Space because
  504. // it can�t detect an alone special key down was pressed.
  505. if (keyEvent.IsAlt && keyEvent.Key == Key.AltMask) {
  506. OnKeyDown (keyEvent);
  507. return true;
  508. }
  509. return false;
  510. }
  511. public override bool ProcessKey (KeyEvent kb)
  512. {
  513. var result = InvokeKeybindings (kb);
  514. if (result != null)
  515. return (bool)result;
  516. // TODO: rune-ify
  517. if (barItems.Children != null && Char.IsLetterOrDigit ((char)kb.KeyValue)) {
  518. var x = Char.ToUpper ((char)kb.KeyValue);
  519. foreach (var item in barItems.Children) {
  520. if (item == null) continue;
  521. if (item.IsEnabled () && item.HotKey == x) {
  522. if (host.CloseMenu ()) {
  523. Run (item.Action);
  524. }
  525. return true;
  526. }
  527. }
  528. }
  529. return false;
  530. }
  531. void RunSelected ()
  532. {
  533. if (barItems.IsTopLevel) {
  534. Run (barItems.Action);
  535. } else if (current > -1) {
  536. Run (barItems.Children [current].Action);
  537. }
  538. }
  539. void CloseAllMenus ()
  540. {
  541. Application.UngrabMouse ();
  542. host.CloseAllMenus ();
  543. }
  544. bool MoveDown ()
  545. {
  546. if (barItems.IsTopLevel) {
  547. return true;
  548. }
  549. bool disabled;
  550. do {
  551. current++;
  552. if (current >= barItems.Children.Length) {
  553. current = 0;
  554. }
  555. if (this != host.openCurrentMenu && barItems.Children [current].IsFromSubMenu && host.selectedSub > -1) {
  556. host.PreviousMenu (true);
  557. host.SelectEnabledItem (barItems.Children, current, out current);
  558. host.openCurrentMenu = this;
  559. }
  560. var item = barItems.Children [current];
  561. if (item?.IsEnabled () != true) {
  562. disabled = true;
  563. } else {
  564. disabled = false;
  565. }
  566. if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  567. !disabled && host.IsMenuOpen) {
  568. if (!CheckSubMenu ())
  569. return false;
  570. break;
  571. }
  572. if (!host.IsMenuOpen) {
  573. host.OpenMenu (host.selected);
  574. }
  575. } while (barItems.Children [current] == null || disabled);
  576. SetNeedsDisplay ();
  577. host.OnMenuOpened ();
  578. return true;
  579. }
  580. bool MoveUp ()
  581. {
  582. if (barItems.IsTopLevel || current == -1) {
  583. return true;
  584. }
  585. bool disabled;
  586. do {
  587. current--;
  588. if (host.UseKeysUpDownAsKeysLeftRight) {
  589. if ((current == -1 || this != host.openCurrentMenu) && barItems.Children [current + 1].IsFromSubMenu && host.selectedSub > -1) {
  590. current++;
  591. host.PreviousMenu (true);
  592. if (current > 0) {
  593. current--;
  594. host.openCurrentMenu = this;
  595. }
  596. break;
  597. }
  598. }
  599. if (current < 0)
  600. current = barItems.Children.Length - 1;
  601. if (!host.SelectEnabledItem (barItems.Children, current, out current, false)) {
  602. current = 0;
  603. if (!host.SelectEnabledItem (barItems.Children, current, out current) && !host.CloseMenu ()) {
  604. return false;
  605. }
  606. break;
  607. }
  608. var item = barItems.Children [current];
  609. if (item?.IsEnabled () != true) {
  610. disabled = true;
  611. } else {
  612. disabled = false;
  613. }
  614. if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
  615. !disabled && host.IsMenuOpen) {
  616. if (!CheckSubMenu ())
  617. return false;
  618. break;
  619. }
  620. } while (barItems.Children [current] == null || disabled);
  621. SetNeedsDisplay ();
  622. host.OnMenuOpened ();
  623. return true;
  624. }
  625. public override bool MouseEvent (MouseEvent me)
  626. {
  627. if (!host.handled && !host.HandleGrabView (me, this)) {
  628. return false;
  629. }
  630. host.handled = false;
  631. bool disabled;
  632. if (me.Flags == MouseFlags.Button1Clicked) {
  633. disabled = false;
  634. if (me.Y < 1)
  635. return true;
  636. var meY = me.Y - 1;
  637. if (meY >= barItems.Children.Length)
  638. return true;
  639. var item = barItems.Children [meY];
  640. if (item == null || !item.IsEnabled ()) disabled = true;
  641. if (item != null && !disabled)
  642. Run (barItems.Children [meY].Action);
  643. return true;
  644. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked ||
  645. me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.ReportMousePosition ||
  646. me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)) {
  647. disabled = false;
  648. if (me.Y < 1 || me.Y - 1 >= barItems.Children.Length) {
  649. return true;
  650. }
  651. var item = barItems.Children [me.Y - 1];
  652. if (item == null) return true;
  653. if (item == null || !item.IsEnabled ()) disabled = true;
  654. if (item != null && !disabled)
  655. current = me.Y - 1;
  656. if (!CheckSubMenu ())
  657. return true;
  658. host.OnMenuOpened ();
  659. return true;
  660. }
  661. return false;
  662. }
  663. internal bool CheckSubMenu ()
  664. {
  665. if (current == -1 || barItems.Children [current] == null) {
  666. return true;
  667. }
  668. var subMenu = barItems.SubMenu (barItems.Children [current]);
  669. if (subMenu != null) {
  670. int pos = -1;
  671. if (host.openSubMenu != null) {
  672. pos = host.openSubMenu.FindIndex (o => o?.barItems == subMenu);
  673. }
  674. if (pos == -1 && this != host.openCurrentMenu && subMenu.Children != host.openCurrentMenu.barItems.Children
  675. && !host.CloseMenu (false, true)) {
  676. return false;
  677. }
  678. host.Activate (host.selected, pos, subMenu);
  679. } else if (host.openSubMenu?.Count == 0 || host.openSubMenu?.Last ().barItems.IsSubMenuOf (barItems.Children [current]) == false) {
  680. return host.CloseMenu (false, true);
  681. } else {
  682. SetNeedsDisplay ();
  683. }
  684. return true;
  685. }
  686. int GetSubMenuIndex (MenuBarItem subMenu)
  687. {
  688. int pos = -1;
  689. if (this != null && Subviews.Count > 0) {
  690. Menu v = null;
  691. foreach (var menu in Subviews) {
  692. if (((Menu)menu).barItems == subMenu)
  693. v = (Menu)menu;
  694. }
  695. if (v != null)
  696. pos = Subviews.IndexOf (v);
  697. }
  698. return pos;
  699. }
  700. ///<inheritdoc/>
  701. public override bool OnEnter (View view)
  702. {
  703. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  704. return base.OnEnter (view);
  705. }
  706. }
  707. /// <summary>
  708. /// The MenuBar provides a menu for Terminal.Gui applications.
  709. /// </summary>
  710. /// <remarks>
  711. /// <para>
  712. /// The <see cref="MenuBar"/> appears on the first row of the terminal.
  713. /// </para>
  714. /// <para>
  715. /// The <see cref="MenuBar"/> provides global hotkeys for the application.
  716. /// </para>
  717. /// </remarks>
  718. public class MenuBar : View {
  719. /// <summary>
  720. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this when the <see cref="MenuBar"/> is vislble.
  721. /// </summary>
  722. /// <value>The menu array.</value>
  723. public MenuBarItem [] Menus { get; set; }
  724. internal int selected;
  725. internal int selectedSub;
  726. Action action;
  727. /// <summary>
  728. /// Used for change the navigation key style.
  729. /// </summary>
  730. public bool UseKeysUpDownAsKeysLeftRight { get; set; } = true;
  731. static ustring shortcutDelimiter = "+";
  732. /// <summary>
  733. /// Used for change the shortcut delimiter separator.
  734. /// </summary>
  735. public static ustring ShortcutDelimiter {
  736. get => shortcutDelimiter;
  737. set {
  738. if (shortcutDelimiter != value) {
  739. shortcutDelimiter = value == ustring.Empty ? " " : value;
  740. }
  741. }
  742. }
  743. /// <summary>
  744. /// Initializes a new instance of the <see cref="MenuBar"/>.
  745. /// </summary>
  746. public MenuBar () : this (new MenuBarItem [] { }) { }
  747. /// <summary>
  748. /// Initializes a new instance of the <see cref="MenuBar"/> class with the specified set of toplevel menu items.
  749. /// </summary>
  750. /// <param name="menus">Individual menu items; a null item will result in a separator being drawn.</param>
  751. public MenuBar (MenuBarItem [] menus) : base ()
  752. {
  753. X = 0;
  754. Y = 0;
  755. Width = Dim.Fill ();
  756. Height = 1;
  757. Menus = menus;
  758. //CanFocus = true;
  759. selected = -1;
  760. selectedSub = -1;
  761. ColorScheme = Colors.Menu;
  762. WantMousePositionReports = true;
  763. IsMenuOpen = false;
  764. // Things this view knows how to do
  765. AddCommand (Command.Left, () => { MoveLeft (); return true; });
  766. AddCommand (Command.Right, () => { MoveRight (); return true; });
  767. AddCommand (Command.Cancel, () => { CloseMenuBar (); return true; });
  768. AddCommand (Command.Accept, () => { ProcessMenu (selected, Menus [selected]); return true; });
  769. // Default keybindings for this view
  770. AddKeyBinding (Key.CursorLeft, Command.Left);
  771. AddKeyBinding (Key.CursorRight, Command.Right);
  772. AddKeyBinding (Key.Esc, Command.Cancel);
  773. AddKeyBinding (Key.C | Key.CtrlMask, Command.Cancel);
  774. AddKeyBinding (Key.CursorDown, Command.Accept);
  775. AddKeyBinding (Key.Enter, Command.Accept);
  776. }
  777. bool openedByAltKey;
  778. bool isCleaning;
  779. ///<inheritdoc/>
  780. public override bool OnLeave (View view)
  781. {
  782. if ((!(view is MenuBar) && !(view is Menu) || !(view is MenuBar) && !(view is Menu) && openMenu != null) && !isCleaning && !reopen) {
  783. CleanUp ();
  784. return true;
  785. }
  786. return false;
  787. }
  788. ///<inheritdoc/>
  789. public override bool OnKeyDown (KeyEvent keyEvent)
  790. {
  791. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  792. openedByAltKey = true;
  793. SetNeedsDisplay ();
  794. openedByHotKey = false;
  795. }
  796. return false;
  797. }
  798. ///<inheritdoc/>
  799. public override bool OnKeyUp (KeyEvent keyEvent)
  800. {
  801. if (keyEvent.IsAlt || (keyEvent.IsCtrl && keyEvent.Key == (Key.CtrlMask | Key.Space))) {
  802. // User pressed Alt - this may be a precursor to a menu accelerator (e.g. Alt-F)
  803. if (openedByAltKey && !IsMenuOpen && openMenu == null && (((uint)keyEvent.Key & (uint)Key.CharMask) == 0
  804. || ((uint)keyEvent.Key & (uint)Key.CharMask) == (uint)Key.Space)) {
  805. // There's no open menu, the first menu item should be highlight.
  806. // The right way to do this is to SetFocus(MenuBar), but for some reason
  807. // that faults.
  808. var mbar = GetMouseGrabViewInstance (this);
  809. if (mbar != null) {
  810. mbar.CleanUp ();
  811. }
  812. //Activate (0);
  813. //StartMenu ();
  814. IsMenuOpen = true;
  815. selected = 0;
  816. CanFocus = true;
  817. lastFocused = SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused;
  818. SetFocus ();
  819. SetNeedsDisplay ();
  820. Application.GrabMouse (this);
  821. } else if (!openedByHotKey) {
  822. // There's an open menu. If this Alt key-up is a pre-cursor to an accelerator
  823. // we don't want to close the menu because it'll flash.
  824. // How to deal with that?
  825. CleanUp ();
  826. }
  827. return true;
  828. }
  829. return false;
  830. }
  831. internal void CleanUp ()
  832. {
  833. isCleaning = true;
  834. if (openMenu != null) {
  835. CloseAllMenus ();
  836. }
  837. openedByAltKey = false;
  838. IsMenuOpen = false;
  839. selected = -1;
  840. CanFocus = false;
  841. if (lastFocused != null) {
  842. lastFocused.SetFocus ();
  843. }
  844. SetNeedsDisplay ();
  845. Application.UngrabMouse ();
  846. isCleaning = false;
  847. }
  848. ///<inheritdoc/>
  849. public override void Redraw (Rect bounds)
  850. {
  851. Move (0, 0);
  852. Driver.SetAttribute (GetNormalColor ());
  853. for (int i = 0; i < Frame.Width; i++)
  854. Driver.AddRune (' ');
  855. Move (1, 0);
  856. int pos = 1;
  857. for (int i = 0; i < Menus.Length; i++) {
  858. var menu = Menus [i];
  859. Move (pos, 0);
  860. Attribute hotColor, normalColor;
  861. if (i == selected && IsMenuOpen) {
  862. hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  863. normalColor = i == selected ? ColorScheme.Focus :
  864. GetNormalColor ();
  865. } else if (openedByAltKey) {
  866. hotColor = ColorScheme.HotNormal;
  867. normalColor = GetNormalColor ();
  868. } else {
  869. hotColor = GetNormalColor ();
  870. normalColor = GetNormalColor ();
  871. }
  872. DrawHotString (menu.Help.IsEmpty ? $" {menu.Title} " : $" {menu.Title} {menu.Help} ", hotColor, normalColor);
  873. pos += 1 + menu.TitleLength + (menu.Help.Length > 0 ? menu.Help.Length + 2 : 0) + 2;
  874. }
  875. PositionCursor ();
  876. }
  877. ///<inheritdoc/>
  878. public override void PositionCursor ()
  879. {
  880. if (selected == -1 && HasFocus && Menus.Length > 0) {
  881. selected = 0;
  882. }
  883. int pos = 0;
  884. for (int i = 0; i < Menus.Length; i++) {
  885. if (i == selected) {
  886. pos++;
  887. if (IsMenuOpen)
  888. Move (pos + 1, 0);
  889. else {
  890. Move (pos + 1, 0);
  891. }
  892. return;
  893. } else if (IsMenuOpen) {
  894. pos += 1 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 2;
  895. } else {
  896. pos += 2 + Menus [i].TitleLength + (Menus [i].Help.Length > 0 ? Menus [i].Help.Length + 2 : 0) + 1;
  897. }
  898. }
  899. //Move (0, 0);
  900. }
  901. void Selected (MenuItem item)
  902. {
  903. // TODO: Running = false;
  904. action = item.Action;
  905. }
  906. /// <summary>
  907. /// Raised as a menu is opening.
  908. /// </summary>
  909. public event Action<MenuOpeningEventArgs> MenuOpening;
  910. /// <summary>
  911. /// Raised when a menu is opened.
  912. /// </summary>
  913. public event Action<MenuItem> MenuOpened;
  914. /// <summary>
  915. /// Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.
  916. /// </summary>
  917. public event Action<MenuClosingEventArgs> MenuClosing;
  918. /// <summary>
  919. /// Raised when all the menu are closed.
  920. /// </summary>
  921. public event Action MenuAllClosed;
  922. internal Menu openMenu;
  923. Menu ocm;
  924. internal Menu openCurrentMenu {
  925. get => ocm;
  926. set {
  927. if (ocm != value) {
  928. ocm = value;
  929. if (ocm.current > -1) {
  930. OnMenuOpened ();
  931. }
  932. }
  933. }
  934. }
  935. internal List<Menu> openSubMenu;
  936. View previousFocused;
  937. internal bool isMenuOpening;
  938. internal bool isMenuClosing;
  939. /// <summary>
  940. /// True if the menu is open; otherwise false.
  941. /// </summary>
  942. public bool IsMenuOpen { get; protected set; }
  943. /// <summary>
  944. /// Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.
  945. /// </summary>
  946. /// <param name="currentMenu">The current menu to be replaced.</param>
  947. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  948. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  949. {
  950. var ev = new MenuOpeningEventArgs (currentMenu);
  951. MenuOpening?.Invoke (ev);
  952. return ev;
  953. }
  954. /// <summary>
  955. /// Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.
  956. /// </summary>
  957. public virtual void OnMenuOpened ()
  958. {
  959. MenuItem mi = null;
  960. if (openCurrentMenu.barItems.Children != null) {
  961. mi = openCurrentMenu.barItems.Children [openCurrentMenu.current];
  962. }
  963. MenuOpened?.Invoke (mi);
  964. }
  965. /// <summary>
  966. /// Virtual method that will invoke the <see cref="MenuClosing"/>
  967. /// </summary>
  968. /// <param name="currentMenu">The current menu to be closed.</param>
  969. /// <param name="reopen">Whether the current menu will be reopen.</param>
  970. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  971. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  972. {
  973. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  974. MenuClosing?.Invoke (ev);
  975. return ev;
  976. }
  977. /// <summary>
  978. /// Virtual method that will invoke the <see cref="MenuAllClosed"/>
  979. /// </summary>
  980. public virtual void OnMenuAllClosed ()
  981. {
  982. MenuAllClosed?.Invoke ();
  983. }
  984. View lastFocused;
  985. /// <summary>
  986. /// Get the lasted focused view before open the menu.
  987. /// </summary>
  988. public View LastFocused { get; private set; }
  989. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  990. {
  991. isMenuOpening = true;
  992. var newMenu = OnMenuOpening (Menus [index]);
  993. if (newMenu.Cancel) {
  994. isMenuOpening = false;
  995. return;
  996. }
  997. if (newMenu.NewMenuBarItem != null) {
  998. Menus [index] = newMenu.NewMenuBarItem;
  999. }
  1000. int pos = 0;
  1001. switch (subMenu) {
  1002. case null:
  1003. lastFocused = lastFocused ?? (SuperView == null ? Application.Current.MostFocused : SuperView.MostFocused);
  1004. if (openSubMenu != null && !CloseMenu (false, true))
  1005. return;
  1006. if (openMenu != null) {
  1007. if (SuperView == null) {
  1008. Application.Current.Remove (openMenu);
  1009. } else {
  1010. SuperView.Remove (openMenu);
  1011. }
  1012. openMenu.Dispose ();
  1013. }
  1014. for (int i = 0; i < index; i++)
  1015. pos += Menus [i].Title.RuneCount + (Menus [i].Help.RuneCount > 0 ? Menus [i].Help.RuneCount + 2 : 0) + 2;
  1016. openMenu = new Menu (this, Frame.X + pos, Frame.Y + 1, Menus [index]);
  1017. openCurrentMenu = openMenu;
  1018. openCurrentMenu.previousSubFocused = openMenu;
  1019. if (SuperView == null) {
  1020. Application.Current.Add (openMenu);
  1021. } else {
  1022. SuperView.Add (openMenu);
  1023. }
  1024. openMenu.SetFocus ();
  1025. break;
  1026. default:
  1027. if (openSubMenu == null)
  1028. openSubMenu = new List<Menu> ();
  1029. if (sIndex > -1) {
  1030. RemoveSubMenu (sIndex);
  1031. } else {
  1032. var last = openSubMenu.Count > 0 ? openSubMenu.Last () : openMenu;
  1033. openCurrentMenu = new Menu (this, last.Frame.Left + last.Frame.Width, last.Frame.Top + 1 + last.current, subMenu);
  1034. openCurrentMenu.previousSubFocused = last.previousSubFocused;
  1035. openSubMenu.Add (openCurrentMenu);
  1036. if (SuperView == null) {
  1037. Application.Current.Add (openCurrentMenu);
  1038. } else {
  1039. SuperView.Add (openCurrentMenu);
  1040. }
  1041. }
  1042. selectedSub = openSubMenu.Count - 1;
  1043. if (selectedSub > -1 && SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1044. openCurrentMenu.SetFocus ();
  1045. }
  1046. break;
  1047. }
  1048. isMenuOpening = false;
  1049. IsMenuOpen = true;
  1050. }
  1051. /// <summary>
  1052. /// Opens the current Menu programatically.
  1053. /// </summary>
  1054. public void OpenMenu ()
  1055. {
  1056. var mbar = GetMouseGrabViewInstance (this);
  1057. if (mbar != null) {
  1058. mbar.CleanUp ();
  1059. }
  1060. if (openMenu != null)
  1061. return;
  1062. selected = 0;
  1063. SetNeedsDisplay ();
  1064. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1065. OpenMenu (selected);
  1066. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu ()) {
  1067. return;
  1068. }
  1069. if (!openCurrentMenu.CheckSubMenu ())
  1070. return;
  1071. Application.GrabMouse (this);
  1072. }
  1073. // Activates the menu, handles either first focus, or activating an entry when it was already active
  1074. // For mouse events.
  1075. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  1076. {
  1077. selected = idx;
  1078. selectedSub = sIdx;
  1079. if (openMenu == null)
  1080. previousFocused = SuperView == null ? Application.Current.Focused : SuperView.Focused;
  1081. OpenMenu (idx, sIdx, subMenu);
  1082. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)
  1083. && subMenu == null && !CloseMenu ()) {
  1084. return;
  1085. }
  1086. SetNeedsDisplay ();
  1087. }
  1088. internal bool SelectEnabledItem (IEnumerable<MenuItem> chldren, int current, out int newCurrent, bool forward = true)
  1089. {
  1090. if (chldren == null) {
  1091. newCurrent = -1;
  1092. return true;
  1093. }
  1094. IEnumerable<MenuItem> childrens;
  1095. if (forward) {
  1096. childrens = chldren;
  1097. } else {
  1098. childrens = chldren.Reverse ();
  1099. }
  1100. int count;
  1101. if (forward) {
  1102. count = -1;
  1103. } else {
  1104. count = childrens.Count ();
  1105. }
  1106. foreach (var child in childrens) {
  1107. if (forward) {
  1108. if (++count < current) {
  1109. continue;
  1110. }
  1111. } else {
  1112. if (--count > current) {
  1113. continue;
  1114. }
  1115. }
  1116. if (child == null || !child.IsEnabled ()) {
  1117. if (forward) {
  1118. current++;
  1119. } else {
  1120. current--;
  1121. }
  1122. } else {
  1123. newCurrent = current;
  1124. return true;
  1125. }
  1126. }
  1127. newCurrent = -1;
  1128. return false;
  1129. }
  1130. /// <summary>
  1131. /// Closes the current Menu programatically, if open and not canceled.
  1132. /// </summary>
  1133. public bool CloseMenu ()
  1134. {
  1135. return CloseMenu (false, false);
  1136. }
  1137. bool reopen;
  1138. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false)
  1139. {
  1140. isMenuClosing = true;
  1141. this.reopen = reopen;
  1142. var args = OnMenuClosing (
  1143. isSubMenu ? openCurrentMenu.barItems : openMenu?.barItems, reopen, isSubMenu);
  1144. if (args.Cancel) {
  1145. isMenuClosing = false;
  1146. if (args.CurrentMenu.Parent != null)
  1147. openMenu.current = ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  1148. return false;
  1149. }
  1150. switch (isSubMenu) {
  1151. case false:
  1152. if (openMenu != null) {
  1153. if (SuperView == null) {
  1154. Application.Current.Remove (openMenu);
  1155. } else {
  1156. SuperView?.Remove (openMenu);
  1157. }
  1158. }
  1159. SetNeedsDisplay ();
  1160. if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
  1161. previousFocused.SetFocus ();
  1162. openMenu?.Dispose ();
  1163. openMenu = null;
  1164. if (lastFocused is Menu || lastFocused is MenuBar) {
  1165. lastFocused = null;
  1166. }
  1167. LastFocused = lastFocused;
  1168. lastFocused = null;
  1169. if (LastFocused != null && LastFocused.CanFocus) {
  1170. if (!reopen) {
  1171. selected = -1;
  1172. }
  1173. LastFocused.SetFocus ();
  1174. } else {
  1175. SetFocus ();
  1176. PositionCursor ();
  1177. }
  1178. IsMenuOpen = false;
  1179. break;
  1180. case true:
  1181. selectedSub = -1;
  1182. SetNeedsDisplay ();
  1183. RemoveAllOpensSubMenus ();
  1184. openCurrentMenu.previousSubFocused.SetFocus ();
  1185. openSubMenu = null;
  1186. IsMenuOpen = true;
  1187. break;
  1188. }
  1189. this.reopen = false;
  1190. isMenuClosing = false;
  1191. return true;
  1192. }
  1193. void RemoveSubMenu (int index)
  1194. {
  1195. if (openSubMenu == null)
  1196. return;
  1197. for (int i = openSubMenu.Count - 1; i > index; i--) {
  1198. isMenuClosing = true;
  1199. Menu menu;
  1200. if (openSubMenu.Count - 1 > 0)
  1201. menu = openSubMenu [i - 1];
  1202. else
  1203. menu = openMenu;
  1204. openCurrentMenu = menu;
  1205. openCurrentMenu.SetFocus ();
  1206. if (openSubMenu != null) {
  1207. menu = openSubMenu [i];
  1208. if (SuperView == null) {
  1209. Application.Current.Remove (menu);
  1210. } else {
  1211. SuperView.Remove (menu);
  1212. }
  1213. openSubMenu.Remove (menu);
  1214. menu.Dispose ();
  1215. }
  1216. RemoveSubMenu (i);
  1217. }
  1218. if (openSubMenu.Count > 0)
  1219. openCurrentMenu = openSubMenu.Last ();
  1220. //if (openMenu.Subviews.Count == 0)
  1221. // return;
  1222. //if (index == 0) {
  1223. // //SuperView.SetFocus (previousSubFocused);
  1224. // FocusPrev ();
  1225. // return;
  1226. //}
  1227. //for (int i = openMenu.Subviews.Count - 1; i > index; i--) {
  1228. // isMenuClosing = true;
  1229. // if (openMenu.Subviews.Count - 1 > 0)
  1230. // SuperView.SetFocus (openMenu.Subviews [i - 1]);
  1231. // else
  1232. // SuperView.SetFocus (openMenu);
  1233. // if (openMenu != null) {
  1234. // Remove (openMenu.Subviews [i]);
  1235. // openMenu.Remove (openMenu.Subviews [i]);
  1236. // }
  1237. // RemoveSubMenu (i);
  1238. //}
  1239. isMenuClosing = false;
  1240. }
  1241. internal void RemoveAllOpensSubMenus ()
  1242. {
  1243. if (openSubMenu != null) {
  1244. foreach (var item in openSubMenu) {
  1245. if (SuperView == null) {
  1246. Application.Current.Remove (item);
  1247. } else {
  1248. SuperView.Remove (item);
  1249. }
  1250. item.Dispose ();
  1251. }
  1252. }
  1253. }
  1254. internal void CloseAllMenus ()
  1255. {
  1256. if (!isMenuOpening && !isMenuClosing) {
  1257. if (openSubMenu != null && !CloseMenu (false, true))
  1258. return;
  1259. if (!CloseMenu ())
  1260. return;
  1261. if (LastFocused != null && LastFocused != this)
  1262. selected = -1;
  1263. }
  1264. IsMenuOpen = false;
  1265. openedByHotKey = false;
  1266. openedByAltKey = false;
  1267. OnMenuAllClosed ();
  1268. }
  1269. View FindDeepestMenu (View view, ref int count)
  1270. {
  1271. count = count > 0 ? count : 0;
  1272. foreach (var menu in view.Subviews) {
  1273. if (menu is Menu) {
  1274. count++;
  1275. return FindDeepestMenu ((Menu)menu, ref count);
  1276. }
  1277. }
  1278. return view;
  1279. }
  1280. internal void PreviousMenu (bool isSubMenu = false)
  1281. {
  1282. switch (isSubMenu) {
  1283. case false:
  1284. if (selected <= 0)
  1285. selected = Menus.Length - 1;
  1286. else
  1287. selected--;
  1288. if (selected > -1 && !CloseMenu (true, false))
  1289. return;
  1290. OpenMenu (selected);
  1291. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current, false)) {
  1292. openCurrentMenu.current = 0;
  1293. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current)) {
  1294. CloseMenu ();
  1295. }
  1296. }
  1297. break;
  1298. case true:
  1299. if (selectedSub > -1) {
  1300. selectedSub--;
  1301. RemoveSubMenu (selectedSub);
  1302. SetNeedsDisplay ();
  1303. } else
  1304. PreviousMenu ();
  1305. break;
  1306. }
  1307. }
  1308. internal void NextMenu (bool isSubMenu = false)
  1309. {
  1310. switch (isSubMenu) {
  1311. case false:
  1312. if (selected == -1)
  1313. selected = 0;
  1314. else if (selected + 1 == Menus.Length)
  1315. selected = 0;
  1316. else
  1317. selected++;
  1318. if (selected > -1 && !CloseMenu (true))
  1319. return;
  1320. OpenMenu (selected);
  1321. SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current);
  1322. break;
  1323. case true:
  1324. if (UseKeysUpDownAsKeysLeftRight) {
  1325. if (CloseMenu (false, true)) {
  1326. NextMenu ();
  1327. }
  1328. } else {
  1329. var subMenu = openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current]);
  1330. if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
  1331. if (openSubMenu != null && !CloseMenu (false, true))
  1332. return;
  1333. NextMenu ();
  1334. } else if (subMenu != null ||
  1335. !openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
  1336. selectedSub++;
  1337. else
  1338. return;
  1339. SetNeedsDisplay ();
  1340. openCurrentMenu.CheckSubMenu ();
  1341. }
  1342. break;
  1343. }
  1344. }
  1345. bool openedByHotKey;
  1346. internal bool FindAndOpenMenuByHotkey (KeyEvent kb)
  1347. {
  1348. //int pos = 0;
  1349. var c = ((uint)kb.Key & (uint)Key.CharMask);
  1350. for (int i = 0; i < Menus.Length; i++) {
  1351. // TODO: this code is duplicated, hotkey should be part of the MenuBarItem
  1352. var mi = Menus [i];
  1353. int p = mi.Title.IndexOf ('_');
  1354. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1355. if (Char.ToUpperInvariant ((char)mi.Title [p + 1]) == c) {
  1356. ProcessMenu (i, mi);
  1357. return true;
  1358. }
  1359. }
  1360. }
  1361. return false;
  1362. }
  1363. internal bool FindAndOpenMenuByShortcut (KeyEvent kb, MenuItem [] children = null)
  1364. {
  1365. if (children == null) {
  1366. children = Menus;
  1367. }
  1368. var key = kb.KeyValue;
  1369. var keys = ShortcutHelper.GetModifiersKey (kb);
  1370. key |= (int)keys;
  1371. for (int i = 0; i < children.Length; i++) {
  1372. var mi = children [i];
  1373. if (mi == null) {
  1374. continue;
  1375. }
  1376. if ((!(mi is MenuBarItem mbiTopLevel) || mbiTopLevel.IsTopLevel) && mi.Shortcut != Key.Null && mi.Shortcut == (Key)key) {
  1377. var action = mi.Action;
  1378. if (action != null) {
  1379. Application.MainLoop.AddIdle (() => {
  1380. action ();
  1381. return false;
  1382. });
  1383. }
  1384. return true;
  1385. }
  1386. if (mi is MenuBarItem menuBarItem && !menuBarItem.IsTopLevel && FindAndOpenMenuByShortcut (kb, menuBarItem.Children)) {
  1387. return true;
  1388. }
  1389. }
  1390. return false;
  1391. }
  1392. private void ProcessMenu (int i, MenuBarItem mi)
  1393. {
  1394. if (selected < 0) {
  1395. return;
  1396. }
  1397. if (mi.IsTopLevel) {
  1398. var menu = new Menu (this, i, 0, mi);
  1399. menu.Run (mi.Action);
  1400. menu.Dispose ();
  1401. } else {
  1402. openedByHotKey = true;
  1403. Application.GrabMouse (this);
  1404. selected = i;
  1405. OpenMenu (i);
  1406. if (!SelectEnabledItem (openCurrentMenu.barItems.Children, openCurrentMenu.current, out openCurrentMenu.current) && !CloseMenu ()) {
  1407. return;
  1408. }
  1409. if (!openCurrentMenu.CheckSubMenu ())
  1410. return;
  1411. }
  1412. SetNeedsDisplay ();
  1413. }
  1414. ///<inheritdoc/>
  1415. public override bool ProcessHotKey (KeyEvent kb)
  1416. {
  1417. if (kb.Key == Key.F9) {
  1418. if (!IsMenuOpen)
  1419. OpenMenu ();
  1420. else
  1421. CloseAllMenus ();
  1422. return true;
  1423. }
  1424. // To ncurses simulate a AltMask key pressing Alt+Space because
  1425. // it can�t detect an alone special key down was pressed.
  1426. if (kb.IsAlt && kb.Key == Key.AltMask && openMenu == null) {
  1427. OnKeyDown (kb);
  1428. OnKeyUp (kb);
  1429. return true;
  1430. } else if (kb.IsAlt && !kb.IsCtrl && !kb.IsShift) {
  1431. if (FindAndOpenMenuByHotkey (kb)) return true;
  1432. }
  1433. //var kc = kb.KeyValue;
  1434. return base.ProcessHotKey (kb);
  1435. }
  1436. ///<inheritdoc/>
  1437. public override bool ProcessKey (KeyEvent kb)
  1438. {
  1439. if (InvokeKeybindings (kb) == true)
  1440. return true;
  1441. var key = kb.KeyValue;
  1442. if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') || (key >= '0' && key <= '9')) {
  1443. char c = Char.ToUpper ((char)key);
  1444. if (selected == -1 || Menus [selected].IsTopLevel)
  1445. return false;
  1446. foreach (var mi in Menus [selected].Children) {
  1447. if (mi == null)
  1448. continue;
  1449. int p = mi.Title.IndexOf ('_');
  1450. if (p != -1 && p + 1 < mi.Title.RuneCount) {
  1451. if (mi.Title [p + 1] == c) {
  1452. Selected (mi);
  1453. return true;
  1454. }
  1455. }
  1456. }
  1457. }
  1458. return false;
  1459. }
  1460. void CloseMenuBar ()
  1461. {
  1462. if (!CloseMenu ())
  1463. return;
  1464. if (openedByAltKey) {
  1465. openedByAltKey = false;
  1466. LastFocused?.SetFocus ();
  1467. }
  1468. SetNeedsDisplay ();
  1469. }
  1470. void MoveRight ()
  1471. {
  1472. selected = (selected + 1) % Menus.Length;
  1473. OpenMenu (selected);
  1474. SetNeedsDisplay ();
  1475. }
  1476. void MoveLeft ()
  1477. {
  1478. selected--;
  1479. if (selected < 0)
  1480. selected = Menus.Length - 1;
  1481. OpenMenu (selected);
  1482. SetNeedsDisplay ();
  1483. }
  1484. ///<inheritdoc/>
  1485. public override bool ProcessColdKey (KeyEvent kb)
  1486. {
  1487. return FindAndOpenMenuByShortcut (kb);
  1488. }
  1489. ///<inheritdoc/>
  1490. public override bool MouseEvent (MouseEvent me)
  1491. {
  1492. if (!handled && !HandleGrabView (me, this)) {
  1493. return false;
  1494. }
  1495. handled = false;
  1496. if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags == MouseFlags.Button1Clicked ||
  1497. (me.Flags == MouseFlags.ReportMousePosition && selected > -1) ||
  1498. (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && selected > -1)) {
  1499. int pos = 1;
  1500. int cx = me.X;
  1501. for (int i = 0; i < Menus.Length; i++) {
  1502. if (cx >= pos && cx < pos + 1 + Menus [i].TitleLength + Menus [i].Help.RuneCount + 2) {
  1503. if (me.Flags == MouseFlags.Button1Clicked) {
  1504. if (Menus [i].IsTopLevel) {
  1505. var menu = new Menu (this, i, 0, Menus [i]);
  1506. menu.Run (Menus [i].Action);
  1507. menu.Dispose ();
  1508. } else if (!IsMenuOpen) {
  1509. Activate (i);
  1510. }
  1511. } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked) {
  1512. if (IsMenuOpen && !Menus [i].IsTopLevel) {
  1513. CloseAllMenus ();
  1514. } else if (!Menus [i].IsTopLevel) {
  1515. Activate (i);
  1516. }
  1517. } else if (selected != i && selected > -1 && (me.Flags == MouseFlags.ReportMousePosition ||
  1518. me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)) {
  1519. if (IsMenuOpen) {
  1520. if (!CloseMenu (true, false)) {
  1521. return true;
  1522. }
  1523. Activate (i);
  1524. }
  1525. } else {
  1526. if (IsMenuOpen)
  1527. Activate (i);
  1528. }
  1529. return true;
  1530. }
  1531. pos += 1 + Menus [i].TitleLength + 2;
  1532. }
  1533. }
  1534. return false;
  1535. }
  1536. internal bool handled;
  1537. internal bool isContextMenuLoading;
  1538. internal bool HandleGrabView (MouseEvent me, View current)
  1539. {
  1540. if (Application.mouseGrabView != null) {
  1541. if (me.View is MenuBar || me.View is Menu) {
  1542. var mbar = GetMouseGrabViewInstance (me.View);
  1543. if (mbar != null) {
  1544. if (me.Flags == MouseFlags.Button1Clicked) {
  1545. mbar.CleanUp ();
  1546. Application.GrabMouse (me.View);
  1547. } else {
  1548. handled = false;
  1549. return false;
  1550. }
  1551. }
  1552. if (me.View != current) {
  1553. Application.UngrabMouse ();
  1554. var v = me.View;
  1555. Application.GrabMouse (v);
  1556. MouseEvent nme;
  1557. if (me.Y > -1) {
  1558. var newxy = v.ScreenToView (me.X, me.Y);
  1559. nme = new MouseEvent () {
  1560. X = newxy.X,
  1561. Y = newxy.Y,
  1562. Flags = me.Flags,
  1563. OfX = me.X - newxy.X,
  1564. OfY = me.Y - newxy.Y,
  1565. View = v
  1566. };
  1567. } else {
  1568. nme = new MouseEvent () {
  1569. X = me.X + current.Frame.X,
  1570. Y = 0,
  1571. Flags = me.Flags,
  1572. View = v
  1573. };
  1574. }
  1575. v.MouseEvent (nme);
  1576. return false;
  1577. }
  1578. } else if (!isContextMenuLoading && !(me.View is MenuBar || me.View is Menu)
  1579. && me.Flags != MouseFlags.ReportMousePosition && me.Flags != 0) {
  1580. Application.UngrabMouse ();
  1581. if (IsMenuOpen)
  1582. CloseAllMenus ();
  1583. handled = false;
  1584. return false;
  1585. } else {
  1586. handled = false;
  1587. isContextMenuLoading = false;
  1588. return false;
  1589. }
  1590. } else if (!IsMenuOpen && (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked || me.Flags == MouseFlags.Button1TripleClicked || me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  1591. Application.GrabMouse (current);
  1592. } else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu)) {
  1593. Application.GrabMouse (me.View);
  1594. } else {
  1595. handled = false;
  1596. return false;
  1597. }
  1598. //if (me.View != this && me.Flags != MouseFlags.Button1Pressed)
  1599. // return true;
  1600. //else if (me.View != this && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1601. // Application.UngrabMouse ();
  1602. // host.CloseAllMenus ();
  1603. // return true;
  1604. //}
  1605. //if (!(me.View is MenuBar) && !(me.View is Menu) && me.Flags != MouseFlags.Button1Pressed))
  1606. // return false;
  1607. //if (Application.mouseGrabView != null) {
  1608. // if (me.View is MenuBar || me.View is Menu) {
  1609. // me.X -= me.OfX;
  1610. // me.Y -= me.OfY;
  1611. // me.View.MouseEvent (me);
  1612. // return true;
  1613. // } else if (!(me.View is MenuBar || me.View is Menu) && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1614. // Application.UngrabMouse ();
  1615. // CloseAllMenus ();
  1616. // }
  1617. //} else if (!isMenuClosed && selected == -1 && me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1618. // Application.GrabMouse (this);
  1619. // return true;
  1620. //}
  1621. //if (Application.mouseGrabView != null) {
  1622. // if (Application.mouseGrabView == me.View && me.View == current) {
  1623. // me.X -= me.OfX;
  1624. // me.Y -= me.OfY;
  1625. // } else if (me.View != current && me.View is MenuBar && me.View is Menu) {
  1626. // Application.UngrabMouse ();
  1627. // Application.GrabMouse (me.View);
  1628. // } else if (me.Flags == MouseFlags.Button1Pressed || me.Flags == MouseFlags.Button1DoubleClicked) {
  1629. // Application.UngrabMouse ();
  1630. // CloseMenu ();
  1631. // }
  1632. //} else if ((!isMenuClosed && selected > -1)) {
  1633. // Application.GrabMouse (current);
  1634. //}
  1635. handled = true;
  1636. return true;
  1637. }
  1638. MenuBar GetMouseGrabViewInstance (View view)
  1639. {
  1640. if (view == null || Application.mouseGrabView == null) {
  1641. return null;
  1642. }
  1643. MenuBar hostView = null;
  1644. if (view is MenuBar) {
  1645. hostView = (MenuBar)view;
  1646. } else if (view is Menu) {
  1647. hostView = ((Menu)view).host;
  1648. }
  1649. var grabView = Application.mouseGrabView;
  1650. MenuBar hostGrabView = null;
  1651. if (grabView is MenuBar) {
  1652. hostGrabView = (MenuBar)grabView;
  1653. } else if (grabView is Menu) {
  1654. hostGrabView = ((Menu)grabView).host;
  1655. }
  1656. return hostView != hostGrabView ? hostGrabView : null;
  1657. }
  1658. ///<inheritdoc/>
  1659. public override bool OnEnter (View view)
  1660. {
  1661. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  1662. return base.OnEnter (view);
  1663. }
  1664. }
  1665. /// <summary>
  1666. /// An <see cref="EventArgs"/> which allows passing a cancelable menu opening event or replacing with a new <see cref="MenuBarItem"/>.
  1667. /// </summary>
  1668. public class MenuOpeningEventArgs : EventArgs {
  1669. /// <summary>
  1670. /// The current <see cref="MenuBarItem"/> parent.
  1671. /// </summary>
  1672. public MenuBarItem CurrentMenu { get; }
  1673. /// <summary>
  1674. /// The new <see cref="MenuBarItem"/> to be replaced.
  1675. /// </summary>
  1676. public MenuBarItem NewMenuBarItem { get; set; }
  1677. /// <summary>
  1678. /// Flag that allows you to cancel the opening of the menu.
  1679. /// </summary>
  1680. public bool Cancel { get; set; }
  1681. /// <summary>
  1682. /// Initializes a new instance of <see cref="MenuOpeningEventArgs"/>
  1683. /// </summary>
  1684. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1685. public MenuOpeningEventArgs (MenuBarItem currentMenu)
  1686. {
  1687. CurrentMenu = currentMenu;
  1688. }
  1689. }
  1690. /// <summary>
  1691. /// An <see cref="EventArgs"/> which allows passing a cancelable menu closing event.
  1692. /// </summary>
  1693. public class MenuClosingEventArgs : EventArgs {
  1694. /// <summary>
  1695. /// The current <see cref="MenuBarItem"/> parent.
  1696. /// </summary>
  1697. public MenuBarItem CurrentMenu { get; }
  1698. /// <summary>
  1699. /// Indicates whether the current menu will be reopen.
  1700. /// </summary>
  1701. public bool Reopen { get; }
  1702. /// <summary>
  1703. /// Indicates whether the current menu is a sub-menu.
  1704. /// </summary>
  1705. public bool IsSubMenu { get; }
  1706. /// <summary>
  1707. /// Flag that allows you to cancel the opening of the menu.
  1708. /// </summary>
  1709. public bool Cancel { get; set; }
  1710. /// <summary>
  1711. /// Initializes a new instance of <see cref="MenuClosingEventArgs"/>
  1712. /// </summary>
  1713. /// <param name="currentMenu">The current <see cref="MenuBarItem"/> parent.</param>
  1714. /// <param name="reopen">Whether the current menu will be reopen.</param>
  1715. /// <param name="isSubMenu">Indicates whether it is a sub-menu.</param>
  1716. public MenuClosingEventArgs (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  1717. {
  1718. CurrentMenu = currentMenu;
  1719. Reopen = reopen;
  1720. IsSubMenu = isSubMenu;
  1721. }
  1722. }
  1723. }