Exemple #1
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);
            CheckParamExists(args, "name");

            object c = controls.GetControl(args["element_id"]);

            bool isExists           = false;
            DynamicValueReply reply = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                PropertyInfo prop = c.GetType().GetProperty(args["name"]);
                FieldInfo field   = c.GetType().GetField(args["name"]);
                if (prop != null)
                {
                    isExists = true;
                    reply    = new DynamicValueReply(prop.GetValue(c, null));
                }
                else if (field != null)
                {
                    isExists = true;
                    reply    = new DynamicValueReply(field.GetValue(c));
                }
            });
            if (isExists)
            {
                return(reply);
            }
            else
            {
                throw new ErrorReplyException(ErrorCodes.NOT_FOUND, "no such field or property: " + args["name"]);
            }
        }
Exemple #2
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);

            DynamicValueReply reply = new DynamicValueReply(-1);

            dynamic c = controls.GetControl(args["element_id"]);

            if (c is Visual)
            {
                Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    HwndSource source = (HwndSource)HwndSource.FromVisual(c);
                    if (source != null)
                    {
                        IntPtr hWnd = source.Handle;
                        reply       = new DynamicValueReply(hWnd.ToInt64());
                    }
                    else
                    {
                        reply = new DynamicValueReply(null);
                    }
                });
            }
            return(reply);
        }
Exemple #3
0
        public override Reply Run<T>(ControlsStorage<T> controls, IDictionary<string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId<T>(args["element_id"], controls);
            CheckParamExists(args, "name");

            object c = controls.GetControl(args["element_id"]);

            if (!(c is UIElement))
            {
                throw new ErrorReplyException(ErrorCodes.UNSUPPORTED_ACTION, "This element is not UIElement");
            }

            bool isFound = false;
            for (Type t = c.GetType(); t != null; t = t.BaseType)
            {
                RoutedEvent[] events = EventManager.GetRoutedEventsForOwner(t);
                if (events != null)
                {
                    RoutedEvent e = Array.Find(events, x => x.Name == args["name"]);
                    if (e != null) 
                    {
                        isFound = true;
                        Application.Current.Dispatcher.Invoke((Action)delegate
                        {
                            (c as UIElement).RaiseEvent(new RoutedEventArgs(e));
                        });
                    }
                }
            }
            if (!isFound)
                throw new ErrorReplyException(ErrorCodes.NOT_FOUND, "can not find such event");
            return new Reply(ErrorCodes.OK);
        }
Exemple #4
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);

            object c = controls.GetControl(args["element_id"]);

            DynamicValueReply reply = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                Visual control = c as Visual;
                if (control != null)
                {
                    DependencyObject parentControl = VisualTreeHelper.GetParent(control);
                    if (parentControl != null)
                    {
                        reply = new DynamicValueReply((controls as ControlsStorage <DependencyObject>).RegisterControl(parentControl));
                    }
                    else
                    {
                        reply = new DynamicValueReply(null);
                    }
                }
                else
                {
                    reply = new DynamicValueReply(null);
                }
            });

            return(reply);
        }
Exemple #5
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);

            dynamic           c     = controls.GetControl(args["element_id"]);
            DynamicValueReply reply = new DynamicValueReply(this.GetRect(c));

            return(reply);
        }
Exemple #6
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);

            dynamic           c     = controls.GetControl(args["element_id"]);
            DynamicValueReply reply = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                reply = new DynamicValueReply(GetNameString(c));
            });
            return(reply);
        }
Exemple #7
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);
            CheckParamExists(args, "name");

            object c = controls.GetControl(args["element_id"]);

            // TODO params support (array of pairs function_type-value?)
            MethodInfo method = c.GetType().GetMethod(args["name"], Type.EmptyTypes);

            dynamic ret = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                ret = method.Invoke(c, null);
            });
            return(new DynamicValueReply(ret));
        }
Exemple #8
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);

            dynamic c = controls.GetControl(args["element_id"]);

            if (!(c is IInputElement))
            {
                throw new ErrorReplyException(ErrorCodes.UNSUPPORTED_ACTION, String.Format("Can not cast element with ID {0} to IInputElement. Not focusable?", args["element_id"]));
            }
            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                Keyboard.Focus(c as IInputElement);
            });

            Reply reply = new Reply(ErrorCodes.OK);

            return(reply);
        }
Exemple #9
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            DynamicValueReply reply = new DynamicValueReply(-1);

            // In WPF there are two main concepts that pertain to focus: keyboard focus and logical focus.
            // Keyboard focus is used here (more: https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/focus-overview?view=netframeworkdesktop-4.8)
            IInputElement inputElem = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                inputElem = Keyboard.FocusedElement;
            });

            if (inputElem != null && inputElem is DependencyObject)
            {
                DependencyObject focusedElem = inputElem as DependencyObject;
                reply = new DynamicValueReply((controls as ControlsStorage <DependencyObject>).RegisterControl(focusedElem));
            }

            return(reply);
        }
Exemple #10
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            ControlsStorage = controls as ControlsStorage <DependencyObject>;

            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], null);

            if (args["element_id"] == 0)
            {
                List <long> windows = null;
                Application.Current.Dispatcher.Invoke((Action) delegate
                {
                    windows = new List <long>();
                    GetWindows(windows);
                });
                return(new ElementsReply(windows));
            }
            else
            {
                CheckValidControlId <T>(args["element_id"], controls);
                dynamic c = controls.GetControl(args["element_id"]);
                if (c == null)
                {
                    throw new ErrorReplyException(ErrorCodes.NOT_FOUND, "element not found: " + args["element_id"]);
                }
                else
                {
                    List <long> children = null;
                    Application.Current.Dispatcher.Invoke((Action) delegate
                    {
                        children = new List <long>();
                        GetChildrenOf(children, c);
                    });
                    return(new ElementsReply(children));
                }
            }
        }
Exemple #11
0
        public override Reply Run <T>(ControlsStorage <T> controls, IDictionary <string, dynamic> args)
        {
            CheckParamExists(args, "element_id");
            CheckValidControlId <T>(args["element_id"], controls);
            CheckParamExists(args, "name");
            CheckParamExists(args, "value");

            object c = controls.GetControl(args["element_id"]);

            bool isExists = false;

            Reply reply = null;

            Application.Current.Dispatcher.Invoke((Action) delegate
            {
                try
                {
                    PropertyInfo prop = c.GetType().GetProperty(args["name"]);
                    FieldInfo field   = c.GetType().GetField(args["name"]);
                    if (prop != null)
                    {
                        isExists = true;

                        dynamic value = args["value"];
                        if (args.ContainsKey("is_enum") && args["is_enum"])
                        {
                            value = Enum.Parse(prop.PropertyType, args["value"]);
                        }

                        try
                        {
                            value = ConvertType(value, prop.PropertyType);
                        }
                        catch (Exception e)
                        {
                            reply = new ErrorReply(ErrorCodes.UNSUPPORTED_TYPE, "can not convert to property type: " + e.ToString());
                        }
                        prop.SetValue(c, value, null);
                    }
                    else if (field != null)
                    {
                        isExists      = true;
                        dynamic value = args["value"];
                        if (args.ContainsKey("is_enum") && args["is_enum"])
                        {
                            value = Enum.Parse(field.FieldType, args["value"]);
                        }

                        try
                        {
                            value = ConvertType(value, field.FieldType);
                        }
                        catch (Exception e)
                        {
                            reply = new ErrorReply(ErrorCodes.UNSUPPORTED_TYPE, "can not convert to field type: " + e.ToString());
                        }

                        field.SetValue(c, value);
                    }

                    reply = new Reply(ErrorCodes.OK);
                }
                catch (Exception e)
                {
                    reply = new ErrorReply(ErrorCodes.RUNTIME_ERROR, e.ToString());
                }
            });
            if (isExists)
            {
                return(reply);
            }
            else
            {
                throw new ErrorReplyException(ErrorCodes.NOT_FOUND, "no such field or property: " + args["name"]);
            }
        }