Browse Source
* web: Use SyntheticMouseEvent as a type for a value in mouse events listeners * add tests for mouse event Co-authored-by: Oleksandr Karpovich <oleksandr.karpovich@jetbrains.com>pull/893/head
Oleksandr Karpovich
3 years ago
committed by
GitHub
8 changed files with 360 additions and 104 deletions
@ -0,0 +1,24 @@
|
||||
package androidx.compose.web.events |
||||
|
||||
import org.w3c.dom.events.Event |
||||
import org.w3c.dom.events.EventTarget |
||||
|
||||
open class SyntheticEvent<Element : EventTarget>( |
||||
val nativeEvent: Event |
||||
) { |
||||
val target: Element = nativeEvent.target.unsafeCast<Element>() |
||||
val bubbles: Boolean = nativeEvent.bubbles |
||||
val cancelable: Boolean = nativeEvent.cancelable |
||||
val composed: Boolean = nativeEvent.composed |
||||
val currentTarget: EventTarget? = nativeEvent.currentTarget |
||||
val eventPhase: Short = nativeEvent.eventPhase |
||||
val defaultPrevented: Boolean = nativeEvent.defaultPrevented |
||||
val timestamp: Number = nativeEvent.timeStamp |
||||
val type: String = nativeEvent.type |
||||
val isTrusted: Boolean = nativeEvent.isTrusted |
||||
|
||||
fun preventDefault(): Unit = nativeEvent.preventDefault() |
||||
fun stopPropagation(): Unit = nativeEvent.stopPropagation() |
||||
fun stopImmediatePropagation(): Unit = nativeEvent.stopImmediatePropagation() |
||||
fun composedPath(): Array<EventTarget> = nativeEvent.composedPath() |
||||
} |
@ -0,0 +1,67 @@
|
||||
package androidx.compose.web.events |
||||
|
||||
import org.w3c.dom.DataTransfer |
||||
import org.w3c.dom.DragEvent |
||||
import org.w3c.dom.HTMLElement |
||||
import org.w3c.dom.events.EventTarget |
||||
import org.w3c.dom.events.MouseEvent |
||||
import org.w3c.dom.events.WheelEvent |
||||
|
||||
/** |
||||
* https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent |
||||
*/ |
||||
open class SyntheticMouseEvent( |
||||
nativeEvent: MouseEvent |
||||
) : SyntheticEvent<EventTarget>(nativeEvent) { |
||||
|
||||
private val mouseEvent = nativeEvent |
||||
|
||||
val altKey: Boolean = nativeEvent.altKey |
||||
val button: Short = nativeEvent.button |
||||
val buttons: Short = nativeEvent.buttons |
||||
val clientX: Int = nativeEvent.clientX |
||||
val clientY: Int = nativeEvent.clientY |
||||
val ctrlKey: Boolean = nativeEvent.ctrlKey |
||||
val metaKey: Boolean = nativeEvent.metaKey |
||||
val movementX: Int = nativeEvent.asDynamic().movementX as Int |
||||
val movementY: Int = nativeEvent.asDynamic().movementY as Int |
||||
val offsetX: Double = nativeEvent.offsetX |
||||
val offsetY: Double = nativeEvent.offsetY |
||||
val pageX: Double = nativeEvent.pageX |
||||
val pageY: Double = nativeEvent.pageY |
||||
val region: String? = nativeEvent.region |
||||
val relatedTarget: EventTarget? = nativeEvent.relatedTarget |
||||
val screenX: Int = nativeEvent.screenX |
||||
val screenY: Int = nativeEvent.screenY |
||||
val shiftKey: Boolean = nativeEvent.shiftKey |
||||
val x: Double = nativeEvent.x |
||||
val y: Double = nativeEvent.y |
||||
|
||||
fun getModifierState(keyArg: String): Boolean = mouseEvent.getModifierState(keyArg) |
||||
} |
||||
|
||||
|
||||
/** |
||||
* https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent |
||||
*/ |
||||
class SyntheticWheelEvent( |
||||
nativeEvent: WheelEvent |
||||
) : SyntheticMouseEvent( |
||||
nativeEvent |
||||
) { |
||||
val deltaX: Double = nativeEvent.deltaX |
||||
val deltaY: Double = nativeEvent.deltaY |
||||
val deltaZ: Double = nativeEvent.deltaZ |
||||
val deltaMode: Int = nativeEvent.deltaMode |
||||
} |
||||
|
||||
/** |
||||
* https://developer.mozilla.org/en-US/docs/Web/API/DragEvent |
||||
*/ |
||||
class SyntheticDragEvent( |
||||
nativeEvent: DragEvent |
||||
) : SyntheticMouseEvent( |
||||
nativeEvent |
||||
) { |
||||
val dataTransfer: DataTransfer? = nativeEvent.dataTransfer |
||||
} |
Loading…
Reference in new issue