Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions app/src/main/java/helium314/keyboard/settings/SearchScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import helium314.keyboard.latin.R
import helium314.keyboard.settings.preferences.PreferenceCategory
Expand Down Expand Up @@ -190,7 +194,17 @@ fun SearchSettingsScreen(
// a setting further down in a large card is not on screen after scrolling to it
val bringIntoView = remember { BringIntoViewRequester() }
LaunchedEffect(highlightKey) { if (key == highlightKey) bringIntoView.bringIntoView() }
Box(Modifier.bringIntoViewRequester(bringIntoView).background(highlight)) {
val nestedLine = MaterialTheme.colorScheme.outlineVariant
val nestedModifier = if (!row.nested) Modifier else Modifier
// a guide line and an indent show that the setting belongs to the fine-tune section above
.drawBehind {
val lineWidth = 2.dp.toPx()
val x = 14.dp.toPx()
val left = if (layoutDirection == LayoutDirection.Rtl) size.width - x - lineWidth else x
drawRect(nestedLine, topLeft = Offset(left, 0f), size = Size(lineWidth, size.height))
}
.padding(start = 20.dp)
Box(Modifier.bringIntoViewRequester(bringIntoView).background(highlight).then(nestedModifier)) {
SettingsActivity.settingsContainer[key]?.Preference()
}
}
Expand Down Expand Up @@ -228,14 +242,22 @@ fun SearchSettingsScreen(
)
}

/** A row of a settings screen: the title of a category ([key] is null) or a setting; first and last row of a card */
private data class SettingRow(val id: String, val titleRes: Int?, val key: String?, val first: Boolean = false, val last: Boolean = false)
/**
* A row of a settings screen: the title of a category ([key] is null) or a setting; first and last row of a card.
* [nested]: the setting is inside an open fine-tune section, i.e. after a fine-tune header in the same card.
*/
private data class SettingRow(val id: String, val titleRes: Int?, val key: String?, val first: Boolean = false, val last: Boolean = false, val nested: Boolean = false)

private fun settingRows(groups: List<Pair<Int?, List<String>>>): List<SettingRow> = buildList {
groups.forEachIndexed { index, (titleRes, keys) ->
val group = buildList {
if (titleRes != null) add(SettingRow("title_${index}_$titleRes", titleRes, null))
keys.forEach { add(SettingRow("${index}_$it", null, it)) }
var inFineTune = false
keys.forEach {
val isFineTuneHeader = it.startsWith(FINE_TUNE_SECTION_PREFIX)
add(SettingRow("${index}_$it", null, it, nested = inFineTune && !isFineTuneHeader))
if (isFineTuneHeader) inFineTune = true
}
}
group.forEachIndexed { i, row -> add(row.copy(first = i == 0, last = i == group.lastIndex)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import helium314.keyboard.latin.utils.JniUtils
import helium314.keyboard.latin.R
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.screens.createAboutSettings
import helium314.keyboard.settings.screens.createAdvancedSettings
import helium314.keyboard.settings.screens.createAIIntegrationSettings
import helium314.keyboard.settings.screens.createAdvancedScreenSettings
import helium314.keyboard.settings.screens.createTranslationSettings
import helium314.keyboard.settings.screens.createAppearanceSettings
import helium314.keyboard.settings.screens.createBackgroundServicesSettings
import helium314.keyboard.settings.screens.createCorrectionSettings
Expand All @@ -19,7 +21,6 @@ import helium314.keyboard.settings.screens.createGestureTypingSettings
import helium314.keyboard.settings.screens.createLanguageSettings
import helium314.keyboard.settings.screens.createLayoutSettings
import helium314.keyboard.settings.screens.createOcrSettings
import helium314.keyboard.settings.screens.createLibrariesSettings
import helium314.keyboard.settings.screens.createPreferencesSettings
import helium314.keyboard.settings.screens.createSoundSettings
import helium314.keyboard.settings.screens.createTextExpanderSettings
Expand Down Expand Up @@ -143,19 +144,19 @@ private val modules = listOf(
SettingsModule(SettingsWithoutKey.SCREEN_NAV_GESTURES, SettingsDestination.GestureTyping, R.string.settings_screen_gesture, iconRes = R.drawable.ic_settings_gesture, provider = ::createGestureTypingSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_TEXT_CORRECTION, SettingsDestination.TextCorrection, R.string.settings_screen_correction, iconRes = R.drawable.ic_settings_correction, provider = ::createCorrectionSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_SUGGESTIONS, SettingsDestination.Suggestions, R.string.settings_screen_suggestions, iconRes = R.drawable.ic_settings_suggestions, provider = ::createSuggestionsSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_AI_INTEGRATION, SettingsDestination.AIIntegration, R.string.settings_screen_ai_integration, iconRes = R.drawable.ic_proofread),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_AI_INTEGRATION, SettingsDestination.AIIntegration, R.string.settings_screen_ai_integration, iconRes = R.drawable.ic_proofread, provider = ::createAIIntegrationSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_TEXT_EXPANDER, SettingsDestination.TextExpander, titleString = "Text Expander", iconRes = R.drawable.ic_edit, provider = ::createTextExpanderSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_ADVANCED, SettingsDestination.Advanced, R.string.settings_screen_advanced, iconRes = R.drawable.ic_settings_advanced, provider = ::createAdvancedSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_ADVANCED, SettingsDestination.Advanced, R.string.settings_screen_advanced, iconRes = R.drawable.ic_settings_advanced, provider = ::createAdvancedScreenSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_ABOUT, SettingsDestination.About, R.string.settings_screen_about, iconRes = R.drawable.ic_settings_about, provider = ::createAboutSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_OCR, SettingsDestination.OCR, R.string.ocr_settings_title, iconRes = R.drawable.ic_ocr, provider = ::createOcrSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_LIBRARIES, SettingsDestination.Libraries, R.string.libraries_hub_title, iconRes = R.drawable.ic_emoji_objects, provider = ::createLibrariesSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_LIBRARIES, SettingsDestination.Libraries, R.string.libraries_hub_title, iconRes = R.drawable.ic_emoji_objects),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_BACKGROUND_SERVICES, SettingsDestination.BackgroundServices, titleString = "Background Services", provider = ::createBackgroundServicesSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_DICTIONARIES, SettingsDestination.Dictionaries, R.string.dictionary_settings_category, iconRes = R.drawable.ic_dictionary),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_PERSONAL_DICTIONARIES, SettingsDestination.PersonalDictionaries, R.string.edit_personal_dictionary, iconRes = R.drawable.ic_dictionary),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_BLOCKED_WORDS, SettingsDestination.BlockedWords, R.string.edit_blocked_words, iconRes = R.drawable.ic_bin),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_SOUND, SettingsDestination.Sound, R.string.sound_packs_title, iconRes = R.drawable.ic_play_arrow, provider = ::createSoundSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_VOICE, SettingsDestination.OfflineVoice, R.string.voice_input_title, iconRes = R.drawable.sym_keyboard_voice_holo, provider = ::createVoiceSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_TRANSLATION, SettingsDestination.Translation, R.string.translation_settings_title, iconRes = R.drawable.ic_translate),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_TRANSLATION, SettingsDestination.Translation, R.string.translation_settings_title, iconRes = R.drawable.ic_translate, provider = ::createTranslationSettings),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_HANDWRITING, SettingsDestination.Handwriting, R.string.libraries_hub_handwriting_title, iconRes = R.drawable.ic_edit),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_CJK, SettingsDestination.Cjk, R.string.cjk_settings_title, iconRes = R.drawable.ic_settings_languages),
SettingsModule(SettingsWithoutKey.SCREEN_NAV_UPDATES, SettingsDestination.Updates, R.string.settings_screen_updates, iconRes = R.drawable.ic_settings_updates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ fun SettingsNavHost(
onClickOcr = { navController.navigate(SettingsDestination.OCR) },
onClickAIIntegration = { navController.navigate(SettingsDestination.AIIntegration) },
onClickSound = { navController.navigate(SettingsDestination.Sound) },
onClickAppProfiles = { navController.navigate(SettingsDestination.AppQuirks) },
onClickCjk = { navController.navigate(SettingsDestination.Cjk) },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import helium314.keyboard.keyboard.KeyboardActionListener
import helium314.keyboard.keyboard.KeyboardLayoutSet
import helium314.keyboard.keyboard.KeyboardSwitcher
import helium314.keyboard.keyboard.emoji.SupportedEmojis
import helium314.keyboard.latin.BuildConfig
import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.splitOnWhitespace
import helium314.keyboard.latin.settings.DebugSettings
import helium314.keyboard.latin.settings.Defaults
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.checkTimestampFormat
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.fineTuneExpanded
import helium314.keyboard.settings.dialogs.ConfirmationDialog
Expand All @@ -60,7 +56,6 @@ import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.preferences.BackupRestorePreference
import helium314.keyboard.settings.preferences.ExpandablePreference
import helium314.keyboard.settings.preferences.LoadGestureLibPreference
import helium314.keyboard.settings.preferences.TextInputPreference
import helium314.keyboard.settings.previewDark
import androidx.core.content.edit
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -92,21 +87,17 @@ fun AdvancedSettingsScreen(
// Primary
Settings.PREF_ALWAYS_INCOGNITO_MODE,
Settings.PREF_DISABLE_NETWORK,
SettingsWithoutKey.APP_QUIRKS,
SettingsWithoutKey.BACKGROUND_SERVICES,
SettingsWithoutKey.BACKUP_RESTORE,
if (BuildConfig.DEBUG || prefs.getBoolean(DebugSettings.PREF_SHOW_DEBUG_SETTINGS, Defaults.PREF_SHOW_DEBUG_SETTINGS))
SettingsWithoutKey.DEBUG_SETTINGS else null,
Settings.PREF_EXPAND_FINE_TUNE_ADVANCED,

// Fine-tune physical keyboards & experimental
if (fineTuneAdvancedExpanded && (Settings.readHorizontalSpaceSwipe(prefs) == KeyboardActionListener.SWIPE_SWITCH_LANGUAGE
|| Settings.readFullKeyboardSwipe(prefs) != KeyboardActionListener.SWIPE_NO_ACTION))
Settings.PREF_LANGUAGE_SWIPE_DISTANCE else null,
if (fineTuneAdvancedExpanded) Settings.PREF_PHYSICAL_KEYBOARD_LAYOUT else null,
if (fineTuneAdvancedExpanded) Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY else null,
if (fineTuneAdvancedExpanded) Settings.PREF_PHYSICAL_KEYBOARD_SUGGESTION_SHORTCUTS else null,
if (fineTuneAdvancedExpanded) Settings.PREF_CUSTOM_CURRENCY_KEY else null,
if (fineTuneAdvancedExpanded) Settings.PREF_TIMESTAMP_FORMAT else null,
if (fineTuneAdvancedExpanded) R.string.settings_category_experimental else null,
if (fineTuneAdvancedExpanded) Settings.PREF_EMOJI_MAX_SDK else null,
if (fineTuneAdvancedExpanded) Settings.PREF_URL_DETECTION else null,
Expand All @@ -119,26 +110,43 @@ fun AdvancedSettingsScreen(
)
}

@SuppressLint("ApplySharedPref")
// Settings defined below but shown on other screens: each list is registered with the screen
// that shows it, so a long press in the search opens that screen and not the advanced one.
private val translationSettingKeys = setOf(
SettingsWithoutKey.TRANSLATION_ENGINE,
SettingsWithoutKey.TRANSLATION_SOURCE_LANGUAGE,
SettingsWithoutKey.GEMINI_TARGET_LANGUAGE,
)
private val aiIntegrationSettingKeys = setOf(
SettingsWithoutKey.GEMINI_API_KEY, SettingsWithoutKey.GEMINI_MODEL, SettingsWithoutKey.AI_PROVIDER,
SettingsWithoutKey.GROQ_TOKEN, SettingsWithoutKey.GROQ_MODEL,
SettingsWithoutKey.HUGGINGFACE_TOKEN, SettingsWithoutKey.HUGGINGFACE_MODEL, SettingsWithoutKey.HUGGINGFACE_ENDPOINT,
SettingsWithoutKey.OPENAI_USE_MAX_COMPLETION_TOKENS, SettingsWithoutKey.OPENAI_OMIT_TEMPERATURE,
SettingsWithoutKey.AI_ALLOW_INSECURE_CONNECTIONS, SettingsWithoutKey.CLOUD_AI_MAX_TOKENS,
SettingsWithoutKey.TRANSLATE_GEMINI_MODEL, SettingsWithoutKey.TRANSLATE_GROQ_MODEL, SettingsWithoutKey.TRANSLATE_HUGGINGFACE_MODEL,
SettingsWithoutKey.VOICE_GEMINI_MODEL, SettingsWithoutKey.VOICE_GROQ_MODEL, SettingsWithoutKey.VOICE_HUGGINGFACE_MODEL,
SettingsWithoutKey.CUSTOM_AI_KEYS, SettingsWithoutKey.LOAD_OFFLINE_AI_PLUGIN,
SettingsWithoutKey.OFFLINE_KEEP_MODEL_LOADED, SettingsWithoutKey.OFFLINE_MODEL_PATH,
)

fun createAdvancedScreenSettings(context: Context) =
createAdvancedSettings(context).filter { it.key !in aiIntegrationSettingKeys && it.key !in translationSettingKeys }

fun createAIIntegrationSettings(context: Context) = createAdvancedSettings(context).filter { it.key in aiIntegrationSettingKeys }

fun createTranslationSettings(context: Context) = createAdvancedSettings(context).filter { it.key in translationSettingKeys }

/** Keeps the saved model in a fetched model list, so the setting still shows it if the provider no longer lists it */
private fun withSavedModel(items: List<Pair<String, String>>, savedModel: String) =
if (savedModel.isBlank() || items.any { it.second == savedModel }) items else items + (savedModel to savedModel)

fun createAdvancedSettings(context: Context) = listOfNotNull(
@SuppressLint("ApplySharedPref")
private fun createAdvancedSettings(context: Context) = listOfNotNull(
Setting(context, Settings.PREF_ALWAYS_INCOGNITO_MODE,
R.string.incognito, R.string.prefs_force_incognito_mode_summary)
{
SwitchPreference(it, Defaults.PREF_ALWAYS_INCOGNITO_MODE) { KeyboardSwitcher.getInstance().setThemeNeedsReload() }
},
Setting(context, Settings.PREF_LANGUAGE_SWIPE_DISTANCE, R.string.prefs_language_swipe_distance) { setting ->
SliderPreference(
name = setting.title,
key = setting.key,
default = Defaults.PREF_LANGUAGE_SWIPE_DISTANCE,
range = 2f..18f,
description = { it.toString() }
)
},
Setting(context, Settings.PREF_PHYSICAL_KEYBOARD_LAYOUT, R.string.prefs_physical_keyboard_layout,
R.string.prefs_physical_keyboard_layout_summary) { setting ->
val items = listOf(
Expand Down Expand Up @@ -171,39 +179,23 @@ fun createAdvancedSettings(context: Context) = listOfNotNull(
)
ListPreference(setting, items, Defaults.PREF_PHYSICAL_KEYBOARD_SUGGESTION_SHORTCUTS)
},
Setting(context, Settings.PREF_CUSTOM_CURRENCY_KEY, R.string.customize_currencies) { setting ->
var showDialog by rememberSaveable { mutableStateOf(false) }
Preference(
name = setting.title,
onClick = { showDialog = true }
)
if (showDialog) {
val prefs = LocalContext.current.prefs()
TextInputDialog(
onDismissRequest = { showDialog = false },
textInputLabel = { Text(stringResource(R.string.customize_currencies_detail)) },
initialText = prefs.getString(setting.key, Defaults.PREF_CUSTOM_CURRENCY_KEY) ?: Defaults.PREF_CUSTOM_CURRENCY_KEY,
onConfirmed = { prefs.edit { putString(setting.key, it) }; KeyboardLayoutSet.onSystemLocaleChanged() },
title = { Text(stringResource(R.string.customize_currencies)) },
neutralButtonText = if (prefs.contains(setting.key)) stringResource(R.string.button_default) else null,
onNeutral = { prefs.edit { remove(setting.key)}; KeyboardLayoutSet.onSystemLocaleChanged() },
checkTextValid = { text -> text.splitOnWhitespace().none { it.length > 8 } }
)
}
},
Setting(context, SettingsWithoutKey.BACKUP_RESTORE, R.string.backup_restore_title) {
BackupRestorePreference(it)
},
Setting(context, SettingsWithoutKey.APP_QUIRKS, R.string.app_quirks_title) {
Preference(
name = it.title,
description = stringResource(R.string.app_quirks_summary),
onClick = { SettingsDestination.navigateTo(SettingsDestination.AppQuirks) }
) { NextScreenIcon() }
},
Setting(context, SettingsWithoutKey.BACKGROUND_SERVICES, R.string.background_services_title) {
Preference(
name = it.title,
description = stringResource(R.string.background_services_summary),
onClick = { SettingsDestination.navigateTo(SettingsDestination.BackgroundServices) }
) { NextScreenIcon() }
},
Setting(context, Settings.PREF_TIMESTAMP_FORMAT, R.string.timestamp_format_title) { setting ->
TextInputPreference(setting, Defaults.PREF_TIMESTAMP_FORMAT) { checkTimestampFormat(it) }
},
Setting(context, SettingsWithoutKey.DEBUG_SETTINGS, R.string.debug_settings_title) {
Preference(
name = it.title,
Expand Down
Loading
Loading