InputElement.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Metadata;
  6. using Avalonia.Input.GestureRecognizers;
  7. using Avalonia.Input.TextInput;
  8. using Avalonia.Interactivity;
  9. using Avalonia.Reactive;
  10. using Avalonia.VisualTree;
  11. namespace Avalonia.Input
  12. {
  13. /// <summary>
  14. /// Implements input-related functionality for a control.
  15. /// </summary>
  16. [PseudoClasses(":disabled", ":focus", ":focus-visible", ":focus-within", ":pointerover")]
  17. public class InputElement : Interactive, IInputElement
  18. {
  19. /// <summary>
  20. /// Defines the <see cref="Focusable"/> property.
  21. /// </summary>
  22. public static readonly StyledProperty<bool> FocusableProperty =
  23. AvaloniaProperty.Register<InputElement, bool>(nameof(Focusable));
  24. /// <summary>
  25. /// Defines the <see cref="IsEnabled"/> property.
  26. /// </summary>
  27. public static readonly StyledProperty<bool> IsEnabledProperty =
  28. AvaloniaProperty.Register<InputElement, bool>(nameof(IsEnabled), true);
  29. /// <summary>
  30. /// Defines the <see cref="IsEffectivelyEnabled"/> property.
  31. /// </summary>
  32. public static readonly DirectProperty<InputElement, bool> IsEffectivelyEnabledProperty =
  33. AvaloniaProperty.RegisterDirect<InputElement, bool>(
  34. nameof(IsEffectivelyEnabled),
  35. o => o.IsEffectivelyEnabled);
  36. /// <summary>
  37. /// Gets or sets associated mouse cursor.
  38. /// </summary>
  39. public static readonly StyledProperty<Cursor?> CursorProperty =
  40. AvaloniaProperty.Register<InputElement, Cursor?>(nameof(Cursor), null, true);
  41. /// <summary>
  42. /// Defines the <see cref="IsKeyboardFocusWithin"/> property.
  43. /// </summary>
  44. public static readonly DirectProperty<InputElement, bool> IsKeyboardFocusWithinProperty =
  45. AvaloniaProperty.RegisterDirect<InputElement, bool>(
  46. nameof(IsKeyboardFocusWithin),
  47. o => o.IsKeyboardFocusWithin);
  48. /// <summary>
  49. /// Defines the <see cref="IsFocused"/> property.
  50. /// </summary>
  51. public static readonly DirectProperty<InputElement, bool> IsFocusedProperty =
  52. AvaloniaProperty.RegisterDirect<InputElement, bool>(nameof(IsFocused), o => o.IsFocused);
  53. /// <summary>
  54. /// Defines the <see cref="IsHitTestVisible"/> property.
  55. /// </summary>
  56. public static readonly StyledProperty<bool> IsHitTestVisibleProperty =
  57. AvaloniaProperty.Register<InputElement, bool>(nameof(IsHitTestVisible), true);
  58. /// <summary>
  59. /// Defines the <see cref="IsPointerOver"/> property.
  60. /// </summary>
  61. public static readonly DirectProperty<InputElement, bool> IsPointerOverProperty =
  62. AvaloniaProperty.RegisterDirect<InputElement, bool>(nameof(IsPointerOver), o => o.IsPointerOver);
  63. /// <summary>
  64. /// Defines the <see cref="IsTabStop"/> property.
  65. /// </summary>
  66. public static readonly StyledProperty<bool> IsTabStopProperty =
  67. KeyboardNavigation.IsTabStopProperty.AddOwner<InputElement>();
  68. /// <summary>
  69. /// Defines the <see cref="GotFocus"/> event.
  70. /// </summary>
  71. public static readonly RoutedEvent<GotFocusEventArgs> GotFocusEvent =
  72. RoutedEvent.Register<InputElement, GotFocusEventArgs>(nameof(GotFocus), RoutingStrategies.Bubble);
  73. /// <summary>
  74. /// Defines the <see cref="LostFocus"/> event.
  75. /// </summary>
  76. public static readonly RoutedEvent<RoutedEventArgs> LostFocusEvent =
  77. RoutedEvent.Register<InputElement, RoutedEventArgs>(nameof(LostFocus), RoutingStrategies.Bubble);
  78. /// <summary>
  79. /// Defines the <see cref="KeyDown"/> event.
  80. /// </summary>
  81. public static readonly RoutedEvent<KeyEventArgs> KeyDownEvent =
  82. RoutedEvent.Register<InputElement, KeyEventArgs>(
  83. nameof(KeyDown),
  84. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  85. /// <summary>
  86. /// Defines the <see cref="KeyUp"/> event.
  87. /// </summary>
  88. public static readonly RoutedEvent<KeyEventArgs> KeyUpEvent =
  89. RoutedEvent.Register<InputElement, KeyEventArgs>(
  90. nameof(KeyUp),
  91. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  92. /// <summary>
  93. /// Defines the <see cref="TabIndex"/> property.
  94. /// </summary>
  95. public static readonly StyledProperty<int> TabIndexProperty =
  96. KeyboardNavigation.TabIndexProperty.AddOwner<InputElement>();
  97. /// <summary>
  98. /// Defines the <see cref="TextInput"/> event.
  99. /// </summary>
  100. public static readonly RoutedEvent<TextInputEventArgs> TextInputEvent =
  101. RoutedEvent.Register<InputElement, TextInputEventArgs>(
  102. nameof(TextInput),
  103. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  104. /// <summary>
  105. /// Defines the <see cref="TextInputMethodClientRequested"/> event.
  106. /// </summary>
  107. public static readonly RoutedEvent<TextInputMethodClientRequestedEventArgs> TextInputMethodClientRequestedEvent =
  108. RoutedEvent.Register<InputElement, TextInputMethodClientRequestedEventArgs>(
  109. nameof(TextInputMethodClientRequested),
  110. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  111. /// <summary>
  112. /// Defines the <see cref="PointerEntered"/> event.
  113. /// </summary>
  114. public static readonly RoutedEvent<PointerEventArgs> PointerEnteredEvent =
  115. RoutedEvent.Register<InputElement, PointerEventArgs>(
  116. nameof(PointerEntered),
  117. RoutingStrategies.Direct);
  118. /// <summary>
  119. /// Defines the <see cref="PointerExited"/> event.
  120. /// </summary>
  121. public static readonly RoutedEvent<PointerEventArgs> PointerExitedEvent =
  122. RoutedEvent.Register<InputElement, PointerEventArgs>(
  123. nameof(PointerExited),
  124. RoutingStrategies.Direct);
  125. /// <summary>
  126. /// Defines the <see cref="PointerMoved"/> event.
  127. /// </summary>
  128. public static readonly RoutedEvent<PointerEventArgs> PointerMovedEvent =
  129. RoutedEvent.Register<InputElement, PointerEventArgs>(
  130. nameof(PointerMoved),
  131. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  132. /// <summary>
  133. /// Defines the <see cref="PointerPressed"/> event.
  134. /// </summary>
  135. public static readonly RoutedEvent<PointerPressedEventArgs> PointerPressedEvent =
  136. RoutedEvent.Register<InputElement, PointerPressedEventArgs>(
  137. nameof(PointerPressed),
  138. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  139. /// <summary>
  140. /// Defines the <see cref="PointerReleased"/> event.
  141. /// </summary>
  142. public static readonly RoutedEvent<PointerReleasedEventArgs> PointerReleasedEvent =
  143. RoutedEvent.Register<InputElement, PointerReleasedEventArgs>(
  144. nameof(PointerReleased),
  145. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  146. /// <summary>
  147. /// Defines the <see cref="PointerCaptureLost"/> routed event.
  148. /// </summary>
  149. public static readonly RoutedEvent<PointerCaptureLostEventArgs> PointerCaptureLostEvent =
  150. RoutedEvent.Register<InputElement, PointerCaptureLostEventArgs>(
  151. nameof(PointerCaptureLost),
  152. RoutingStrategies.Direct);
  153. /// <summary>
  154. /// Defines the <see cref="PointerWheelChanged"/> event.
  155. /// </summary>
  156. public static readonly RoutedEvent<PointerWheelEventArgs> PointerWheelChangedEvent =
  157. RoutedEvent.Register<InputElement, PointerWheelEventArgs>(
  158. nameof(PointerWheelChanged),
  159. RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
  160. /// <summary>
  161. /// Defines the <see cref="Tapped"/> event.
  162. /// </summary>
  163. public static readonly RoutedEvent<TappedEventArgs> TappedEvent = Gestures.TappedEvent;
  164. /// <summary>
  165. /// Defines the <see cref="Holding"/> event.
  166. /// </summary>
  167. public static readonly RoutedEvent<HoldingRoutedEventArgs> HoldingEvent = Gestures.HoldingEvent;
  168. /// <summary>
  169. /// Defines the <see cref="DoubleTapped"/> event.
  170. /// </summary>
  171. public static readonly RoutedEvent<TappedEventArgs> DoubleTappedEvent = Gestures.DoubleTappedEvent;
  172. private bool _isEffectivelyEnabled = true;
  173. private bool _isFocused;
  174. private bool _isKeyboardFocusWithin;
  175. private bool _isFocusVisible;
  176. private bool _isPointerOver;
  177. private GestureRecognizerCollection? _gestureRecognizers;
  178. /// <summary>
  179. /// Initializes static members of the <see cref="InputElement"/> class.
  180. /// </summary>
  181. static InputElement()
  182. {
  183. IsEnabledProperty.Changed.Subscribe(IsEnabledChanged);
  184. GotFocusEvent.AddClassHandler<InputElement>((x, e) => x.OnGotFocusCore(e));
  185. LostFocusEvent.AddClassHandler<InputElement>((x, e) => x.OnLostFocusCore(e));
  186. KeyDownEvent.AddClassHandler<InputElement>((x, e) => x.OnKeyDown(e));
  187. KeyUpEvent.AddClassHandler<InputElement>((x, e) => x.OnKeyUp(e));
  188. TextInputEvent.AddClassHandler<InputElement>((x, e) => x.OnTextInput(e));
  189. PointerEnteredEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerEnteredCore(e));
  190. PointerExitedEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerExitedCore(e));
  191. PointerMovedEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerMoved(e));
  192. PointerPressedEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerPressed(e));
  193. PointerReleasedEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerReleased(e));
  194. PointerCaptureLostEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerCaptureLost(e));
  195. PointerWheelChangedEvent.AddClassHandler<InputElement>((x, e) => x.OnPointerWheelChanged(e));
  196. // Gesture only handlers
  197. PointerMovedEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerMoved(e), handledEventsToo: true);
  198. PointerPressedEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerPressed(e), handledEventsToo: true);
  199. PointerReleasedEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerReleased(e), handledEventsToo: true);
  200. PointerCaptureLostEvent.AddClassHandler<InputElement>((x, e) => x.OnGesturePointerCaptureLost(e), handledEventsToo: true);
  201. // Access Key Handling
  202. AccessKeyHandler.AccessKeyEvent.AddClassHandler<InputElement>((x, e) => x.OnAccessKey(e));
  203. }
  204. public InputElement()
  205. {
  206. UpdatePseudoClasses(IsFocused, IsPointerOver);
  207. }
  208. /// <summary>
  209. /// Occurs when the control receives focus.
  210. /// </summary>
  211. public event EventHandler<GotFocusEventArgs>? GotFocus
  212. {
  213. add { AddHandler(GotFocusEvent, value); }
  214. remove { RemoveHandler(GotFocusEvent, value); }
  215. }
  216. /// <summary>
  217. /// Occurs when the control loses focus.
  218. /// </summary>
  219. public event EventHandler<RoutedEventArgs>? LostFocus
  220. {
  221. add { AddHandler(LostFocusEvent, value); }
  222. remove { RemoveHandler(LostFocusEvent, value); }
  223. }
  224. /// <summary>
  225. /// Occurs when a key is pressed while the control has focus.
  226. /// </summary>
  227. public event EventHandler<KeyEventArgs>? KeyDown
  228. {
  229. add { AddHandler(KeyDownEvent, value); }
  230. remove { RemoveHandler(KeyDownEvent, value); }
  231. }
  232. /// <summary>
  233. /// Occurs when a key is released while the control has focus.
  234. /// </summary>
  235. public event EventHandler<KeyEventArgs>? KeyUp
  236. {
  237. add { AddHandler(KeyUpEvent, value); }
  238. remove { RemoveHandler(KeyUpEvent, value); }
  239. }
  240. /// <summary>
  241. /// Occurs when a user typed some text while the control has focus.
  242. /// </summary>
  243. public event EventHandler<TextInputEventArgs>? TextInput
  244. {
  245. add { AddHandler(TextInputEvent, value); }
  246. remove { RemoveHandler(TextInputEvent, value); }
  247. }
  248. /// <summary>
  249. /// Occurs when an input element gains input focus and input method is looking for the corresponding client
  250. /// </summary>
  251. public event EventHandler<TextInputMethodClientRequestedEventArgs>? TextInputMethodClientRequested
  252. {
  253. add { AddHandler(TextInputMethodClientRequestedEvent, value); }
  254. remove { RemoveHandler(TextInputMethodClientRequestedEvent, value); }
  255. }
  256. /// <summary>
  257. /// Occurs when the pointer enters the control.
  258. /// </summary>
  259. public event EventHandler<PointerEventArgs>? PointerEntered
  260. {
  261. add { AddHandler(PointerEnteredEvent, value); }
  262. remove { RemoveHandler(PointerEnteredEvent, value); }
  263. }
  264. /// <summary>
  265. /// Occurs when the pointer leaves the control.
  266. /// </summary>
  267. public event EventHandler<PointerEventArgs>? PointerExited
  268. {
  269. add { AddHandler(PointerExitedEvent, value); }
  270. remove { RemoveHandler(PointerExitedEvent, value); }
  271. }
  272. /// <summary>
  273. /// Occurs when the pointer moves over the control.
  274. /// </summary>
  275. public event EventHandler<PointerEventArgs>? PointerMoved
  276. {
  277. add { AddHandler(PointerMovedEvent, value); }
  278. remove { RemoveHandler(PointerMovedEvent, value); }
  279. }
  280. /// <summary>
  281. /// Occurs when the pointer is pressed over the control.
  282. /// </summary>
  283. public event EventHandler<PointerPressedEventArgs>? PointerPressed
  284. {
  285. add { AddHandler(PointerPressedEvent, value); }
  286. remove { RemoveHandler(PointerPressedEvent, value); }
  287. }
  288. /// <summary>
  289. /// Occurs when the pointer is released over the control.
  290. /// </summary>
  291. public event EventHandler<PointerReleasedEventArgs>? PointerReleased
  292. {
  293. add { AddHandler(PointerReleasedEvent, value); }
  294. remove { RemoveHandler(PointerReleasedEvent, value); }
  295. }
  296. /// <summary>
  297. /// Occurs when the control or its child control loses the pointer capture for any reason,
  298. /// event will not be triggered for a parent control if capture was transferred to another child of that parent control
  299. /// </summary>
  300. public event EventHandler<PointerCaptureLostEventArgs>? PointerCaptureLost
  301. {
  302. add => AddHandler(PointerCaptureLostEvent, value);
  303. remove => RemoveHandler(PointerCaptureLostEvent, value);
  304. }
  305. /// <summary>
  306. /// Occurs when the mouse is scrolled over the control.
  307. /// </summary>
  308. public event EventHandler<PointerWheelEventArgs>? PointerWheelChanged
  309. {
  310. add { AddHandler(PointerWheelChangedEvent, value); }
  311. remove { RemoveHandler(PointerWheelChangedEvent, value); }
  312. }
  313. /// <summary>
  314. /// Occurs when a tap gesture occurs on the control.
  315. /// </summary>
  316. public event EventHandler<TappedEventArgs>? Tapped
  317. {
  318. add { AddHandler(TappedEvent, value); }
  319. remove { RemoveHandler(TappedEvent, value); }
  320. }
  321. /// <summary>
  322. /// Occurs when a hold gesture occurs on the control.
  323. /// </summary>
  324. public event EventHandler<HoldingRoutedEventArgs>? Holding
  325. {
  326. add { AddHandler(HoldingEvent, value); }
  327. remove { RemoveHandler(HoldingEvent, value); }
  328. }
  329. /// <summary>
  330. /// Occurs when a double-tap gesture occurs on the control.
  331. /// </summary>
  332. public event EventHandler<TappedEventArgs>? DoubleTapped
  333. {
  334. add { AddHandler(DoubleTappedEvent, value); }
  335. remove { RemoveHandler(DoubleTappedEvent, value); }
  336. }
  337. /// <summary>
  338. /// Gets or sets a value indicating whether the control can receive focus.
  339. /// </summary>
  340. public bool Focusable
  341. {
  342. get { return GetValue(FocusableProperty); }
  343. set { SetValue(FocusableProperty, value); }
  344. }
  345. /// <summary>
  346. /// Gets or sets a value indicating whether the control is enabled for user interaction.
  347. /// </summary>
  348. public bool IsEnabled
  349. {
  350. get { return GetValue(IsEnabledProperty); }
  351. set { SetValue(IsEnabledProperty, value); }
  352. }
  353. /// <summary>
  354. /// Gets or sets associated mouse cursor.
  355. /// </summary>
  356. public Cursor? Cursor
  357. {
  358. get { return GetValue(CursorProperty); }
  359. set { SetValue(CursorProperty, value); }
  360. }
  361. /// <summary>
  362. /// Gets a value indicating whether keyboard focus is anywhere within the element or its visual tree child elements.
  363. /// </summary>
  364. public bool IsKeyboardFocusWithin
  365. {
  366. get => _isKeyboardFocusWithin;
  367. internal set => SetAndRaise(IsKeyboardFocusWithinProperty, ref _isKeyboardFocusWithin, value);
  368. }
  369. /// <summary>
  370. /// Gets a value indicating whether the control is focused.
  371. /// </summary>
  372. public bool IsFocused
  373. {
  374. get { return _isFocused; }
  375. private set { SetAndRaise(IsFocusedProperty, ref _isFocused, value); }
  376. }
  377. /// <summary>
  378. /// Gets or sets a value indicating whether the control is considered for hit testing.
  379. /// </summary>
  380. public bool IsHitTestVisible
  381. {
  382. get { return GetValue(IsHitTestVisibleProperty); }
  383. set { SetValue(IsHitTestVisibleProperty, value); }
  384. }
  385. /// <summary>
  386. /// Gets a value indicating whether the pointer is currently over the control.
  387. /// </summary>
  388. public bool IsPointerOver
  389. {
  390. get { return _isPointerOver; }
  391. internal set { SetAndRaise(IsPointerOverProperty, ref _isPointerOver, value); }
  392. }
  393. /// <summary>
  394. /// Gets or sets a value that indicates whether the control is included in tab navigation.
  395. /// </summary>
  396. public bool IsTabStop
  397. {
  398. get => GetValue(IsTabStopProperty);
  399. set => SetValue(IsTabStopProperty, value);
  400. }
  401. /// <inheritdoc/>
  402. public bool IsEffectivelyEnabled
  403. {
  404. get => _isEffectivelyEnabled;
  405. private set
  406. {
  407. SetAndRaise(IsEffectivelyEnabledProperty, ref _isEffectivelyEnabled, value);
  408. PseudoClasses.Set(":disabled", !value);
  409. if (!IsEffectivelyEnabled && FocusManager.GetFocusManager(this) is {} focusManager
  410. && Equals(focusManager.GetFocusedElement(), this))
  411. {
  412. focusManager.ClearFocus();
  413. }
  414. }
  415. }
  416. /// <summary>
  417. /// Gets or sets a value that determines the order in which elements receive focus when the
  418. /// user navigates through controls by pressing the Tab key.
  419. /// </summary>
  420. public int TabIndex
  421. {
  422. get => GetValue(TabIndexProperty);
  423. set => SetValue(TabIndexProperty, value);
  424. }
  425. public List<KeyBinding> KeyBindings { get; } = new List<KeyBinding>();
  426. /// <summary>
  427. /// Allows a derived class to override the enabled state of the control.
  428. /// </summary>
  429. /// <remarks>
  430. /// Derived controls may wish to disable the enabled state of the control without overwriting the
  431. /// user-supplied <see cref="IsEnabled"/> setting. This can be done by overriding this property
  432. /// to return the overridden enabled state. If the value returned from <see cref="IsEnabledCore"/>
  433. /// should change, then the derived control should call <see cref="UpdateIsEffectivelyEnabled()"/>.
  434. /// </remarks>
  435. protected virtual bool IsEnabledCore => IsEnabled;
  436. public GestureRecognizerCollection GestureRecognizers
  437. => _gestureRecognizers ?? (_gestureRecognizers = new GestureRecognizerCollection(this));
  438. /// <inheritdoc />
  439. public bool Focus(NavigationMethod method = NavigationMethod.Unspecified, KeyModifiers keyModifiers = KeyModifiers.None)
  440. {
  441. return FocusManager.GetFocusManager(this)?.Focus(this, method, keyModifiers) ?? false;
  442. }
  443. /// <inheritdoc/>
  444. protected override void OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
  445. {
  446. base.OnDetachedFromVisualTreeCore(e);
  447. if (IsFocused)
  448. {
  449. FocusManager.GetFocusManager(this)?.ClearFocusOnElementRemoved(this, e.Parent);
  450. }
  451. }
  452. /// <summary>
  453. /// This method is used to execute the action on an effective IInputElement when a corresponding access key has been invoked.
  454. /// By default, the Focus() method is invoked with the NavigationMethod.Tab to indicate a visual focus adorner.
  455. /// Overwrite this method if other methods or additional functionality is needed when an item should receive the focus.
  456. /// </summary>
  457. /// <param name="e">AccessKeyEventArgs are passed on to indicate if there are multiple matches or not.</param>
  458. protected virtual void OnAccessKey(RoutedEventArgs e)
  459. {
  460. Focus(NavigationMethod.Tab);
  461. }
  462. /// <inheritdoc/>
  463. protected override void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e)
  464. {
  465. base.OnAttachedToVisualTreeCore(e);
  466. UpdateIsEffectivelyEnabled();
  467. }
  468. private void OnGotFocusCore(GotFocusEventArgs e)
  469. {
  470. var isFocused = e.Source == this;
  471. _isFocusVisible = isFocused && (e.NavigationMethod == NavigationMethod.Directional || e.NavigationMethod == NavigationMethod.Tab);
  472. IsFocused = isFocused;
  473. OnGotFocus(e);
  474. }
  475. /// <summary>
  476. /// Invoked when an unhandled <see cref="GotFocusEvent"/> reaches an element in its
  477. /// route that is derived from this class. Implement this method to add class handling
  478. /// for this event.
  479. /// </summary>
  480. /// <param name="e">Data about the event.</param>
  481. protected virtual void OnGotFocus(GotFocusEventArgs e)
  482. {
  483. }
  484. private void OnLostFocusCore(RoutedEventArgs e)
  485. {
  486. _isFocusVisible = false;
  487. IsFocused = false;
  488. OnLostFocus(e);
  489. }
  490. /// <summary>
  491. /// Invoked when an unhandled <see cref="LostFocusEvent"/> reaches an element in its
  492. /// route that is derived from this class. Implement this method to add class handling
  493. /// for this event.
  494. /// </summary>
  495. /// <param name="e">Data about the event.</param>
  496. protected virtual void OnLostFocus(RoutedEventArgs e)
  497. {
  498. }
  499. /// <summary>
  500. /// Invoked when an unhandled <see cref="KeyDownEvent"/> reaches an element in its
  501. /// route that is derived from this class. Implement this method to add class handling
  502. /// for this event.
  503. /// </summary>
  504. /// <param name="e">Data about the event.</param>
  505. protected virtual void OnKeyDown(KeyEventArgs e)
  506. {
  507. }
  508. /// <summary>
  509. /// Invoked when an unhandled <see cref="KeyUpEvent"/> reaches an element in its
  510. /// route that is derived from this class. Implement this method to add class handling
  511. /// for this event.
  512. /// </summary>
  513. /// <param name="e">Data about the event.</param>
  514. protected virtual void OnKeyUp(KeyEventArgs e)
  515. {
  516. }
  517. /// <summary>
  518. /// Invoked when an unhandled <see cref="TextInputEvent"/> reaches an element in its
  519. /// route that is derived from this class. Implement this method to add class handling
  520. /// for this event.
  521. /// </summary>
  522. /// <param name="e">Data about the event.</param>
  523. protected virtual void OnTextInput(TextInputEventArgs e)
  524. {
  525. }
  526. /// <summary>
  527. /// Invoked when an unhandled <see cref="PointerEnteredEvent"/> reaches an element in its
  528. /// route that is derived from this class. Implement this method to add class handling
  529. /// for this event.
  530. /// </summary>
  531. /// <param name="e">Data about the event.</param>
  532. protected virtual void OnPointerEntered(PointerEventArgs e)
  533. {
  534. }
  535. /// <summary>
  536. /// Invoked when an unhandled <see cref="PointerExitedEvent"/> reaches an element in its
  537. /// route that is derived from this class. Implement this method to add class handling
  538. /// for this event.
  539. /// </summary>
  540. /// <param name="e">Data about the event.</param>
  541. protected virtual void OnPointerExited(PointerEventArgs e)
  542. {
  543. }
  544. /// <summary>
  545. /// Invoked when an unhandled <see cref="PointerMovedEvent"/> reaches an element in its
  546. /// route that is derived from this class. Implement this method to add class handling
  547. /// for this event.
  548. /// </summary>
  549. /// <param name="e">Data about the event.</param>
  550. protected virtual void OnPointerMoved(PointerEventArgs e)
  551. {
  552. }
  553. /// <summary>
  554. /// Invoked when an unhandled <see cref="PointerPressedEvent"/> reaches an element in its
  555. /// route that is derived from this class. Implement this method to add class handling
  556. /// for this event.
  557. /// </summary>
  558. /// <param name="e">Data about the event.</param>
  559. protected virtual void OnPointerPressed(PointerPressedEventArgs e)
  560. {
  561. }
  562. /// <summary>
  563. /// Invoked when an unhandled <see cref="PointerReleasedEvent"/> reaches an element in its
  564. /// route that is derived from this class. Implement this method to add class handling
  565. /// for this event.
  566. /// </summary>
  567. /// <param name="e">Data about the event.</param>
  568. protected virtual void OnPointerReleased(PointerReleasedEventArgs e)
  569. {
  570. }
  571. private void OnGesturePointerReleased(PointerReleasedEventArgs e)
  572. {
  573. if (!e.IsGestureRecognitionSkipped)
  574. if (_gestureRecognizers?.HandlePointerReleased(e) == true)
  575. {
  576. e.Handled = true;
  577. }
  578. }
  579. private void OnGesturePointerCaptureLost(PointerCaptureLostEventArgs e)
  580. {
  581. _gestureRecognizers?.HandleCaptureLost(e.Pointer);
  582. }
  583. private void OnGesturePointerPressed(PointerPressedEventArgs e)
  584. {
  585. if (!e.IsGestureRecognitionSkipped)
  586. if (_gestureRecognizers?.HandlePointerPressed(e) == true)
  587. {
  588. e.Handled = true;
  589. }
  590. }
  591. private void OnGesturePointerMoved(PointerEventArgs e)
  592. {
  593. if (!e.IsGestureRecognitionSkipped)
  594. if (_gestureRecognizers?.HandlePointerMoved(e) == true)
  595. {
  596. e.Handled = true;
  597. }
  598. }
  599. /// <summary>
  600. /// Invoked when an unhandled <see cref="PointerCaptureLostEvent"/> reaches an element in its
  601. /// route that is derived from this class. Implement this method to add class handling
  602. /// for this event.
  603. /// </summary>
  604. /// <param name="e">Data about the event.</param>
  605. protected virtual void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
  606. {
  607. }
  608. /// <summary>
  609. /// Invoked when an unhandled <see cref="PointerWheelChangedEvent"/> reaches an element in its
  610. /// route that is derived from this class. Implement this method to add class handling
  611. /// for this event.
  612. /// </summary>
  613. /// <param name="e">Data about the event.</param>
  614. protected virtual void OnPointerWheelChanged(PointerWheelEventArgs e)
  615. {
  616. }
  617. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  618. {
  619. base.OnPropertyChanged(change);
  620. if (change.Property == IsFocusedProperty)
  621. {
  622. UpdatePseudoClasses(change.GetNewValue<bool>(), null);
  623. }
  624. else if (change.Property == IsPointerOverProperty)
  625. {
  626. UpdatePseudoClasses(null, change.GetNewValue<bool>());
  627. }
  628. else if (change.Property == IsKeyboardFocusWithinProperty)
  629. {
  630. PseudoClasses.Set(":focus-within", change.GetNewValue<bool>());
  631. }
  632. else if (change.Property == IsVisibleProperty && !change.GetNewValue<bool>() && IsFocused)
  633. {
  634. FocusManager.GetFocusManager(this)?.ClearFocus();
  635. }
  636. }
  637. /// <summary>
  638. /// Updates the <see cref="IsEffectivelyEnabled"/> property value according to the parent
  639. /// control's enabled state and <see cref="IsEnabledCore"/>.
  640. /// </summary>
  641. protected void UpdateIsEffectivelyEnabled()
  642. {
  643. UpdateIsEffectivelyEnabled(this.GetVisualParent<InputElement>());
  644. }
  645. private static void IsEnabledChanged(AvaloniaPropertyChangedEventArgs e)
  646. {
  647. ((InputElement)e.Sender).UpdateIsEffectivelyEnabled();
  648. }
  649. /// <summary>
  650. /// Called before the <see cref="PointerEntered"/> event occurs.
  651. /// </summary>
  652. /// <param name="e">The event args.</param>
  653. private void OnPointerEnteredCore(PointerEventArgs e)
  654. {
  655. IsPointerOver = true;
  656. OnPointerEntered(e);
  657. }
  658. /// <summary>
  659. /// Called before the <see cref="PointerExited"/> event occurs.
  660. /// </summary>
  661. /// <param name="e">The event args.</param>
  662. private void OnPointerExitedCore(PointerEventArgs e)
  663. {
  664. IsPointerOver = false;
  665. OnPointerExited(e);
  666. }
  667. /// <summary>
  668. /// Updates the <see cref="IsEffectivelyEnabled"/> property based on the parent's
  669. /// <see cref="IsEffectivelyEnabled"/>.
  670. /// </summary>
  671. /// <param name="parent">The parent control.</param>
  672. private void UpdateIsEffectivelyEnabled(InputElement? parent)
  673. {
  674. IsEffectivelyEnabled = IsEnabledCore && (parent?.IsEffectivelyEnabled ?? true);
  675. // PERF-SENSITIVE: This is called on entire hierarchy and using foreach or LINQ
  676. // will cause extra allocations and overhead.
  677. var children = VisualChildren;
  678. // ReSharper disable once ForCanBeConvertedToForeach
  679. for (int i = 0; i < children.Count; ++i)
  680. {
  681. var child = children[i] as InputElement;
  682. child?.UpdateIsEffectivelyEnabled(this);
  683. }
  684. }
  685. private void UpdatePseudoClasses(bool? isFocused, bool? isPointerOver)
  686. {
  687. if (isFocused.HasValue)
  688. {
  689. PseudoClasses.Set(":focus", isFocused.Value);
  690. PseudoClasses.Set(":focus-visible", _isFocusVisible);
  691. }
  692. if (isPointerOver.HasValue)
  693. {
  694. PseudoClasses.Set(":pointerover", isPointerOver.Value);
  695. }
  696. }
  697. }
  698. }