InputElement.cs 26 KB

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