42 lines
1.3 KiB
Kotlin
42 lines
1.3 KiB
Kotlin
package se.ajpanton.notificationlog
|
|
|
|
import android.content.Context
|
|
import android.graphics.Canvas
|
|
import android.util.AttributeSet
|
|
import androidx.drawerlayout.widget.DrawerLayout
|
|
|
|
/**
|
|
* Clips drawer rendering away from display cutouts and system-bar side insets.
|
|
* DrawerLayout otherwise lets its drawer render into that unsafe area.
|
|
*/
|
|
class SafeInsetDrawerLayout @JvmOverloads constructor(
|
|
context: Context,
|
|
attrs: AttributeSet? = null,
|
|
defStyleAttr: Int = 0,
|
|
) : DrawerLayout(context, attrs, defStyleAttr) {
|
|
private var safeInsetLeft = 0
|
|
private var safeInsetRight = 0
|
|
|
|
fun setDrawerSafeInsets(left: Int, right: Int) {
|
|
val safeLeft = left.coerceAtLeast(0)
|
|
val safeRight = right.coerceAtLeast(0)
|
|
if (safeInsetLeft == safeLeft && safeInsetRight == safeRight) {
|
|
return
|
|
}
|
|
safeInsetLeft = safeLeft
|
|
safeInsetRight = safeRight
|
|
invalidate()
|
|
}
|
|
|
|
override fun dispatchDraw(canvas: Canvas) {
|
|
if (safeInsetLeft == 0 && safeInsetRight == 0) {
|
|
super.dispatchDraw(canvas)
|
|
return
|
|
}
|
|
val saveCount = canvas.save()
|
|
canvas.clipRect(safeInsetLeft, 0, (width - safeInsetRight).coerceAtLeast(safeInsetLeft), height)
|
|
super.dispatchDraw(canvas)
|
|
canvas.restoreToCount(saveCount)
|
|
}
|
|
}
|