NavigationDirection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. namespace Avalonia.Input
  4. {
  5. /// <summary>
  6. /// Describes how focus should be moved by directional or tab keys.
  7. /// </summary>
  8. public enum NavigationDirection
  9. {
  10. /// <summary>
  11. /// Move the focus to the next control in the tab order.
  12. /// </summary>
  13. Next,
  14. /// <summary>
  15. /// Move the focus to the previous control in the tab order.
  16. /// </summary>
  17. Previous,
  18. /// <summary>
  19. /// Move the focus to the first control in the tab order.
  20. /// </summary>
  21. First,
  22. /// <summary>
  23. /// Move the focus to the last control in the tab order.
  24. /// </summary>
  25. Last,
  26. /// <summary>
  27. /// Move the focus to the left.
  28. /// </summary>
  29. Left,
  30. /// <summary>
  31. /// Move the focus to the right.
  32. /// </summary>
  33. Right,
  34. /// <summary>
  35. /// Move the focus up.
  36. /// </summary>
  37. Up,
  38. /// <summary>
  39. /// Move the focus down.
  40. /// </summary>
  41. Down,
  42. /// <summary>
  43. /// Move the focus up a page.
  44. /// </summary>
  45. PageUp,
  46. /// <summary>
  47. /// Move the focus down a page.
  48. /// </summary>
  49. PageDown,
  50. }
  51. public static class NavigationDirectionExtensions
  52. {
  53. /// <summary>
  54. /// Checks whether a <see cref="NavigationDirection"/> represents a tab movement.
  55. /// </summary>
  56. /// <param name="direction">The direction.</param>
  57. /// <returns>
  58. /// True if the direction represents a tab movement (<see cref="NavigationDirection.Next"/>
  59. /// or <see cref="NavigationDirection.Previous"/>); otherwise false.
  60. /// </returns>
  61. public static bool IsTab(this NavigationDirection direction)
  62. {
  63. return direction == NavigationDirection.Next ||
  64. direction == NavigationDirection.Previous;
  65. }
  66. /// <summary>
  67. /// Checks whether a <see cref="NavigationDirection"/> represents a directional movement.
  68. /// </summary>
  69. /// <param name="direction">The direction.</param>
  70. /// <returns>
  71. /// True if the direction represents a directional movement (any value except
  72. /// <see cref="NavigationDirection.Next"/> and <see cref="NavigationDirection.Previous"/>);
  73. /// otherwise false.
  74. /// </returns>
  75. public static bool IsDirectional(this NavigationDirection direction)
  76. {
  77. return direction > NavigationDirection.Previous ||
  78. direction <= NavigationDirection.PageDown;
  79. }
  80. /// <summary>
  81. /// Converts a keypress into a <see cref="NavigationDirection"/>.
  82. /// </summary>
  83. /// <param name="key">The key.</param>
  84. /// <param name="modifiers">The keyboard modifiers.</param>
  85. /// <returns>
  86. /// A <see cref="NavigationDirection"/> if the keypress represents a navigation keypress.
  87. /// </returns>
  88. public static NavigationDirection? ToNavigationDirection(
  89. this Key key,
  90. KeyModifiers modifiers = KeyModifiers.None)
  91. {
  92. switch (key)
  93. {
  94. case Key.Tab:
  95. return (modifiers & KeyModifiers.Shift) != 0 ?
  96. NavigationDirection.Next : NavigationDirection.Previous;
  97. case Key.Up:
  98. return NavigationDirection.Up;
  99. case Key.Down:
  100. return NavigationDirection.Down;
  101. case Key.Left:
  102. return NavigationDirection.Left;
  103. case Key.Right:
  104. return NavigationDirection.Right;
  105. case Key.Home:
  106. return NavigationDirection.First;
  107. case Key.End:
  108. return NavigationDirection.Last;
  109. case Key.PageUp:
  110. return NavigationDirection.PageUp;
  111. case Key.PageDown:
  112. return NavigationDirection.PageDown;
  113. default:
  114. return null;
  115. }
  116. }
  117. }
  118. }