Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<ImageButton, Drawable?>>()

private val keyDimension: Int
get() {
Expand Down Expand Up @@ -673,23 +677,23 @@ 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

// 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<ImageButton>(ToolbarKey.PROOFREAD)
?: pinnedKeys.findViewWithTag<ImageButton>(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<ImageButton>(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)
Expand Down Expand Up @@ -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<ImageButton>(ToolbarKey.PROOFREAD)
?: pinnedKeys.findViewWithTag<ImageButton>(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<ImageButton>(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
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,7 @@ New dictionary:
<string name="proofread_no_text">No text to proofread</string>
<string name="proofread_no_api_key">Gemini API key not set. Go to Settings → AI integration.</string>
<string name="proofread_output_truncated">Proofread output truncated by model; replacement aborted.</string>
<string name="ai_cancelled">AI request cancelled</string>
<string name="translation_output_truncated">Translation output truncated by model; replacement aborted.</string>
<string name="translation_unchanged">Translation unchanged or identical to source text.</string>
<string name="error_getting_suggestions">Error getting suggestions</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
onSuccess: (String) -> Unit,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
onSuccess: (String) -> Unit,
onError: (String) -> Unit,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
) {
Expand All @@ -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,
Expand Down
Loading