Ejemplo n.º 1
0
    public void Read_Works()
    {
        // Arrange
        var args = new PointerEventArgs
        {
            AltKey      = false,
            Button      = 72,
            Buttons     = 61,
            ClientX     = 3.1,
            ClientY     = 4.2,
            CtrlKey     = true,
            PointerId   = 17,
            PointerType = "pointer1",
            Width       = 90.1f,
            Height      = 43.87f,
            IsPrimary   = true,
            Pressure    = 0.8f,
            TiltX       = 1.1f,
            TiltY       = 9.2f,
            Type        = "type2",
            Detail      = 7,
            MetaKey     = true,
            OffsetX     = 8.2,
            OffsetY     = 7.1,
            PageX       = 5.6,
            PageY       = 7.8,
            ScreenX     = 0.1,
            ScreenY     = 4.4,
            ShiftKey    = true,
        };
        var jsonElement = GetJsonElement(args);

        // Act
        var result = PointerEventArgsReader.Read(jsonElement);

        // Assert
        MouseEventArgsReaderTest.AssertEqual(args, result);
        Assert.Equal(args.PointerId, result.PointerId);
        Assert.Equal(args.PointerType, result.PointerType);
        Assert.Equal(args.Width, result.Width);
        Assert.Equal(args.Height, result.Height);
        Assert.Equal(args.IsPrimary, result.IsPrimary);
        Assert.Equal(args.Pressure, result.Pressure);
        Assert.Equal(args.TiltX, result.TiltX);
        Assert.Equal(args.TiltY, result.TiltY);
    }
Ejemplo n.º 2
0
    private static bool TryDeserializeStandardWebEventArgs(
        string eventName,
        JsonElement eventArgsJson,
        [NotNullWhen(true)] out EventArgs?eventArgs)
    {
        // For back-compatibility, we recognize the built-in list of web event names and hard-code
        // rules about the deserialization type for their eventargs. This makes it possible to declare
        // an event handler as receiving EventArgs, and have it actually receive a subclass at runtime
        // depending on the event that was raised.
        //
        // The following list should remain in sync with EventArgsFactory.ts.

        switch (eventName)
        {
        case "input":
        case "change":
            // Special case for ChangeEventArgs because its value type can be one of
            // several types, and System.Text.Json doesn't pick types dynamically
            eventArgs = ChangeEventArgsReader.Read(eventArgsJson);
            return(true);

        case "copy":
        case "cut":
        case "paste":
            eventArgs = ClipboardEventArgsReader.Read(eventArgsJson);
            return(true);

        case "drag":
        case "dragend":
        case "dragenter":
        case "dragleave":
        case "dragover":
        case "dragstart":
        case "drop":
            eventArgs = DragEventArgsReader.Read(eventArgsJson);
            return(true);

        case "focus":
        case "blur":
        case "focusin":
        case "focusout":
            eventArgs = FocusEventArgsReader.Read(eventArgsJson);
            return(true);

        case "keydown":
        case "keyup":
        case "keypress":
            eventArgs = KeyboardEventArgsReader.Read(eventArgsJson);
            return(true);

        case "contextmenu":
        case "click":
        case "mouseover":
        case "mouseout":
        case "mousemove":
        case "mousedown":
        case "mouseup":
        case "dblclick":
            eventArgs = MouseEventArgsReader.Read(eventArgsJson);
            return(true);

        case "error":
            eventArgs = ErrorEventArgsReader.Read(eventArgsJson);
            return(true);

        case "loadstart":
        case "timeout":
        case "abort":
        case "load":
        case "loadend":
        case "progress":
            eventArgs = ProgressEventArgsReader.Read(eventArgsJson);
            return(true);

        case "touchcancel":
        case "touchend":
        case "touchmove":
        case "touchenter":
        case "touchleave":
        case "touchstart":
            eventArgs = TouchEventArgsReader.Read(eventArgsJson);
            return(true);

        case "gotpointercapture":
        case "lostpointercapture":
        case "pointercancel":
        case "pointerdown":
        case "pointerenter":
        case "pointerleave":
        case "pointermove":
        case "pointerout":
        case "pointerover":
        case "pointerup":
            eventArgs = PointerEventArgsReader.Read(eventArgsJson);
            return(true);

        case "wheel":
        case "mousewheel":
            eventArgs = WheelEventArgsReader.Read(eventArgsJson);
            return(true);

        case "toggle":
            eventArgs = EventArgs.Empty;
            return(true);

        default:
            // For custom event types, there are no built-in rules, so the deserialization type is
            // determined by the parameter declared on the delegate.
            eventArgs = null;
            return(false);
        }
    }