mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
chore(ui): add extension functions for applying edge to edge insets
This commit is contained in:
parent
fc16aee47a
commit
e37b53351d
1 changed files with 42 additions and 0 deletions
|
|
@ -0,0 +1,42 @@
|
||||||
|
package fr.free.nrw.commons.utils
|
||||||
|
|
||||||
|
import androidx.core.graphics.Insets
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup.MarginLayoutParams
|
||||||
|
import androidx.core.view.ViewCompat
|
||||||
|
import androidx.core.view.WindowInsetsCompat
|
||||||
|
import androidx.core.view.updateLayoutParams
|
||||||
|
|
||||||
|
fun View.applyEdgeToEdgeInsets(
|
||||||
|
typeMask: Int = WindowInsetsCompat.Type.systemBars(),
|
||||||
|
block: MarginLayoutParams.(Insets) -> Unit
|
||||||
|
) {
|
||||||
|
ViewCompat.setOnApplyWindowInsetsListener(this) { v, windowInsets ->
|
||||||
|
val insets = windowInsets.getInsets(typeMask)
|
||||||
|
|
||||||
|
v.updateLayoutParams<MarginLayoutParams> {
|
||||||
|
apply { block(insets) }
|
||||||
|
}
|
||||||
|
|
||||||
|
WindowInsetsCompat.CONSUMED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun applyEdgeToEdgeAllInsets(view: View) = view.applyEdgeToEdgeInsets { insets ->
|
||||||
|
leftMargin = insets.left
|
||||||
|
rightMargin = insets.right
|
||||||
|
topMargin = insets.top
|
||||||
|
bottomMargin = insets.bottom
|
||||||
|
}
|
||||||
|
|
||||||
|
fun applyEdgeToEdgeTopInsets(view: View) = view.applyEdgeToEdgeInsets { insets ->
|
||||||
|
leftMargin = insets.left
|
||||||
|
rightMargin = insets.right
|
||||||
|
topMargin = insets.top
|
||||||
|
}
|
||||||
|
|
||||||
|
fun applyEdgeToEdgeBottomInsets(view: View) = view.applyEdgeToEdgeInsets { insets ->
|
||||||
|
leftMargin = insets.left
|
||||||
|
rightMargin = insets.right
|
||||||
|
bottomMargin = insets.bottom
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue