scoop:scoop.ui.component:0.1.0
platform/desktop/native/popup/presentation.scoop
Selected production source · platform/desktop/native/popup/presentation.scoop
1
package scoop.ui.component3
import scoop.surface.InputArea4
import scoop.ui.*5
import scoop.window.*7
internal 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)14
)16
internal class NativePopupPresentation(17
private val scope: ComponentScope,18
private val area: InputArea,19
private val tree: NativeMenuTree,20
private val read: () -> NativePopupInput21
): Closeable {22
private var session: PopupSession? = None23
private var handle: NativePopupHandle? = None24
private var closed = false25
val isPending: Boolean get() =26
when (session) {27
case Some(_) -> true;28
case None -> false29
}31
fun update() {32
val next = read().handle33
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.isInputAllowed52
)53
return false54
val window = scope.nativeWindow55
if (window.state != WindowState.Open || window.isInputBlocked)56
return false57
val bounds = area.visibleBounds ?: return false58
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 = None74
handle?.changed(false)75
if (!closed)76
read().onClosed(result)77
}78
return true79
}80
fun cancel() {81
session?.close()82
}83
public override fun close() {84
if (closed)85
return86
closed = true87
val previous = handle88
handle = None89
try {90
session?.close()91
}92
finally {93
session = None94
previous?.unbind(this)95
}96
}97
}