@@ -14,13 +14,13 @@ import dev.icerock.moko.widgets.core.style.background.Corner
1414import kotlinx.cinterop.useContents
1515import platform.CoreGraphics.CGPointMake
1616import platform.CoreGraphics.CGRectMake
17+ import platform.Foundation.NSRunLoop
18+ import platform.Foundation.NSRunLoopCommonModes
19+ import platform.QuartzCore.CADisplayLink
1720import platform.QuartzCore.CAGradientLayer
1821import platform.QuartzCore.CALayer
1922import platform.QuartzCore.CATransaction
20- import platform.UIKit.UIButton
21- import platform.UIKit.UIColor
22- import platform.UIKit.UIView
23- import platform.UIKit.backgroundColor
23+ import platform.UIKit.*
2424
2525fun Background <out Fill >.caLayer (): CALayer {
2626
@@ -110,19 +110,26 @@ fun UIButton.applyStateBackgroundIfNeeded(background: PressableState<Background<
110110 updateLayers()
111111
112112 // FIXME memoryleak, perfomance problem !!!
113- displayLink {
114- val (width, height) = layer.bounds.useContents { size.width to size.height }
113+ var link: CADisplayLink ? = null
115114
116- CATransaction .begin()
117- CATransaction .setDisableActions(true )
115+ link = displayLink {
116+ if (window != null ) {
117+ val (width, height) = layer.bounds.useContents { size.width to size.height }
118118
119- normalBg.frame = CGRectMake (0.0 , 0.0 , width, height)
120- disabledBg.frame = CGRectMake (0.0 , 0.0 , width, height)
121- pressedBg.frame = CGRectMake (0.0 , 0.0 , width, height)
119+ CATransaction .begin()
120+ CATransaction .setDisableActions(true )
122121
123- updateLayers()
122+ normalBg.frame = CGRectMake (0.0 , 0.0 , width, height)
123+ disabledBg.frame = CGRectMake (0.0 , 0.0 , width, height)
124+ pressedBg.frame = CGRectMake (0.0 , 0.0 , width, height)
124125
125- CATransaction .commit()
126+ updateLayers()
127+
128+ CATransaction .commit()
129+
130+ } else {
131+ link?.removeFromRunLoop(NSRunLoop .currentRunLoop, NSRunLoopCommonModes )
132+ }
126133 }
127134}
128135
@@ -149,14 +156,20 @@ fun UIView.applyBackgroundIfNeeded(background: Background<out Fill>?) {
149156 layer.insertSublayer(bgLayer, 0U )
150157
151158 // FIXME memoryleak, perfomance problem !!!
152- displayLink {
153- val (width, height) = layer.bounds.useContents { size.width to size.height }
159+ var link: CADisplayLink ? = null
154160
155- CATransaction .begin()
156- CATransaction .setDisableActions(true )
161+ link = displayLink {
162+ if (window != null ) {
163+ val (width, height) = layer.bounds.useContents { size.width to size.height }
157164
158- bgLayer.frame = CGRectMake (0.0 , 0.0 , width, height)
165+ CATransaction .begin()
166+ CATransaction .setDisableActions(true )
159167
160- CATransaction .commit()
168+ bgLayer.frame = CGRectMake (0.0 , 0.0 , width, height)
169+
170+ CATransaction .commit()
171+ } else {
172+ link?.removeFromRunLoop(NSRunLoop .currentRunLoop, NSRunLoopCommonModes )
173+ }
161174 }
162175}
0 commit comments