Skip to content

Commit

Permalink
Remove js/wasm events handling from skiko (almost) completely (#864)
Browse files Browse the repository at this point in the history
This is first part of achieving goal of COMPOSE-891
  • Loading branch information
Schahen authored Feb 6, 2024
1 parent 21d7a12 commit f49e746
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 558 deletions.
203 changes: 0 additions & 203 deletions skiko/src/jsMain/kotlin/org/jetbrains/skiko/Convertors.js.kt

This file was deleted.

73 changes: 0 additions & 73 deletions skiko/src/jsMain/kotlin/org/jetbrains/skiko/SkiaLayer.js.kt
Original file line number Diff line number Diff line change
@@ -1,82 +1,9 @@
package org.jetbrains.skiko

import kotlinx.browser.window
import org.jetbrains.skiko.w3c.HTMLCanvasElement
import org.w3c.dom.TouchEvent
import org.w3c.dom.events.KeyboardEvent
import org.w3c.dom.events.MouseEvent
import org.w3c.dom.events.WheelEvent
import org.w3c.dom.events.UIEvent

internal actual fun SkiaLayer.bindCanvasEventsToSkikoView(
canvas: HTMLCanvasElement
) {
val htmlCanvas = canvas.unsafeCast<org.w3c.dom.HTMLCanvasElement>()
var offsetX = 0.0
var offsetY = 0.0
htmlCanvas.addEventListener("touchstart", { event ->
event.preventDefault()
event as TouchEvent
htmlCanvas.getBoundingClientRect().apply {
offsetX = left
offsetY = top
}
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.DOWN, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchmove", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.MOVE, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchend", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("touchcancel", { event ->
event.preventDefault()
event as TouchEvent
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP, offsetX, offsetY))
})
htmlCanvas.addEventListener("mousedown", { event ->
event as MouseEvent
isPointerPressed = true
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.DOWN))
})
htmlCanvas.addEventListener("mouseup", { event ->
event as MouseEvent
isPointerPressed = false
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.UP))
})
htmlCanvas.addEventListener("mousemove", { event ->
event as MouseEvent
if (isPointerPressed) {
skikoView?.onPointerEvent(toSkikoDragEvent(event))
} else {
skikoView?.onPointerEvent(toSkikoEvent(event, SkikoPointerEventKind.MOVE))
}
})
htmlCanvas.addEventListener("wheel", { event ->
event as WheelEvent
skikoView?.onPointerEvent(toSkikoScrollEvent(event))
})
htmlCanvas.addEventListener("contextmenu", { event ->
event.preventDefault()
})
htmlCanvas.addEventListener("keydown", { event ->
event as KeyboardEvent
skikoView?.onKeyboardEvent(toSkikoEvent(event, SkikoKeyboardEventKind.DOWN))

toSkikoTypeEvent(event.key, event)?.let { inputEvent ->
skikoView?.input?.onInputEvent(inputEvent)
}
})
htmlCanvas.addEventListener("keyup", { event ->
event as KeyboardEvent
skikoView?.onKeyboardEvent(toSkikoEvent(event, SkikoKeyboardEventKind.UP))
})
}

actual typealias SkikoGesturePlatformEvent = Any
actual typealias SkikoPlatformInputEvent = KeyboardEvent
actual typealias SkikoPlatformKeyboardEvent = KeyboardEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ actual open class SkiaLayer {
}.apply { initCanvas(desiredWidth, desiredHeight, contentScale, pixelGeometry) }
// See https://www.w3schools.com/jsref/dom_obj_event.asp
// https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events
bindCanvasEventsToSkikoView(htmlCanvas)
}

internal actual fun draw(canvas: Canvas) {
Expand All @@ -115,5 +114,4 @@ actual open class SkiaLayer {
}


internal expect fun SkiaLayer.bindCanvasEventsToSkikoView(canvas: HTMLCanvasElement)
internal expect fun SkiaLayer.setOnChangeScaleNotifier()
Loading

0 comments on commit f49e746

Please sign in to comment.