scoop:scoop.ui:0.1.0
src/overlay/focus_session.scoop
Selected production source · src/overlay/focus_session.scoop
1
package scoop.ui3
import scoop.graphics.Rect4
import scoop.surface.*6
/** Retains the boundary until presentation or a non-drawable logical update removes the layer. */7
internal class OverlayFocusSession(8
private val portal: PortalInstance,9
initial: PortalOptions,10
private val previous: OverlayFocusSession?11
) {12
private val root = portal.root13
private val node = portal.node14
private val saved = root.focus.capture()15
private val input = node.scope.input16
private var mode = initial.mode17
private val constrained: Boolean get() = mode != OverlayMode.NonModal18
private var focusOptions = initial.focusOptions19
private val proxy = node.scope.createArea(focusable = constrained)20
private var fallbackTarget: FocusTarget? =21
if (constrained)22
Some(node.scope.focusTarget(proxy, FocusOptions(tabStop = false)))23
else24
None25
private val fallback: FocusHandle? get() =26
if (constrained)27
fallbackTarget?.handle28
else29
None30
private var dismissal: Closeable? = None31
private var finished = false32
val group =33
node.scope.focusScope(34
FocusScopeOptions(35
loop = constrained,36
defaultFocus = initial.focusOptions?.initialFocus ?: None,37
fallback = fallback38
)39
)40
private var openingRevision = root.surface.focusRevision41
private val barrier = InputBoundary(input, boundaryMode(), constrained && !node.layoutConcealed)42
val boundary: InputBoundary? get() =43
if (constrained)44
Some(barrier)45
else46
None47
private var initialPending = true48
private var constraintPending = false49
private var restoreOnClose = initial.focusOptions?.restoreFocus ?: false50
private var restoreTarget = initial.focusOptions?.restoreTarget ?: None51
private var closingRevision: ULong? = None52
private var focusLeftBeforeClose = false53
var heldFocusOnClose = false54
var presented: OverlayFrame? = None55
val isClosing: Boolean get() = closingRevision != None57
init {58
try {59
proxy.ignoreSelf = true60
node.scope.routeChildren(proxy)61
input.beforePress = { press -> root.overlays.beforePress(press) }62
dismissal =63
Some(64
input.addDefaultAction {65
context,66
event -> root.overlays.dismiss(context, event)67
}68
)69
} catch (error: Throwable) {70
throwUi(cleanupUi(Some(error)) { barrier.close() })71
throw error72
}73
}74
private fun boundaryMode(): InputBoundaryMode =75
if (mode == OverlayMode.FocusContained)76
InputBoundaryMode.FocusOnly77
else78
InputBoundaryMode.AllInput79
fun replace(options: PortalOptions) {80
if (isClosing || finished)81
return82
val changed = mode != options.mode83
val wasConstrained = constrained84
mode = options.mode85
focusOptions = options.focusOptions86
if (changed) {87
proxy.focusable = constrained88
if (!portal.active || finished)89
return90
if (constrained) {91
when (fallbackTarget) {92
case None -> fallbackTarget =93
Some(node.scope.focusTarget(proxy, FocusOptions(tabStop = false)))94
case Some(_) -> {}95
}96
} else97
constraintPending = false98
if (!wasConstrained && constrained) {99
constraintPending = true100
openingRevision = root.surface.focusRevision101
}102
}103
restoreOnClose = options.focusOptions?.restoreFocus ?: false104
restoreTarget = options.focusOptions?.restoreTarget ?: None105
group.options =106
FocusScopeOptions(107
loop = constrained,108
defaultFocus = options.focusOptions?.initialFocus ?: None,109
fallback = fallback110
)111
if (changed) {112
updateLayoutVisibility()113
root.overlays.reclassify()114
node.request(UiInvalidation.Scene)115
}116
}117
fun updateLayoutVisibility() {118
if (!finished) barrier.update(boundaryMode(), constrained && !node.layoutConcealed)119
}120
fun place(context: FrameContext) {121
context.place(proxy, Rect(0.0, 0.0, context.size.width, context.size.height))122
}123
fun beginClose(revision: ULong) {124
if (isClosing)125
return126
heldFocusOnClose = input.activity.hasFocus127
focusLeftBeforeClose =128
!constrained && root.surface.input.activity.hasFocus && !heldFocusOnClose129
closingRevision = Some(revision)130
initialPending = false131
constraintPending = false132
if (!input.isClosed)133
input.enabled = false134
}135
fun reopen() {136
closingRevision = None137
heldFocusOnClose = false138
initialPending = true139
openingRevision = root.surface.focusRevision140
replace(portal.options)141
}142
fun finish() {143
if (finished)144
return145
finished = true146
var failure = cleanupUi(None) { barrier.close() }147
failure = cleanupUi(failure) { dismissal?.close() }148
dismissal = None149
failure = cleanupUi(failure) { group.close() }150
failure = cleanupUi(failure) { fallbackTarget?.close() }151
failure = cleanupUi(failure) { proxy.close() }152
when (portal.focusSession) {153
case Some(val current) -> if (current === this) {154
portal.focusSession = None155
portal.domain = containingModal(node.parent)156
}157
case None -> {}158
}159
throwUi(failure)160
}161
fun wasPresented(frame: OverlayFrame): Boolean =162
when (presented) {163
case Some(val value) -> value === frame164
case None -> false165
}166
fun canComplete(frame: OverlayFrame?): Boolean =167
isClosing &&168
when (frame) {169
case Some(val value) -> !wasPresented(value)170
case None -> portal.canCompleteClose171
}172
val isModal: Boolean get() = mode == OverlayMode.Modal173
val inCurrentDomain: Boolean174
get() = !node.layoutConcealed &&175
when (boundary) {176
case Some(val value) -> value.isCurrent177
case None -> withinCurrentFocusDomain()178
}179
private fun withinCurrentFocusDomain(): Boolean {180
val boundary = root.surface.focusBoundary ?: return true181
var cursor: InputScope? = Some(input)182
while (true) {183
val current = cursor ?: return false184
if (current === boundary.input)185
return true186
cursor = current.parent187
}188
}189
val acceptsFocus: Boolean get() =190
when (focusOptions) {191
case Some(_) -> true192
case None -> false193
}194
private val isCurrentFocus: Boolean get() =195
when (root.overlays.currentFocus) {196
case Some(val value) -> value === this197
case None -> false198
}199
fun complete(frame: OverlayFrame) {200
if (201
(!initialPending && !constraintPending) ||202
isClosing ||203
!wasPresented(frame) ||204
!isCurrentFocus205
)206
return207
val preserve = constraintPending && input.activity.hasFocus208
initialPending = false209
constraintPending = false210
if (211
!preserve &&212
(focusOptions?.autoFocus ?: false) &&213
openingRevision ==214
root.surface.focusRevision215
)216
group.requestFocus()217
}218
val canRestoreFocus: Boolean get() =219
root.isOpen &&220
restoreOnClose &&221
!focusLeftBeforeClose &&222
closingRevision ==223
Some(root.surface.focusRevision)224
fun restore() {225
if (!canRestoreFocus)226
return227
val candidates = ArrayList<FocusHandle>()228
when (restoreTarget) {229
case Some(val handle) -> candidates.add(handle)230
case None -> {}231
}232
when (saved) {233
case Some(val handle) -> candidates.add(handle)234
case None -> {}235
}236
var cursor = previous237
while (true) {238
val prior = cursor ?: break239
if (!prior.finished)240
break241
if (prior.restoreOnClose) {242
when (prior.restoreTarget) {243
case Some(val handle) -> candidates.add(handle)244
case None -> {}245
}246
when (prior.saved) {247
case Some(val handle) -> candidates.add(handle)248
case None -> {}249
}250
}251
cursor = prior.previous252
}253
root.focus.restore(candidates.toArray(), root.overlays.currentFocus?.group)254
}255
}257
internal fun containingModal(owner: MountedNode?): OverlayFocusSession? {258
var cursor = owner259
while (true) {260
val node = cursor ?: return None261
when (node.body) {262
case Portal(val value) -> when (value.focusSession) {263
case Some(val modal) -> if (modal.isModal)264
return Some(modal)265
case None -> {}266
}267
else -> {}268
}269
cursor = node.parent270
}271
}273
internal fun sameModal(left: OverlayFocusSession?, right: OverlayFocusSession?): Boolean =274
when ((left, right)) {275
case(Some(val a), Some(val b)) -> a === b276
case(None, None) -> true277
else -> false278
}