make custom font setting work

This commit is contained in:
Helium314 2025-02-05 17:00:06 +01:00
parent ca89eaa51c
commit 534bfb2f13
5 changed files with 84 additions and 15 deletions

View file

@ -17,7 +17,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
// make all prefs actually work // make all prefs actually work
// appearance // appearance
// click on bg image does nothing when already set (but works after reload) // click on bg image does nothing when already set (but works after reload)
// custom font loading not implemented
// have large bg image, and first-time load the keyboard on new search field -> bg image expands full size // have large bg image, and first-time load the keyboard on new search field -> bg image expands full size
// advanced // advanced
// preferences // preferences
@ -27,6 +26,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
// more similar dialog style args (for all dialogs, or for none) // more similar dialog style args (for all dialogs, or for none)
// check whether dialogs have the same colors, i think currently it's a bit inconsistent // check whether dialogs have the same colors, i think currently it's a bit inconsistent
// see all the properties for each alertDialog -> any way to set it in a single place? // see all the properties for each alertDialog -> any way to set it in a single place?
// yes/no/default can now be confirmDialog
// title too huge for bg image and text on spacebar dialogs, also maybe somewhere else -> where to set in one place? // title too huge for bg image and text on spacebar dialogs, also maybe somewhere else -> where to set in one place?
// check dark and light theme (don't have dynamic) // check dark and light theme (don't have dynamic)
// rename both settingsActivities // rename both settingsActivities

View file

@ -26,23 +26,26 @@ fun ConfirmationDialog(
text: @Composable (() -> Unit)? = null, text: @Composable (() -> Unit)? = null,
confirmButtonText: String = stringResource(android.R.string.ok), confirmButtonText: String = stringResource(android.R.string.ok),
cancelButtonText: String = stringResource(android.R.string.cancel), cancelButtonText: String = stringResource(android.R.string.cancel),
neutralButtonText: String? = null,
onNeutral: () -> Unit = { },
shape: Shape = MaterialTheme.shapes.medium, shape: Shape = MaterialTheme.shapes.medium,
backgroundColor: Color = MaterialTheme.colorScheme.surface, backgroundColor: Color = MaterialTheme.colorScheme.surface,
contentColor: Color = contentColorFor(backgroundColor), contentColor: Color = contentColorFor(backgroundColor),
properties: DialogProperties = DialogProperties(), properties: DialogProperties = DialogProperties(),
) { ) {
AlertDialog( ThreeButtonAlertDialog(
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
confirmButton = { onConfirmed = onConfirmed,
TextButton(onClick = { onConfirmed(); onDismissRequest() }) { Text(confirmButtonText) } confirmButtonText = confirmButtonText,
}, cancelButtonText = cancelButtonText,
neutralButtonText = neutralButtonText,
onNeutral = onNeutral,
modifier = modifier, modifier = modifier,
dismissButton = { TextButton(onClick = onDismissRequest) { Text(cancelButtonText) } },
title = title, title = title,
text = text, text = text,
shape = shape, shape = shape,
containerColor = backgroundColor, backgroundColor = backgroundColor,
textContentColor = contentColor, contentColor = contentColor,
properties = properties, properties = properties,
) )
} }
@ -53,6 +56,7 @@ private fun PreviewConfirmDialog() {
ConfirmationDialog( ConfirmationDialog(
onDismissRequest = { }, onDismissRequest = { },
onConfirmed = {}, onConfirmed = {},
neutralButtonText = "hi",
confirmButtonText = "I don't care", confirmButtonText = "I don't care",
text = { Text(stringResource(R.string.disable_personalized_dicts_message)) } text = { Text(stringResource(R.string.disable_personalized_dicts_message)) }
) )

View file

@ -0,0 +1,17 @@
package helium314.keyboard.settings.dialogs
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
@Composable
fun InfoDialog(
message: String,
onDismissRequest: () -> Unit
) {
ThreeButtonAlertDialog(
onDismissRequest = onDismissRequest,
text = { Text(message) },
onConfirmed = { },
confirmButtonText = null
)
}

View file

@ -23,7 +23,7 @@ fun ThreeButtonAlertDialog(
text: @Composable (() -> Unit)? = null, text: @Composable (() -> Unit)? = null,
onNeutral: () -> Unit = { }, onNeutral: () -> Unit = { },
checkOk: () -> Boolean = { true }, checkOk: () -> Boolean = { true },
confirmButtonText: String = stringResource(android.R.string.ok), confirmButtonText: String? = stringResource(android.R.string.ok),
cancelButtonText: String = stringResource(android.R.string.cancel), cancelButtonText: String = stringResource(android.R.string.cancel),
neutralButtonText: String? = null, neutralButtonText: String? = null,
shape: Shape = MaterialTheme.shapes.medium, shape: Shape = MaterialTheme.shapes.medium,
@ -41,10 +41,11 @@ fun ThreeButtonAlertDialog(
) { Text(neutralButtonText) } ) { Text(neutralButtonText) }
Spacer(modifier.weight(1f)) Spacer(modifier.weight(1f))
TextButton(onClick = onDismissRequest) { Text(cancelButtonText) } TextButton(onClick = onDismissRequest) { Text(cancelButtonText) }
TextButton( if (confirmButtonText != null)
enabled = checkOk(), TextButton(
onClick = { onDismissRequest(); onConfirmed() }, enabled = checkOk(),
) { Text(confirmButtonText) } onClick = { onDismissRequest(); onConfirmed() },
) { Text(confirmButtonText) }
} }
}, },
modifier = modifier, modifier = modifier,

View file

@ -5,6 +5,7 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Typeface
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
@ -31,9 +32,11 @@ import helium314.keyboard.latin.settings.ColorsNightSettingsFragment
import helium314.keyboard.latin.settings.ColorsSettingsFragment import helium314.keyboard.latin.settings.ColorsSettingsFragment
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.settings.SettingsValues import helium314.keyboard.latin.settings.SettingsValues
import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.latin.utils.Log import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.getStringResourceOrName import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.latin.utils.infoDialog
import helium314.keyboard.latin.utils.prefs import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.latin.utils.switchTo import helium314.keyboard.latin.utils.switchTo
import helium314.keyboard.settings.AllPrefs import helium314.keyboard.settings.AllPrefs
@ -47,11 +50,14 @@ import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SliderPreference import helium314.keyboard.settings.SliderPreference
import helium314.keyboard.settings.SwitchPreference import helium314.keyboard.settings.SwitchPreference
import helium314.keyboard.settings.Theme import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import helium314.keyboard.settings.dialogs.CustomizeIconsDialog import helium314.keyboard.settings.dialogs.CustomizeIconsDialog
import helium314.keyboard.settings.dialogs.InfoDialog
import helium314.keyboard.settings.dialogs.TextInputDialog import helium314.keyboard.settings.dialogs.TextInputDialog
import helium314.keyboard.settings.keyboardNeedsReload import helium314.keyboard.settings.keyboardNeedsReload
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File
@Composable @Composable
fun AppearanceScreen( fun AppearanceScreen(
@ -338,11 +344,52 @@ fun createAppearancePrefs(context: Context) = listOf(
} }
}, },
PrefDef(context, NonSettingsPrefs.CUSTOM_FONT, R.string.custom_font) { def -> PrefDef(context, NonSettingsPrefs.CUSTOM_FONT, R.string.custom_font) { def ->
val ctx = LocalContext.current
var showDialog by remember { mutableStateOf(false) } var showDialog by remember { mutableStateOf(false) }
var showErrorDialog by remember { mutableStateOf(false) }
val fontFile = Settings.getCustomFontFile(ctx)
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode != Activity.RESULT_OK) return@rememberLauncherForActivityResult
val uri = it.data?.data ?: return@rememberLauncherForActivityResult
val tempFile = File(DeviceProtectedUtils.getFilesDir(context), "temp_file")
FileUtils.copyContentUriToNewFile(uri, ctx, tempFile)
try {
val typeface = Typeface.createFromFile(tempFile)
fontFile.delete()
tempFile.renameTo(fontFile)
Settings.clearCachedTypeface()
keyboardNeedsReload = true
} catch (_: Exception) {
showErrorDialog = true
tempFile.delete()
}
}
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
Preference( Preference(
name = def.title, name = def.title,
onClick = { showDialog = true }, onClick = {
) // todo: create and show the dialog if (fontFile.exists())
showDialog = true
else launcher.launch(intent)
},
)
if (showDialog)
ConfirmationDialog(
onDismissRequest = { showDialog = false },
onConfirmed = { launcher.launch(intent) },
onNeutral = {
fontFile.delete()
Settings.clearCachedTypeface()
keyboardNeedsReload = true
},
neutralButtonText = stringResource(R.string.delete),
confirmButtonText = stringResource(R.string.load),
title = { Text(stringResource(R.string.custom_font)) }
)
if (showErrorDialog)
InfoDialog(stringResource(R.string.file_read_error)) { showErrorDialog = false }
}, },
) )