Scoop API

scoop:scoop.ui:0.1.0

src/overlay/dismiss.scoop

Selected production source · src/overlay/dismiss.scoop

1package scoop.ui
3import scoop.surface.*
4import scoop.window.*
6internal fun dismissOverlayPress(
7 root: UiRoot,
8 sessions: ArrayList<PortalInstance>,
9 domain: OverlayFocusSession?,
10 press: InputPress
11): Boolean {
12 if (!root.isOpen)
13 return false
14 var index = sessions.size
15 while (index > 0L) {
16 index -= 1L
17 val portal = sessions[index]
18 if (!portal.isOpen || portal.node.layoutConcealed || !sameModal(portal.domain, domain))
19 continue
20 val policy = portal.options.dismiss.outsidePress
21 if (policy == OutsidePress.Ignore)
22 continue
23 if (portal.containsInput(press.target) || portal.containsAnchorInput(press.target))
24 return false
25 portal.requestClose(OverlayCloseReason.OutsidePress)
26 return policy == OutsidePress.CloseAndConsume
27 }
28 return false
31internal fun dismissOverlayKey(
32 root: UiRoot,
33 sessions: ArrayList<PortalInstance>,
34 domain: OverlayFocusSession?,
35 context: RoutedEventContext,
36 event: InputEvent
37) {
38 if (!root.isOpen)
39 return
40 val key =
41 when (event) {
42 case Keyboard(val value) -> value
43 else -> return
44 }
45 if (
46 key.state !=
47 ElementState.Pressed ||
48 key.synthetic ||
49 key.isRepeat ||
50 key.logicalKey !=
51 LogicalKey.Named(NamedKey.Escape)
52 )
53 return
54 val modifiers = key.modifiers
55 if (
56 modifiers.shift ||
57 modifiers.control ||
58 modifiers.alt ||
59 modifiers.superKey ||
60 modifiers.function
61 )
62 return
63 var index = sessions.size
64 while (index > 0L) {
65 index -= 1L
66 val portal = sessions[index]
67 if (
68 portal.isOpen &&
69 sameModal(portal.domain, domain) &&
70 portal.input.isFocusAllowed &&
71 portal.options.dismiss.escape
72 ) {
73 context.consume()
74 portal.requestClose(OverlayCloseReason.Escape)
75 return
76 }
77 }
80private fun PortalInstance.containsInput(target: InputScope): Boolean {
81 if (target === input)
82 return false
83 var cursor: InputScope? = Some(target)
84 while (true) {
85 val current = cursor ?: return false
86 if (current === input)
87 return true
88 cursor = current.parent
89 }
92private fun PortalInstance.containsAnchorInput(target: InputScope): Boolean {
93 if (!options.dismiss.includeAnchor)
94 return false
95 val handle = anchor ?: return false
96 val owner = handle.owner ?: return false
97 val input = owner.scope.input
98 var cursor: InputScope? = Some(target)
99 while (true) {
100 val current = cursor ?: return false
101 if (current === input)
102 return true
103 cursor = current.parent
104 }