Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 2cb7265

Browse files
authored
Merge pull request #955 from matrix-org/fix/android-composition-issues-with-compose
Android: fix composition issues with Compose
2 parents 24de89f + b092080 commit 2cb7265

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

platforms/android/library/src/main/java/io/element/android/wysiwyg/EditorEditText.kt

+19-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.text.TextWatcher
1414
import android.util.AttributeSet
1515
import android.view.KeyEvent
1616
import android.view.MotionEvent
17+
import android.view.inputmethod.BaseInputConnection
1718
import android.view.inputmethod.EditorInfo
1819
import android.view.inputmethod.InputConnection
1920
import android.widget.EditText
@@ -77,8 +78,6 @@ class EditorEditText : AppCompatEditText {
7778
set(value) {
7879
field = value
7980
viewModel.htmlConverter = value
80-
81-
rerender()
8281
}
8382

8483
private fun createHtmlConverter(styleConfig: StyleConfig, mentionDisplayHandler: MentionDisplayHandler?): HtmlConverter {
@@ -282,13 +281,23 @@ class EditorEditText : AppCompatEditText {
282281
* @param mentionDisplayHandler Used to decide how to display any mentions found in the HTML text.
283282
*/
284283
fun updateStyle(styleConfig: StyleConfig, mentionDisplayHandler: MentionDisplayHandler?) {
285-
this.styleConfig = styleConfig
286-
this.mentionDisplayHandler = mentionDisplayHandler
284+
var forceNewRendering = false
285+
val hasNewStyle = !this::styleConfig.isInitialized || this.styleConfig != styleConfig
286+
val hasNewMentionDisplayHandler = (this.mentionDisplayHandler as? MemoizingMentionDisplayHandler)
287+
?.delegateEquals(mentionDisplayHandler) != true
288+
if (hasNewStyle || hasNewMentionDisplayHandler) {
289+
this.styleConfig = styleConfig
290+
this.mentionDisplayHandler = mentionDisplayHandler
291+
forceNewRendering = true
292+
}
287293

288294
inlineCodeBgHelper = SpanBackgroundHelperFactory.createInlineCodeBackgroundHelper(styleConfig.inlineCode)
289295
codeBlockBgHelper = SpanBackgroundHelperFactory.createCodeBlockBackgroundHelper(styleConfig.codeBlock)
290296

291297
htmlConverter = createHtmlConverter(styleConfig, mentionDisplayHandler)
298+
if (forceNewRendering) {
299+
rerender()
300+
}
292301
}
293302

294303
fun setOnRichContentSelected(onRichContentSelected: ((Uri) -> Unit)?) {
@@ -600,8 +609,14 @@ class EditorEditText : AppCompatEditText {
600609
* will be updated to reflect this.
601610
*/
602611
private fun rerender() {
612+
val compositionStart = BaseInputConnection.getComposingSpanStart(editableText)
613+
val compositionEnd = BaseInputConnection.getComposingSpanEnd(editableText)
603614
val text = viewModel.rerender()
604615
setTextFromComposerUpdate(text)
616+
val indices = text.indices
617+
if (compositionStart in indices && compositionEnd in indices) {
618+
inputConnection?.setComposingRegion(compositionStart, compositionEnd)
619+
}
605620
}
606621

607622
private fun setTextFromComposerUpdate(text: CharSequence) {

platforms/android/library/src/main/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnection.kt

+4-12
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,6 @@ internal class InterceptInputConnection(
121121
// Called when started typing
122122
override fun setComposingText(text: CharSequence, newCursorPosition: Int): Boolean {
123123
val (start, end) = getCurrentCompositionOrSelection()
124-
// Some Chinese keyboards send empty text when the user is typing English characters
125-
if (text.isEmpty() && Selection.getSelectionStart(editable) == Selection.getSelectionEnd(editable)) {
126-
finishComposingText()
127-
return false
128-
}
129124
val result = processTextEntry(text, start, end, null)
130125

131126
return if (result != null) {
@@ -284,13 +279,10 @@ internal class InterceptInputConnection(
284279
// append to the current composition with the hardware keyboard.
285280
finishComposingText()
286281

287-
return if (newChars.isDigitsOnly()) {
288-
// Digits should be sent using `commitText`, as that's the default behaviour in the IME
289-
commitText(newChars, 1)
290-
} else {
291-
// Any other printable character can be sent using `setComposingText`
292-
setComposingText(newChars, 1)
293-
}
282+
// We previously tried to have hardware keys behave as composing text, but it's not
283+
// possible, we run into issues where existing keyboards (i.e. Trime) send empty composing
284+
// text and that breaks the input. So we need to use `commitText` instead.
285+
return commitText(newChars, 1)
294286
}
295287

296288
override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {

platforms/android/library/src/main/java/io/element/android/wysiwyg/internal/display/MemoizedLinkDisplayHandler.kt

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ internal class MemoizingMentionDisplayHandler(
3838

3939
return calculated
4040
}
41+
42+
fun delegateEquals(other: MentionDisplayHandler?): Boolean {
43+
return delegate == other
44+
}
4145
}

0 commit comments

Comments
 (0)