AccessText.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Avalonia.Automation.Peers;
  2. using Avalonia.Input;
  3. using Avalonia.Reactive;
  4. using Avalonia.Media;
  5. using Avalonia.Media.TextFormatting;
  6. namespace Avalonia.Controls.Primitives
  7. {
  8. /// <summary>
  9. /// A text block that displays a character prefixed with an underscore as an access key.
  10. /// </summary>
  11. public class AccessText : TextBlock
  12. {
  13. /// <summary>
  14. /// Defines the <see cref="ShowAccessKey"/> attached property.
  15. /// </summary>
  16. public static readonly AttachedProperty<bool> ShowAccessKeyProperty =
  17. AvaloniaProperty.RegisterAttached<AccessText, Control, bool>("ShowAccessKey", inherits: true);
  18. /// <summary>
  19. /// The access key handler for the current window.
  20. /// </summary>
  21. private IAccessKeyHandler? _accessKeys;
  22. /// <summary>
  23. /// Initializes static members of the <see cref="AccessText"/> class.
  24. /// </summary>
  25. static AccessText()
  26. {
  27. AffectsRender<AccessText>(ShowAccessKeyProperty);
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="AccessText"/> class.
  31. /// </summary>
  32. public AccessText()
  33. {
  34. this.GetObservable(TextProperty).Subscribe(TextChanged);
  35. }
  36. /// <summary>
  37. /// Gets the access key.
  38. /// </summary>
  39. public char AccessKey
  40. {
  41. get;
  42. private set;
  43. }
  44. /// <summary>
  45. /// Gets or sets a value indicating whether the access key should be underlined.
  46. /// </summary>
  47. public bool ShowAccessKey
  48. {
  49. get { return GetValue(ShowAccessKeyProperty); }
  50. set { SetValue(ShowAccessKeyProperty, value); }
  51. }
  52. /// <summary>
  53. /// Renders the <see cref="AccessText"/> to a drawing context.
  54. /// </summary>
  55. /// <param name="context">The drawing context.</param>
  56. private protected override void RenderCore(DrawingContext context)
  57. {
  58. base.RenderCore(context);
  59. int underscore = Text?.IndexOf('_') ?? -1;
  60. if (underscore != -1 && ShowAccessKey)
  61. {
  62. var rect = TextLayout!.HitTestTextPosition(underscore);
  63. var offset = new Vector(0, -1.5);
  64. context.DrawLine(
  65. new Pen(Foreground, 1),
  66. rect.BottomLeft + offset,
  67. rect.BottomRight + offset);
  68. }
  69. }
  70. /// <inheritdoc/>
  71. protected override TextLayout CreateTextLayout(string? text)
  72. {
  73. return base.CreateTextLayout(RemoveAccessKeyMarker(text));
  74. }
  75. /// <inheritdoc/>
  76. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  77. {
  78. base.OnAttachedToVisualTree(e);
  79. _accessKeys = (e.Root as TopLevel)?.AccessKeyHandler;
  80. if (_accessKeys != null && AccessKey != 0)
  81. {
  82. _accessKeys.Register(AccessKey, this);
  83. }
  84. }
  85. /// <inheritdoc/>
  86. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  87. {
  88. base.OnDetachedFromVisualTree(e);
  89. if (_accessKeys != null && AccessKey != 0)
  90. {
  91. _accessKeys.Unregister(this);
  92. _accessKeys = null;
  93. }
  94. }
  95. protected override AutomationPeer OnCreateAutomationPeer()
  96. {
  97. return new NoneAutomationPeer(this);
  98. }
  99. internal static string? RemoveAccessKeyMarker(string? text)
  100. {
  101. if (!string.IsNullOrEmpty(text))
  102. {
  103. var accessKeyMarker = "_";
  104. var doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;
  105. int index = FindAccessKeyMarker(text);
  106. if (index >= 0 && index < text.Length - 1)
  107. text = text.Remove(index, 1);
  108. text = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
  109. }
  110. return text;
  111. }
  112. private static int FindAccessKeyMarker(string text)
  113. {
  114. var length = text.Length;
  115. var startIndex = 0;
  116. while (startIndex < length)
  117. {
  118. int index = text.IndexOf('_', startIndex);
  119. if (index == -1)
  120. return -1;
  121. if (index + 1 < length && text[index + 1] != '_')
  122. return index;
  123. startIndex = index + 2;
  124. }
  125. return -1;
  126. }
  127. /// <summary>
  128. /// Called when the <see cref="TextBlock.Text"/> property changes.
  129. /// </summary>
  130. /// <param name="text">The new text.</param>
  131. private void TextChanged(string? text)
  132. {
  133. var key = (char)0;
  134. if (text != null)
  135. {
  136. int underscore = text.IndexOf('_');
  137. if (underscore != -1 && underscore < text.Length - 1)
  138. {
  139. key = text[underscore + 1];
  140. }
  141. }
  142. AccessKey = key;
  143. if (_accessKeys != null && AccessKey != 0)
  144. {
  145. _accessKeys.Register(AccessKey, this);
  146. }
  147. }
  148. }
  149. }