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

Commit d3069fd

Browse files
committed
fix missed highlighted & enabled update of background
1 parent 864e7d7 commit d3069fd

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/ButtonWithIconViewFactory.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import dev.icerock.moko.widgets.core.style.view.WidgetSize
2020
import dev.icerock.moko.widgets.core.utils.applyStateBackgroundIfNeeded
2121
import dev.icerock.moko.widgets.core.utils.applyTextStyleIfNeeded
2222
import dev.icerock.moko.widgets.core.utils.bind
23-
import dev.icerock.moko.widgets.core.utils.onBoundsChanged
23+
import dev.icerock.moko.widgets.core.utils.observeKeyChanges
2424
import dev.icerock.moko.widgets.core.utils.setEventHandler
2525
import dev.icerock.moko.widgets.core.widget.ButtonWidget
2626
import kotlinx.cinterop.useContents
@@ -141,7 +141,8 @@ actual class ButtonWithIconViewFactory actual constructor(
141141
}
142142

143143
private fun setupLayoutUpdate(button: UIButton, viewFactory: ButtonWithIconViewFactory) {
144-
button.onBoundsChanged(
144+
button.observeKeyChanges(
145+
keyPath = "bounds",
145146
context = viewFactory,
146147
) { button, viewFactory ->
147148
val icPadding: Double = viewFactory.iconPadding?.toDouble() ?: 0.0

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/SystemButtonViewFactory.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import dev.icerock.moko.widgets.core.utils.bind
2222
import dev.icerock.moko.widgets.core.utils.setEventHandler
2323
import dev.icerock.moko.widgets.core.widget.ButtonWidget
2424
import platform.UIKit.UIButton
25+
import platform.UIKit.UIButtonType
2526
import platform.UIKit.UIButtonTypeCustom
2627
import platform.UIKit.UIButtonTypeSystem
2728
import platform.UIKit.UIControlEventTouchUpInside
@@ -44,14 +45,13 @@ actual class SystemButtonViewFactory actual constructor(
4445
size: WS,
4546
viewFactoryContext: ViewFactoryContext
4647
): ViewBundle<WS> {
47-
48-
val buttonType = if (widget.content is ButtonWidget.Content.Icon) {
48+
val buttonType: UIButtonType = if (widget.content is ButtonWidget.Content.Icon) {
4949
UIButtonTypeCustom
5050
} else {
5151
UIButtonTypeSystem
5252
}
5353

54-
val button = UIButton.buttonWithType(buttonType).apply {
54+
val button: UIButton = UIButton.buttonWithType(buttonType).apply {
5555
translatesAutoresizingMaskIntoConstraints = false
5656

5757
applyStateBackgroundIfNeeded(background)
@@ -81,6 +81,7 @@ actual class SystemButtonViewFactory actual constructor(
8181
button.setTitle(title = processedText, forState = UIControlStateNormal)
8282
}
8383
}
84+
8485
is ButtonWidget.Content.Icon -> {
8586
content.image.bind { image ->
8687
image.apply(button) {

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ fun UIButton.applyStateBackgroundIfNeeded(background: PressableState<Background<
9898

9999
stateLayers.update(this)
100100

101-
this.onBoundsChanged(
101+
this.observeKeyChanges(
102+
keyPath = "bounds",
102103
context = stateLayers,
103104
) { button, stateLayers ->
104105
val (width: CGFloat, height: CGFloat) = button.layer.bounds.useContents {
@@ -112,10 +113,22 @@ fun UIButton.applyStateBackgroundIfNeeded(background: PressableState<Background<
112113
stateLayers.disabled.frame = CGRectMake(0.0, 0.0, width, height)
113114
stateLayers.pressed.frame = CGRectMake(0.0, 0.0, width, height)
114115

115-
stateLayers.update(button)
116-
117116
CATransaction.commit()
118117
}
118+
119+
listOf("highlighted", "enabled").forEach { keyPath ->
120+
this.observeKeyChanges(
121+
keyPath = keyPath,
122+
context = stateLayers,
123+
) { button, stateLayers ->
124+
CATransaction.begin()
125+
CATransaction.setDisableActions(true)
126+
127+
stateLayers.update(button)
128+
129+
CATransaction.commit()
130+
}
131+
}
119132
}
120133

121134
private data class StateLayers(
@@ -164,7 +177,8 @@ fun UIView.applyBackgroundIfNeeded(background: Background<out Fill>?) {
164177
val bgLayer: CALayer = background.caLayer()
165178
layer.insertSublayer(bgLayer, 0U)
166179

167-
this.onBoundsChanged(
180+
this.observeKeyChanges(
181+
keyPath = "bounds",
168182
context = bgLayer,
169183
) { view, bgLayer ->
170184
val (width: CGFloat, height: CGFloat) = view.layer.bounds.useContents {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import platform.objc.OBJC_ASSOCIATION_RETAIN
2020
import platform.objc.objc_setAssociatedObject
2121
import kotlin.native.ref.WeakReference
2222

23-
fun <V: UIControl> V.setEventHandler(controlEvent: UIControlEvents, action: (V) -> Unit) {
23+
fun <V : UIControl> V.setEventHandler(controlEvent: UIControlEvents, action: (V) -> Unit) {
2424
val weakReference: WeakReference<V> = WeakReference(this)
2525
val target = LambdaTarget {
2626
val strongRef: V = weakReference.get() ?: return@LambdaTarget
@@ -58,7 +58,8 @@ fun UIGestureRecognizer.setHandler(action: () -> Unit) {
5858
)
5959
}
6060

61-
fun <V : UIView, CTX> V.onBoundsChanged(
61+
fun <V : UIView, CTX> V.observeKeyChanges(
62+
keyPath: String,
6263
context: CTX,
6364
action: (V, CTX) -> Unit
6465
) {
@@ -72,14 +73,14 @@ fun <V : UIView, CTX> V.onBoundsChanged(
7273

7374
objc_setAssociatedObject(
7475
`object` = this,
75-
key = "onBoundsChanged".cstr,
76+
key = "observeKeyChanges-$keyPath".cstr,
7677
value = target,
7778
policy = OBJC_ASSOCIATION_RETAIN
7879
)
7980

8081
this.addObserver(
8182
observer = target,
82-
forKeyPath = "bounds",
83+
forKeyPath = keyPath,
8384
options = NSKeyValueObservingOptionNew,
8485
context = null
8586
)

0 commit comments

Comments
 (0)