scoop:scoop.ui:0.1.0
src/text/editor/keyboard.scoop
Selected production source · src/text/editor/keyboard.scoop
1
package scoop.ui.text3
import scoop.graphics.Rect4
import scoop.surface.FocusOrigin5
import scoop.ui.*6
import scoop.window.*8
public fun TextEditHost.requestSoftwareKeyboard(visible: Boolean = true): SoftwareKeyboardRequest {9
checkOpen()10
return keyboard.request(visible)11
}13
/** Activates an editor through an outer control hit area without changing its selection. */14
public fun TextEditHost.requestFocus(15
origin: FocusOrigin = FocusOrigin.Programmatic,16
softwareKeyboard: Boolean = false17
): Boolean {18
checkOpen()19
if (!options.enabled || !focus.handle.requestFocus(origin))20
return false21
if (softwareKeyboard)22
keyboard.touch()23
return true24
}26
/** One request per explicit contact action, with no retry when the OS declines. */27
internal class EditorKeyboard(private val host: TextEditHost): Closeable {28
private var connection: TextInputConnection? = None29
private var purpose = TextInputPurpose.Normal30
private var pending = false31
private val activity =32
host.scope.observeWindowActivity {33
value -> if (!value.focused || !value.visible || value.occluded)34
deactivate()35
}37
private val available: Boolean get() =38
!host.closed &&39
host.options.enabled &&40
!host.options.readOnly &&41
host.area.isFocusActive &&42
host.area.scope.isInputAllowed44
fun publish() {45
if (!available)46
return47
val current = host.connection ?: return48
val desired = editorInputPurpose(host.options)49
if (connection != Some(current) || purpose != desired) {50
host.window.setTextInputPurpose(current, desired)51
connection = Some(current)52
purpose = desired53
}54
if (pending && host.options.softwareKeyboard && !host.retiring) {55
pending = false56
request(true)57
}58
}59
fun touch() {60
if (!available || !host.options.softwareKeyboard)61
return62
pending = true63
host.publishNative()64
}65
fun request(visible: Boolean): SoftwareKeyboardRequest {66
pending = false67
if (!available || !host.sessionEnabled || host.retiring)68
return SoftwareKeyboardRequest.Unavailable69
val current = host.connection ?: return SoftwareKeyboardRequest.Unavailable70
return host.window.requestSoftwareKeyboard(current, visible)71
}72
fun cancelPending() {73
pending = false74
}75
fun connectionClosed() {76
connection = None77
}78
fun update(rebound: Boolean) {79
if (80
rebound ||81
!host.options.enabled ||82
host.options.readOnly ||83
!host.options.softwareKeyboard84
)85
deactivate()86
}87
fun changed(state: SoftwareKeyboardState) {88
host.focus.node.root.focus.clearKeyboard(Some(host.focus))89
if (!available || !host.options.softwareKeyboard)90
return91
when (state) {92
case Shown(Some(_)) -> {93
host.revealNeeded = true94
host.revealRevision = host.focus.node.root.surface.focusRevision95
host.scope.requestFrame()96
}97
else -> {}98
}99
}100
fun reveal(caret: Rect) {101
val overlap =102
if (host.options.softwareKeyboard)103
when (host.window.softwareKeyboard) {104
case Shown(Some(val value)) -> Some(105
Rect(value.origin.x, value.origin.y, value.size.width, value.size.height)106
)107
else -> None108
}109
else110
None111
host.focus.node.root.focus.reveal(host.focus, caret, overlap)112
}113
fun deactivate() {114
pending = false115
connection = None116
host.focus.node.root.focus.clearKeyboard(Some(host.focus))117
}118
public override fun close() {119
deactivate()120
activity.close()121
}122
}124
internal fun editorInputPurpose(options: TextEditOptions): TextInputPurpose =125
if (options.protectedText)126
TextInputPurpose.Password127
else128
options.inputPurpose