Scoop API

scoop:scoop.ui:0.1.0

src/overlay/focus_session.scoop

Selected production source · src/overlay/focus_session.scoop

1package scoop.ui
3import scoop.graphics.Rect
4import scoop.surface.*
6/** Retains the boundary until presentation or a non-drawable logical update removes the layer. */
7internal class OverlayFocusSession(
8 private val portal: PortalInstance,
9 initial: PortalOptions,
10 private val previous: OverlayFocusSession?
11) {
12 private val root = portal.root
13 private val node = portal.node
14 private val saved = root.focus.capture()
15 private val input = node.scope.input
16 private var mode = initial.mode
17 private val constrained: Boolean get() = mode != OverlayMode.NonModal
18 private var focusOptions = initial.focusOptions
19 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 else
24 None
25 private val fallback: FocusHandle? get() =
26 if (constrained)
27 fallbackTarget?.handle
28 else
29 None
30 private var dismissal: Closeable? = None
31 private var finished = false
32 val group =
33 node.scope.focusScope(
34 FocusScopeOptions(
35 loop = constrained,
36 defaultFocus = initial.focusOptions?.initialFocus ?: None,
37 fallback = fallback
38 )
39 )
40 private var openingRevision = root.surface.focusRevision
41 private val barrier = InputBoundary(input, boundaryMode(), constrained && !node.layoutConcealed)
42 val boundary: InputBoundary? get() =
43 if (constrained)
44 Some(barrier)
45 else
46 None
47 private var initialPending = true
48 private var constraintPending = false
49 private var restoreOnClose = initial.focusOptions?.restoreFocus ?: false
50 private var restoreTarget = initial.focusOptions?.restoreTarget ?: None
51 private var closingRevision: ULong? = None
52 private var focusLeftBeforeClose = false
53 var heldFocusOnClose = false
54 var presented: OverlayFrame? = None
55 val isClosing: Boolean get() = closingRevision != None
57 init {
58 try {
59 proxy.ignoreSelf = true
60 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 error
72 }
73 }
74 private fun boundaryMode(): InputBoundaryMode =
75 if (mode == OverlayMode.FocusContained)
76 InputBoundaryMode.FocusOnly
77 else
78 InputBoundaryMode.AllInput
79 fun replace(options: PortalOptions) {
80 if (isClosing || finished)
81 return
82 val changed = mode != options.mode
83 val wasConstrained = constrained
84 mode = options.mode
85 focusOptions = options.focusOptions
86 if (changed) {
87 proxy.focusable = constrained
88 if (!portal.active || finished)
89 return
90 if (constrained) {
91 when (fallbackTarget) {
92 case None -> fallbackTarget =
93 Some(node.scope.focusTarget(proxy, FocusOptions(tabStop = false)))
94 case Some(_) -> {}
95 }
96 } else
97 constraintPending = false
98 if (!wasConstrained && constrained) {
99 constraintPending = true
100 openingRevision = root.surface.focusRevision
101 }
102 }
103 restoreOnClose = options.focusOptions?.restoreFocus ?: false
104 restoreTarget = options.focusOptions?.restoreTarget ?: None
105 group.options =
106 FocusScopeOptions(
107 loop = constrained,
108 defaultFocus = options.focusOptions?.initialFocus ?: None,
109 fallback = fallback
110 )
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 return
126 heldFocusOnClose = input.activity.hasFocus
127 focusLeftBeforeClose =
128 !constrained && root.surface.input.activity.hasFocus && !heldFocusOnClose
129 closingRevision = Some(revision)
130 initialPending = false
131 constraintPending = false
132 if (!input.isClosed)
133 input.enabled = false
134 }
135 fun reopen() {
136 closingRevision = None
137 heldFocusOnClose = false
138 initialPending = true
139 openingRevision = root.surface.focusRevision
140 replace(portal.options)
141 }
142 fun finish() {
143 if (finished)
144 return
145 finished = true
146 var failure = cleanupUi(None) { barrier.close() }
147 failure = cleanupUi(failure) { dismissal?.close() }
148 dismissal = None
149 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 = None
155 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 === frame
164 case None -> false
165 }
166 fun canComplete(frame: OverlayFrame?): Boolean =
167 isClosing &&
168 when (frame) {
169 case Some(val value) -> !wasPresented(value)
170 case None -> portal.canCompleteClose
171 }
172 val isModal: Boolean get() = mode == OverlayMode.Modal
173 val inCurrentDomain: Boolean
174 get() = !node.layoutConcealed &&
175 when (boundary) {
176 case Some(val value) -> value.isCurrent
177 case None -> withinCurrentFocusDomain()
178 }
179 private fun withinCurrentFocusDomain(): Boolean {
180 val boundary = root.surface.focusBoundary ?: return true
181 var cursor: InputScope? = Some(input)
182 while (true) {
183 val current = cursor ?: return false
184 if (current === boundary.input)
185 return true
186 cursor = current.parent
187 }
188 }
189 val acceptsFocus: Boolean get() =
190 when (focusOptions) {
191 case Some(_) -> true
192 case None -> false
193 }
194 private val isCurrentFocus: Boolean get() =
195 when (root.overlays.currentFocus) {
196 case Some(val value) -> value === this
197 case None -> false
198 }
199 fun complete(frame: OverlayFrame) {
200 if (
201 (!initialPending && !constraintPending) ||
202 isClosing ||
203 !wasPresented(frame) ||
204 !isCurrentFocus
205 )
206 return
207 val preserve = constraintPending && input.activity.hasFocus
208 initialPending = false
209 constraintPending = false
210 if (
211 !preserve &&
212 (focusOptions?.autoFocus ?: false) &&
213 openingRevision ==
214 root.surface.focusRevision
215 )
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 return
227 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 = previous
237 while (true) {
238 val prior = cursor ?: break
239 if (!prior.finished)
240 break
241 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.previous
252 }
253 root.focus.restore(candidates.toArray(), root.overlays.currentFocus?.group)
254 }
257internal fun containingModal(owner: MountedNode?): OverlayFocusSession? {
258 var cursor = owner
259 while (true) {
260 val node = cursor ?: return None
261 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.parent
270 }
273internal fun sameModal(left: OverlayFocusSession?, right: OverlayFocusSession?): Boolean =
274 when ((left, right)) {
275 case(Some(val a), Some(val b)) -> a === b
276 case(None, None) -> true
277 else -> false
278 }