summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/widget/EdgeToEdge.kt
blob: c31d1bb1079eaa48909eeea05a57399b502c93ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * Copyright © 2020 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
package com.wireguard.android.widget

import android.view.View
import android.view.ViewGroup
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton

/**
 * A utility for edge-to-edge display. It provides several features needed to make the app
 * displayed edge-to-edge on Android Q with gestural navigation.
 */

object EdgeToEdge {
    @JvmStatic
    fun setUpRoot(root: ViewGroup) {
        root.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

    @JvmStatic
    fun setUpScrollingContent(scrollingContent: ViewGroup, fab: ExtendedFloatingActionButton?) {
        val originalPaddingLeft = scrollingContent.paddingLeft
        val originalPaddingRight = scrollingContent.paddingRight
        val originalPaddingBottom = scrollingContent.paddingBottom

        val fabPaddingBottom = fab?.height ?: 0

        val originalMarginTop = scrollingContent.marginTop

        scrollingContent.setOnApplyWindowInsetsListener { _, windowInsets ->
            scrollingContent.updatePadding(
                    left = originalPaddingLeft + windowInsets.systemWindowInsetLeft,
                    right = originalPaddingRight + windowInsets.systemWindowInsetRight,
                    bottom = originalPaddingBottom + fabPaddingBottom + windowInsets.systemWindowInsetBottom
            )
            scrollingContent.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                topMargin = originalMarginTop + windowInsets.systemWindowInsetTop
            }
            windowInsets
        }
    }

    @JvmStatic
    fun setUpFAB(fab: ExtendedFloatingActionButton) {
        val originalMarginLeft = fab.marginLeft
        val originalMarginRight = fab.marginRight
        val originalMarginBottom = fab.marginBottom
        fab.setOnApplyWindowInsetsListener { _, windowInsets ->
            fab.updateLayoutParams<ViewGroup.MarginLayoutParams> {
                leftMargin = originalMarginLeft + windowInsets.systemWindowInsetLeft
                rightMargin = originalMarginRight + windowInsets.systemWindowInsetRight
                bottomMargin = originalMarginBottom + windowInsets.systemWindowInsetBottom
            }
            windowInsets
        }
    }
}