/// <summary> /// Populate element with list of properties /// </summary> /// <param name="element"></param> /// <param name="list"></param> private static void PopulateWithIndicatedProperties(this A11yElement element, List <int> list) { element.Clear(); if (element.IsSafeToRefresh()) { A11yAutomation.GetUIAutomationObject().PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns); // build a cache based on the lists var cache = DesktopElementHelper.BuildCacheRequest(list, null); // buildupdate cached element var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache); var properties = RetrievePropertiesFromCache(uia, list); element.Properties = properties.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id); element.UpdateGlimpse(); // release previous UIAElement Marshal.ReleaseComObject(uia); // release cache interface. Marshal.ReleaseComObject(cache); } }
/// <summary> /// Adds the value of the ClickablePoint property to the A11yElement's Properties dictionary /// </summary> /// <remarks> /// Requesting the clickable point property in Edge can cause a crash, /// so the clickable point property is not initially populated by <see cref="DesktopElementExtensionMethods.PopulatePropertiesAndPatternsFromCache(A11yElement)"/>. /// </remarks> private static void InitClickablePointProperty(this A11yElement a11yElement, IUIAutomationElement uiaElement) { if (a11yElement.IsEdgeElement()) { return; } int id = PropertyType.UIA_ClickablePointPropertyId; double[] clickablePoint = uiaElement.GetCurrentPropertyValue(id); if (clickablePoint == null) { return; } string name = A11yAutomation.GetUIAutomationObject().GetPropertyProgrammaticName(id); var prop = new A11yProperty { Id = id, Name = name, Value = new Point((int)clickablePoint[0], (int)clickablePoint[1]) }; a11yElement.Properties.Add(id, prop); }
/// <summary> /// Get IUIAutomationTreeWalker based on inidcated mode. /// </summary> /// <param name="mode">TreeViewMode to get walker</param> /// <returns></returns> public static IUIAutomationTreeWalker GetTreeWalker(TreeViewMode mode) { IUIAutomationTreeWalker walker = null; var uia = A11yAutomation.GetUIAutomationObject(); switch (mode) { case TreeViewMode.Content: walker = uia.ContentViewWalker; break; case TreeViewMode.Control: walker = uia.ControlViewWalker; break; case TreeViewMode.Raw: walker = uia.RawViewWalker; break; } return(walker); }
/// <summary> /// Build a cacherequest for properties and patterns /// </summary> /// <param name="pps">Property ids</param> /// <param name="pts">Pattern ids</param> public static IUIAutomationCacheRequest BuildCacheRequest(List <int> pps, List <int> pts) { return(GetPropertiesCache(A11yAutomation.GetUIAutomationObject(), pps, pts)); }
/// <summary> /// Refresh the property and patterns with Live data. /// it also set the Glimpse /// the update is done via caching to improve performance. /// </summary> /// <param name="useProperties">default is false to refresh it from UIElement directly</param> private static void PopulatePropertiesAndPatternsFromCache(this A11yElement element) { try { // Get the list of properties to retrieve A11yAutomation.GetUIAutomationObject().PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns); var ppl = new List <Tuple <int, string> >(); for (int i = 0; i < ppids.Length; i++) { var id = (int)ppids.GetValue(i); var name = (string)ppns.GetValue(i); if (DesktopElement.IsExcludedProperty(id, name) == false) { ppl.Add(new Tuple <int, string>(id, name)); } } A11yAutomation.GetUIAutomationObject().PollForPotentialSupportedPatterns((IUIAutomationElement)element.PlatformObject, out int[] ptids, out string[] ptns); var ptl = new List <Tuple <int, string> >(); for (int i = 0; i < ptids.Length; i++) { ptl.Add(new Tuple <int, string>((int)ptids.GetValue(i), (string)ptns.GetValue(i))); } var pplist = (from pp in ppl select pp.Item1).ToList(); var ptlist = (from pt in ptl select pt.Item1).ToList(); // build a cache based on the lists var cache = DesktopElementHelper.BuildCacheRequest(pplist, ptlist); // buildupdate cached element var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache); // retrieve properties from cache var ps = (from pp in ppl//.AsParallel() let val = GetPropertyValueFromCache(uia, pp.Item1) where val != null select new A11yProperty(pp.Item1, val, pp.Item2)); element.Properties = ps.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id); element.UpdateGlimpse(); // retrieve patterns from cache var ptlst = from pt in ptl let pi = A11yPatternFactory.GetPatternInstance(element, uia, pt.Item1, pt.Item2) where pi != null select pi; element.Patterns = ptlst.ToList(); // release previous UIAElement Marshal.ReleaseComObject(uia); // release cache interface. Marshal.ReleaseComObject(cache); ppl?.Clear(); ptl?.Clear(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); } #pragma warning restore CA1031 // Do not catch general exception types }