public async Task SetElement()

in src/AccessibilityInsights/Modes/SnapshotModeControl.xaml.cs [124:253]


        public async Task SetElement(Guid ecId)
        {
            this.ctrlProgressRing.Activate();

            EnableMenuButtons(this.DataContextMode != DataContextMode.Load);

            bool selectionFailure = false;

            try
            {
                this.ctrlHierarchy.IsEnabled = false;
                ElementContext ec = null;
                await Task.Run(() =>
                {
                    bool contextChanged = CaptureAction.SetTestModeDataContext(ecId,
                        this.DataContextMode, Configuration.TreeViewMode);
                    ec = GetDataAction.GetElementContext(ecId);

                    if (contextChanged && this.DataContextMode != DataContextMode.Load)
                    {
                        // send telemetry of scan results.
                        var dc = GetDataAction.GetElementDataContext(ecId);
                        dc.PublishScanResults();
                    }
                }).ConfigureAwait(false);

                Application.Current.Dispatcher.Invoke(() =>
                {
                    if (ec != null && ec.DataContext != null)
                    {
                        this.ElementContext = ec;
                        this.ctrlTabs.Clear();
                        if (!SelectAction.GetDefaultInstance().IsPaused)
                        {
                            this.ctrlHierarchy.CleanUpTreeView();
                        }
                        this.ctrlHierarchy.SetElement(ec);
                        this.ctrlTabs.SetElement(ec.Element, false, ec.Id);

                        if (ec.DataContext.Screenshot == null && ec.DataContext.Mode != DataContextMode.Load)
                        {
                            Application.Current.MainWindow.WindowStyle = WindowStyle.ToolWindow;
                            Application.Current.MainWindow.Visibility = Visibility.Hidden;
                            HollowHighlightDriver.GetDefaultInstance().IsEnabled = false;

                            this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
                            {
                                ScreenShotAction.CaptureScreenShot(ecId);
                                Application.Current.MainWindow.Visibility = Visibility.Visible;
                                Application.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                            })).Wait();
                        }
                        var imageOverlay = ImageOverlayDriver.GetDefaultInstance();

                        imageOverlay.SetImageElement(ecId);
                        //disable button on highlighter.
                        imageOverlay.SetHighlighterButtonClickHandler(null);

                        if (Configuration.IsHighlighterOn)
                        {
                            imageOverlay.Show();
                        }
                    }

                    // if it is enforced to select a specific element(like fastpass click case)
                    if (ec.DataContext.FocusedElementUniqueId.HasValue)
                    {
                        this.ctrlHierarchy.SelectElement(ec.DataContext.FocusedElementUniqueId.Value);
                    }

                    if (!ec.DataContext.Elements.TryGetValue(ec.DataContext.FocusedElementUniqueId ?? 0, out A11yElement selectedElement))
                    {
                        selectedElement = ec.DataContext.Elements[0];
                    }
                    AutomationProperties.SetName(this, string.Format(CultureInfo.InvariantCulture, Properties.Resources.SetElementInspectTestDetail, selectedElement.Glimpse));

                    FireAsyncContentLoadedEvent(AsyncContentLoadedState.Completed);

                    // Set focus on hierarchy tree
                    Dispatcher.InvokeAsync(() => this.ctrlHierarchy.SetFocusOnHierarchyTree(), DispatcherPriority.Input).Wait();

                    ec.DataContext.FocusedElementUniqueId = null;
                });
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();
                Application.Current.Dispatcher.Invoke(() =>
                {
                    Application.Current.MainWindow.Visibility = Visibility.Visible;
                    EnableMenuButtons(false);
                    this.ElementContext = null;
                    selectionFailure = true;
                });
            }
#pragma warning restore CA1031 // Do not catch general exception types
            finally
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    this.ctrlProgressRing.Deactivate();
                    this.ctrlHierarchy.IsEnabled = true;

                    // if focus has gone to the window, we set focus to the hierarchy control. We do this because disabling the hierarchy control
                    // will throw keyboard focus to mainwindow, where it is not very useful.
                    if (Keyboard.FocusedElement is Window)
                    {
                        this.ctrlHierarchy.Focus();
                    }

                    if (selectionFailure)
                    {
                        MainWin.HandleFailedSelectionReset();
                    }
                    else
                    {
                        // Set the right view state :
                        if (MainWin.CurrentView is TestView iv && iv == TestView.ElementHowToFix)
                        {
                            MainWin.SetCurrentViewAndUpdateUI(TestView.ElementHowToFix);
                        }
                        else
                        {
                            MainWin.SetCurrentViewAndUpdateUI(TestView.ElementDetails);
                        }
                    }
                });
            }
        }