GCD usage example.

This commit is contained in:
Nikolay Igotti
2018-06-01 16:39:09 +03:00
committed by Nikolay Igotti
parent d33afd6007
commit ece39a27d8
+30
View File
@@ -3,6 +3,8 @@ import platform.AppKit.*
import platform.Foundation.* import platform.Foundation.*
import platform.objc.* import platform.objc.*
import platform.osx.* import platform.osx.*
import platform.darwin.*
import platform.posix.*
fun main(args: Array<String>) { fun main(args: Array<String>) {
autoreleasepool { autoreleasepool {
@@ -20,9 +22,29 @@ private fun runApp() {
app.run() app.run()
} }
data class Data(val stamp: Long)
private class Controller : NSObject() {
private val asyncQueue = dispatch_queue_create("com.jetbrains.CustomQueue", null)
@ObjCAction
fun onClick() {
// Execute some async action on button click.
dispatch_async_f(asyncQueue, konan.worker.detachObjectGraph {
Data(clock_gettime_nsec_np(CLOCK_REALTIME))
}, staticCFunction {
it ->
initRuntimeIfNeeded()
val data = konan.worker.attachObjectGraph<Data>(it)
println("in async: $data")
})
}
}
private class MyAppDelegate() : NSObject(), NSApplicationDelegateProtocol { private class MyAppDelegate() : NSObject(), NSApplicationDelegateProtocol {
private val window: NSWindow private val window: NSWindow
private val controller = Controller()
init { init {
val mainDisplayRect = NSScreen.mainScreen()!!.frame val mainDisplayRect = NSScreen.mainScreen()!!.frame
@@ -33,6 +55,7 @@ private class MyAppDelegate() : NSObject(), NSApplicationDelegateProtocol {
size.width * 0.5, size.width * 0.5,
size.height * 0.5 size.height * 0.5
) )
} }
val windowStyle = NSWindowStyleMaskTitled or NSWindowStyleMaskMiniaturizable or val windowStyle = NSWindowStyleMaskTitled or NSWindowStyleMaskMiniaturizable or
@@ -54,6 +77,13 @@ private class MyAppDelegate() : NSObject(), NSApplicationDelegateProtocol {
} }
} }
} }
val button = NSButton().initWithFrame(NSMakeRect(10.0, 10.0, 100.0, 40.0)).apply {
title = "Press me"
target = controller
action = NSSelectorFromString("onClick")
}
window.contentView!!.addSubview(button)
} }
override fun applicationWillFinishLaunching(notification: NSNotification) { override fun applicationWillFinishLaunching(notification: NSNotification) {