Unverified Commit 46716ddb authored by Roman Sedaikin's avatar Roman Sedaikin Committed by GitHub

Added DPI support on macos-native. Fixed closing application on macos-native. (#298)

parent 7b51fa16
...@@ -6,11 +6,17 @@ import org.jetbrains.skia.* ...@@ -6,11 +6,17 @@ import org.jetbrains.skia.*
import org.jetbrains.skiko.* import org.jetbrains.skiko.*
import kotlinx.cinterop.* import kotlinx.cinterop.*
import platform.Foundation.NSMakeRect import platform.Foundation.NSMakeRect
import platform.darwin.NSObject
fun makeApp() = BouncingBalls(true) fun makeApp() = Clocks()
fun main() { fun main() {
NSApplication.sharedApplication() val app = NSApplication.sharedApplication()
app.delegate = object: NSObject(), NSApplicationDelegateProtocol {
override fun applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication): Boolean {
return true
}
}
val windowStyle = NSWindowStyleMaskTitled or val windowStyle = NSWindowStyleMaskTitled or
NSWindowStyleMaskMiniaturizable or NSWindowStyleMaskMiniaturizable or
NSWindowStyleMaskClosable or NSWindowStyleMaskClosable or
...@@ -24,5 +30,5 @@ fun main() { ...@@ -24,5 +30,5 @@ fun main() {
skiaLayer.skikoView = GenericSkikoView(skiaLayer, makeApp()) skiaLayer.skikoView = GenericSkikoView(skiaLayer, makeApp())
skiaLayer.attachTo(window) skiaLayer.attachTo(window)
window.orderFrontRegardless() window.orderFrontRegardless()
NSApp?.run() app.run()
} }
...@@ -7,17 +7,19 @@ import org.jetbrains.skia.* ...@@ -7,17 +7,19 @@ import org.jetbrains.skia.*
import org.jetbrains.skiko.redrawer.Redrawer import org.jetbrains.skiko.redrawer.Redrawer
import platform.AppKit.* import platform.AppKit.*
import platform.Foundation.NSMakeRect import platform.Foundation.NSMakeRect
import platform.Foundation.NSNotification
import platform.Foundation.NSNotificationCenter import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSSelectorFromString import platform.Foundation.NSSelectorFromString
import platform.Foundation.addObserver import platform.Foundation.addObserver
import platform.darwin.NSObject import platform.darwin.NSObject
import platform.CoreGraphics.CGRectMake
actual open class SkiaLayer( actual open class SkiaLayer(
private val properties: SkiaLayerProperties = makeDefaultSkiaLayerProperties() private val properties: SkiaLayerProperties = makeDefaultSkiaLayerProperties()
) { ) {
actual var renderApi: GraphicsApi = GraphicsApi.OPENGL actual var renderApi: GraphicsApi = GraphicsApi.OPENGL
actual val contentScale: Float actual val contentScale: Float
get() = _contentScale get() = if (this::nsView.isInitialized) nsView.window!!.backingScaleFactor.toFloat() else 1.0f
actual var fullscreen: Boolean actual var fullscreen: Boolean
get() = false get() = false
...@@ -32,7 +34,6 @@ actual open class SkiaLayer( ...@@ -32,7 +34,6 @@ actual open class SkiaLayer(
} }
lateinit var nsView: NSView lateinit var nsView: NSView
var _contentScale: Float = 1.0f
actual var skikoView: SkikoView? = null actual var skikoView: SkikoView? = null
...@@ -123,8 +124,26 @@ actual open class SkiaLayer( ...@@ -123,8 +124,26 @@ actual open class SkiaLayer(
val center = NSNotificationCenter.defaultCenter() val center = NSNotificationCenter.defaultCenter()
center.addObserver(nsView, NSSelectorFromString("onWindowClose:"), center.addObserver(nsView, NSSelectorFromString("onWindowClose:"),
NSWindowWillCloseNotification!!, window) NSWindowWillCloseNotification!!, window)
window.delegate = object : NSObject(), NSWindowDelegateProtocol {
override fun windowDidResize(notification: NSNotification) {
val (w, h) = window.contentView!!.frame.useContents {
size.width to size.height
}
nsView.frame = CGRectMake(0.0, 0.0, w, h)
redrawer?.syncSize()
initedCanvas = false
redrawer?.redrawImmediately()
}
override fun windowDidChangeBackingProperties(notification: NSNotification) {
redrawer?.syncSize()
initedCanvas = false
redrawer?.redrawImmediately()
}
}
window.contentView!!.addSubview(nsView) window.contentView!!.addSubview(nsView)
redrawer = createNativeRedrawer(this, GraphicsApi.OPENGL, properties).apply { redrawer = createNativeRedrawer(this, GraphicsApi.OPENGL, properties).apply {
syncSize()
needRedraw() needRedraw()
} }
} }
......
...@@ -20,7 +20,11 @@ internal class MacOsOpenGLRedrawer( ...@@ -20,7 +20,11 @@ internal class MacOsOpenGLRedrawer(
private val skiaLayer: SkiaLayer, private val skiaLayer: SkiaLayer,
private val properties: SkiaLayerProperties private val properties: SkiaLayerProperties
) : Redrawer { ) : Redrawer {
private val glLayer = MacosGLLayer(skiaLayer) private val glLayer = MacosGLLayer()
init {
glLayer.init(skiaLayer)
}
private val frameDispatcher = FrameDispatcher(SkikoDispatchers.Main) { private val frameDispatcher = FrameDispatcher(SkikoDispatchers.Main) {
redrawImmediately() redrawImmediately()
...@@ -31,7 +35,7 @@ internal class MacOsOpenGLRedrawer( ...@@ -31,7 +35,7 @@ internal class MacOsOpenGLRedrawer(
} }
override fun syncSize() { override fun syncSize() {
// TODO: What do we really do here? syncContentScale()
skiaLayer.nsView.frame.useContents { skiaLayer.nsView.frame.useContents {
glLayer.setFrame( glLayer.setFrame(
origin.x.toInt(), origin.x.toInt(),
...@@ -42,6 +46,14 @@ internal class MacOsOpenGLRedrawer( ...@@ -42,6 +46,14 @@ internal class MacOsOpenGLRedrawer(
} }
} }
private fun syncContentScale() {
CATransaction.begin()
CATransaction.setDisableActions(true)
glLayer.contentsScale = skiaLayer.nsView.window!!.backingScaleFactor
CATransaction.commit()
CATransaction.flush()
}
override fun needRedraw() { override fun needRedraw() {
frameDispatcher.scheduleFrame() frameDispatcher.scheduleFrame()
} }
...@@ -52,22 +64,29 @@ internal class MacOsOpenGLRedrawer( ...@@ -52,22 +64,29 @@ internal class MacOsOpenGLRedrawer(
} }
} }
internal class MacosGLLayer(val layer: SkiaLayer) : CAOpenGLLayer() { internal class MacosGLLayer : CAOpenGLLayer {
init { private lateinit var layer: SkiaLayer
@OverrideInit
constructor(): super()
@OverrideInit
constructor(layer: Any): super(layer)
fun init(layer: SkiaLayer) {
this.layer = layer
this.setNeedsDisplayOnBoundsChange(true) this.setNeedsDisplayOnBoundsChange(true)
this.removeAllAnimations() this.removeAllAnimations()
this.setAutoresizingMask(kCALayerWidthSizable or kCALayerHeightSizable ) this.setAutoresizingMask(kCALayerWidthSizable or kCALayerHeightSizable )
layer.nsView.layer = this layer.nsView.layer = this
layer.nsView.wantsLayer = true layer.nsView.wantsLayer = true
this.contentsGravity = kCAGravityTopLeft;
} }
fun setFrame(x: Int, y: Int, width: Int, height: Int) { fun setFrame(x: Int, y: Int, width: Int, height: Int) {
val newY = layer.nsView.frame.useContents { size.height } - y - height
CATransaction.begin() CATransaction.begin()
CATransaction.setDisableActions(true) CATransaction.setDisableActions(true)
this.frame = CGRectMake(x.toDouble(), newY, width.toDouble(), height.toDouble()) this.frame = CGRectMake(x.toDouble(), y.toDouble(), width.toDouble(), height.toDouble())
CATransaction.commit() CATransaction.commit()
CATransaction.flush()
} }
fun dispose() { fun dispose() {
......
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