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