diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt index f894e8993b..79c65068c1 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.kt @@ -54,6 +54,7 @@ import helium314.keyboard.latin.cjk.CjkCandidateStripView import helium314.keyboard.latin.cjk.CjkInputManager import helium314.keyboard.latin.settings.SettingsValues import helium314.keyboard.latin.suggestions.SuggestionStripView +import helium314.keyboard.latin.utils.ToolbarKey import helium314.keyboard.latin.utils.CapsModeUtils import helium314.keyboard.latin.utils.LanguageOnSpacebarUtils import helium314.keyboard.latin.utils.Log @@ -1188,8 +1189,9 @@ class KeyboardSwitcher private constructor() : KeyboardState.SwitchActions { } } - fun showLoadingAnimation() { - mSuggestionStripView?.showLoadingAnimation() + /** [key]: the toolbar key that started the AI request, it shows an X while the request runs */ + fun showLoadingAnimation(key: ToolbarKey = ToolbarKey.PROOFREAD) { + mSuggestionStripView?.showLoadingAnimation(key) } fun hideLoadingAnimation() { diff --git a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt index 4ead40cfc4..6a04db1bfa 100644 --- a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt +++ b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt @@ -69,6 +69,7 @@ import helium314.keyboard.latin.utils.CustomAIKeyText import helium314.keyboard.latin.utils.TextExpanderUtils import helium314.keyboard.latin.utils.TextPlacement import helium314.keyboard.latin.utils.TextRange +import helium314.keyboard.latin.utils.ToolbarKey import helium314.keyboard.latin.utils.getTimestamp import java.text.BreakIterator import java.util.ArrayList @@ -749,6 +750,12 @@ class InputLogic( } private fun handleTranslate() { + // a second tap while the AI is working cancels it + if (ProofreadHelper.isOperationInProgress) { + ProofreadHelper.cancelCurrentOperation() + KeyboardSwitcher.getInstance().showToast(mLatinIME.getString(R.string.ai_cancelled), true) + return + } val textToTranslate: String val hasSelection = mConnection.hasSelection() @@ -3161,6 +3168,12 @@ class InputLogic( } private fun handleCustomAIKey(index: Int) { + // a second tap while the AI is working cancels it + if (ProofreadHelper.isOperationInProgress) { + ProofreadHelper.cancelCurrentOperation() + KeyboardSwitcher.getInstance().showToast(mLatinIME.getString(R.string.ai_cancelled), true) + return + } val prefs = helium314.keyboard.latin.utils.DeviceProtectedUtils.getSharedPreferences(mLatinIME) var prompt = prefs.getString("pref_custom_ai_prompt_$index", "") ?: "" val systemInstructionBuilder = StringBuilder() @@ -3271,6 +3284,7 @@ class InputLogic( ProofreadHelper.customAsync( mLatinIME, textToProcess, prompt, hasSelection, showThinking, + toolbarKey = ToolbarKey.entries.firstOrNull { it.name == "CUSTOM_AI_$index" }, onSuccess = { resultText -> if (!mLatinIME.isInputViewShown || !mConnection.isConnected()) { Log.w(TAG, "Input view hidden or disconnected during custom AI operation, ignoring result") diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt index ed78cd004b..c9c13f7c38 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt @@ -176,6 +176,10 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) setColor(Color.TRANSPARENT) } private var isLoadingAnimationActive = false + /** the toolbar key that started the running AI request, it shows an X */ + private var loadingKey: ToolbarKey? = null + /** buttons showing the X, with their own icon */ + private val loadingKeyIcons = mutableListOf>() private val keyDimension: Int get() { @@ -673,7 +677,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) * Shows pulse border loading animation on the whole toolbar. * Used during proofreading/translation API calls. */ - fun showLoadingAnimation() { + fun showLoadingAnimation(activeKey: ToolbarKey = ToolbarKey.PROOFREAD) { if (isLoadingAnimationActive) return if (!isAttachedToWindow || visibility != View.VISIBLE || windowVisibility != View.VISIBLE) return isLoadingAnimationActive = true @@ -681,15 +685,15 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) // Set loading border on the whole toolbar view this.foreground = loadingBorderDrawable - // Change proofread key icon to cancel/close icon - val closeIcon = KeyboardIconsSet.instance.getNewDrawable(ToolbarKey.CLOSE_HISTORY.name.lowercase(java.util.Locale.US), context) - if (closeIcon != null) { - val proofreadKey = toolbar.findViewWithTag(ToolbarKey.PROOFREAD) - ?: pinnedKeys.findViewWithTag(ToolbarKey.PROOFREAD) - proofreadKey?.setImageDrawable(closeIcon) - if (proofreadKey != null) { - Settings.getValues().mColors.setColor(proofreadKey, ColorType.TOOL_BAR_KEY) - } + // the key that started the request shows an X: tapping it again cancels + loadingKey = activeKey + for (container in listOf(toolbar, pinnedKeys)) { + val button = container.findViewWithTag(activeKey) ?: continue + val closeIcon = KeyboardIconsSet.instance.getNewDrawable(ToolbarKey.CLOSE_HISTORY.name.lowercase(java.util.Locale.US), context) + ?: continue + loadingKeyIcons.add(button to button.drawable) + button.setImageDrawable(closeIcon) + Settings.getValues().mColors.setColor(button, ColorType.TOOL_BAR_KEY) } // Get accent color from theme (GESTURE_TRAIL is the accent color) @@ -721,16 +725,19 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) loadingBorderDrawable.setStroke(4, Color.TRANSPARENT) this.foreground = null - // Restore proofread key icon - val proofreadIcon = KeyboardIconsSet.instance.getNewDrawable(ToolbarKey.PROOFREAD.name.lowercase(java.util.Locale.US), context) - if (proofreadIcon != null) { - val proofreadKey = toolbar.findViewWithTag(ToolbarKey.PROOFREAD) - ?: pinnedKeys.findViewWithTag(ToolbarKey.PROOFREAD) - proofreadKey?.setImageDrawable(proofreadIcon) - if (proofreadKey != null) { - Settings.getValues().mColors.setColor(proofreadKey, ColorType.TOOL_BAR_KEY) + // restore the icon of the key that showed the X; buttons are looked up again, as the toolbar + // may have been rebuilt meanwhile (then the new buttons already have their own icon) + val key = loadingKey + loadingKey = null + if (key != null) { + for (container in listOf(toolbar, pinnedKeys)) { + val button = container.findViewWithTag(key) ?: continue + val icon = loadingKeyIcons.firstOrNull { it.first === button } ?: continue + button.setImageDrawable(icon.second) + Settings.getValues().mColors.setColor(button, ColorType.TOOL_BAR_KEY) } } + loadingKeyIcons.clear() } private var isVoiceActive = false diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d6d569facb..40a89c18d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2124,6 +2124,7 @@ New dictionary: No text to proofread Gemini API key not set. Go to Settings → AI integration. Proofread output truncated by model; replacement aborted. + AI request cancelled Translation output truncated by model; replacement aborted. Translation unchanged or identical to source text. Error getting suggestions diff --git a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt index 97d37d7b61..f84f813d12 100644 --- a/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt +++ b/app/src/offline/java/helium314/keyboard/latin/utils/ProofreadHelper.kt @@ -76,6 +76,8 @@ object ProofreadHelper { text: String, noTextErrorResId: Int, errorResId: Int, + /** the toolbar key that started the request, it shows an X while the request runs */ + loadingKey: ToolbarKey = ToolbarKey.PROOFREAD, skipModelCheck: Boolean = false, apiCall: suspend (ProofreadService) -> Result, onSuccess: (String) -> Unit, @@ -109,7 +111,7 @@ object ProofreadHelper { // Show loading animation on suggestion strip mainHandler.post { - KeyboardSwitcher.getInstance().showLoadingAnimation() + KeyboardSwitcher.getInstance().showLoadingAnimation(loadingKey) } // Launch coroutine for inference and track it for cancellation @@ -320,6 +322,7 @@ object ProofreadHelper { text = text, noTextErrorResId = R.string.translate_no_text, errorResId = R.string.translate_error, + loadingKey = ToolbarKey.TRANSLATE, skipModelCheck = usePlugin, apiCall = { service -> val pluginProvider = if (usePlugin) TranslationLoader.getProvider(context) else null @@ -461,6 +464,8 @@ object ProofreadHelper { prompt: String, hasSelection: Boolean, showThinking: Boolean, + /** the key that started the request, it shows an X while it runs */ + toolbarKey: ToolbarKey? = null, onSuccess: (String) -> Unit, onError: (String) -> Unit ) { @@ -469,6 +474,7 @@ object ProofreadHelper { text = text, noTextErrorResId = R.string.proofread_no_text, errorResId = R.string.proofread_error, + loadingKey = toolbarKey ?: ToolbarKey.PROOFREAD, apiCall = { service -> service.proofread(text, overridePrompt = prompt, showThinking = showThinking) }, onSuccess = onSuccess, onError = onError diff --git a/app/src/standard/java/helium314/keyboard/latin/utils/ProofreadHelper.kt b/app/src/standard/java/helium314/keyboard/latin/utils/ProofreadHelper.kt index 848ef7b4fa..1403055987 100644 --- a/app/src/standard/java/helium314/keyboard/latin/utils/ProofreadHelper.kt +++ b/app/src/standard/java/helium314/keyboard/latin/utils/ProofreadHelper.kt @@ -67,6 +67,8 @@ object ProofreadHelper { text: String, noTextErrorResId: Int, errorResId: Int, + /** the toolbar key that started the request, it shows an X while the request runs */ + loadingKey: ToolbarKey = ToolbarKey.PROOFREAD, apiCall: suspend (ProofreadService) -> Result, onSuccess: (String) -> Unit, onError: (String) -> Unit, @@ -140,7 +142,7 @@ object ProofreadHelper { // Show loading animation on suggestion strip mainHandler.post { - KeyboardSwitcher.getInstance().showLoadingAnimation() + KeyboardSwitcher.getInstance().showLoadingAnimation(loadingKey) } // Launch coroutine for API call and track it for cancellation @@ -374,6 +376,7 @@ object ProofreadHelper { text = text, noTextErrorResId = R.string.translate_no_text, errorResId = R.string.translate_error, + loadingKey = ToolbarKey.TRANSLATE, skipApiKeyCheck = usePlugin || isOfflineOnly, apiCall = { service -> val pluginProvider = if (usePlugin) helium314.keyboard.latin.translation.TranslationLoader.getProvider(context) else null @@ -529,6 +532,8 @@ object ProofreadHelper { prompt: String, hasSelection: Boolean, showThinking: Boolean, + /** the key that started the request, it shows an X while it runs */ + toolbarKey: ToolbarKey? = null, onSuccess: (String) -> Unit, onError: (String) -> Unit ) { @@ -537,6 +542,7 @@ object ProofreadHelper { text = text, noTextErrorResId = R.string.proofread_no_text, errorResId = R.string.proofread_error, + loadingKey = toolbarKey ?: ToolbarKey.PROOFREAD, apiCall = { service -> service.proofread(text, overridePrompt = prompt, showThinking = showThinking) }, onSuccess = onSuccess, onError = onError,