MenuBar.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// <para>Provides a menu bar that spans the top of a <see cref="Toplevel"/> View with drop-down and cascading menus.</para>
  4. /// <para>
  5. /// By default, any sub-sub-menus (sub-menus of the <see cref="MenuItem"/>s added to <see cref="MenuBarItem"/>s)
  6. /// are displayed in a cascading manner, where each sub-sub-menu pops out of the sub-menu frame (either to the
  7. /// right or left, depending on where the sub-menu is relative to the edge of the screen). By setting
  8. /// <see cref="UseSubMenusSingleFrame"/> to <see langword="true"/>, this behavior can be changed such that all
  9. /// sub-sub-menus are drawn within a single frame below the MenuBar.
  10. /// </para>
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// The <see cref="MenuBar"/> appears on the first row of the <see cref="Toplevel"/> SuperView and uses the full
  15. /// width.
  16. /// </para>
  17. /// <para>See also: <see cref="ContextMenu"/></para>
  18. /// <para>The <see cref="MenuBar"/> provides global hot keys for the application. See <see cref="MenuItem.HotKey"/>.</para>
  19. /// <para>
  20. /// When the menu is created key bindings for each menu item and its sub-menu items are added for each menu
  21. /// item's hot key (both alone AND with AltMask) and shortcut, if defined.
  22. /// </para>
  23. /// <para>
  24. /// If a key press matches any of the menu item's hot keys or shortcuts, the menu item's action is invoked or
  25. /// sub-menu opened.
  26. /// </para>
  27. /// <para>
  28. /// * If the menu bar is not open * Any shortcut defined within the menu will be invoked * Only hot keys defined
  29. /// for the menu bar items will be invoked, and only if Alt is pressed too. * If the menu bar is open * Un-shifted
  30. /// hot keys defined for the menu bar items will be invoked, only if the menu they belong to is open (the menu bar
  31. /// item's text is visible). * Alt-shifted hot keys defined for the menu bar items will be invoked, only if the
  32. /// menu they belong to is open (the menu bar item's text is visible). * If there is a visible hot key that
  33. /// duplicates a shortcut (e.g. _File and Alt-F), the hot key wins.
  34. /// </para>
  35. /// </remarks>
  36. public class MenuBar : View
  37. {
  38. // Spaces before the Title
  39. private static readonly int _leftPadding = 1;
  40. // Spaces after the submenu Title, before Help
  41. private static readonly int _parensAroundHelp = 3;
  42. // Spaces after the Title
  43. private static readonly int _rightPadding = 1;
  44. // The column where the MenuBar starts
  45. private static readonly int _xOrigin = 0;
  46. internal bool _isMenuClosing;
  47. internal bool _isMenuOpening;
  48. internal Menu _openMenu;
  49. internal List<Menu> _openSubMenu;
  50. internal int _selected;
  51. internal int _selectedSub;
  52. private bool _initialCanFocus;
  53. private bool _isCleaning;
  54. private View _lastFocused;
  55. private Menu _ocm;
  56. private View _previousFocused;
  57. private bool _reopen;
  58. private bool _useSubMenusSingleFrame;
  59. /// <summary>Initializes a new instance of the <see cref="MenuBar"/>.</summary>
  60. public MenuBar ()
  61. {
  62. X = 0;
  63. Y = 0;
  64. Width = Dim.Fill ();
  65. Height = 1; // BUGBUG: Views should avoid setting Height as doing so implies Frame.Size == GetContentSize ().
  66. Menus = new MenuBarItem [] { };
  67. //CanFocus = true;
  68. _selected = -1;
  69. _selectedSub = -1;
  70. ColorScheme = Colors.ColorSchemes ["Menu"];
  71. WantMousePositionReports = true;
  72. IsMenuOpen = false;
  73. Added += MenuBar_Added;
  74. // Things this view knows how to do
  75. AddCommand (
  76. Command.Left,
  77. () =>
  78. {
  79. MoveLeft ();
  80. return true;
  81. }
  82. );
  83. AddCommand (
  84. Command.Right,
  85. () =>
  86. {
  87. MoveRight ();
  88. return true;
  89. }
  90. );
  91. AddCommand (
  92. Command.Cancel,
  93. () =>
  94. {
  95. CloseMenuBar ();
  96. return true;
  97. }
  98. );
  99. AddCommand (
  100. Command.Accept,
  101. () =>
  102. {
  103. ProcessMenu (_selected, Menus [_selected]);
  104. return true;
  105. }
  106. );
  107. AddCommand (Command.ToggleExpandCollapse, ctx => Select ((int)ctx.KeyBinding?.Context!));
  108. AddCommand (Command.Select, ctx => Run ((ctx.KeyBinding?.Context as MenuItem)?.Action));
  109. // Default key bindings for this view
  110. KeyBindings.Add (Key.CursorLeft, Command.Left);
  111. KeyBindings.Add (Key.CursorRight, Command.Right);
  112. KeyBindings.Add (Key.Esc, Command.Cancel);
  113. KeyBindings.Add (Key.CursorDown, Command.Accept);
  114. KeyBindings.Add (Key.Enter, Command.Accept);
  115. KeyBinding keyBinding = new ([Command.ToggleExpandCollapse], KeyBindingScope.HotKey, -1); // -1 indicates Key was used
  116. KeyBindings.Add (Key, keyBinding);
  117. // TODO: Why do we have two keybindings for opening the menu? Ctrl-Space and Key?
  118. KeyBindings.Add (Key.Space.WithCtrl, keyBinding);
  119. // TODO: Figure out how to make Alt work (on Windows)
  120. //KeyBindings.Add (Key.WithAlt, keyBinding);
  121. }
  122. /// <summary><see langword="true"/> if the menu is open; otherwise <see langword="true"/>.</summary>
  123. public bool IsMenuOpen { get; protected set; }
  124. /// <summary>Gets the view that was last focused before opening the menu.</summary>
  125. public View LastFocused { get; private set; }
  126. /// <summary>
  127. /// Gets or sets the array of <see cref="MenuBarItem"/>s for the menu. Only set this after the
  128. /// <see cref="MenuBar"/> is visible.
  129. /// </summary>
  130. /// <value>The menu array.</value>
  131. public MenuBarItem [] Menus
  132. {
  133. get => _menus;
  134. set
  135. {
  136. _menus = value;
  137. if (Menus is null)
  138. {
  139. return;
  140. }
  141. // TODO: Hotkeys should not work for sub-menus if they are not visible!
  142. for (var i = 0; i < Menus.Length; i++)
  143. {
  144. MenuBarItem menuBarItem = Menus [i];
  145. if (menuBarItem?.HotKey != default (Rune))
  146. {
  147. KeyBinding keyBinding = new ([Command.ToggleExpandCollapse], KeyBindingScope.HotKey, i);
  148. KeyBindings.Add ((KeyCode)menuBarItem.HotKey.Value, keyBinding);
  149. KeyBindings.Add ((KeyCode)menuBarItem.HotKey.Value | KeyCode.AltMask, keyBinding);
  150. }
  151. if (menuBarItem?.Shortcut != KeyCode.Null)
  152. {
  153. // Technically this will never run because MenuBarItems don't have shortcuts
  154. KeyBinding keyBinding = new ([Command.Select], KeyBindingScope.HotKey, i);
  155. KeyBindings.Add (menuBarItem.Shortcut, keyBinding);
  156. }
  157. menuBarItem?.AddShortcutKeyBindings (this);
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// The default <see cref="LineStyle"/> for <see cref="Menus"/>'s border. The default is
  163. /// <see cref="LineStyle.Single"/>.
  164. /// </summary>
  165. public LineStyle MenusBorderStyle { get; set; } = LineStyle.Single;
  166. /// <summary>
  167. /// Gets or sets if the sub-menus must be displayed in a single or multiple frames.
  168. /// <para>
  169. /// By default, any sub-sub-menus (sub-menus of the main <see cref="MenuItem"/>s) are displayed in a cascading
  170. /// manner, where each sub-sub-menu pops out of the sub-menu frame (either to the right or left, depending on where
  171. /// the sub-menu is relative to the edge of the screen). By setting <see cref="UseSubMenusSingleFrame"/> to
  172. /// <see langword="true"/>, this behavior can be changed such that all sub-sub-menus are drawn within a single
  173. /// frame below the MenuBar.
  174. /// </para>
  175. /// </summary>
  176. public bool UseSubMenusSingleFrame
  177. {
  178. get => _useSubMenusSingleFrame;
  179. set
  180. {
  181. _useSubMenusSingleFrame = value;
  182. if (value && UseKeysUpDownAsKeysLeftRight)
  183. {
  184. _useKeysUpDownAsKeysLeftRight = false;
  185. SetNeedsDisplay ();
  186. }
  187. }
  188. }
  189. /// <inheritdoc/>
  190. public override bool Visible
  191. {
  192. get => base.Visible;
  193. set
  194. {
  195. base.Visible = value;
  196. if (!value)
  197. {
  198. CloseAllMenus ();
  199. }
  200. }
  201. }
  202. internal Menu OpenCurrentMenu
  203. {
  204. get => _ocm;
  205. set
  206. {
  207. if (_ocm != value)
  208. {
  209. _ocm = value;
  210. if (_ocm is { } && _ocm._currentChild > -1)
  211. {
  212. OnMenuOpened ();
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>Closes the Menu programmatically if open and not canceled (as though F9 were pressed).</summary>
  218. public bool CloseMenu (bool ignoreUseSubMenusSingleFrame = false) { return CloseMenu (false, false, ignoreUseSubMenusSingleFrame); }
  219. /// <summary>Raised when all the menu is closed.</summary>
  220. public event EventHandler MenuAllClosed;
  221. /// <summary>Raised when a menu is closing passing <see cref="MenuClosingEventArgs"/>.</summary>
  222. public event EventHandler<MenuClosingEventArgs> MenuClosing;
  223. /// <summary>Raised when a menu is opened.</summary>
  224. public event EventHandler<MenuOpenedEventArgs> MenuOpened;
  225. /// <summary>Raised as a menu is opening.</summary>
  226. public event EventHandler<MenuOpeningEventArgs> MenuOpening;
  227. /// <inheritdoc/>
  228. public override void OnDrawContent (Rectangle viewport)
  229. {
  230. Driver.SetAttribute (GetNormalColor ());
  231. Clear ();
  232. var pos = 0;
  233. for (var i = 0; i < Menus.Length; i++)
  234. {
  235. MenuBarItem menu = Menus [i];
  236. Move (pos, 0);
  237. Attribute hotColor, normalColor;
  238. if (i == _selected && IsMenuOpen)
  239. {
  240. hotColor = i == _selected ? ColorScheme.HotFocus : GetHotNormalColor ();
  241. normalColor = i == _selected ? GetFocusColor () : GetNormalColor ();
  242. }
  243. else
  244. {
  245. hotColor = GetHotNormalColor ();
  246. normalColor = GetNormalColor ();
  247. }
  248. // Note Help on MenuBar is drawn with parens around it
  249. DrawHotString (
  250. string.IsNullOrEmpty (menu.Help) ? $" {menu.Title} " : $" {menu.Title} ({menu.Help}) ",
  251. hotColor,
  252. normalColor
  253. );
  254. pos += _leftPadding
  255. + menu.TitleLength
  256. + (menu.Help.GetColumns () > 0
  257. ? _leftPadding + menu.Help.GetColumns () + _parensAroundHelp
  258. : 0)
  259. + _rightPadding;
  260. }
  261. //PositionCursor ();
  262. }
  263. /// <summary>Virtual method that will invoke the <see cref="MenuAllClosed"/>.</summary>
  264. public virtual void OnMenuAllClosed () { MenuAllClosed?.Invoke (this, EventArgs.Empty); }
  265. /// <summary>Virtual method that will invoke the <see cref="MenuClosing"/>.</summary>
  266. /// <param name="currentMenu">The current menu to be closed.</param>
  267. /// <param name="reopen">Whether the current menu will be reopen.</param>
  268. /// <param name="isSubMenu">Whether is a sub-menu or not.</param>
  269. public virtual MenuClosingEventArgs OnMenuClosing (MenuBarItem currentMenu, bool reopen, bool isSubMenu)
  270. {
  271. var ev = new MenuClosingEventArgs (currentMenu, reopen, isSubMenu);
  272. MenuClosing?.Invoke (this, ev);
  273. return ev;
  274. }
  275. /// <summary>Virtual method that will invoke the <see cref="MenuOpened"/> event if it's defined.</summary>
  276. public virtual void OnMenuOpened ()
  277. {
  278. MenuItem mi = null;
  279. MenuBarItem parent;
  280. if (OpenCurrentMenu.BarItems.Children != null
  281. && OpenCurrentMenu.BarItems!.Children.Length > 0
  282. && OpenCurrentMenu?._currentChild > -1)
  283. {
  284. parent = OpenCurrentMenu.BarItems;
  285. mi = parent.Children [OpenCurrentMenu._currentChild];
  286. }
  287. else if (OpenCurrentMenu!.BarItems.IsTopLevel)
  288. {
  289. parent = null;
  290. mi = OpenCurrentMenu.BarItems;
  291. }
  292. else
  293. {
  294. parent = _openMenu.BarItems;
  295. mi = parent.Children?.Length > 0 ? parent.Children [_openMenu._currentChild] : null;
  296. }
  297. MenuOpened?.Invoke (this, new (parent, mi));
  298. }
  299. /// <summary>Virtual method that will invoke the <see cref="MenuOpening"/> event if it's defined.</summary>
  300. /// <param name="currentMenu">The current menu to be replaced.</param>
  301. /// <returns>Returns the <see cref="MenuOpeningEventArgs"/></returns>
  302. public virtual MenuOpeningEventArgs OnMenuOpening (MenuBarItem currentMenu)
  303. {
  304. var ev = new MenuOpeningEventArgs (currentMenu);
  305. MenuOpening?.Invoke (this, ev);
  306. return ev;
  307. }
  308. /// <summary>Opens the Menu programatically, as though the F9 key were pressed.</summary>
  309. public void OpenMenu ()
  310. {
  311. MenuBar mbar = GetMouseGrabViewInstance (this);
  312. if (mbar is { })
  313. {
  314. mbar.CleanUp ();
  315. }
  316. if (!Enabled || _openMenu is { })
  317. {
  318. return;
  319. }
  320. _selected = 0;
  321. SetNeedsDisplay ();
  322. _previousFocused = SuperView is null ? Application.Current?.Focused : SuperView.Focused;
  323. OpenMenu (_selected);
  324. if (!SelectEnabledItem (
  325. OpenCurrentMenu.BarItems.Children,
  326. OpenCurrentMenu._currentChild,
  327. out OpenCurrentMenu._currentChild
  328. )
  329. && !CloseMenu (false))
  330. {
  331. return;
  332. }
  333. if (!OpenCurrentMenu.CheckSubMenu ())
  334. {
  335. return;
  336. }
  337. Application.GrabMouse (this);
  338. }
  339. /// <inheritdoc/>
  340. public override Point? PositionCursor ()
  341. {
  342. if (_selected == -1 && HasFocus && Menus.Length > 0)
  343. {
  344. _selected = 0;
  345. }
  346. var pos = 0;
  347. for (var i = 0; i < Menus.Length; i++)
  348. {
  349. if (i == _selected)
  350. {
  351. pos++;
  352. Move (pos + 1, 0);
  353. return null; // Don't show the cursor
  354. }
  355. pos += _leftPadding
  356. + Menus [i].TitleLength
  357. + (Menus [i].Help.GetColumns () > 0
  358. ? Menus [i].Help.GetColumns () + _parensAroundHelp
  359. : 0)
  360. + _rightPadding;
  361. }
  362. return null; // Don't show the cursor
  363. }
  364. // Activates the menu, handles either first focus, or activating an entry when it was already active
  365. // For mouse events.
  366. internal void Activate (int idx, int sIdx = -1, MenuBarItem subMenu = null)
  367. {
  368. _selected = idx;
  369. _selectedSub = sIdx;
  370. if (_openMenu is null)
  371. {
  372. _previousFocused = SuperView is null ? Application.Current?.Focused ?? null : SuperView.Focused;
  373. }
  374. OpenMenu (idx, sIdx, subMenu);
  375. SetNeedsDisplay ();
  376. }
  377. internal void CleanUp ()
  378. {
  379. _isCleaning = true;
  380. if (_openMenu is { })
  381. {
  382. CloseAllMenus ();
  383. }
  384. _openedByAltKey = false;
  385. IsMenuOpen = false;
  386. _selected = -1;
  387. CanFocus = _initialCanFocus;
  388. if (_lastFocused is { })
  389. {
  390. _lastFocused.SetFocus ();
  391. }
  392. SetNeedsDisplay ();
  393. Application.UngrabMouse ();
  394. _isCleaning = false;
  395. }
  396. internal void CloseAllMenus ()
  397. {
  398. if (!_isMenuOpening && !_isMenuClosing)
  399. {
  400. if (_openSubMenu is { } && !CloseMenu (false, true, true))
  401. {
  402. return;
  403. }
  404. if (!CloseMenu (false))
  405. {
  406. return;
  407. }
  408. if (LastFocused is { } && LastFocused != this)
  409. {
  410. _selected = -1;
  411. }
  412. Application.UngrabMouse ();
  413. }
  414. if (OpenCurrentMenu is { })
  415. {
  416. OpenCurrentMenu = null;
  417. }
  418. IsMenuOpen = false;
  419. _openedByAltKey = false;
  420. OnMenuAllClosed ();
  421. }
  422. internal bool CloseMenu (bool reopen = false, bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  423. {
  424. MenuBarItem mbi = isSubMenu ? OpenCurrentMenu.BarItems : _openMenu?.BarItems;
  425. if (UseSubMenusSingleFrame && mbi is { } && !ignoreUseSubMenusSingleFrame && mbi.Parent is { })
  426. {
  427. return false;
  428. }
  429. _isMenuClosing = true;
  430. _reopen = reopen;
  431. MenuClosingEventArgs args = OnMenuClosing (mbi, reopen, isSubMenu);
  432. if (args.Cancel)
  433. {
  434. _isMenuClosing = false;
  435. if (args.CurrentMenu.Parent is { } && _openMenu is { })
  436. {
  437. _openMenu._currentChild =
  438. ((MenuBarItem)args.CurrentMenu.Parent).Children.IndexOf (args.CurrentMenu);
  439. }
  440. return false;
  441. }
  442. switch (isSubMenu)
  443. {
  444. case false:
  445. if (_openMenu is { })
  446. {
  447. Application.Current?.Remove (_openMenu);
  448. }
  449. SetNeedsDisplay ();
  450. if (_previousFocused is Menu && _openMenu is { } && _previousFocused.ToString () != OpenCurrentMenu.ToString ())
  451. {
  452. _previousFocused.SetFocus ();
  453. }
  454. _openMenu?.Dispose ();
  455. _openMenu = null;
  456. if (_lastFocused is Menu or MenuBar)
  457. {
  458. _lastFocused = null;
  459. }
  460. LastFocused = _lastFocused;
  461. _lastFocused = null;
  462. if (LastFocused is { CanFocus: true })
  463. {
  464. if (!reopen)
  465. {
  466. _selected = -1;
  467. }
  468. if (_openSubMenu is { })
  469. {
  470. _openSubMenu = null;
  471. }
  472. if (OpenCurrentMenu is { })
  473. {
  474. Application.Current?.Remove (OpenCurrentMenu);
  475. OpenCurrentMenu.Dispose ();
  476. OpenCurrentMenu = null;
  477. }
  478. LastFocused.SetFocus ();
  479. }
  480. else if (_openSubMenu is null || _openSubMenu.Count == 0)
  481. {
  482. CloseAllMenus ();
  483. }
  484. else
  485. {
  486. SetFocus ();
  487. }
  488. IsMenuOpen = false;
  489. break;
  490. case true:
  491. _selectedSub = -1;
  492. SetNeedsDisplay ();
  493. RemoveAllOpensSubMenus ();
  494. OpenCurrentMenu._previousSubFocused.SetFocus ();
  495. _openSubMenu = null;
  496. IsMenuOpen = true;
  497. break;
  498. }
  499. _reopen = false;
  500. _isMenuClosing = false;
  501. return true;
  502. }
  503. /// <summary>Gets the superview location offset relative to the <see cref="ConsoleDriver"/> location.</summary>
  504. /// <returns>The location offset.</returns>
  505. internal Point GetScreenOffset ()
  506. {
  507. if (Driver is null)
  508. {
  509. return Point.Empty;
  510. }
  511. Rectangle superViewFrame = SuperView is null ? Driver.Screen : SuperView.Frame;
  512. View sv = SuperView is null ? Application.Current : SuperView;
  513. if (sv is null)
  514. {
  515. // Support Unit Tests
  516. return Point.Empty;
  517. }
  518. Point viewportOffset = sv?.GetViewportOffsetFromFrame () ?? Point.Empty;
  519. return new (
  520. superViewFrame.X - sv.Frame.X - viewportOffset.X,
  521. superViewFrame.Y - sv.Frame.Y - viewportOffset.Y
  522. );
  523. }
  524. internal void NextMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  525. {
  526. switch (isSubMenu)
  527. {
  528. case false:
  529. if (_selected == -1)
  530. {
  531. _selected = 0;
  532. }
  533. else if (_selected + 1 == Menus.Length)
  534. {
  535. _selected = 0;
  536. }
  537. else
  538. {
  539. _selected++;
  540. }
  541. if (_selected > -1 && !CloseMenu (true, ignoreUseSubMenusSingleFrame))
  542. {
  543. return;
  544. }
  545. OpenMenu (_selected);
  546. SelectEnabledItem (
  547. OpenCurrentMenu.BarItems.Children,
  548. OpenCurrentMenu._currentChild,
  549. out OpenCurrentMenu._currentChild
  550. );
  551. break;
  552. case true:
  553. if (UseKeysUpDownAsKeysLeftRight)
  554. {
  555. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame))
  556. {
  557. NextMenu (false, ignoreUseSubMenusSingleFrame);
  558. }
  559. }
  560. else
  561. {
  562. MenuBarItem subMenu = OpenCurrentMenu._currentChild > -1 && OpenCurrentMenu.BarItems.Children.Length > 0
  563. ? OpenCurrentMenu.BarItems.SubMenu (
  564. OpenCurrentMenu.BarItems.Children [OpenCurrentMenu._currentChild]
  565. )
  566. : null;
  567. if ((_selectedSub == -1 || _openSubMenu is null || _openSubMenu?.Count - 1 == _selectedSub) && subMenu is null)
  568. {
  569. if (_openSubMenu is { } && !CloseMenu (false, true))
  570. {
  571. return;
  572. }
  573. NextMenu (false, ignoreUseSubMenusSingleFrame);
  574. }
  575. else if (subMenu != null
  576. || (OpenCurrentMenu._currentChild > -1
  577. && !OpenCurrentMenu.BarItems
  578. .Children [OpenCurrentMenu._currentChild]
  579. .IsFromSubMenu))
  580. {
  581. _selectedSub++;
  582. OpenCurrentMenu.CheckSubMenu ();
  583. }
  584. else
  585. {
  586. if (CloseMenu (false, true, ignoreUseSubMenusSingleFrame))
  587. {
  588. NextMenu (false, ignoreUseSubMenusSingleFrame);
  589. }
  590. return;
  591. }
  592. SetNeedsDisplay ();
  593. if (UseKeysUpDownAsKeysLeftRight)
  594. {
  595. OpenCurrentMenu.CheckSubMenu ();
  596. }
  597. }
  598. break;
  599. }
  600. }
  601. internal void OpenMenu (int index, int sIndex = -1, MenuBarItem subMenu = null)
  602. {
  603. _isMenuOpening = true;
  604. MenuOpeningEventArgs newMenu = OnMenuOpening (Menus [index]);
  605. if (newMenu.Cancel)
  606. {
  607. _isMenuOpening = false;
  608. return;
  609. }
  610. if (newMenu.NewMenuBarItem is { })
  611. {
  612. Menus [index] = newMenu.NewMenuBarItem;
  613. }
  614. var pos = 0;
  615. switch (subMenu)
  616. {
  617. case null:
  618. // Open a submenu below a MenuBar
  619. _lastFocused ??= SuperView is null ? Application.Current?.MostFocused : SuperView.MostFocused;
  620. if (_openSubMenu is { } && !CloseMenu (false, true))
  621. {
  622. return;
  623. }
  624. if (_openMenu is { })
  625. {
  626. Application.Current?.Remove (_openMenu);
  627. _openMenu.Dispose ();
  628. _openMenu = null;
  629. }
  630. // This positions the submenu horizontally aligned with the first character of the
  631. // text belonging to the menu
  632. for (var i = 0; i < index; i++)
  633. {
  634. pos += Menus [i].TitleLength + (Menus [i].Help.GetColumns () > 0 ? Menus [i].Help.GetColumns () + 2 : 0) + _leftPadding + _rightPadding;
  635. }
  636. var locationOffset = Point.Empty;
  637. // if SuperView is null then it's from a ContextMenu
  638. if (SuperView is null)
  639. {
  640. locationOffset = GetScreenOffset ();
  641. }
  642. if (SuperView is { } && SuperView != Application.Current)
  643. {
  644. locationOffset.X += SuperView.Border.Thickness.Left;
  645. locationOffset.Y += SuperView.Border.Thickness.Top;
  646. }
  647. _openMenu = new ()
  648. {
  649. Host = this,
  650. X = Frame.X + pos + locationOffset.X,
  651. Y = Frame.Y + 1 + locationOffset.Y,
  652. BarItems = Menus [index],
  653. Parent = null
  654. };
  655. OpenCurrentMenu = _openMenu;
  656. OpenCurrentMenu._previousSubFocused = _openMenu;
  657. if (Application.Current is { })
  658. {
  659. Application.Current.Add (_openMenu);
  660. }
  661. else
  662. {
  663. _openMenu.BeginInit ();
  664. _openMenu.EndInit ();
  665. }
  666. _openMenu.SetFocus ();
  667. break;
  668. default:
  669. // Opens a submenu next to another submenu (openSubMenu)
  670. if (_openSubMenu is null)
  671. {
  672. _openSubMenu = new ();
  673. }
  674. if (sIndex > -1)
  675. {
  676. RemoveSubMenu (sIndex);
  677. }
  678. else
  679. {
  680. Menu last = _openSubMenu.Count > 0 ? _openSubMenu.Last () : _openMenu;
  681. if (!UseSubMenusSingleFrame)
  682. {
  683. locationOffset = GetLocationOffset ();
  684. OpenCurrentMenu = new ()
  685. {
  686. Host = this,
  687. X = last.Frame.Left + last.Frame.Width + locationOffset.X,
  688. Y = last.Frame.Top + locationOffset.Y + last._currentChild,
  689. BarItems = subMenu,
  690. Parent = last
  691. };
  692. }
  693. else
  694. {
  695. Menu first = _openSubMenu.Count > 0 ? _openSubMenu.First () : _openMenu;
  696. // 2 is for the parent and the separator
  697. MenuItem [] mbi = new MenuItem [2 + subMenu.Children.Length];
  698. mbi [0] = new () { Title = subMenu.Title, Parent = subMenu };
  699. mbi [1] = null;
  700. for (var j = 0; j < subMenu.Children.Length; j++)
  701. {
  702. mbi [j + 2] = subMenu.Children [j];
  703. }
  704. var newSubMenu = new MenuBarItem (mbi) { Parent = subMenu };
  705. OpenCurrentMenu = new ()
  706. {
  707. Host = this, X = first.Frame.Left, Y = first.Frame.Top, BarItems = newSubMenu
  708. };
  709. last.Visible = false;
  710. Application.GrabMouse (OpenCurrentMenu);
  711. }
  712. OpenCurrentMenu._previousSubFocused = last._previousSubFocused;
  713. _openSubMenu.Add (OpenCurrentMenu);
  714. Application.Current?.Add (OpenCurrentMenu);
  715. if (!OpenCurrentMenu.IsInitialized)
  716. {
  717. // Supports unit tests
  718. OpenCurrentMenu.BeginInit ();
  719. OpenCurrentMenu.EndInit ();
  720. }
  721. }
  722. _selectedSub = _openSubMenu.Count - 1;
  723. if (_selectedSub > -1
  724. && SelectEnabledItem (
  725. OpenCurrentMenu.BarItems.Children,
  726. OpenCurrentMenu._currentChild,
  727. out OpenCurrentMenu._currentChild
  728. ))
  729. {
  730. OpenCurrentMenu.SetFocus ();
  731. }
  732. break;
  733. }
  734. _isMenuOpening = false;
  735. IsMenuOpen = true;
  736. }
  737. internal void PreviousMenu (bool isSubMenu = false, bool ignoreUseSubMenusSingleFrame = false)
  738. {
  739. switch (isSubMenu)
  740. {
  741. case false:
  742. if (_selected <= 0)
  743. {
  744. _selected = Menus.Length - 1;
  745. }
  746. else
  747. {
  748. _selected--;
  749. }
  750. if (_selected > -1 && !CloseMenu (true, false, ignoreUseSubMenusSingleFrame))
  751. {
  752. return;
  753. }
  754. OpenMenu (_selected);
  755. if (!SelectEnabledItem (
  756. OpenCurrentMenu.BarItems.Children,
  757. OpenCurrentMenu._currentChild,
  758. out OpenCurrentMenu._currentChild,
  759. false
  760. ))
  761. {
  762. OpenCurrentMenu._currentChild = 0;
  763. }
  764. break;
  765. case true:
  766. if (_selectedSub > -1)
  767. {
  768. _selectedSub--;
  769. RemoveSubMenu (_selectedSub, ignoreUseSubMenusSingleFrame);
  770. SetNeedsDisplay ();
  771. }
  772. else
  773. {
  774. PreviousMenu ();
  775. }
  776. break;
  777. }
  778. }
  779. internal void RemoveAllOpensSubMenus ()
  780. {
  781. if (_openSubMenu is { })
  782. {
  783. foreach (Menu item in _openSubMenu)
  784. {
  785. Application.Current.Remove (item);
  786. item.Dispose ();
  787. }
  788. }
  789. }
  790. internal bool Run (Action action)
  791. {
  792. if (action is null)
  793. {
  794. return false;
  795. }
  796. Application.MainLoop.AddIdle (
  797. () =>
  798. {
  799. action ();
  800. return false;
  801. }
  802. );
  803. return true;
  804. }
  805. internal bool SelectEnabledItem (
  806. IEnumerable<MenuItem> children,
  807. int current,
  808. out int newCurrent,
  809. bool forward = true
  810. )
  811. {
  812. if (children is null)
  813. {
  814. newCurrent = -1;
  815. return true;
  816. }
  817. IEnumerable<MenuItem> childMenuItems = forward ? children : children.Reverse ();
  818. int count;
  819. IEnumerable<MenuItem> menuItems = childMenuItems as MenuItem [] ?? childMenuItems.ToArray ();
  820. if (forward)
  821. {
  822. count = -1;
  823. }
  824. else
  825. {
  826. count = menuItems.Count ();
  827. }
  828. foreach (MenuItem child in menuItems)
  829. {
  830. if (forward)
  831. {
  832. if (++count < current)
  833. {
  834. continue;
  835. }
  836. }
  837. else
  838. {
  839. if (--count > current)
  840. {
  841. continue;
  842. }
  843. }
  844. if (child is null || !child.IsEnabled ())
  845. {
  846. if (forward)
  847. {
  848. current++;
  849. }
  850. else
  851. {
  852. current--;
  853. }
  854. }
  855. else
  856. {
  857. newCurrent = current;
  858. return true;
  859. }
  860. }
  861. newCurrent = -1;
  862. return false;
  863. }
  864. /// <summary>Called when an item is selected; Runs the action.</summary>
  865. /// <param name="item"></param>
  866. internal bool SelectItem (MenuItem item)
  867. {
  868. if (item?.Action is null)
  869. {
  870. return false;
  871. }
  872. Application.UngrabMouse ();
  873. CloseAllMenus ();
  874. Application.Refresh ();
  875. _openedByAltKey = true;
  876. return Run (item?.Action);
  877. }
  878. private void CloseMenuBar ()
  879. {
  880. if (!CloseMenu (false))
  881. {
  882. return;
  883. }
  884. if (_openedByAltKey)
  885. {
  886. _openedByAltKey = false;
  887. LastFocused?.SetFocus ();
  888. }
  889. SetNeedsDisplay ();
  890. }
  891. private Point GetLocationOffset ()
  892. {
  893. if (MenusBorderStyle != LineStyle.None)
  894. {
  895. return new (0, 1);
  896. }
  897. return new (-2, 0);
  898. }
  899. private void MenuBar_Added (object sender, SuperViewChangedEventArgs e)
  900. {
  901. _initialCanFocus = CanFocus;
  902. Added -= MenuBar_Added;
  903. }
  904. private void MoveLeft ()
  905. {
  906. _selected--;
  907. if (_selected < 0)
  908. {
  909. _selected = Menus.Length - 1;
  910. }
  911. OpenMenu (_selected);
  912. SetNeedsDisplay ();
  913. }
  914. private void MoveRight ()
  915. {
  916. _selected = (_selected + 1) % Menus.Length;
  917. OpenMenu (_selected);
  918. SetNeedsDisplay ();
  919. }
  920. private void ProcessMenu (int i, MenuBarItem mi)
  921. {
  922. if (_selected < 0 && IsMenuOpen)
  923. {
  924. return;
  925. }
  926. if (mi.IsTopLevel)
  927. {
  928. Point screen = ViewportToScreen (new Point (0, i));
  929. var menu = new Menu { Host = this, X = screen.X, Y = screen.Y, BarItems = mi };
  930. menu.Run (mi.Action);
  931. menu.Dispose ();
  932. }
  933. else
  934. {
  935. Application.GrabMouse (this);
  936. _selected = i;
  937. OpenMenu (i);
  938. if (!SelectEnabledItem (
  939. OpenCurrentMenu.BarItems.Children,
  940. OpenCurrentMenu._currentChild,
  941. out OpenCurrentMenu._currentChild
  942. )
  943. && !CloseMenu (false))
  944. {
  945. return;
  946. }
  947. if (!OpenCurrentMenu.CheckSubMenu ())
  948. {
  949. return;
  950. }
  951. }
  952. SetNeedsDisplay ();
  953. }
  954. private void RemoveSubMenu (int index, bool ignoreUseSubMenusSingleFrame = false)
  955. {
  956. if (_openSubMenu == null
  957. || (UseSubMenusSingleFrame
  958. && !ignoreUseSubMenusSingleFrame
  959. && _openSubMenu.Count == 0))
  960. {
  961. return;
  962. }
  963. for (int i = _openSubMenu.Count - 1; i > index; i--)
  964. {
  965. _isMenuClosing = true;
  966. Menu menu;
  967. if (_openSubMenu.Count - 1 > 0)
  968. {
  969. menu = _openSubMenu [i - 1];
  970. }
  971. else
  972. {
  973. menu = _openMenu;
  974. }
  975. if (!menu.Visible)
  976. {
  977. menu.Visible = true;
  978. }
  979. OpenCurrentMenu = menu;
  980. OpenCurrentMenu.SetFocus ();
  981. if (_openSubMenu is { })
  982. {
  983. menu = _openSubMenu [i];
  984. Application.Current.Remove (menu);
  985. _openSubMenu.Remove (menu);
  986. if (Application.MouseGrabView == menu)
  987. {
  988. Application.GrabMouse (this);
  989. }
  990. menu.Dispose ();
  991. }
  992. RemoveSubMenu (i, ignoreUseSubMenusSingleFrame);
  993. }
  994. if (_openSubMenu.Count > 0)
  995. {
  996. OpenCurrentMenu = _openSubMenu.Last ();
  997. }
  998. _isMenuClosing = false;
  999. }
  1000. #region Keyboard handling
  1001. private Key _key = Key.F9;
  1002. /// <summary>
  1003. /// The <see cref="Key"/> used to activate or close the menu bar by keyboard. The default is <see cref="Key.F9"/>
  1004. /// .
  1005. /// </summary>
  1006. /// <remarks>
  1007. /// <para>
  1008. /// If the user presses any <see cref="MenuItem.HotKey"/>s defined in the <see cref="MenuBarItem"/>s, the menu
  1009. /// bar will be activated and the sub-menu will be opened.
  1010. /// </para>
  1011. /// <para><see cref="Key.Esc"/> will close the menu bar and any open sub-menus.</para>
  1012. /// </remarks>
  1013. public Key Key
  1014. {
  1015. get => _key;
  1016. set
  1017. {
  1018. if (_key == value)
  1019. {
  1020. return;
  1021. }
  1022. KeyBindings.Remove (_key);
  1023. KeyBinding keyBinding = new ([Command.ToggleExpandCollapse], KeyBindingScope.HotKey, -1); // -1 indicates Key was used
  1024. KeyBindings.Add (value, keyBinding);
  1025. _key = value;
  1026. }
  1027. }
  1028. private bool _useKeysUpDownAsKeysLeftRight;
  1029. /// <summary>Used for change the navigation key style.</summary>
  1030. public bool UseKeysUpDownAsKeysLeftRight
  1031. {
  1032. get => _useKeysUpDownAsKeysLeftRight;
  1033. set
  1034. {
  1035. _useKeysUpDownAsKeysLeftRight = value;
  1036. if (value && UseSubMenusSingleFrame)
  1037. {
  1038. UseSubMenusSingleFrame = false;
  1039. SetNeedsDisplay ();
  1040. }
  1041. }
  1042. }
  1043. private static Rune _shortcutDelimiter = new ('+');
  1044. /// <summary>Sets or gets the shortcut delimiter separator. The default is "+".</summary>
  1045. public static Rune ShortcutDelimiter
  1046. {
  1047. get => _shortcutDelimiter;
  1048. set
  1049. {
  1050. if (_shortcutDelimiter != value)
  1051. {
  1052. _shortcutDelimiter = value == default (Rune) ? new ('+') : value;
  1053. }
  1054. }
  1055. }
  1056. /// <summary>The specifier character for the hot keys.</summary>
  1057. public new static Rune HotKeySpecifier => (Rune)'_';
  1058. // TODO: This doesn't actually work. Figure out why.
  1059. private bool _openedByAltKey;
  1060. /// <summary>
  1061. /// Called when a key bound to Command.Select is pressed. Either activates the menu item or runs it, depending on
  1062. /// whether it has a sub-menu. If the menu is open, it will close the menu bar.
  1063. /// </summary>
  1064. /// <param name="index">The index of the menu bar item to select. -1 if the selection was via <see cref="Key"/>.</param>
  1065. /// <returns></returns>
  1066. private bool Select (int index)
  1067. {
  1068. if (!IsInitialized || !Visible)
  1069. {
  1070. return true;
  1071. }
  1072. // If the menubar is open and the menu that's open is 'index' then close it. Otherwise activate it.
  1073. if (IsMenuOpen)
  1074. {
  1075. if (index == -1)
  1076. {
  1077. CloseAllMenus ();
  1078. return true;
  1079. }
  1080. // Find the index of the open submenu and close the menu if it matches
  1081. for (var i = 0; i < Menus.Length; i++)
  1082. {
  1083. MenuBarItem open = Menus [i];
  1084. if (open is null)
  1085. {
  1086. continue;
  1087. }
  1088. if (open == OpenCurrentMenu.BarItems && i == index)
  1089. {
  1090. CloseAllMenus ();
  1091. return true;
  1092. }
  1093. }
  1094. }
  1095. if (index == -1)
  1096. {
  1097. OpenMenu ();
  1098. }
  1099. else
  1100. {
  1101. Activate (index);
  1102. }
  1103. return true;
  1104. }
  1105. #endregion Keyboard handling
  1106. #region Mouse Handling
  1107. /// <inheritdoc/>
  1108. public override bool OnLeave (View view)
  1109. {
  1110. if (((!(view is MenuBar) && !(view is Menu)) || (!(view is MenuBar) && !(view is Menu) && _openMenu is { })) && !_isCleaning && !_reopen)
  1111. {
  1112. CleanUp ();
  1113. }
  1114. return base.OnLeave (view);
  1115. }
  1116. /// <inheritdoc/>
  1117. protected internal override bool OnMouseEvent (MouseEvent me)
  1118. {
  1119. if (!_handled && !HandleGrabView (me, this))
  1120. {
  1121. return false;
  1122. }
  1123. _handled = false;
  1124. if (me.Flags == MouseFlags.Button1Pressed
  1125. || me.Flags == MouseFlags.Button1DoubleClicked
  1126. || me.Flags == MouseFlags.Button1TripleClicked
  1127. || me.Flags == MouseFlags.Button1Clicked
  1128. || (me.Flags == MouseFlags.ReportMousePosition && _selected > -1)
  1129. || (me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && _selected > -1))
  1130. {
  1131. int pos = _xOrigin;
  1132. Point locationOffset = default;
  1133. if (SuperView is { })
  1134. {
  1135. locationOffset.X += SuperView.Border.Thickness.Left;
  1136. locationOffset.Y += SuperView.Border.Thickness.Top;
  1137. }
  1138. int cx = me.Position.X - locationOffset.X;
  1139. for (var i = 0; i < Menus.Length; i++)
  1140. {
  1141. if (cx >= pos && cx < pos + _leftPadding + Menus [i].TitleLength + Menus [i].Help.GetColumns () + _rightPadding)
  1142. {
  1143. if (me.Flags == MouseFlags.Button1Clicked)
  1144. {
  1145. if (Menus [i].IsTopLevel)
  1146. {
  1147. Point screen = ViewportToScreen (new Point (0, i));
  1148. var menu = new Menu { Host = this, X = screen.X, Y = screen.Y, BarItems = Menus [i] };
  1149. menu.Run (Menus [i].Action);
  1150. menu.Dispose ();
  1151. }
  1152. else if (!IsMenuOpen)
  1153. {
  1154. Activate (i);
  1155. }
  1156. }
  1157. else if (me.Flags == MouseFlags.Button1Pressed
  1158. || me.Flags == MouseFlags.Button1DoubleClicked
  1159. || me.Flags == MouseFlags.Button1TripleClicked)
  1160. {
  1161. if (IsMenuOpen && !Menus [i].IsTopLevel)
  1162. {
  1163. CloseAllMenus ();
  1164. }
  1165. else if (!Menus [i].IsTopLevel)
  1166. {
  1167. Activate (i);
  1168. }
  1169. }
  1170. else if (_selected != i
  1171. && _selected > -1
  1172. && (me.Flags == MouseFlags.ReportMousePosition
  1173. || (me.Flags == MouseFlags.Button1Pressed && me.Flags == MouseFlags.ReportMousePosition)))
  1174. {
  1175. if (IsMenuOpen)
  1176. {
  1177. if (!CloseMenu (true, false))
  1178. {
  1179. return me.Handled = true;
  1180. }
  1181. Activate (i);
  1182. }
  1183. }
  1184. else if (IsMenuOpen)
  1185. {
  1186. if (!UseSubMenusSingleFrame
  1187. || (UseSubMenusSingleFrame
  1188. && OpenCurrentMenu != null
  1189. && OpenCurrentMenu.BarItems.Parent != null
  1190. && OpenCurrentMenu.BarItems.Parent.Parent != Menus [i]))
  1191. {
  1192. Activate (i);
  1193. }
  1194. }
  1195. return me.Handled = true;
  1196. }
  1197. if (i == Menus.Length - 1 && me.Flags == MouseFlags.Button1Clicked)
  1198. {
  1199. if (IsMenuOpen && !Menus [i].IsTopLevel)
  1200. {
  1201. CloseAllMenus ();
  1202. return me.Handled = true;
  1203. }
  1204. }
  1205. pos += _leftPadding + Menus [i].TitleLength + _rightPadding;
  1206. }
  1207. }
  1208. return false;
  1209. }
  1210. internal bool _handled;
  1211. internal bool _isContextMenuLoading;
  1212. private MenuBarItem [] _menus;
  1213. internal bool HandleGrabView (MouseEvent me, View current)
  1214. {
  1215. if (Application.MouseGrabView is { })
  1216. {
  1217. if (me.View is MenuBar || me.View is Menu)
  1218. {
  1219. MenuBar mbar = GetMouseGrabViewInstance (me.View);
  1220. if (mbar is { })
  1221. {
  1222. if (me.Flags == MouseFlags.Button1Clicked)
  1223. {
  1224. mbar.CleanUp ();
  1225. Application.GrabMouse (me.View);
  1226. }
  1227. else
  1228. {
  1229. _handled = false;
  1230. return false;
  1231. }
  1232. }
  1233. if (me.View != current)
  1234. {
  1235. Application.UngrabMouse ();
  1236. View v = me.View;
  1237. Application.GrabMouse (v);
  1238. MouseEvent nme;
  1239. if (me.Position.Y > -1)
  1240. {
  1241. Point frameLoc = v.ScreenToFrame (me.Position);
  1242. nme = new ()
  1243. {
  1244. Position = frameLoc,
  1245. Flags = me.Flags,
  1246. View = v
  1247. };
  1248. }
  1249. else
  1250. {
  1251. nme = new ()
  1252. {
  1253. Position = new (me.Position.X + current.Frame.X, me.Position.Y + current.Frame.Y),
  1254. Flags = me.Flags, View = v
  1255. };
  1256. }
  1257. v.NewMouseEvent (nme);
  1258. return false;
  1259. }
  1260. }
  1261. else if (!_isContextMenuLoading
  1262. && !(me.View is MenuBar || me.View is Menu)
  1263. && me.Flags != MouseFlags.ReportMousePosition
  1264. && me.Flags != 0)
  1265. {
  1266. Application.UngrabMouse ();
  1267. if (IsMenuOpen)
  1268. {
  1269. CloseAllMenus ();
  1270. }
  1271. _handled = false;
  1272. return false;
  1273. }
  1274. else
  1275. {
  1276. _handled = false;
  1277. _isContextMenuLoading = false;
  1278. return false;
  1279. }
  1280. }
  1281. else if (!IsMenuOpen
  1282. && (me.Flags == MouseFlags.Button1Pressed
  1283. || me.Flags == MouseFlags.Button1DoubleClicked
  1284. || me.Flags == MouseFlags.Button1TripleClicked
  1285. || me.Flags.HasFlag (
  1286. MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  1287. )))
  1288. {
  1289. Application.GrabMouse (current);
  1290. }
  1291. else if (IsMenuOpen && (me.View is MenuBar || me.View is Menu))
  1292. {
  1293. Application.GrabMouse (me.View);
  1294. }
  1295. else
  1296. {
  1297. _handled = false;
  1298. return false;
  1299. }
  1300. _handled = true;
  1301. return true;
  1302. }
  1303. private MenuBar GetMouseGrabViewInstance (View view)
  1304. {
  1305. if (view is null || Application.MouseGrabView is null)
  1306. {
  1307. return null;
  1308. }
  1309. MenuBar hostView = null;
  1310. if (view is MenuBar)
  1311. {
  1312. hostView = (MenuBar)view;
  1313. }
  1314. else if (view is Menu)
  1315. {
  1316. hostView = ((Menu)view).Host;
  1317. }
  1318. View grabView = Application.MouseGrabView;
  1319. MenuBar hostGrabView = null;
  1320. if (grabView is MenuBar)
  1321. {
  1322. hostGrabView = (MenuBar)grabView;
  1323. }
  1324. else if (grabView is Menu)
  1325. {
  1326. hostGrabView = ((Menu)grabView).Host;
  1327. }
  1328. return hostView != hostGrabView ? hostGrabView : null;
  1329. }
  1330. #endregion Mouse Handling
  1331. }