Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/keyboard.scoop

Selected production source · src/text/editor/keyboard.scoop

1package scoop.ui.text
3import scoop.graphics.Rect
4import scoop.surface.FocusOrigin
5import scoop.ui.*
6import scoop.window.*
8public fun TextEditHost.requestSoftwareKeyboard(visible: Boolean = true): SoftwareKeyboardRequest {
9 checkOpen()
10 return keyboard.request(visible)
13/** Activates an editor through an outer control hit area without changing its selection. */
14public fun TextEditHost.requestFocus(
15 origin: FocusOrigin = FocusOrigin.Programmatic,
16 softwareKeyboard: Boolean = false
17): Boolean {
18 checkOpen()
19 if (!options.enabled || !focus.handle.requestFocus(origin))
20 return false
21 if (softwareKeyboard)
22 keyboard.touch()
23 return true
26/** One request per explicit contact action, with no retry when the OS declines. */
27internal class EditorKeyboard(private val host: TextEditHost): Closeable {
28 private var connection: TextInputConnection? = None
29 private var purpose = TextInputPurpose.Normal
30 private var pending = false
31 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.isInputAllowed
44 fun publish() {
45 if (!available)
46 return
47 val current = host.connection ?: return
48 val desired = editorInputPurpose(host.options)
49 if (connection != Some(current) || purpose != desired) {
50 host.window.setTextInputPurpose(current, desired)
51 connection = Some(current)
52 purpose = desired
53 }
54 if (pending && host.options.softwareKeyboard && !host.retiring) {
55 pending = false
56 request(true)
57 }
58 }
59 fun touch() {
60 if (!available || !host.options.softwareKeyboard)
61 return
62 pending = true
63 host.publishNative()
64 }
65 fun request(visible: Boolean): SoftwareKeyboardRequest {
66 pending = false
67 if (!available || !host.sessionEnabled || host.retiring)
68 return SoftwareKeyboardRequest.Unavailable
69 val current = host.connection ?: return SoftwareKeyboardRequest.Unavailable
70 return host.window.requestSoftwareKeyboard(current, visible)
71 }
72 fun cancelPending() {
73 pending = false
74 }
75 fun connectionClosed() {
76 connection = None
77 }
78 fun update(rebound: Boolean) {
79 if (
80 rebound ||
81 !host.options.enabled ||
82 host.options.readOnly ||
83 !host.options.softwareKeyboard
84 )
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 return
91 when (state) {
92 case Shown(Some(_)) -> {
93 host.revealNeeded = true
94 host.revealRevision = host.focus.node.root.surface.focusRevision
95 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 -> None
108 }
109 else
110 None
111 host.focus.node.root.focus.reveal(host.focus, caret, overlap)
112 }
113 fun deactivate() {
114 pending = false
115 connection = None
116 host.focus.node.root.focus.clearKeyboard(Some(host.focus))
117 }
118 public override fun close() {
119 deactivate()
120 activity.close()
121 }
124internal fun editorInputPurpose(options: TextEditOptions): TextInputPurpose =
125 if (options.protectedText)
126 TextInputPurpose.Password
127 else
128 options.inputPurpose