scoop:scoop.surface:0.1.0
src/shared/input/text_continuation.scoop
Selected production source · src/shared/input/text_continuation.scoop
1
package scoop.surface3
import scoop.window.*5
/** A pending receiver change for the same native text document. */6
internal class InputTextContinuation(7
val source: InputArea,8
val target: InputArea,9
val connection: TextInputConnection,10
val revision: ULong11
)13
internal fun InputRouter.continueTextInput(source: InputArea, target: InputArea): Boolean {14
checkOpen()15
requireInput(16
source.owner === this && target.owner === this,17
"text areas belong to different windows"18
)19
if (source === target || !source.hasFocus || !source.isFocusActive)20
return false21
val connection = imeConnection ?: return false22
val value = InputTextContinuation(source, target, connection, source.textInputRevision)23
if (!acceptsTextContinuation(value))24
return false25
textContinuation = Some(value)26
return true27
}29
private fun InputRouter.acceptsTextContinuation(value: InputTextContinuation): Boolean =30
osFocused &&31
!value.source.closed &&32
!value.target.closed &&33
value.source.textInput &&34
value.target.textInput &&35
allowsFocus(value.source.scope) &&36
allowsFocus(value.target.scope) &&37
value.source.textInputRevision ==38
value.revision &&39
imeRequest ==40
value.revision &&41
imeConnection ==42
Some(value.connection) &&43
when (ime) {44
case On(val area) -> area === value.source45
else -> false46
}48
internal fun InputRouter.validateTextContinuation() {49
when (textContinuation) {50
case Some(val value) -> if (!acceptsTextContinuation(value))51
textContinuation = None52
case None -> {}53
}54
}56
internal fun InputRouter.completeTextContinuation(previous: InputArea?, next: InputArea?) {57
val value = textContinuation ?: return58
textContinuation = None59
if (!sameArea(previous, Some(value.source)) || !sameArea(next, Some(value.target)))60
return61
if (!acceptsTextContinuation(value))62
return63
ime = ImeSession.On(value.target)64
imeRequest = value.target.textInputRevision65
imeCaret = None66
// The receiving editor adopts the shared document before the source retires it.67
var failure =68
cleanupFailure(None) {69
notify(70
value.target.scope,71
InputEvent.TextInput(TextInputEvent.Enabled(Some(value.connection)))72
)73
}74
failure =75
cleanupFailure(failure) {76
notify(77
value.source.scope,78
InputEvent.TextInput(TextInputEvent.Disabled(Some(value.connection))),79
inactive = true80
)81
}82
throwFailure(failure)83
}