Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/popup/presentation.scoop

Selected production source · platform/desktop/native/popup/presentation.scoop

1package scoop.ui.component
3import scoop.surface.InputArea
4import scoop.ui.*
5import scoop.window.*
7internal class NativePopupInput(
8 val menu: NativeMenuSnapshot,
9 val enabled: Boolean,
10 val contextMenu: Boolean,
11 val handle: NativePopupHandle,
12 val onClosed: (PopupResult) -> Unit,
13 val longPress: LongPressOptions = LongPressOptions(enabled = false)
16internal class NativePopupPresentation(
17 private val scope: ComponentScope,
18 private val area: InputArea,
19 private val tree: NativeMenuTree,
20 private val read: () -> NativePopupInput
21): Closeable {
22 private var session: PopupSession? = None
23 private var handle: NativePopupHandle? = None
24 private var closed = false
25 val isPending: Boolean get() =
26 when (session) {
27 case Some(_) -> true;
28 case None -> false
29 }
31 fun update() {
32 val next = read().handle
33 when (handle) {
34 case Some(val current) -> if (current !== next) {
35 next.bind(this)
36 current.unbind(this)
37 handle = Some(next)
38 }
39 case None -> {
40 next.bind(this)
41 handle = Some(next)
42 }
43 }
44 tree.update(read().menu)
45 if (!read().enabled)
46 cancel()
47 }
49 fun open(pointer: LogicalPosition?): Boolean {
50 if (
51 closed || !scope.isMounted || !read().enabled || isPending || !area.scope.isInputAllowed
52 )
53 return false
54 val window = scope.nativeWindow
55 if (window.state != WindowState.Open || window.isInputBlocked)
56 return false
57 val bounds = area.visibleBounds ?: return false
58 val placement =
59 when (pointer) {
60 case Some(val position) -> PopupPlacement.Pointer(position)
61 case None -> PopupPlacement.Anchor(
62 LogicalRect(
63 LogicalPosition(bounds.x, bounds.y),
64 LogicalSize(bounds.width, bounds.height)
65 )
66 )
67 }
68 val current = window.showPopup(tree.menu, placement)
69 session = Some(current)
70 handle?.changed(true)
71 scope.observePopup(current) {
72 result ->
73 session = None
74 handle?.changed(false)
75 if (!closed)
76 read().onClosed(result)
77 }
78 return true
79 }
80 fun cancel() {
81 session?.close()
82 }
83 public override fun close() {
84 if (closed)
85 return
86 closed = true
87 val previous = handle
88 handle = None
89 try {
90 session?.close()
91 }
92 finally {
93 session = None
94 previous?.unbind(this)
95 }
96 }