DefaultMenuInteractionHandler.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. using System;
  2. using Avalonia.Input;
  3. using Avalonia.Input.Raw;
  4. using Avalonia.Interactivity;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Rendering;
  7. using Avalonia.Threading;
  8. using Avalonia.VisualTree;
  9. #nullable enable
  10. namespace Avalonia.Controls.Platform
  11. {
  12. /// <summary>
  13. /// Provides the default keyboard and pointer interaction for menus.
  14. /// </summary>
  15. public class DefaultMenuInteractionHandler : IMenuInteractionHandler
  16. {
  17. private readonly bool _isContextMenu;
  18. private IDisposable? _inputManagerSubscription;
  19. private IRenderRoot? _root;
  20. public DefaultMenuInteractionHandler(bool isContextMenu)
  21. : this(isContextMenu, Input.InputManager.Instance, DefaultDelayRun)
  22. {
  23. }
  24. public DefaultMenuInteractionHandler(
  25. bool isContextMenu,
  26. IInputManager? inputManager,
  27. Action<Action, TimeSpan> delayRun)
  28. {
  29. delayRun = delayRun ?? throw new ArgumentNullException(nameof(delayRun));
  30. _isContextMenu = isContextMenu;
  31. InputManager = inputManager;
  32. DelayRun = delayRun;
  33. }
  34. public virtual void Attach(IMenu menu)
  35. {
  36. if (Menu != null)
  37. {
  38. throw new NotSupportedException("DefaultMenuInteractionHandler is already attached.");
  39. }
  40. Menu = menu;
  41. Menu.GotFocus += GotFocus;
  42. Menu.LostFocus += LostFocus;
  43. Menu.KeyDown += KeyDown;
  44. Menu.PointerPressed += PointerPressed;
  45. Menu.PointerReleased += PointerReleased;
  46. Menu.AddHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
  47. Menu.AddHandler(Avalonia.Controls.Menu.MenuOpenedEvent, this.MenuOpened);
  48. Menu.AddHandler(MenuItem.PointerEnterItemEvent, PointerEnter);
  49. Menu.AddHandler(MenuItem.PointerLeaveItemEvent, PointerLeave);
  50. _root = Menu.VisualRoot;
  51. if (_root is InputElement inputRoot)
  52. {
  53. inputRoot.AddHandler(InputElement.PointerPressedEvent, RootPointerPressed, RoutingStrategies.Tunnel);
  54. }
  55. if (_root is WindowBase window)
  56. {
  57. window.Deactivated += WindowDeactivated;
  58. }
  59. if (_root is TopLevel tl)
  60. tl.PlatformImpl.LostFocus += TopLevelLostPlatformFocus;
  61. _inputManagerSubscription = InputManager?.Process.Subscribe(RawInput);
  62. }
  63. public virtual void Detach(IMenu menu)
  64. {
  65. if (Menu != menu)
  66. {
  67. throw new NotSupportedException("DefaultMenuInteractionHandler is not attached to the menu.");
  68. }
  69. Menu.GotFocus -= GotFocus;
  70. Menu.LostFocus -= LostFocus;
  71. Menu.KeyDown -= KeyDown;
  72. Menu.PointerPressed -= PointerPressed;
  73. Menu.PointerReleased -= PointerReleased;
  74. Menu.RemoveHandler(AccessKeyHandler.AccessKeyPressedEvent, AccessKeyPressed);
  75. Menu.RemoveHandler(Avalonia.Controls.Menu.MenuOpenedEvent, this.MenuOpened);
  76. Menu.RemoveHandler(MenuItem.PointerEnterItemEvent, PointerEnter);
  77. Menu.RemoveHandler(MenuItem.PointerLeaveItemEvent, PointerLeave);
  78. if (_root is InputElement inputRoot)
  79. {
  80. inputRoot.RemoveHandler(InputElement.PointerPressedEvent, RootPointerPressed);
  81. }
  82. if (_root is WindowBase root)
  83. {
  84. root.Deactivated -= WindowDeactivated;
  85. }
  86. if (_root is TopLevel tl)
  87. tl.PlatformImpl.LostFocus -= TopLevelLostPlatformFocus;
  88. _inputManagerSubscription?.Dispose();
  89. Menu = null;
  90. _root = null;
  91. }
  92. protected Action<Action, TimeSpan> DelayRun { get; }
  93. protected IInputManager? InputManager { get; }
  94. protected IMenu? Menu { get; private set; }
  95. protected static TimeSpan MenuShowDelay { get; } = TimeSpan.FromMilliseconds(400);
  96. protected internal virtual void GotFocus(object sender, GotFocusEventArgs e)
  97. {
  98. var item = GetMenuItem(e.Source as IControl);
  99. if (item?.Parent != null)
  100. {
  101. item.SelectedItem = item;
  102. }
  103. }
  104. protected internal virtual void LostFocus(object sender, RoutedEventArgs e)
  105. {
  106. var item = GetMenuItem(e.Source as IControl);
  107. if (item != null)
  108. {
  109. item.SelectedItem = null;
  110. }
  111. }
  112. protected internal virtual void KeyDown(object sender, KeyEventArgs e)
  113. {
  114. KeyDown(GetMenuItem(e.Source as IControl), e);
  115. }
  116. protected internal virtual void KeyDown(IMenuItem? item, KeyEventArgs e)
  117. {
  118. switch (e.Key)
  119. {
  120. case Key.Up:
  121. case Key.Down:
  122. {
  123. if (item?.IsTopLevel == true)
  124. {
  125. if (item.HasSubMenu && !item.IsSubMenuOpen)
  126. {
  127. Open(item, true);
  128. e.Handled = true;
  129. }
  130. }
  131. else
  132. {
  133. goto default;
  134. }
  135. break;
  136. }
  137. case Key.Left:
  138. {
  139. if (item?.Parent is IMenuItem parent && !parent.IsTopLevel && parent.IsSubMenuOpen)
  140. {
  141. parent.Close();
  142. parent.Focus();
  143. e.Handled = true;
  144. }
  145. else
  146. {
  147. goto default;
  148. }
  149. break;
  150. }
  151. case Key.Right:
  152. {
  153. if (item != null && !item.IsTopLevel && item.HasSubMenu)
  154. {
  155. Open(item, true);
  156. e.Handled = true;
  157. }
  158. else
  159. {
  160. goto default;
  161. }
  162. break;
  163. }
  164. case Key.Enter:
  165. {
  166. if (item != null)
  167. {
  168. if (!item.HasSubMenu)
  169. {
  170. Click(item);
  171. }
  172. else
  173. {
  174. Open(item, true);
  175. }
  176. e.Handled = true;
  177. }
  178. break;
  179. }
  180. case Key.Escape:
  181. {
  182. if (item?.Parent is IMenuElement parent)
  183. {
  184. parent.Close();
  185. parent.Focus();
  186. }
  187. else
  188. {
  189. Menu!.Close();
  190. }
  191. e.Handled = true;
  192. break;
  193. }
  194. default:
  195. {
  196. var direction = e.Key.ToNavigationDirection();
  197. if (direction?.IsDirectional() == true)
  198. {
  199. if (item == null && _isContextMenu)
  200. {
  201. if (Menu!.MoveSelection(direction.Value, true) == true)
  202. {
  203. e.Handled = true;
  204. }
  205. }
  206. else if (item?.Parent?.MoveSelection(direction.Value, true) == true)
  207. {
  208. // If the the parent is an IMenu which successfully moved its selection,
  209. // and the current menu is open then close the current menu and open the
  210. // new menu.
  211. if (item.IsSubMenuOpen &&
  212. item.Parent is IMenu &&
  213. item.Parent.SelectedItem is object)
  214. {
  215. item.Close();
  216. Open(item.Parent.SelectedItem, true);
  217. }
  218. e.Handled = true;
  219. }
  220. }
  221. break;
  222. }
  223. }
  224. if (!e.Handled && item?.Parent is IMenuItem parentItem)
  225. {
  226. KeyDown(parentItem, e);
  227. }
  228. }
  229. protected internal virtual void AccessKeyPressed(object sender, RoutedEventArgs e)
  230. {
  231. var item = GetMenuItem(e.Source as IControl);
  232. if (item == null)
  233. {
  234. return;
  235. }
  236. if (item.HasSubMenu)
  237. {
  238. Open(item, true);
  239. }
  240. else
  241. {
  242. Click(item);
  243. }
  244. e.Handled = true;
  245. }
  246. protected internal virtual void PointerEnter(object sender, PointerEventArgs e)
  247. {
  248. var item = GetMenuItem(e.Source as IControl);
  249. if (item?.Parent == null)
  250. {
  251. return;
  252. }
  253. if (item.IsTopLevel)
  254. {
  255. if (item != item.Parent.SelectedItem &&
  256. item.Parent.SelectedItem?.IsSubMenuOpen == true)
  257. {
  258. item.Parent.SelectedItem.Close();
  259. SelectItemAndAncestors(item);
  260. Open(item, false);
  261. }
  262. else
  263. {
  264. SelectItemAndAncestors(item);
  265. }
  266. }
  267. else
  268. {
  269. SelectItemAndAncestors(item);
  270. if (item.HasSubMenu)
  271. {
  272. OpenWithDelay(item);
  273. }
  274. else if (item.Parent != null)
  275. {
  276. foreach (var sibling in item.Parent.SubItems)
  277. {
  278. if (sibling.IsSubMenuOpen)
  279. {
  280. CloseWithDelay(sibling);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. protected internal virtual void PointerLeave(object sender, PointerEventArgs e)
  287. {
  288. var item = GetMenuItem(e.Source as IControl);
  289. if (item?.Parent == null)
  290. {
  291. return;
  292. }
  293. if (item.Parent.SelectedItem == item)
  294. {
  295. if (item.IsTopLevel)
  296. {
  297. if (!((IMenu)item.Parent).IsOpen)
  298. {
  299. item.Parent.SelectedItem = null;
  300. }
  301. }
  302. else if (!item.HasSubMenu)
  303. {
  304. item.Parent.SelectedItem = null;
  305. }
  306. else if (!item.IsPointerOverSubMenu)
  307. {
  308. item.IsSubMenuOpen = false;
  309. }
  310. }
  311. }
  312. protected internal virtual void PointerPressed(object sender, PointerPressedEventArgs e)
  313. {
  314. var item = GetMenuItem(e.Source as IControl);
  315. var visual = (IVisual)sender;
  316. if (e.GetCurrentPoint(visual).Properties.IsLeftButtonPressed && item?.HasSubMenu == true)
  317. {
  318. if (item.IsSubMenuOpen)
  319. {
  320. if (item.IsTopLevel)
  321. {
  322. CloseMenu(item);
  323. }
  324. }
  325. else
  326. {
  327. if (item.IsTopLevel && item.Parent is IMainMenu mainMenu)
  328. {
  329. mainMenu.Open();
  330. }
  331. Open(item, false);
  332. }
  333. e.Handled = true;
  334. }
  335. }
  336. protected internal virtual void PointerReleased(object sender, PointerReleasedEventArgs e)
  337. {
  338. var item = GetMenuItem(e.Source as IControl);
  339. if (e.InitialPressMouseButton == MouseButton.Left && item?.HasSubMenu == false)
  340. {
  341. Click(item);
  342. e.Handled = true;
  343. }
  344. }
  345. protected internal virtual void MenuOpened(object sender, RoutedEventArgs e)
  346. {
  347. if (e.Source == Menu)
  348. {
  349. Menu?.MoveSelection(NavigationDirection.First, true);
  350. }
  351. }
  352. protected internal virtual void RawInput(RawInputEventArgs e)
  353. {
  354. var mouse = e as RawPointerEventArgs;
  355. if (mouse?.Type == RawPointerEventType.NonClientLeftButtonDown)
  356. {
  357. Menu?.Close();
  358. }
  359. }
  360. protected internal virtual void RootPointerPressed(object sender, PointerPressedEventArgs e)
  361. {
  362. if (Menu?.IsOpen == true)
  363. {
  364. var control = e.Source as ILogical;
  365. if (!Menu.IsLogicalAncestorOf(control))
  366. {
  367. Menu.Close();
  368. }
  369. }
  370. }
  371. protected internal virtual void WindowDeactivated(object sender, EventArgs e)
  372. {
  373. Menu?.Close();
  374. }
  375. private void TopLevelLostPlatformFocus()
  376. {
  377. Menu?.Close();
  378. }
  379. protected void Click(IMenuItem item)
  380. {
  381. item.RaiseClick();
  382. CloseMenu(item);
  383. }
  384. protected void CloseMenu(IMenuItem item)
  385. {
  386. var current = (IMenuElement?)item;
  387. while (current != null && !(current is IMenu))
  388. {
  389. current = (current as IMenuItem)?.Parent;
  390. }
  391. current?.Close();
  392. }
  393. protected void CloseWithDelay(IMenuItem item)
  394. {
  395. void Execute()
  396. {
  397. if (item.Parent?.SelectedItem != item)
  398. {
  399. item.Close();
  400. }
  401. }
  402. DelayRun(Execute, MenuShowDelay);
  403. }
  404. protected void Open(IMenuItem item, bool selectFirst)
  405. {
  406. item.Open();
  407. if (selectFirst)
  408. {
  409. item.MoveSelection(NavigationDirection.First, true);
  410. }
  411. }
  412. protected void OpenWithDelay(IMenuItem item)
  413. {
  414. void Execute()
  415. {
  416. if (item.Parent?.SelectedItem == item)
  417. {
  418. Open(item, false);
  419. }
  420. }
  421. DelayRun(Execute, MenuShowDelay);
  422. }
  423. protected void SelectItemAndAncestors(IMenuItem item)
  424. {
  425. var current = (IMenuItem?)item;
  426. while (current?.Parent != null)
  427. {
  428. current.Parent.SelectedItem = current;
  429. current = current.Parent as IMenuItem;
  430. }
  431. }
  432. protected static IMenuItem? GetMenuItem(IControl? item)
  433. {
  434. while (true)
  435. {
  436. if (item == null)
  437. return null;
  438. if (item is IMenuItem menuItem)
  439. return menuItem;
  440. item = item.Parent;
  441. }
  442. }
  443. private static void DefaultDelayRun(Action action, TimeSpan timeSpan)
  444. {
  445. DispatcherTimer.RunOnce(action, timeSpan);
  446. }
  447. }
  448. }