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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.kazumaproject.markdownhelperkeyboard.converter.candidate

const val QWERTY_GLIDE_CANDIDATE_TYPE: Byte = 45
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kazumaproject.markdownhelperkeyboard.converter.glide

import com.kazumaproject.markdownhelperkeyboard.converter.candidate.Candidate
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.QWERTY_GLIDE_CANDIDATE_TYPE
import com.kazumaproject.qwerty_keyboard.glide.QwertyInputPointers
import com.kazumaproject.qwerty_keyboard.glide.QwertyKeyboardProximityInfo
import kotlin.math.ceil
Expand Down Expand Up @@ -113,7 +114,7 @@ class QwertyGlideDecoder(
.map { scoredWord ->
Candidate(
string = scoredWord.entry.word,
type = 36.toByte(),
type = QWERTY_GLIDE_CANDIDATE_TYPE,
length = scoredWord.entry.word.length.toUByte(),
score = (scoredWord.totalCost * 1000f).toInt().coerceAtLeast(1)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ import com.kazumaproject.markdownhelperkeyboard.clipboard_history.database.Clipb
import com.kazumaproject.markdownhelperkeyboard.clipboard_history.database.ItemType
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.BunsetsuCandidateResult
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.Candidate
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.QWERTY_GLIDE_CANDIDATE_TYPE
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.ZenzCandidate
import com.kazumaproject.markdownhelperkeyboard.converter.engine.EnglishEngine
import com.kazumaproject.markdownhelperkeyboard.converter.engine.KanaKanjiEngine
Expand Down Expand Up @@ -211,9 +212,6 @@ import com.kazumaproject.markdownhelperkeyboard.setting_activity.circular_slot.C
import com.kazumaproject.markdownhelperkeyboard.short_cut.ShortcutType
import com.kazumaproject.markdownhelperkeyboard.variant.AppVariantConfig
import com.kazumaproject.qwerty_keyboard.ui.QWERTYKeyboardView
import com.kazumaproject.qwerty_keyboard.glide.QwertyGlideInputListener
import com.kazumaproject.qwerty_keyboard.glide.QwertyInputPointers
import com.kazumaproject.qwerty_keyboard.glide.QwertyKeyboardProximityInfo
import com.kazumaproject.symbol_keyboard.CustomSymbolKeyboardView
import com.kazumaproject.tenkey.TenKey
import com.kazumaproject.tenkey.extensions.getDakutenFlickLeft
Expand Down Expand Up @@ -597,6 +595,8 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
private var qwertyRomajiHankakuSymbolPreference: Boolean? = false
private var qwertyShowPopupWindowPreference: Boolean? = true
private var qwertyGlideInputPreference: Boolean = false
private var qwertyGlideCommitPreviousCandidateOnNewGlidePreference: Boolean = false
private var qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference: Boolean = false
private var qwertyShowCursorButtonsPreference: Boolean? = false
private var qwertyShowNumberButtonsPreference: Boolean? = false
private var qwertyShowSwitchRomajiEnglishPreference: Boolean? = false
Expand Down Expand Up @@ -739,6 +739,7 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
private var qwertySwitchNumberKeyWithoutNumberPreference: Boolean? = false
private var qwertyGlideInputCoordinator: QwertyGlideInputCoordinator? = null
private var suppressNextQwertyGlideSuggestionRefresh: Boolean = false
private var currentQwertyGlideCompositionText: String? = null

private var customRomajiZenkakuConversionEnablePreference: Boolean? = true

Expand Down Expand Up @@ -1292,9 +1293,14 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
qwertyShowSwitchRomajiEnglishPreference =
preferences.qwertyShowSwitchRomajiEnglishPreference
qwertyGlideInputPreference = preferences.qwertyGlideInputPreference
qwertyGlideCommitPreviousCandidateOnNewGlidePreference =
preferences.qwertyGlideCommitPreviousCandidateOnNewGlidePreference
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference =
preferences.qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference
if (qwertyGlidePreferenceChanged) {
qwertyGlideInputCoordinator?.cancelPending()
englishEngine.invalidateQwertyGlideCache()
currentQwertyGlideCompositionText = null
}
if (preferences.qwertyGlideInputPreference) {
englishEngine.warmUpQwertyGlideDecoderAsync()
Expand Down Expand Up @@ -13286,6 +13292,9 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
onFinalCandidates = { candidates ->
showQwertyGlideCandidates(candidates, applyFirstCandidate = true)
},
onGlideStarted = {
commitPreviousQwertyGlideCandidateOnNewGlideIfNeeded()
},
onCancel = {},
onProcessingChanged = { isProcessing ->
setSuggestionProgressVisible(
Expand Down Expand Up @@ -13857,6 +13866,13 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
clearDeletedBuffer()
refreshEditHistoryUi()
val first = candidates.first()
currentQwertyGlideCompositionText = if (
first.type == QWERTY_GLIDE_CANDIDATE_TYPE
) {
first.string
} else {
null
}
suppressNextQwertyGlideSuggestionRefresh = true
_inputString.update { first.string }
val spannable = createSpannableWithTail(first.string)
Expand All @@ -13878,6 +13894,35 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
}
}

private fun commitPreviousQwertyGlideCandidateOnNewGlideIfNeeded() {
val firstCandidate = suggestionAdapter?.suggestions?.firstOrNull()
val decision = QwertyGlideCommitPolicy.resolvePreviousGlideCommitDecision(
qwertyGlideInputPreference = qwertyGlideInputPreference,
qwertyGlideCommitPreviousCandidateOnNewGlidePreference =
qwertyGlideCommitPreviousCandidateOnNewGlidePreference,
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference =
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference,
inputString = inputString.value,
stringInTail = stringInTail.get(),
currentQwertyRomajiModeForSession = currentQwertyRomajiModeForSession,
firstCandidate = firstCandidate,
currentQwertyGlideCompositionText = currentQwertyGlideCompositionText
)
if (decision !is QwertyGlidePreviousCandidateCommitDecision.Commit) return

beginBatchEdit()
try {
setComposingText("", 0)
finishComposingText()
commitText(decision.commitText, 1)
} finally {
endBatchEdit()
}
stringInTail.set("")
currentQwertyGlideCompositionText = null
resetFlagsSuggestionClick()
}

private suspend fun setSymbols(mainView: MainLayoutBinding) {
coroutineScope {
if (cachedEmoji == null || cachedEmoticons == null || cachedSymbols == null) {
Expand Down Expand Up @@ -13992,6 +14037,14 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
if (insertString.isNotEmpty()) {
isHenkan.set(false)
henkanPressedWithBunsetsuDetect = false
val qwertyGlideDecision = QwertyGlideCommitPolicy.resolveTapCommitDecision(
candidate = candidate,
insertString = insertString
)
if (qwertyGlideDecision is QwertyGlideTapCommitDecision.CommitQwertyGlideCandidate) {
commitQwertyGlideCandidate(candidate)
return
}
processCandidate(
candidate = candidate,
insertString = insertString,
Expand Down Expand Up @@ -14682,10 +14735,32 @@ class IMEService : InputMethodService(), LifecycleOwner, InputConnection,
commitAndClearInput(candidateString)
}

private fun commitQwertyGlideCandidate(candidate: Candidate) {
beginBatchEdit()
try {
setComposingText("", 0)
finishComposingText()
commitText(candidate.string, 1)
} finally {
endBatchEdit()
}
stringInTail.set("")
currentQwertyGlideCompositionText = null
resetFlagsSuggestionClick()
}

private fun processCandidate(
candidate: Candidate, insertString: String, currentInputMode: InputMode, position: Int
) {
Timber.d("processCandidate ${candidate.type.toInt()} ${insertString.length == candidate.length.toInt()}")
val qwertyGlideDecision = QwertyGlideCommitPolicy.resolveTapCommitDecision(
candidate = candidate,
insertString = insertString
)
if (qwertyGlideDecision is QwertyGlideTapCommitDecision.CommitQwertyGlideCandidate) {
commitQwertyGlideCandidate(candidate)
return
}
when (candidate.type.toInt()) {
15 -> {
val readingCorrection = candidate.string.correctReading()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ data class ImePreferencesSnapshot(
val qwertyShowNumberButtonsPreference: Boolean,
val qwertyShowSwitchRomajiEnglishPreference: Boolean,
val qwertyGlideInputPreference: Boolean,
val qwertyGlideCommitPreviousCandidateOnNewGlidePreference: Boolean,
val qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference: Boolean,
val qwertyShowPopupWindowPreference: Boolean,
val qwertyEnableFlickUpPreference: Boolean,
val qwertyEnableFlickDownPreference: Boolean,
Expand Down Expand Up @@ -203,6 +205,10 @@ data class ImePreferencesSnapshot(
qwertyShowSwitchRomajiEnglishPreference =
appPreference.qwerty_show_switch_romaji_english_button ?: true,
qwertyGlideInputPreference = appPreference.qwerty_glide_input_preference,
qwertyGlideCommitPreviousCandidateOnNewGlidePreference =
appPreference.qwerty_glide_commit_previous_candidate_on_new_glide_preference,
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference =
appPreference.qwerty_glide_insert_space_after_committing_previous_candidate_preference,
qwertyShowPopupWindowPreference = appPreference.qwerty_show_popup_window ?: true,
qwertyEnableFlickUpPreference =
appPreference.qwerty_enable_flick_up_preference ?: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.kazumaproject.markdownhelperkeyboard.ime_service

import com.kazumaproject.markdownhelperkeyboard.converter.candidate.Candidate
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.QWERTY_GLIDE_CANDIDATE_TYPE

sealed class QwertyGlideTapCommitDecision {
data class CommitQwertyGlideCandidate(val commitText: String) : QwertyGlideTapCommitDecision()
data class UseStandardCandidateFlow(val tailForPartialOrExcess: String?) :
QwertyGlideTapCommitDecision()
}

sealed class QwertyGlidePreviousCandidateCommitDecision {
data class Commit(val commitText: String) : QwertyGlidePreviousCandidateCommitDecision()
data object Skip : QwertyGlidePreviousCandidateCommitDecision()
}

object QwertyGlideCommitPolicy {
fun isQwertyGlideCandidate(candidate: Candidate): Boolean {
return candidate.type == QWERTY_GLIDE_CANDIDATE_TYPE
}

fun resolveTapCommitDecision(
candidate: Candidate,
insertString: String
): QwertyGlideTapCommitDecision {
if (isQwertyGlideCandidate(candidate)) {
return QwertyGlideTapCommitDecision.CommitQwertyGlideCandidate(candidate.string)
}
val tail = if (insertString.length > candidate.length.toInt()) {
insertString.substring(candidate.length.toInt())
} else {
null
}
return QwertyGlideTapCommitDecision.UseStandardCandidateFlow(tail)
}

fun shouldCommitPreviousGlideCandidateOnNewGlide(
qwertyGlideInputPreference: Boolean,
qwertyGlideCommitPreviousCandidateOnNewGlidePreference: Boolean,
inputString: String,
stringInTail: String,
currentQwertyRomajiModeForSession: Boolean,
firstCandidate: Candidate?,
currentQwertyGlideCompositionText: String?
): Boolean {
if (!qwertyGlideInputPreference) return false
if (!qwertyGlideCommitPreviousCandidateOnNewGlidePreference) return false
if (inputString.isEmpty()) return false
if (stringInTail.isNotEmpty()) return false
if (currentQwertyRomajiModeForSession) return false
val candidate = firstCandidate ?: return false
if (!isQwertyGlideCandidate(candidate)) return false
if (currentQwertyGlideCompositionText == null) return false
return inputString == candidate.string && currentQwertyGlideCompositionText == candidate.string
}

fun resolvePreviousGlideCommitText(
firstCandidate: Candidate,
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference: Boolean
): String {
return if (qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference) {
firstCandidate.string + " "
} else {
firstCandidate.string
}
}

fun resolvePreviousGlideCommitDecision(
qwertyGlideInputPreference: Boolean,
qwertyGlideCommitPreviousCandidateOnNewGlidePreference: Boolean,
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference: Boolean,
inputString: String,
stringInTail: String,
currentQwertyRomajiModeForSession: Boolean,
firstCandidate: Candidate?,
currentQwertyGlideCompositionText: String?
): QwertyGlidePreviousCandidateCommitDecision {
if (!shouldCommitPreviousGlideCandidateOnNewGlide(
qwertyGlideInputPreference = qwertyGlideInputPreference,
qwertyGlideCommitPreviousCandidateOnNewGlidePreference =
qwertyGlideCommitPreviousCandidateOnNewGlidePreference,
inputString = inputString,
stringInTail = stringInTail,
currentQwertyRomajiModeForSession = currentQwertyRomajiModeForSession,
firstCandidate = firstCandidate,
currentQwertyGlideCompositionText = currentQwertyGlideCompositionText
)
) {
return QwertyGlidePreviousCandidateCommitDecision.Skip
}
val candidate = requireNotNull(firstCandidate)
return QwertyGlidePreviousCandidateCommitDecision.Commit(
resolvePreviousGlideCommitText(
firstCandidate = candidate,
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference =
qwertyGlideInsertSpaceAfterCommittingPreviousCandidatePreference
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class QwertyGlideInputCoordinator(
private val previousTextProvider: () -> String,
private val onPreviewCandidates: (List<Candidate>) -> Unit,
private val onFinalCandidates: (List<Candidate>) -> Unit,
private val onGlideStarted: () -> Unit = {},
private val onCancel: () -> Unit = {},
private val onProcessingChanged: (Boolean) -> Unit = {},
private val decodeDispatcher: CoroutineDispatcher = Dispatchers.Default
Expand All @@ -39,6 +40,7 @@ class QwertyGlideInputCoordinator(
previewJob?.cancel()
finalJob?.cancel()
clearProcessing()
onGlideStarted()
}

override fun onQwertyGlideUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.kazumaproject.core.domain.extensions.setDrawableSolidColor
import com.kazumaproject.core.domain.state.TenKeyQWERTYMode
import com.kazumaproject.markdownhelperkeyboard.R
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.Candidate
import com.kazumaproject.markdownhelperkeyboard.converter.candidate.QWERTY_GLIDE_CANDIDATE_TYPE
import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.data.CustomKeyboardLayout
import com.kazumaproject.markdownhelperkeyboard.gemma.GemmaTranslationManager
import com.kazumaproject.markdownhelperkeyboard.ime_service.extensions.correctReading
Expand Down Expand Up @@ -638,6 +639,7 @@ class SuggestionAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
(38).toByte() -> ""
(39).toByte() -> ""
(40).toByte() -> "[AI]"
QWERTY_GLIDE_CANDIDATE_TYPE -> ""
GemmaTranslationManager.TRANSLATED_CANDIDATE_TYPE.toByte() -> "[訳]"
GemmaTranslationManager.PROMPT_RESULT_CANDIDATE_TYPE.toByte() -> "[AI]"
GemmaTranslationManager.SELECTION_TRANSLATE_ACTION_CANDIDATE_TYPE.toByte() -> "[訳]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ object AppPreference {
private val QWERTY_GLIDE_INPUT_PREFERENCE =
Pair("qwerty_glide_input_preference", false)

private val QWERTY_GLIDE_COMMIT_PREVIOUS_CANDIDATE_ON_NEW_GLIDE_PREFERENCE =
Pair("qwerty_glide_commit_previous_candidate_on_new_glide_preference", false)

private val QWERTY_GLIDE_INSERT_SPACE_AFTER_COMMITTING_PREVIOUS_CANDIDATE_PREFERENCE =
Pair("qwerty_glide_insert_space_after_committing_previous_candidate_preference", false)

private val QWERTY_ENABLE_FLICK_UP_WINDOW = Pair("qwerty_enable_flick_up_preference", false)

private val QWERTY_ENABLE_FLICK_DOWN_WINDOW = Pair("qwerty_enable_flick_down_preference", false)
Expand Down Expand Up @@ -664,6 +670,30 @@ object AppPreference {
it.putBoolean(QWERTY_GLIDE_INPUT_PREFERENCE.first, value)
}

var qwerty_glide_commit_previous_candidate_on_new_glide_preference: Boolean
get() = preferences.getBoolean(
QWERTY_GLIDE_COMMIT_PREVIOUS_CANDIDATE_ON_NEW_GLIDE_PREFERENCE.first,
QWERTY_GLIDE_COMMIT_PREVIOUS_CANDIDATE_ON_NEW_GLIDE_PREFERENCE.second
)
set(value) = preferences.edit {
it.putBoolean(
QWERTY_GLIDE_COMMIT_PREVIOUS_CANDIDATE_ON_NEW_GLIDE_PREFERENCE.first,
value
)
}

var qwerty_glide_insert_space_after_committing_previous_candidate_preference: Boolean
get() = preferences.getBoolean(
QWERTY_GLIDE_INSERT_SPACE_AFTER_COMMITTING_PREVIOUS_CANDIDATE_PREFERENCE.first,
QWERTY_GLIDE_INSERT_SPACE_AFTER_COMMITTING_PREVIOUS_CANDIDATE_PREFERENCE.second
)
set(value) = preferences.edit {
it.putBoolean(
QWERTY_GLIDE_INSERT_SPACE_AFTER_COMMITTING_PREVIOUS_CANDIDATE_PREFERENCE.first,
value
)
}

var switch_qwerty_password: Boolean?
get() = preferences.getBoolean(
SWITCH_QWERTY_PASSWORD.first, SWITCH_QWERTY_PASSWORD.second
Expand Down
Loading
Loading