Exemplo n.º 1
0
        public WpfPresentationSource(UIElement rootElement, IWpfValueConverter converter)
        {
            this.RootElement = rootElement;
            this.converter = converter;

            RootElement.IsRootElement = true;

            MouseDevice = new MouseDevice(this);
            KeyboardDevice = new KeyboardDevice(this);

            container = new wpf::System.Windows.Controls.Canvas { Background = wpf::System.Windows.Media.Brushes.Transparent };
            container.PreviewMouseMove += OnContainerMouseMove;
            container.PreviewMouseDown += OnContainerMouseDown;
            container.PreviewMouseUp += OnContainerMouseUp;
            container.PreviewMouseWheel += (sender, e) => e.Handled = MouseDevice.ProcessRawEvent(new RawMouseWheelEventArgs(e.Delta, converter.ConvertBack(e.GetPosition(container)), GetTimestamp()));

            MouseDevice.CursorChanged += (sender, e) => container.Cursor = converter.Convert(MouseDevice.Cursor);
            container.Cursor = converter.Convert(MouseDevice.Cursor);

            window = new wpf::System.Windows.Window { UseLayoutRounding = true, Content = container };
            window.Activated += (sender, e) => MouseDevice.Activate();
            window.Deactivated += (sender, e) => MouseDevice.Deactivate();
            window.SizeChanged += (sender, e) => SetRootElementSize();
            window.PreviewKeyDown += (sender, e) => e.Handled = KeyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.PreviewKeyUp += (sender, e) => e.Handled = KeyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.Show();

            container.Children.Add(((IWpfRenderElement)rootElement.GetRenderElement(WpfRenderElementFactory.Default)).WpfElement);
            SetRootElementSize();

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
Exemplo n.º 2
0
        public void MouseDeviceButtonTest()
        {
            Border element = new Border { Background = Brushes.Transparent, Width = 100, Height = 100, IsRootElement = true };
            element.Measure(Size.Infinity);
            element.Arrange(new Rect(element.DesiredSize));

            int eventIndex = 0;
            int previewMouseDownIndex = 0;
            int previewMouseUpIndex = 0;
            int mouseDownIndex = 0;
            int mouseUpIndex = 0;

            element.PreviewMouseDown += (sender, e) => previewMouseDownIndex = ++eventIndex;
            element.PreviewMouseUp += (sender, e) => previewMouseUpIndex = ++eventIndex;
            element.MouseDown += (sender, e) => mouseDownIndex = ++eventIndex;
            element.MouseUp += (sender, e) => mouseUpIndex = ++eventIndex;

            TestPresentationSource presentationSource = new TestPresentationSource();
            presentationSource.RootElement = element;

            MouseDevice mouseDevice = new MouseDevice(presentationSource);

            mouseDevice.Activate();

            mouseDevice.ProcessRawEvent(new RawMouseButtonEventArgs(MouseButton.Left, MouseButtonState.Pressed, new Point(10, 10), 0));
            Assert.AreEqual(MouseButtonState.Pressed, mouseDevice.GetButtonState(MouseButton.Left));
            Assert.AreEqual(1, previewMouseDownIndex);
            Assert.AreEqual(2, mouseDownIndex);

            mouseDevice.ProcessRawEvent(new RawMouseButtonEventArgs(MouseButton.Left, MouseButtonState.Released, new Point(10, 10), 0));
            Assert.AreEqual(MouseButtonState.Released, mouseDevice.GetButtonState(MouseButton.Left));
            Assert.AreEqual(3, previewMouseUpIndex);
            Assert.AreEqual(4, mouseUpIndex);

            mouseDevice.ProcessRawEvent(new RawMouseButtonEventArgs(MouseButton.Left, MouseButtonState.Pressed, new Point(10, 10), 0));
            Assert.AreEqual(MouseButtonState.Pressed, mouseDevice.GetButtonState(MouseButton.Left));
            Assert.AreEqual(5, previewMouseDownIndex);
            Assert.AreEqual(6, mouseDownIndex);

            mouseDevice.Deactivate();
            Assert.AreEqual(MouseButtonState.Released, mouseDevice.GetButtonState(MouseButton.Left));
            Assert.AreEqual(7, previewMouseUpIndex);
            Assert.AreEqual(8, mouseUpIndex);
        }
Exemplo n.º 3
0
        public PresentationSource(UIElement rootElement, IHtmlValueConverter converter)
        {
            this.RootElement = rootElement;
            this.converter = converter;

            RootElement.IsRootElement = true;

            MouseDevice = new MouseDevice(this);
            KeyboardDevice = new KeyboardDevice(this);

            window = System.Html.Window.Instance;

            MouseDevice.CursorChanged += (sender, e) => System.Html.Window.Document.Body.Style.Cursor = converter.ToCursorString(MouseDevice.Cursor);
            System.Html.Window.Document.Body.Style.Cursor = converter.ToCursorString(MouseDevice.Cursor);

            System.Html.Window.OnKeydown = OnKeyDown;
            System.Html.Window.OnKeyup = OnKeyUp;
            System.Html.Window.OnKeypress = PreventKeyboardHandled;
            System.Html.Window.OnMousemove = OnMouseMove;
            System.Html.Window.OnMousedown = OnMouseDown;
            System.Html.Window.OnMouseup = OnMouseUp;
            System.Html.Window.OnScroll = OnMouseWheel;
            System.Html.Window.OnFocus = e => MouseDevice.Activate();
            System.Html.Window.OnBlur = e => MouseDevice.Deactivate();
            System.Html.Window.OnResize = e => SetRootElementSize();
            System.Html.Window.OnClick = PreventMouseHandled;
            System.Html.Window.OnDblclick = PreventMouseHandled;
            System.Html.Window.OnContextmenu = PreventMouseHandled;
            System.Html.Window.AddEventListener("wheel", OnMouseWheel);

            SetRootElementSize();
            System.Html.Window.Document.Body.Style.Overflow = "hidden";
            System.Html.Window.Document.Body.AppendChild(((HtmlRenderElement)RootElement.GetRenderElement(HtmlRenderElementFactory.Default)).HtmlElement);

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
Exemplo n.º 4
0
        public void MouseDeviceEnterLeaveTest()
        {
            Border child1 = new Border { Background = Brushes.Transparent, Width = 40, Height = 40 };
            Border child2 = new Border { Background = Brushes.Transparent, Width = 40, Height = 40 };
            Canvas.SetTop(child1, 20);
            Canvas.SetTop(child2, 20);
            Canvas.SetLeft(child1, 20);
            Canvas.SetLeft(child2, 80);

            Canvas canvas = new Canvas { Background = Brushes.Transparent, Width = 140, Height = 80, Margin = new Thickness(20), IsRootElement = true };
            canvas.Children.Add(child1);
            canvas.Children.Add(child2);
            canvas.Measure(Size.Infinity);
            canvas.Arrange(new Rect(canvas.DesiredSize));

            TestPresentationSource presentationSource = new TestPresentationSource();
            presentationSource.RootElement = canvas;

            MouseDevice mouseDevice = new MouseDevice(presentationSource);

            List<object> enterList = new List<object>();
            List<object> leaveList = new List<object>();

            canvas.MouseEnter += (sender, e) => enterList.Add(sender);
            canvas.MouseLeave += (sender, e) => leaveList.Add(sender);
            child1.MouseEnter += (sender, e) => enterList.Add(sender);
            child1.MouseLeave += (sender, e) => leaveList.Add(sender);
            child2.MouseEnter += (sender, e) => enterList.Add(sender);
            child2.MouseLeave += (sender, e) => leaveList.Add(sender);

            mouseDevice.Activate();

            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(10, 10), 0));
            Assert.AreEqual(0, enterList.Count());
            Assert.AreEqual(0, leaveList.Count());
            Assert.IsFalse(canvas.IsMouseOver);
            Assert.IsFalse(child1.IsMouseOver);
            Assert.IsFalse(child2.IsMouseOver);

            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(30, 30), 0));
            CollectionAssert.AreEqual(enterList, new object[] { canvas });
            Assert.AreEqual(0, leaveList.Count());
            Assert.IsTrue(canvas.IsMouseOver);
            Assert.IsFalse(child1.IsMouseOver);
            Assert.IsFalse(child2.IsMouseOver);

            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(60, 60), 0));
            CollectionAssert.AreEqual(enterList, new object[] { canvas, child1 });
            Assert.AreEqual(0, leaveList.Count());
            Assert.IsTrue(canvas.IsMouseOver);
            Assert.IsTrue(child1.IsMouseOver);
            Assert.IsFalse(child2.IsMouseOver);

            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(120, 60), 0));
            CollectionAssert.AreEqual(enterList, new object[] { canvas, child1, child2 });
            CollectionAssert.AreEqual(leaveList, new object[] { child1 });
            Assert.IsTrue(canvas.IsMouseOver);
            Assert.IsFalse(child1.IsMouseOver);
            Assert.IsTrue(child2.IsMouseOver);

            mouseDevice.Deactivate();
            CollectionAssert.AreEqual(enterList, new object[] { canvas, child1, child2 });
            CollectionAssert.AreEqual(leaveList, new object[] { child1, child2, canvas });
            Assert.IsFalse(canvas.IsMouseOver);
            Assert.IsFalse(child1.IsMouseOver);
            Assert.IsFalse(child2.IsMouseOver);

            mouseDevice.Activate();
            CollectionAssert.AreEqual(enterList, new object[] { canvas, child1, child2, canvas, child2 });
            CollectionAssert.AreEqual(leaveList, new object[] { child1, child2, canvas });
            Assert.IsTrue(canvas.IsMouseOver);
            Assert.IsFalse(child1.IsMouseOver);
            Assert.IsTrue(child2.IsMouseOver);
        }