Skip to content

Commit

Permalink
Handle mixed/notsupported values in textpattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Roemer committed Apr 8, 2021
1 parent df78b1c commit 616c060
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions src/FlaUInspect/ViewModels/ElementViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using FlaUI.Core.Conditions;
using FlaUI.Core.Definitions;
using FlaUI.Core.Identifiers;
using FlaUI.Core.Patterns;
using FlaUI.Core.Tools;
using FlaUI.UIA3.Identifiers;
using FlaUInspect.Core;
Expand Down Expand Up @@ -316,23 +317,42 @@ private List<DetailGroupViewModel> LoadDetails()
if (allSupportedPatterns.Contains(AutomationElement.Automation.PatternLibrary.TextPattern))
{
var pattern = AutomationElement.Patterns.Text.Pattern;
try

// TODO: This can in the future be replaced with automation.MixedAttributeValue
object mixedValue = AutomationElement.AutomationType == AutomationType.UIA2
? System.Windows.Automation.TextPattern.MixedAttributeValue
: ((FlaUI.UIA3.UIA3Automation)AutomationElement.Automation).NativeAutomation.ReservedMixedAttributeValue;

var foreColor = GetTextAttribute<int>(pattern, TextAttributes.ForegroundColor, mixedValue, (x) =>
{
var foreColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.ForegroundColor);
var backColor = (int)pattern.DocumentRange.GetAttributeValue(TextAttributes.BackgroundColor);
var patternDetails = new List<DetailViewModel>
{
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(foreColor)} ({foreColor})"),
new DetailViewModel("ForeColor", $"{System.Drawing.Color.FromArgb(backColor)} ({backColor})"),
};
return $"{System.Drawing.Color.FromArgb(x)} ({x})";
});
var backColor = GetTextAttribute<int>(pattern, TextAttributes.BackgroundColor, mixedValue, (x) =>
{
return $"{System.Drawing.Color.FromArgb(x)} ({x})";
});
var fontName = GetTextAttribute<string>(pattern, TextAttributes.FontName, mixedValue, (x) =>
{
return $"{x}";
});
var fontSize = GetTextAttribute<double>(pattern, TextAttributes.FontSize, mixedValue, (x) =>
{
return $"{x}";
});
var fontWeight = GetTextAttribute<int>(pattern, TextAttributes.FontWeight, mixedValue, (x) =>
{
return $"{x}";
});

var c = System.Drawing.Color.FromArgb(32768);
detailGroups.Add(new DetailGroupViewModel("Text Pattern", patternDetails));
}
catch (InvalidCastException ex)
var patternDetails = new List<DetailViewModel>
{
Console.WriteLine($"Exception: {ex.Message}");
}
new DetailViewModel("ForeColor", foreColor),
new DetailViewModel("BackgroundColor", backColor),
new DetailViewModel("FontName", fontName),
new DetailViewModel("FontSize", fontSize),
new DetailViewModel("FontWeight", fontWeight),
};
detailGroups.Add(new DetailGroupViewModel("Text Pattern", patternDetails));
}
// TogglePattern
if (allSupportedPatterns.Contains(AutomationElement.Automation.PatternLibrary.TogglePattern))
Expand Down Expand Up @@ -374,6 +394,32 @@ private List<DetailGroupViewModel> LoadDetails()
return detailGroups;
}

private string GetTextAttribute<T>(ITextPattern pattern, TextAttributeId textAttribute, object mixedValue, Func<T, string> func)
{
var value = pattern.DocumentRange.GetAttributeValue(textAttribute);

if (value == mixedValue)
{
return "Mixed";
}
else if (value == AutomationElement.Automation.NotSupportedValue)
{
return "Not supported";
}
else
{
try
{
var converted = (T)value;
return func(converted);
}
catch
{
return $"Conversion to ${typeof(T)} failed";
}
}
}

private string NormalizeString(string value)
{
if (String.IsNullOrEmpty(value))
Expand Down

0 comments on commit 616c060

Please sign in to comment.