private List GetTextRangeAttributeKeyValuePair()

in src/AccessibilityInsights.SharedUx/ViewModels/TextRangeViewModel.cs [147:375]


        private List<TextAttributeViewModel> GetTextRangeAttributeKeyValuePair(TextRange tr, KeyValuePair<int, string> kv, bool collapse)
        {
            List<TextAttributeViewModel> list = new List<TextAttributeViewModel>();

            dynamic value = tr.GetAttributeValue(kv.Key);

            switch (kv.Key)
            {
                case TextAttributeType.UIA_AnimationStyleAttributeId:
                    if(value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, AnimationStyle.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_BackgroundColorAttributeId:
                case TextAttributeType.UIA_ForegroundColorAttributeId:
                case TextAttributeType.UIA_OverlineColorAttributeId:
                case TextAttributeType.UIA_StrikethroughColorAttributeId:
                case TextAttributeType.UIA_UnderlineColorAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, string.Format("#{0:X8}", value)));
                    }
                    break;
                case TextAttributeType.UIA_BulletStyleAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, BulletStyle.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_CapStyleAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CapStyle.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_CultureAttributeId:
                    if (value is int culture)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Invariant($"{CultureInfo.GetCultureInfo(culture).EnglishName} ({culture})")));
                    }
                    break;
                case TextAttributeType.UIA_StyleIdAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, StyleId.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_SayAsInterpretAsAttributeId:
                    // VT_I4
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, SayAsInterpretAs.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_FontNameAttributeId:
                case TextAttributeType.UIA_StyleNameAttributeId:
                case TextAttributeType.UIA_LineSpacingAttributeId:
                    // VT_BSTR
                    if (value is string)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value));
                    }
                    break;
                case TextAttributeType.UIA_FontSizeAttributeId:
                case TextAttributeType.UIA_IndentationFirstLineAttributeId:
                case TextAttributeType.UIA_IndentationLeadingAttributeId:
                case TextAttributeType.UIA_IndentationTrailingAttributeId:
                case TextAttributeType.UIA_MarginBottomAttributeId:
                case TextAttributeType.UIA_MarginLeadingAttributeId:
                case TextAttributeType.UIA_MarginTopAttributeId:
                case TextAttributeType.UIA_MarginTrailingAttributeId:
                case TextAttributeType.UIA_BeforeParagraphSpacingAttributeId:
                case TextAttributeType.UIA_AfterParagraphSpacingAttributeId:
                    // VT_R8
                    if (value is double || value is long)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value.ToString()));
                    }
                    break;
                case TextAttributeType.UIA_FontWeightAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FontWeight.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_HorizontalTextAlignmentAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FontWeight.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_IsHiddenAttributeId:
                case TextAttributeType.UIA_IsItalicAttributeId:
                case TextAttributeType.UIA_IsReadOnlyAttributeId:
                case TextAttributeType.UIA_IsSubscriptAttributeId:
                case TextAttributeType.UIA_IsSuperscriptAttributeId:
                case TextAttributeType.UIA_IsActiveAttributeId:
                    if (value is bool)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value.ToString()));
                    }
                    break;
                case TextAttributeType.UIA_OutlineStylesAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, OutlineStyle.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_OverlineStyleAttributeId:
                case TextAttributeType.UIA_StrikethroughStyleAttributeId:
                case TextAttributeType.UIA_UnderlineStyleAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, TextDecorationLineStyle.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_TabsAttributeId:
                    var txt = ConvertArrayToString(value);

                    if(txt != null)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, txt));
                    }
                    break;
                case TextAttributeType.UIA_TextFlowDirectionsAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FlowDirection.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_AnnotationTypesAttributeId:
                    StringBuilder sb = new StringBuilder();
                    if (value is double)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, AnnotationType.GetInstance().GetNameById((int)value)));
                    }
                    else if (value is Array arr)
                    {
                        if (collapse && arr.Length > 0) // collapse the array into a single row
                        {
                            var count = new Dictionary<string, int>();
                            foreach (var val in arr)
                            {
                                var key = AnnotationType.GetInstance().GetNameById((int)val);
                                if (count.ContainsKey(key))
                                {
                                    count[key]++;
                                }
                                else
                                {
                                    count[key] = 1;
                                }
                            }

                            StringBuilder strBuild = new StringBuilder();
                            foreach (var item in count)
                            {
                                strBuild.Append(Invariant($"{item.Key}: {item.Value}, "));
                            }
                            strBuild.Length-=2; //remove final , and <space>
                            list.Add(new TextAttributeViewModel(kv.Key, kv.Value,strBuild.ToString()));
                        }
                        else // create a row for each array value
                        {
                            if (arr.Length > 0)
                            {
                                for (int i = 0; i < arr.Length; i++)
                                {
                                    list.Add(new TextAttributeViewModel(kv.Key, string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", kv.Value, i), AnnotationType.GetInstance().GetNameById((int)arr.GetValue(i))));
                                }
                            }
                        }
                    }
                    break;
                case TextAttributeType.UIA_SelectionActiveEndAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, ActiveEnd.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_CaretPositionAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CaretPosition.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_CaretBidiModeAttributeId:
                    if (value is int)
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CaretBidiMode.GetInstance().GetNameById(value)));
                    }
                    break;
                case TextAttributeType.UIA_AnnotationObjectsAttributeId:
                    if (value is IUIAutomationElementArray)
                    {
                        IUIAutomationElementArray arr = value;
                        if (arr.Length > 0)
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                list.Add(new TextAttributeViewModel(kv.Key, string.Format(CultureInfo.InvariantCulture, Resources.TextRangeViewModel_GetTextRangeAttributeKeyValuePair_AnnotationObjects_0, i), new DesktopElement((IUIAutomationElement)arr.GetElement(i))));
                            }
                        }
                    }
                    break;
                case TextAttributeType.UIA_LinkAttributeId:
                    // do nothing for now until it is shown as necessary information.
                    //try
                    //{
                    //    IUIAutomationTextRange lnk = Marshal.GetObjectForIUnknown(value) as IUIAutomationTextRange;
                    //    list.Add(new TextAttributeViewModel(kv.Value, new TextRangeViewModel(new TextRange(lnk))));
                    //}
                    //catch (Exception e)
                    //{
                    //    e.ReportException();
                    //}
                    break;
                default:
                    // need to make a decision for these Attributes since it return Object.
                    if (value.GetType().Name != "__ComObject")
                    {
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value));
                    }
                    break;
            }

            return list;
        }