Browse Source

Fix AccessText after merge.

ui-automation-test
Steven Kirk 5 years ago
parent
commit
7bb26473d1
  1. 52
      src/Avalonia.Controls/Primitives/AccessText.cs

52
src/Avalonia.Controls/Primitives/AccessText.cs

@ -76,26 +76,12 @@ namespace Avalonia.Controls.Primitives
rect.BottomLeft + offset, rect.BottomLeft + offset,
rect.BottomRight + offset); rect.BottomRight + offset);
} }
internal static string RemoveAccessKeyMarker(string text)
{
if (!string.IsNullOrEmpty(text))
{
var accessKeyMarker = "_";
var doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;
int index = FindAccessKeyMarker(text);
if (index >= 0 && index < text.Length - 1)
text = text.Remove(index, 1);
text = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
}
return text;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
protected override TextLayout CreateTextLayout(Size constraint, string text) protected override TextLayout CreateTextLayout(Size constraint, string text)
{ {
return base.CreateTextLayout(constraint, StripAccessKey(text)); return base.CreateTextLayout(constraint, RemoveAccessKeyMarker(text));
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -127,23 +113,35 @@ namespace Avalonia.Controls.Primitives
return new NoneAutomationPeer(factory, this); return new NoneAutomationPeer(factory, this);
} }
/// <summary> internal static string RemoveAccessKeyMarker(string text)
/// Returns a string with the first underscore stripped.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The text with the first underscore stripped.</returns>
private string StripAccessKey(string text)
{ {
var position = text.IndexOf('_'); if (!string.IsNullOrEmpty(text))
if (position == -1)
{ {
return text; var accessKeyMarker = "_";
var doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;
int index = FindAccessKeyMarker(text);
if (index >= 0 && index < text.Length - 1)
text = text.Remove(index, 1);
text = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
} }
else return text;
}
private static int FindAccessKeyMarker(string text)
{
var length = text.Length;
var startIndex = 0;
while (startIndex < length)
{ {
return text.Substring(0, position) + text.Substring(position + 1); int index = text.IndexOf('_', startIndex);
if (index == -1)
return -1;
if (index + 1 < length && text[index + 1] != '_')
return index;
startIndex = index + 2;
} }
return -1;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save