Exemple #1
0
            private void UpdateAncestors(NotifyCollectionChangedEventArgs eventArgs)
            {
                switch (eventArgs.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    CheckIndexValid(eventArgs.NewStartingIndex, "NewStartingIndex", NotifyCollectionChangedAction.Add);
                    Ancestors.Insert(eventArgs.NewStartingIndex, CreateSubscriberForCollectionItem(eventArgs.NewItems.Cast <object>().Single()));
                    return;

                case NotifyCollectionChangedAction.Remove:
                    CheckIndexValid(eventArgs.OldStartingIndex, "OldStartingIndex", NotifyCollectionChangedAction.Remove);
                    Ancestors[eventArgs.OldStartingIndex].Dispose();
                    Ancestors.RemoveAt(eventArgs.OldStartingIndex);
                    return;

                case NotifyCollectionChangedAction.Replace:
                    CheckIndexValid(eventArgs.OldStartingIndex, "OldStartingIndex", NotifyCollectionChangedAction.Replace);
                    Ancestors[eventArgs.OldStartingIndex].Dispose();
                    Ancestors[eventArgs.OldStartingIndex] = CreateSubscriberForCollectionItem(eventArgs.NewItems.Cast <object>().Single());
                    return;

                case NotifyCollectionChangedAction.Move:
                    CheckIndexValid(eventArgs.OldStartingIndex, "OldStartingIndex", NotifyCollectionChangedAction.Move);
                    CheckIndexValid(eventArgs.NewStartingIndex, "NewStartingIndex", NotifyCollectionChangedAction.Move);
                    var movingAncestor = Ancestors[eventArgs.OldStartingIndex];
                    Ancestors.RemoveAt(eventArgs.OldStartingIndex);
                    Ancestors.Insert(eventArgs.NewStartingIndex, movingAncestor);
                    return;

                case NotifyCollectionChangedAction.Reset:
                    DisposeAndClearAncestors();
                    foreach (var ancestor in InitAncestors(EffectiveObject))
                    {
                        Ancestors.Add(ancestor);
                    }
                    return;

                default:
                    throw new ArgumentException(string.Format("Unknown eventArgs.Action enum action: {0}", eventArgs.Action), "eventArgs");
                }
            }
        public override void Execute()
        {
            if (!IsEnabled)
            {
                return;
            }
            Prepare();

            if (Root.IsWPF())
            {
                Task.Run(() =>
                {
                    AutomationElement element = Root;
                    Ancestors.ForEach(x =>
                    {
                        element = GetCurrentElement(element, x);
                        element.TryExpand();
                    });
                    element = GetCurrentElement(element);
                    element.TryDoDefaultAction();
                });
                return;
            }

            Task.Run(() =>
            {
                if (Ancestors.Count > 0)
                {
                    var observer       = SearchByText.UIAssistantAPI.AutomationAPI.CreateObserver(Interfaces.Events.ObserverKinds.PopupObserver);
                    var shouldBeClosed = false;
                    observer.Callback += x =>
                    {
                        try
                        {
                            if (Ancestors.Count == 0)
                            {
                                shouldBeClosed = true;
                                observer.Dispose();
                                var element = GetCurrentElement(x);
                                if (element == null)
                                {
                                    SearchByText.UIAssistantAPI.NotificationAPI.NotifyWarnMessage(Consts.PluginName, "Cannot find the selected menu item");
                                    return;
                                }

                                // 見えない要素(要スクロール)の場合、実行できないので実行は諦める
                                if (element.Current.IsOffscreen)
                                {
                                    SearchByText.UIAssistantAPI.NotificationAPI.NotifyWarnMessage(Consts.PluginName, "Cannot run the selected menu item");
                                    return;
                                    //System.Diagnostics.Debug.Print($"1: {element.Current.AccessKey}, {element.Current.IsOffscreen}, {element.Current.IsEnabled}, {element.Current.BoundingRectangle}");
                                    //Core.Input.KeyboardOperation.SendKeys(System.Windows.Input.Key.Up);
                                    //System.Threading.Thread.Sleep(200);
                                    //Task.Run(() => element = GetCurrentElement(x)).Wait();
                                    //System.Diagnostics.Debug.Print($"1: {element.Current.AccessKey}, {element.Current.IsOffscreen}, {element.Current.IsEnabled}, {element.Current.BoundingRectangle}");
                                }

                                element.TryDoDefaultAction();
                                return;
                            }
                            var elements = GetCandidates(x, AutomationElement.NameProperty, Ancestors[0]);
                            Ancestors.RemoveAt(0);
                            foreach (var element in elements)
                            {
                                if (element.CanExpandElement(element.Current.ControlType))
                                {
                                    element.TryExpand();
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            SearchByText.UIAssistantAPI.NotificationAPI.NotifyErrorMessage(Consts.PluginName, ex.Message + "\nThe menu may not be scrolled");
                        }
                        finally
                        {
                            if (shouldBeClosed)
                            {
                                System.Threading.Thread.Sleep(200);
                                NativeMethods.SendMessage(Root.Current.NativeWindowHandle, NativeMethods.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
                            }
                        }
                    };
                    observer.Observe();

                    var propCondition = new PropertyCondition(AutomationElement.NameProperty, Ancestors[0]);
                    var candidate     = Root.FindAll(TreeScope.Descendants, propCondition);
                    if (candidate.Count == 0)
                    {
                        SearchByText.UIAssistantAPI.NotificationAPI.NotifyWarnMessage(Consts.PluginName, "Cannot find the selected menu item");
                        return;
                    }
                    Ancestors.RemoveAt(0);
                    candidate[0].TryExpand();

                    observer.Wait();
                }
                else
                {
                    Element.DoAction();
                }
            });
        }