summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/ThemeHelper.kt
blob: ce6396e9115c31a703ad607a050e45270c7aee24 (plain) (blame)
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
67
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.utils

import android.app.Activity
import android.content.res.Configuration
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import com.google.android.material.color.MaterialColors
import org.yuzu.yuzu_emu.R
import kotlin.math.roundToInt

object ThemeHelper {
    private const val NAV_BAR_ALPHA = 0.9f

    @JvmStatic
    fun setTheme(activity: AppCompatActivity) {
        val windowController = WindowCompat.getInsetsController(
            activity.window,
            activity.window.decorView
        )
        val isLightMode =
            (activity.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO
        windowController.isAppearanceLightStatusBars = isLightMode
        windowController.isAppearanceLightNavigationBars = isLightMode

        activity.window.statusBarColor = ContextCompat.getColor(activity, android.R.color.transparent)

        val navigationBarColor =
            MaterialColors.getColor(activity.window.decorView, R.attr.colorSurface)
        setNavigationBarColor(activity, navigationBarColor)
    }

    @JvmStatic
    fun setNavigationBarColor(activity: Activity, @ColorInt color: Int) {
        val gestureType = InsetsHelper.getSystemGestureType(activity.applicationContext)
        val orientation = activity.resources.configuration.orientation

        if ((gestureType == InsetsHelper.THREE_BUTTON_NAVIGATION ||
                    gestureType == InsetsHelper.TWO_BUTTON_NAVIGATION) &&
            orientation == Configuration.ORIENTATION_LANDSCAPE
        ) {
            activity.window.navigationBarColor = color
        } else if (gestureType == InsetsHelper.THREE_BUTTON_NAVIGATION ||
            gestureType == InsetsHelper.TWO_BUTTON_NAVIGATION
        ) {
            activity.window.navigationBarColor = getColorWithOpacity(color, NAV_BAR_ALPHA)
        } else {
            activity.window.navigationBarColor = ContextCompat.getColor(
                activity.applicationContext,
                android.R.color.transparent
            )
        }
    }

    @ColorInt
    private fun getColorWithOpacity(@ColorInt color: Int, alphaFactor: Float): Int {
        return Color.argb(
            (alphaFactor * Color.alpha(color)).roundToInt(), Color.red(color),
            Color.green(color), Color.blue(color)
        )
    }
}