@@ -13,13 +13,13 @@ import dev.icerock.moko.widgets.core.objc.cgColors
1313import kotlinx.cinterop.useContents
1414import platform.CoreGraphics.CGPointMake
1515import platform.CoreGraphics.CGRectMake
16+ import platform.Foundation.NSRunLoop
17+ import platform.Foundation.NSRunLoopCommonModes
18+ import platform.QuartzCore.CADisplayLink
1619import platform.QuartzCore.CAGradientLayer
1720import platform.QuartzCore.CALayer
1821import platform.QuartzCore.CATransaction
19- import platform.UIKit.UIButton
20- import platform.UIKit.UIColor
21- import platform.UIKit.UIView
22- import platform.UIKit.backgroundColor
22+ import platform.UIKit.*
2323
2424fun Background <out Fill >.caLayer (): CALayer {
2525
@@ -105,19 +105,26 @@ fun UIButton.applyStateBackgroundIfNeeded(background: PressableState<Background<
105105 updateLayers()
106106
107107 // FIXME memoryleak, perfomance problem !!!
108- displayLink {
109- val (width, height) = layer.bounds.useContents { size.width to size.height }
108+ var link: CADisplayLink ? = null
110109
111- CATransaction .begin()
112- CATransaction .setDisableActions(true )
110+ link = displayLink {
111+ if (window != null ) {
112+ val (width, height) = layer.bounds.useContents { size.width to size.height }
113113
114- normalBg.frame = CGRectMake (0.0 , 0.0 , width, height)
115- disabledBg.frame = CGRectMake (0.0 , 0.0 , width, height)
116- pressedBg.frame = CGRectMake (0.0 , 0.0 , width, height)
114+ CATransaction .begin()
115+ CATransaction .setDisableActions(true )
117116
118- updateLayers()
117+ normalBg.frame = CGRectMake (0.0 , 0.0 , width, height)
118+ disabledBg.frame = CGRectMake (0.0 , 0.0 , width, height)
119+ pressedBg.frame = CGRectMake (0.0 , 0.0 , width, height)
119120
120- CATransaction .commit()
121+ updateLayers()
122+
123+ CATransaction .commit()
124+
125+ } else {
126+ link?.removeFromRunLoop(NSRunLoop .currentRunLoop, NSRunLoopCommonModes )
127+ }
121128 }
122129}
123130
@@ -141,14 +148,20 @@ fun UIView.applyBackgroundIfNeeded(background: Background<out Fill>?) {
141148 layer.insertSublayer(bgLayer, 0U )
142149
143150 // FIXME memoryleak, perfomance problem !!!
144- displayLink {
145- val (width, height) = layer.bounds.useContents { size.width to size.height }
151+ var link: CADisplayLink ? = null
146152
147- CATransaction .begin()
148- CATransaction .setDisableActions(true )
153+ link = displayLink {
154+ if (window != null ) {
155+ val (width, height) = layer.bounds.useContents { size.width to size.height }
149156
150- bgLayer.frame = CGRectMake (0.0 , 0.0 , width, height)
157+ CATransaction .begin()
158+ CATransaction .setDisableActions(true )
151159
152- CATransaction .commit()
160+ bgLayer.frame = CGRectMake (0.0 , 0.0 , width, height)
161+
162+ CATransaction .commit()
163+ } else {
164+ link?.removeFromRunLoop(NSRunLoop .currentRunLoop, NSRunLoopCommonModes )
165+ }
153166 }
154167}
0 commit comments