Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit b6c1fea

Browse files
authored
Merge pull request #293 from stevesoltys/bugfix/ios-performance
Fix iOS background update performance issues
2 parents a1539a4 + 18b3fa1 commit b6c1fea

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/utils/BackgroundExt.kt

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import dev.icerock.moko.widgets.core.style.background.Corner
1414
import kotlinx.cinterop.useContents
1515
import platform.CoreGraphics.CGPointMake
1616
import platform.CoreGraphics.CGRectMake
17+
import platform.Foundation.NSRunLoop
18+
import platform.Foundation.NSRunLoopCommonModes
19+
import platform.QuartzCore.CADisplayLink
1720
import platform.QuartzCore.CAGradientLayer
1821
import platform.QuartzCore.CALayer
1922
import 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

2525
fun 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

Comments
 (0)