Skip to content

Commit

Permalink
UIKit interop touches (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-avdeev-jb authored Mar 27, 2023
1 parent f21b197 commit db11bf1
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
7 changes: 6 additions & 1 deletion skiko/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ build configuration.

## Publishing

##### Publish to Maven local
##### Publish JVM target to Maven local
```
./gradlew publishToMavenLocal
```

##### Publish all targets to Maven Local
```
./gradlew publishToMavenLocal -Pskiko.native.enabled=true -Pskiko.wasm.enabled=true -Pskiko.android.enabled=true
```

##### Publish to `build/repo` directory
```
./gradlew publishToBuildRepo
Expand Down
8 changes: 8 additions & 0 deletions skiko/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ fun configureNativeTarget(os: OS, arch: Arch, target: KotlinNativeTarget) {
val allLibraries = skiaStaticLibraries(skiaDir, targetString) + bridgesLibrary


if (os == OS.IOS) {
target.compilations.getByName("main") {
val uikit by cinterops.creating {
defFile("src/iosMain/objc/ios.def")
packageName("org.jetbrains.skiko.objc")
}
}
}
val skiaBinDir = "$skiaDir/out/${buildType.id}-$targetString"
val linkerFlags = when (os) {
OS.MacOS -> mutableListOf("-linker-option", "-framework", "-linker-option", "Metal",
Expand Down
3 changes: 3 additions & 0 deletions skiko/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ dependencies.skia.android-arm64=m110-ad42464-1
org.gradle.jvmargs=-Xmx3G -XX:MaxMetaspaceSize=512m
kotlin.native.cacheKind.macosX64=none
kotlin.native.cacheKind.iosX64=none

# This line needs for maven publication with ObjC defFile. Should be removed after Kotlin 1.8.20
kotlin.mpp.enableCInteropCommonization=true
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.skiko.context.MetalContextHandler
import org.jetbrains.skiko.redrawer.MetalRedrawer
import platform.UIKit.*
import kotlin.system.getTimeNanos
import org.jetbrains.skia.*

actual open class SkiaLayer {

Expand Down
24 changes: 22 additions & 2 deletions skiko/src/iosMain/kotlin/org/jetbrains/skiko/SkikoUIView.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetbrains.skiko

import kotlinx.cinterop.*
import org.jetbrains.skia.Point
import org.jetbrains.skia.Rect
import platform.CoreGraphics.*
import platform.Foundation.*
Expand All @@ -9,16 +10,22 @@ import platform.darwin.NSInteger
import kotlin.math.max
import kotlin.math.min

/*
TODO: remove org.jetbrains.skiko.objc.UIViewExtensionProtocol after Kotlin 1.8.20
https://youtrack.jetbrains.com/issue/KT-40426
*/
@Suppress("CONFLICTING_OVERLOADS")
@ExportObjCClass
class SkikoUIView : UIView, UIKeyInputProtocol, UITextInputProtocol {
class SkikoUIView : UIView, UIKeyInputProtocol, UITextInputProtocol,
org.jetbrains.skiko.objc.UIViewExtensionProtocol {
@OverrideInit
constructor(frame: CValue<CGRect>) : super(frame)

@OverrideInit
constructor(coder: NSCoder) : super(coder)

private var skiaLayer: SkiaLayer? = null
private lateinit var _pointInside: (Point, UIEvent?) -> Boolean
private var _inputDelegate: UITextInputDelegateProtocol? = null
private var _currentTextMenuActions: TextActions? = null
var currentKeyboardType: UIKeyboardType = UIKeyboardTypeDefault
Expand All @@ -30,8 +37,13 @@ class SkikoUIView : UIView, UIKeyInputProtocol, UITextInputProtocol {
var currentAutocapitalizationType: UITextAutocapitalizationType = UITextAutocapitalizationType.UITextAutocapitalizationTypeSentences
var currentAutocorrectionType: UITextAutocorrectionType = UITextAutocorrectionType.UITextAutocorrectionTypeYes

constructor(skiaLayer: SkiaLayer, frame: CValue<CGRect> = CGRectNull.readValue()) : super(frame) {
constructor(
skiaLayer: SkiaLayer,
frame: CValue<CGRect> = CGRectNull.readValue(),
pointInside: (Point, UIEvent?) -> Boolean = {_,_-> true }
) : super(frame) {
this.skiaLayer = skiaLayer
_pointInside = pointInside
}

/**
Expand Down Expand Up @@ -157,6 +169,14 @@ class SkikoUIView : UIView, UIKeyInputProtocol, UITextInputProtocol {
super.pressesEnded(presses, withEvent)
}

/**
* https://developer.apple.com/documentation/uikit/uiview/1622533-point
*/
override fun pointInside(point: CValue<CGPoint>, withEvent: UIEvent?): Boolean {
val skiaPoint: Point = point.useContents { Point(x.toFloat(), y.toFloat()) }
return _pointInside(skiaPoint, withEvent)
}

override fun touchesBegan(touches: Set<*>, withEvent: UIEvent?) {
super.touchesBegan(touches, withEvent)
sendTouchEventToSkikoView(touches, SkikoTouchEventKind.STARTED)
Expand Down
11 changes: 11 additions & 0 deletions skiko/src/iosMain/objc/ios.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package = org.jetbrains.skiko.objc
language = Objective-C
---

#import <Foundation/Foundation.h>
#import <UIKit/UIView.h>

// This protocol helps to override ObjC UIView extensions in Kotlin
@protocol UIViewExtension
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event;
@end

0 comments on commit db11bf1

Please sign in to comment.