0
0
mirror of https://github.com/florisboard/florisboard.git synced 2024-09-20 03:52:18 +02:00

Tie composing region indicator to suggestion enabled state (#1911)

This commit is contained in:
Patrick Goldinger 2022-06-19 23:46:14 +02:00
parent 9559dbdcd6
commit b36bcf7733
5 changed files with 25 additions and 11 deletions

View File

@ -112,7 +112,8 @@ private fun DevtoolsInputStateOverlay() {
DevtoolsText(text = "Before: \"${content.textBeforeSelection}\"")
DevtoolsText(text = "Selected: \"${content.selectedText}\"")
DevtoolsText(text = "After: \"${content.textAfterSelection}\"")
DevtoolsText(text = "ComposingWord: ${content.composing}")
DevtoolsText(text = "Composing: ${content.composing}")
DevtoolsText(text = "CurrentWord: ${content.currentWord}")
}
}
}

View File

@ -218,12 +218,13 @@ abstract class AbstractEditorInstance(context: Context) {
}
// Determine local composing word range, if any
val localComposing =
val localCurrentWord =
if (shouldDetermineComposingRegion(editorInfo) && localSelection.isCursorMode && textBeforeSelection.isNotEmpty()) {
determineLocalComposing(textBeforeSelection)
} else {
EditorRange.Unspecified
}
val localComposing = if (determineComposingEnabled()) localCurrentWord else EditorRange.Unspecified
// Build and publish text and content
val text = buildString {
@ -231,7 +232,7 @@ abstract class AbstractEditorInstance(context: Context) {
append(selectedText)
append(textAfterSelection)
}
return EditorContent(text, offset, localSelection, localComposing)
return EditorContent(text, offset, localSelection, localComposing, localCurrentWord)
}
private suspend fun EditorContent.generateCopy(
@ -255,6 +256,8 @@ abstract class AbstractEditorInstance(context: Context) {
}
}
abstract fun determineComposingEnabled(): Boolean
abstract fun determineComposer(composerName: ExtensionComponentName): Composer
protected open fun shouldDetermineComposingRegion(editorInfo: FlorisEditorInfo): Boolean {

View File

@ -29,6 +29,7 @@ data class EditorContent(
val offset: Int,
val localSelection: EditorRange,
val localComposing: EditorRange,
val localCurrentWord: EditorRange,
) {
val textBeforeSelection: String
get() = if (localSelection.isValid) text.safeSubstring(0, localSelection.start) else ""
@ -39,18 +40,23 @@ data class EditorContent(
val textAfterSelection: String
get() = if (localSelection.isValid) text.safeSubstring(localSelection.end) else ""
val composingText: String
get() = if (localComposing.isValid) text.safeSubstring(localComposing.start, localComposing.end) else ""
val selection: EditorRange
get() = if (offset > 0) localSelection.translatedBy(offset) else localSelection
val composing: EditorRange
get() = if (offset > 0) localComposing.translatedBy(offset) else localComposing
companion object {
val Unspecified = EditorContent("", -1, EditorRange.Unspecified, EditorRange.Unspecified)
val currentWord: EditorRange
get() = if (offset > 0) localCurrentWord.translatedBy(offset) else localCurrentWord
fun selectionOnly(selection: EditorRange) = EditorContent("", -1, selection, EditorRange.Unspecified)
val currentWordText: String
get() = if (localCurrentWord.isValid) text.safeSubstring(localCurrentWord.start, localCurrentWord.end) else ""
companion object {
val Unspecified =
EditorContent("", -1, EditorRange.Unspecified, EditorRange.Unspecified, EditorRange.Unspecified)
fun selectionOnly(selection: EditorRange) =
EditorContent("", -1, selection, EditorRange.Unspecified, EditorRange.Unspecified)
}
}

View File

@ -131,6 +131,10 @@ class EditorInstance(context: Context) : AbstractEditorInstance(context) {
super.handleFinishInputView()
}
override fun determineComposingEnabled(): Boolean {
return prefs.suggestion.enabled.get()
}
override fun determineComposer(composerName: ExtensionComponentName): Composer {
return keyboardManager.resources.composers.value?.get(composerName) ?: Appender.DefaultInstance
}
@ -276,7 +280,7 @@ class EditorInstance(context: Context) : AbstractEditorInstance(context) {
*/
fun deleteBackwards(): Boolean {
val content = activeContent
if (phantomSpace.isActive && content.composing.isValid && prefs.glide.immediateBackspaceDeletesWord.get()) {
if (phantomSpace.isActive && content.currentWord.isValid && prefs.glide.immediateBackspaceDeletesWord.get()) {
return deleteWordBackwards()
}
phantomSpace.setInactive()

View File

@ -165,7 +165,7 @@ class KeyboardManager(context: Context) : InputKeyEventReceiver {
nlpManager.clearSuggestions()
return@collect
}
nlpManager.suggest(content.composingText, listOf())
nlpManager.suggest(content.currentWordText, listOf())
}
}
}