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();
        }
        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();
        }
        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();
        }
        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();
        }
Exemple #5
0
        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();
        }
        public void KeyboardDeviceBasicTest()
        {
            FrameworkElement element = new FrameworkElement();

            int eventIndex = 0;

            int previewKeyDownIndex           = 0;
            int previewKeyUpIndex             = 0;
            int previewGotKeyboardFocusIndex  = 0;
            int previewLostKeyboardFocusIndex = 0;
            int keyDownIndex           = 0;
            int keyUpIndex             = 0;
            int gotKeyboardFocusIndex  = 0;
            int lostKeyboardFocusIndex = 0;

            element.PreviewKeyDown           += (sender, e) => previewKeyDownIndex = ++eventIndex;
            element.PreviewKeyUp             += (sender, e) => previewKeyUpIndex = ++eventIndex;
            element.PreviewGotKeyboardFocus  += (sender, e) => previewGotKeyboardFocusIndex = ++eventIndex;
            element.PreviewLostKeyboardFocus += (sender, e) => previewLostKeyboardFocusIndex = ++eventIndex;
            element.KeyDown           += (sender, e) => keyDownIndex = ++eventIndex;
            element.KeyUp             += (sender, e) => keyUpIndex = ++eventIndex;
            element.GotKeyboardFocus  += (sender, e) => gotKeyboardFocusIndex = ++eventIndex;
            element.LostKeyboardFocus += (sender, e) => lostKeyboardFocusIndex = ++eventIndex;

            TestPresentationSource presentationSource = new TestPresentationSource();
            KeyboardDevice         keyboardDevice     = new KeyboardDevice(presentationSource);

            keyboardDevice.Activate();

            IDisposable focus = keyboardDevice.Focus(element);

            Assert.AreEqual(1, previewGotKeyboardFocusIndex);
            Assert.AreEqual(2, gotKeyboardFocusIndex);
            Assert.IsTrue(element.IsKeyboardFocused);

            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            Assert.AreEqual(KeyStates.Down, keyboardDevice.GetKeyStates(Key.Enter));
            Assert.AreEqual(3, previewKeyDownIndex);
            Assert.AreEqual(4, keyDownIndex);

            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.None, false, 0));
            Assert.AreEqual(KeyStates.None, keyboardDevice.GetKeyStates(Key.Enter));
            Assert.AreEqual(5, previewKeyUpIndex);
            Assert.AreEqual(6, keyUpIndex);

            focus.Dispose();
            Assert.AreEqual(7, previewLostKeyboardFocusIndex);
            Assert.AreEqual(8, lostKeyboardFocusIndex);
            Assert.IsFalse(element.IsKeyboardFocused);

            focus = keyboardDevice.Focus(element);
            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            focus.Dispose();
            Assert.AreEqual(13, previewKeyUpIndex);
            Assert.AreEqual(14, keyUpIndex);
            Assert.AreEqual(15, previewLostKeyboardFocusIndex);
            Assert.AreEqual(16, lostKeyboardFocusIndex);

            keyboardDevice.Focus(element);
            keyboardDevice.ProcessRawEvent(new RawKeyboardEventArgs(Key.Enter, KeyStates.Down, false, 0));
            keyboardDevice.Deactivate();
            Assert.AreEqual(23, previewKeyUpIndex);
            Assert.AreEqual(24, keyUpIndex);
        }