コード例 #1
0
ファイル: MouseDeviceTest.cs プロジェクト: will8886/Granular
        public void MouseDeviceCaptureTest()
        {
            Border child1 = new Border {
                Background = Brushes.Transparent, Width = 100, Height = 100
            };
            Border child2 = new Border {
                Background = Brushes.Transparent, Width = 100, Height = 100
            };
            Border child3 = new Border {
                Background = Brushes.Transparent, Width = 100, Height = 100
            };

            StackPanel panel = new StackPanel {
                IsRootElement = true
            };

            panel.Children.Add(child1);
            panel.Children.Add(child2);

            MouseEventArgs lastArgs = null;

            panel.MouseMove += (sender, e) => lastArgs = e;

            TestPresentationSource presentationSource = new TestPresentationSource();

            presentationSource.RootElement = panel;

            panel.Measure(Size.Infinity);
            panel.Arrange(new Rect(panel.DesiredSize));

            MouseDevice mouseDevice = new MouseDevice(presentationSource);

            mouseDevice.Activate();
            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0));
            Assert.AreEqual(child1, lastArgs.Source);
            Assert.AreEqual(child1, mouseDevice.HitTarget);
            Assert.AreEqual(null, mouseDevice.CaptureTarget);
            Assert.AreEqual(child1, mouseDevice.Target);

            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 150), 0));
            Assert.AreEqual(child2, lastArgs.Source);
            Assert.AreEqual(child2, mouseDevice.HitTarget);
            Assert.AreEqual(null, mouseDevice.CaptureTarget);
            Assert.AreEqual(child2, mouseDevice.Target);

            mouseDevice.Capture(child2);
            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0));
            Assert.AreEqual(child2, lastArgs.Source);
            Assert.AreEqual(child1, mouseDevice.HitTarget);
            Assert.AreEqual(child2, mouseDevice.CaptureTarget);
            Assert.AreEqual(child2, mouseDevice.Target);

            mouseDevice.ReleaseCapture();
            mouseDevice.ProcessRawEvent(new RawMouseEventArgs(new Point(50, 50), 0));
            Assert.AreEqual(child1, lastArgs.Source);
            Assert.AreEqual(child1, mouseDevice.HitTarget);
            Assert.AreEqual(null, mouseDevice.CaptureTarget);
            Assert.AreEqual(child1, mouseDevice.Target);
        }
コード例 #2
0
        public PresentationSource(UIElement rootElement, HtmlRenderElementFactory htmlRenderElementFactory, HtmlValueConverter converter, ImageElementContainer imageElementContainer, SvgDefinitionContainer svgDefinitionContainer)
        {
            this.RootElement = rootElement;
            this.converter   = converter;

            RootElement.IsRootElement = true;

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

            window = Bridge.Html5.Window.Instance;

            MouseDevice.CursorChanged += (sender, e) => Bridge.Html5.Window.Document.Body.SetHtmlStyleProperty("cursor", converter.ToCursorString(MouseDevice.Cursor, htmlRenderElementFactory));
            Bridge.Html5.Window.Document.Body.SetHtmlStyleProperty("cursor", converter.ToCursorString(MouseDevice.Cursor, htmlRenderElementFactory));

            Bridge.Html5.Window.OnKeyDown     = OnKeyDown;
            Bridge.Html5.Window.OnKeyUp       = OnKeyUp;
            Bridge.Html5.Window.OnKeyPress    = PreventKeyboardHandled;
            Bridge.Html5.Window.OnMouseMove   = OnMouseMove;
            Bridge.Html5.Window.OnMouseDown   = OnMouseDown;
            Bridge.Html5.Window.OnMouseUp     = OnMouseUp;
            Bridge.Html5.Window.OnScroll      = OnMouseWheel;
            Bridge.Html5.Window.OnFocus       = e => MouseDevice.Activate();
            Bridge.Html5.Window.OnBlur        = e => MouseDevice.Deactivate();
            Bridge.Html5.Window.OnResize      = e => SetRootElementSize();
            Bridge.Html5.Window.OnClick       = PreventMouseHandled;
            Bridge.Html5.Window.OnContextMenu = PreventMouseHandled;
            Bridge.Html5.Window.AddEventListener("ondblclick", PreventMouseHandled);
            Bridge.Html5.Window.AddEventListener("wheel", OnMouseWheel);

            SetRootElementSize();
            ((FrameworkElement)RootElement).Arrange(new Rect(window.InnerWidth, window.InnerHeight));

            IHtmlRenderElement renderElement = ((IHtmlRenderElement)RootElement.GetRenderElement(htmlRenderElementFactory));

            renderElement.Load();

            Bridge.Html5.Window.Document.Body.Style.Overflow = Overflow.Hidden;
            Bridge.Html5.Window.Document.Body.AppendChild(imageElementContainer.HtmlElement);
            Bridge.Html5.Window.Document.Body.AppendChild(svgDefinitionContainer.HtmlElement);
            Bridge.Html5.Window.Document.Body.AppendChild(renderElement.HtmlElement);

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
コード例 #3
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 = ProcessMouseEvent(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 = ProcessKeyboardEvent(new RawKeyboardEventArgs(converter.ConvertBack(e.Key), converter.ConvertBack(e.KeyStates), e.IsRepeat, GetTimestamp()));
            window.PreviewKeyUp   += (sender, e) => e.Handled = ProcessKeyboardEvent(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();
            ((FrameworkElement)RootElement).Arrange(new Rect(container.ActualWidth, container.ActualHeight));

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
コード例 #4
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();
        }
コード例 #5
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 = Bridge.Html5.Window.Instance;

            MouseDevice.CursorChanged += (sender, e) => Bridge.Html5.Window.Document.Body.Style.SetProperty("cursor", converter.ToCursorString(MouseDevice.Cursor));
            Bridge.Html5.Window.Document.Body.Style.SetProperty("cursor", converter.ToCursorString(MouseDevice.Cursor));

            Bridge.Html5.Window.OnKeyDown     = OnKeyDown;
            Bridge.Html5.Window.OnKeyUp       = OnKeyUp;
            Bridge.Html5.Window.OnKeyPress    = PreventKeyboardHandled;
            Bridge.Html5.Window.OnMouseMove   = OnMouseMove;
            Bridge.Html5.Window.OnMouseDown   = OnMouseDown;
            Bridge.Html5.Window.OnMouseUp     = OnMouseUp;
            Bridge.Html5.Window.OnScroll      = OnMouseWheel;
            Bridge.Html5.Window.OnFocus       = e => MouseDevice.Activate();
            Bridge.Html5.Window.OnBlur        = e => MouseDevice.Deactivate();
            Bridge.Html5.Window.OnResize      = e => SetRootElementSize();
            Bridge.Html5.Window.OnClick       = PreventMouseHandled;
            Bridge.Html5.Window.OnContextMenu = PreventMouseHandled;
            Bridge.Html5.Window.AddEventListener("ondblclick", PreventMouseHandled);
            Bridge.Html5.Window.AddEventListener("wheel", OnMouseWheel);

            SetRootElementSize();
            Bridge.Html5.Window.Document.Body.Style.Overflow = Overflow.Hidden;
            Bridge.Html5.Window.Document.Body.AppendChild(((HtmlRenderElement)RootElement.GetRenderElement(HtmlRenderElementFactory.Default)).HtmlElement);

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
コード例 #6
0
ファイル: MouseDeviceTest.cs プロジェクト: will8886/Granular
        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);
        }
コード例 #7
0
ファイル: PresentationSource.cs プロジェクト: Ref12/Grazor
        public PresentationSource(
            UIElement rootElement,
            HtmlRenderElementFactory htmlRenderElementFactory,
            HtmlValueConverter converter,
            ImageElementContainer imageElementContainer,
            SvgDefinitionContainer svgDefinitionContainer)
        {
            // TODO: Set SynchronizationContext so that all messages get flushed when async callbacks get completed
            this.RootElement = rootElement;
            this.converter   = converter;

            RootElement.IsRootElement = true;

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

            MouseDevice.CursorChanged += (sender, e) => Window.Body.SetHtmlStyleProperty("cursor", converter.ToCursorString(MouseDevice.Cursor, htmlRenderElementFactory));
            Window.Body.SetHtmlStyleProperty("cursor", converter.ToCursorString(MouseDevice.Cursor, htmlRenderElementFactory));
            Window.SetApplicationListener(this);

            var dimensions = SetRootElementSize();

            ((FrameworkElement)RootElement).Arrange(new Rect(dimensions.width, dimensions.height));

            IHtmlRenderElement renderElement = ((IHtmlRenderElement)RootElement.GetRenderElement(htmlRenderElementFactory));

            renderElement.Load();

            Window.Body.SetHtmlStyleProperty("overflow", "hidden");
            Window.Body.AppendChild(imageElementContainer.HtmlElement);
            Window.Body.AppendChild(svgDefinitionContainer.HtmlElement);
            Window.Body.AppendChild(renderElement.HtmlElement);

            MouseDevice.Activate();
            KeyboardDevice.Activate();
        }
コード例 #8
0
ファイル: MouseDeviceTest.cs プロジェクト: will8886/Granular
        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);
        }
コード例 #9
0
ファイル: PresentationSource.cs プロジェクト: Ref12/Grazor
 public void OnFocus()
 {
     MouseDevice.Activate();
 }