Add SettingsFontFamily for SpaLib

So we could get the font family from config and apply them to SPA.

Bug: 235727273
Test: Manual with Settings App
Change-Id: I2af44e3a73db2c1a1d21630b6c16cb58a745c4b1
This commit is contained in:
Chaohui Wang
2022-10-25 17:26:42 +08:00
parent deda3d4737
commit 7bae0f83e6
2 changed files with 70 additions and 5 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalTextApi::class)
package com.android.settingslib.spa.framework.theme
import android.annotation.SuppressLint
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.font.DeviceFontFamilyName
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
internal data class SettingsFontFamily(
val brand: FontFamily = FontFamily.Default,
val plain: FontFamily = FontFamily.Default,
)
private fun Context.getSettingsFontFamily(inInspection: Boolean): SettingsFontFamily {
if (inInspection) {
return SettingsFontFamily()
}
return SettingsFontFamily(
brand = FontFamily(
Font(getFontFamilyName("config_headlineFontFamily"), FontWeight.Normal),
Font(getFontFamilyName("config_headlineFontFamilyMedium"), FontWeight.Medium),
),
plain = FontFamily(
Font(getFontFamilyName("config_bodyFontFamily"), FontWeight.Normal),
Font(getFontFamilyName("config_bodyFontFamilyMedium"), FontWeight.Medium),
),
)
}
private fun Context.getFontFamilyName(configName: String): DeviceFontFamilyName {
@SuppressLint("DiscouragedApi")
val configId = resources.getIdentifier(configName, "string", "android")
return DeviceFontFamilyName(resources.getString(configId))
}
@Composable
internal fun rememberSettingsFontFamily(): SettingsFontFamily {
val context = LocalContext.current
val inInspection = LocalInspectionMode.current
return remember { context.getSettingsFontFamily(inInspection) }
}

View File

@@ -20,14 +20,13 @@ import androidx.compose.material3.Typography
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
private class SettingsTypography {
private val brand = FontFamily.Default
private val plain = FontFamily.Default
private class SettingsTypography(settingsFontFamily: SettingsFontFamily) {
private val brand = settingsFontFamily.brand
private val plain = settingsFontFamily.plain
val typography = Typography(
displayLarge = TextStyle(
@@ -140,5 +139,6 @@ private class SettingsTypography {
@Composable
internal fun rememberSettingsTypography(): Typography {
return remember { SettingsTypography().typography }
val settingsFontFamily = rememberSettingsFontFamily()
return remember { SettingsTypography(settingsFontFamily).typography }
}