Skip to content

Commit 91ab3e8

Browse files
authored
Merge pull request #1135 from square/ray/do-not-stomp-bounds
BREAKING: Stop stomping `View.layoutParams`, new parameter for `replaceViewInParent`
2 parents 87ade41 + b11e61e commit 91ab3e8

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

workflow-ui/core-android/api/core-android.api

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,15 @@ public final class com/squareup/workflow1/ui/WorkflowViewStub : android/view/Vie
270270
public synthetic fun <init> (Landroid/content/Context;Landroid/util/AttributeSet;IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
271271
public final fun getActual ()Landroid/view/View;
272272
public final fun getInflatedId ()I
273-
public final fun getReplaceOldViewInParent ()Lkotlin/jvm/functions/Function2;
273+
public final fun getPropagatesLayoutParams ()Z
274+
public final fun getReplaceOldViewInParent ()Lkotlin/jvm/functions/Function3;
274275
public final fun getUpdatesVisibility ()Z
275276
public fun getVisibility ()I
276277
public fun setBackground (Landroid/graphics/drawable/Drawable;)V
277278
public fun setId (I)V
278279
public final fun setInflatedId (I)V
279-
public final fun setReplaceOldViewInParent (Lkotlin/jvm/functions/Function2;)V
280+
public final fun setPropagatesLayoutParams (Z)V
281+
public final fun setReplaceOldViewInParent (Lkotlin/jvm/functions/Function3;)V
280282
public final fun setUpdatesVisibility (Z)V
281283
public fun setVisibility (I)V
282284
public final fun show (Lcom/squareup/workflow1/ui/Screen;Lcom/squareup/workflow1/ui/ViewEnvironment;)V

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/ScreenViewFactoryFinder.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.squareup.workflow1.ui.container.EnvironmentScreen
2020
* by ScreenViewFactory(
2121
* buildView = { environment, context, _ ->
2222
* val view = MyBackStackContainer(context)
23-
* .apply { layoutParams = (LayoutParams(MATCH_PARENT, MATCH_PARENT)) }
2423
* ScreenViewHolder(environment, view) { rendering, environment ->
2524
* view.update(rendering, environment)
2625
* }

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/WorkflowLayout.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import android.os.Parcelable.Creator
88
import android.util.AttributeSet
99
import android.util.SparseArray
1010
import android.view.View
11-
import android.view.ViewGroup
12-
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
1311
import android.widget.FrameLayout
1412
import androidx.lifecycle.Lifecycle
1513
import androidx.lifecycle.Lifecycle.State
@@ -51,7 +49,8 @@ public class WorkflowLayout(
5149

5250
private val showing: WorkflowViewStub = WorkflowViewStub(context).also { rootStub ->
5351
rootStub.updatesVisibility = false
54-
addView(rootStub, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
52+
rootStub.propagatesLayoutParams = false
53+
addView(rootStub)
5554
}
5655

5756
private var restoredChildState: SparseArray<Parcelable>? = null

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/WorkflowViewStub.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable
55
import android.util.AttributeSet
66
import android.view.View
77
import android.view.ViewGroup
8+
import android.view.ViewGroup.LayoutParams
89
import androidx.annotation.IdRes
910
import androidx.savedstate.SavedStateRegistryOwner
1011
import androidx.savedstate.findViewTreeSavedStateRegistryOwner
@@ -85,6 +86,14 @@ public class WorkflowViewStub @JvmOverloads constructor(
8586
*/
8687
public var updatesVisibility: Boolean = true
8788

89+
/**
90+
* If true, the [layoutParams][getLayoutParams] of this stub will be applied
91+
* to new views as they are added to the stub's original parent.
92+
* Specifically, the third parameter passed to [replaceOldViewInParent]
93+
* will be non-null.
94+
*/
95+
public var propagatesLayoutParams: Boolean = true
96+
8897
/**
8998
* The id to be assigned to new views created by [update]. If the inflated id is
9099
* [View.NO_ID] (its default value), new views keep their original ids.
@@ -121,15 +130,19 @@ public class WorkflowViewStub @JvmOverloads constructor(
121130
/**
122131
* Function called from [update] to replace this stub, or the current [actual],
123132
* with a new view. Can be updated to provide custom transition effects.
133+
* The default implementation simply calls [ViewGroup.addView], including
134+
* the `layoutParams` parameter if it is non-null.
124135
*
125-
* Note that this method is responsible for copying the [layoutParams][getLayoutParams]
126-
* from the stub to the new view. Also note that in a [WorkflowViewStub] that has never
127-
* been updated, [actual] is the stub itself.
136+
* @see propagatesLayoutParams
128137
*/
129-
public var replaceOldViewInParent: (ViewGroup, View) -> Unit = { parent, newView ->
138+
public var replaceOldViewInParent: (
139+
parent: ViewGroup,
140+
newView: View,
141+
layoutParams: LayoutParams?
142+
) -> Unit = { parent, newView, layoutParams ->
130143
val index = parent.indexOfChild(actual)
131144
parent.removeView(actual)
132-
actual.layoutParams
145+
layoutParams
133146
?.let { parent.addView(newView, index, it) }
134147
?: run { parent.addView(newView, index) }
135148
}
@@ -241,7 +254,7 @@ public class WorkflowViewStub @JvmOverloads constructor(
241254
if (updatesVisibility) newView.visibility = visibility
242255
background?.let { newView.background = it }
243256
propagateSavedStateRegistryOwner(newView)
244-
replaceOldViewInParent(parent, newView)
257+
replaceOldViewInParent(parent, newView, layoutParams?.takeIf { propagatesLayoutParams })
245258
}
246259
}
247260

workflow-ui/core-android/src/main/java/com/squareup/workflow1/ui/container/BodyAndOverlaysContainer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.os.Parcelable.Creator
99
import android.util.AttributeSet
1010
import android.view.KeyEvent
1111
import android.view.MotionEvent
12-
import android.view.ViewGroup
1312
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
1413
import android.widget.FrameLayout
1514
import com.squareup.workflow1.ui.Compatible
@@ -42,8 +41,9 @@ internal class BodyAndOverlaysContainer @JvmOverloads constructor(
4241
*/
4342
private lateinit var savedStateParentKey: String
4443

45-
private val baseViewStub: WorkflowViewStub = WorkflowViewStub(context).also {
46-
addView(it, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
44+
private val baseViewStub: WorkflowViewStub = WorkflowViewStub(context).apply {
45+
propagatesLayoutParams = false
46+
addView(this)
4747
}
4848

4949
private val dialogs = LayeredDialogSessions.forView(

0 commit comments

Comments
 (0)