From 616c060945eccd26ed65e709f976f3b9194f768e Mon Sep 17 00:00:00 2001 From: Roman Baeriswyl Date: Thu, 8 Apr 2021 13:41:21 +0200 Subject: [PATCH] Handle mixed/notsupported values in textpattern --- .../ViewModels/ElementViewModel.cs | 74 +++++++++++++++---- 1 file changed, 60 insertions(+), 14 deletions(-) diff --git a/src/FlaUInspect/ViewModels/ElementViewModel.cs b/src/FlaUInspect/ViewModels/ElementViewModel.cs index 75c3e59..20969b1 100644 --- a/src/FlaUInspect/ViewModels/ElementViewModel.cs +++ b/src/FlaUInspect/ViewModels/ElementViewModel.cs @@ -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; @@ -316,23 +317,42 @@ private List 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(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 - { - 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(pattern, TextAttributes.BackgroundColor, mixedValue, (x) => + { + return $"{System.Drawing.Color.FromArgb(x)} ({x})"; + }); + var fontName = GetTextAttribute(pattern, TextAttributes.FontName, mixedValue, (x) => + { + return $"{x}"; + }); + var fontSize = GetTextAttribute(pattern, TextAttributes.FontSize, mixedValue, (x) => + { + return $"{x}"; + }); + var fontWeight = GetTextAttribute(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 { - 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)) @@ -374,6 +394,32 @@ private List LoadDetails() return detailGroups; } + private string GetTextAttribute(ITextPattern pattern, TextAttributeId textAttribute, object mixedValue, Func 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))