Unverified Commit 83394aa1 authored by Roman Sedaikin's avatar Roman Sedaikin Committed by GitHub

Added support to show/hide screen keyboard. Refactored [SkikoUIViewController]. (#455)

parent d3979724
...@@ -6,6 +6,7 @@ import org.jetbrains.skiko.GenericSkikoView ...@@ -6,6 +6,7 @@ import org.jetbrains.skiko.GenericSkikoView
import org.jetbrains.skiko.SkiaLayer import org.jetbrains.skiko.SkiaLayer
import org.jetbrains.skiko.SkikoView import org.jetbrains.skiko.SkikoView
import org.jetbrains.skiko.SkikoViewController import org.jetbrains.skiko.SkikoViewController
import org.jetbrains.skiko.SkikoUIView
import org.jetbrains.skiko.SkikoGestureEventKind import org.jetbrains.skiko.SkikoGestureEventKind
import platform.UIKit.* import platform.UIKit.*
import platform.Foundation.* import platform.Foundation.*
...@@ -37,13 +38,13 @@ class SkikoAppDelegate : UIResponder, UIApplicationDelegateProtocol { ...@@ -37,13 +38,13 @@ class SkikoAppDelegate : UIResponder, UIApplicationDelegateProtocol {
override fun application(application: UIApplication, didFinishLaunchingWithOptions: Map<Any?, *>?): Boolean { override fun application(application: UIApplication, didFinishLaunchingWithOptions: Map<Any?, *>?): Boolean {
window = UIWindow(frame = UIScreen.mainScreen.bounds) window = UIWindow(frame = UIScreen.mainScreen.bounds)
window!!.rootViewController = SkikoViewController(SkikoGestureEventKind.values()).apply { window!!.rootViewController = SkikoViewController(
setAppFactory { layer -> SkikoUIView(
GenericSkikoView(layer, makeApp(layer)).also { SkiaLayer(SkikoGestureEventKind.values()).apply {
layer.skikoView = it skikoView = GenericSkikoView(this, makeApp(this))
}
}
} }
)
)
window!!.makeKeyAndVisible() window!!.makeKeyAndVisible()
return true return true
} }
......
...@@ -24,6 +24,16 @@ actual open class SkiaLayer { ...@@ -24,6 +24,16 @@ actual open class SkiaLayer {
return true return true
} }
fun showScreenKeyboard() {
view?.becomeFirstResponder()
}
fun hideScreenKeyboard() { view?.resignFirstResponder() }
fun isScreenKeyboardOpen(): Boolean {
return if (view == null) false else view!!.isFirstResponder
}
actual var renderApi: GraphicsApi actual var renderApi: GraphicsApi
get() = GraphicsApi.METAL get() = GraphicsApi.METAL
set(value) { throw UnsupportedOperationException() } set(value) { throw UnsupportedOperationException() }
......
package org.jetbrains.skiko package org.jetbrains.skiko
import kotlinx.cinterop.CValue
import kotlinx.cinterop.ExportObjCClass import kotlinx.cinterop.ExportObjCClass
import kotlinx.cinterop.useContents import kotlinx.cinterop.useContents
import platform.CoreGraphics.CGRect
import platform.CoreGraphics.CGRectMake import platform.CoreGraphics.CGRectMake
import platform.Foundation.NSCoder import platform.Foundation.NSCoder
import platform.UIKit.UIEvent import platform.UIKit.UIEvent
import platform.UIKit.UITouch import platform.UIKit.UITouch
import platform.UIKit.UIScreen import platform.UIKit.UIScreen
import platform.UIKit.UIView
import platform.UIKit.UIViewController import platform.UIKit.UIViewController
import platform.UIKit.setFrame import platform.UIKit.setFrame
import platform.UIKit.contentScaleFactor import platform.UIKit.contentScaleFactor
...@@ -15,22 +18,63 @@ import platform.UIKit.UIPress ...@@ -15,22 +18,63 @@ import platform.UIKit.UIPress
import platform.UIKit.UIPressesEvent import platform.UIKit.UIPressesEvent
@ExportObjCClass @ExportObjCClass
class SkikoViewController : UIViewController, UIKeyInputProtocol { class SkikoViewController : UIViewController {
private var gestures: Array<SkikoGestureEventKind>? = null
@OverrideInit @OverrideInit
constructor() : super(nibName = null, bundle = null) constructor() : super(nibName = null, bundle = null)
@OverrideInit
constructor(coder: NSCoder) : super(coder)
constructor(skikoUIView: SkikoUIView) : this() {
this.skikoUIView = skikoUIView
}
private var skikoUIView: SkikoUIView? = null
override fun loadView() {
if (skikoUIView == null) {
super.loadView()
} else {
this.view = skikoUIView!!.load()
}
}
// viewDidUnload() is deprecated and not called.
override fun viewDidDisappear(animated: Boolean) {
skikoUIView?.detach()
}
}
@ExportObjCClass
class SkikoUIView : UIView, UIKeyInputProtocol {
@OverrideInit
constructor(frame: CValue<CGRect>) : super(frame)
@OverrideInit @OverrideInit
constructor(coder: NSCoder) : super(coder) constructor(coder: NSCoder) : super(coder)
constructor(gestures: Array<SkikoGestureEventKind>? = null) : this() { private var skiaLayer: SkiaLayer? = null
this.gestures = gestures
constructor(skiaLayer: SkiaLayer, frame: CValue<CGRect> = CGRectMake(0.0, 0.0, 1.0, 1.0)) : super(frame) {
this.skiaLayer = skiaLayer
} }
override fun canBecomeFirstResponder() = true fun detach() = skiaLayer?.detach()
fun load(): SkikoUIView {
val (width, height) = UIScreen.mainScreen.bounds.useContents {
this.size.width to this.size.height
}
setFrame(CGRectMake(0.0, 0.0, width, height))
contentScaleFactor = UIScreen.mainScreen.scale
skiaLayer?.attachTo(this)
return this
}
fun showScreenKeyboard() = becomeFirstResponder()
fun hideScreenKeyboard() = resignFirstResponder()
fun isScreenKeyboardOpen() = isFirstResponder
private var keyEvent: UIPress? = null var keyEvent: UIPress? = null
private var inputText: String = "" private var inputText: String = ""
override fun hasText(): Boolean { override fun hasText(): Boolean {
return inputText.length > 0 return inputText.length > 0
...@@ -38,18 +82,20 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -38,18 +82,20 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
override fun insertText(theText: String) { override fun insertText(theText: String) {
inputText += theText inputText += theText
skikoLayer.skikoView?.onInputEvent(toSkikoTypeEvent(theText, keyEvent)) skiaLayer?.skikoView?.onInputEvent(toSkikoTypeEvent(theText, keyEvent))
} }
override fun deleteBackward() { override fun deleteBackward() {
inputText = inputText.dropLast(1) inputText = inputText.dropLast(1)
} }
override fun canBecomeFirstResponder() = true
override fun pressesBegan(presses: Set<*>, withEvent: UIPressesEvent?) { override fun pressesBegan(presses: Set<*>, withEvent: UIPressesEvent?) {
if (withEvent != null) { if (withEvent != null) {
for (press in withEvent.allPresses) { for (press in withEvent.allPresses) {
keyEvent = press as UIPress keyEvent = press as UIPress
skikoLayer.skikoView?.onKeyboardEvent( skiaLayer?.skikoView?.onKeyboardEvent(
toSkikoKeyboardEvent(press, SkikoKeyboardEventKind.DOWN) toSkikoKeyboardEvent(press, SkikoKeyboardEventKind.DOWN)
) )
} }
...@@ -61,7 +107,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -61,7 +107,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
if (withEvent != null) { if (withEvent != null) {
for (press in withEvent.allPresses) { for (press in withEvent.allPresses) {
keyEvent = press as UIPress keyEvent = press as UIPress
skikoLayer.skikoView?.onKeyboardEvent( skiaLayer?.skikoView?.onKeyboardEvent(
toSkikoKeyboardEvent(press, SkikoKeyboardEventKind.UP) toSkikoKeyboardEvent(press, SkikoKeyboardEventKind.UP)
) )
} }
...@@ -80,7 +126,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -80,7 +126,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
SkikoTouchEvent(x, y, SkikoTouchEventKind.STARTED, timestamp, event) SkikoTouchEvent(x, y, SkikoTouchEventKind.STARTED, timestamp, event)
) )
} }
skikoLayer.skikoView?.onTouchEvent(events.toTypedArray()) skiaLayer?.skikoView?.onTouchEvent(events.toTypedArray())
} }
override fun touchesEnded(touches: Set<*>, withEvent: UIEvent?) { override fun touchesEnded(touches: Set<*>, withEvent: UIEvent?) {
...@@ -94,7 +140,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -94,7 +140,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
SkikoTouchEvent(x, y, SkikoTouchEventKind.ENDED, timestamp, event) SkikoTouchEvent(x, y, SkikoTouchEventKind.ENDED, timestamp, event)
) )
} }
skikoLayer.skikoView?.onTouchEvent(events.toTypedArray()) skiaLayer?.skikoView?.onTouchEvent(events.toTypedArray())
} }
override fun touchesMoved(touches: Set<*>, withEvent: UIEvent?) { override fun touchesMoved(touches: Set<*>, withEvent: UIEvent?) {
...@@ -108,7 +154,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -108,7 +154,7 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
SkikoTouchEvent(x, y, SkikoTouchEventKind.MOVED, timestamp, event) SkikoTouchEvent(x, y, SkikoTouchEventKind.MOVED, timestamp, event)
) )
} }
skikoLayer.skikoView?.onTouchEvent(events.toTypedArray()) skiaLayer?.skikoView?.onTouchEvent(events.toTypedArray())
} }
override fun touchesCancelled(touches: Set<*>, withEvent: UIEvent?) { override fun touchesCancelled(touches: Set<*>, withEvent: UIEvent?) {
...@@ -122,31 +168,6 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol { ...@@ -122,31 +168,6 @@ class SkikoViewController : UIViewController, UIKeyInputProtocol {
SkikoTouchEvent(x, y, SkikoTouchEventKind.CANCELLED, timestamp, event) SkikoTouchEvent(x, y, SkikoTouchEventKind.CANCELLED, timestamp, event)
) )
} }
skikoLayer.skikoView?.onTouchEvent(events.toTypedArray()) skiaLayer?.skikoView?.onTouchEvent(events.toTypedArray())
}
internal lateinit var appFactory: (SkiaLayer) -> SkikoView
fun setAppFactory(appFactory: (SkiaLayer) -> SkikoView) {
this.appFactory = appFactory
}
private lateinit var skikoLayer: SkiaLayer
override fun viewDidLoad() {
super.viewDidLoad()
val (width, height) = UIScreen.mainScreen.bounds.useContents {
this.size.width to this.size.height
}
skikoLayer = SkiaLayer(gestures).apply {
skikoView = appFactory(this)
}
view.contentScaleFactor = UIScreen.mainScreen.scale
view.setFrame(CGRectMake(0.0, 0.0, width, height))
skikoLayer.attachTo(this.view)
}
// viewDidUnload() is deprecated and not called.
override fun viewDidDisappear(animated: Boolean) {
skikoLayer.detach()
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment