Scoop API

scoop:scoop.window:0.1.0

src/native/lifetime.scoop

Selected production source · src/native/lifetime.scoop

1package scoop.window.native
3import scoop.window.*
5/** An explicit native borrow that prevents active destruction until owner-side retirement. */
6public class NativeWindowLease internal constructor(private val owner: WindowOwner) : Closeable {
7 private val closed: AtomicBoolean = AtomicBoolean(false)
8 private var closing: (() -> Unit)? = None
9 internal fun onClosing(callback: () -> Unit) {
10 closing = Some(callback)
11 owner.closingLeases.add(this)
12 }
13 internal fun requestClose() {
14 val callback = closing ?: return
15 closing = None
16 owner.application.mailbox.postControl { if (!closed.load()) callback() }
17 }
18 /** Queries the typed native handles on the owner thread while this lease is active. */
19 public val handle: NativeWindowHandle
20 @Unsafe get() {
21 owner.application.checkThread()
22 if (closed.load()) throw IllegalStateException(Some("native window lease is closed"))
23 return platformNativeHandle(owner.pointer!!)
24 }
25 /** Ends the borrow once, from any thread; actual release remains on the owner. */
26 public override fun close() {
27 if (closed.exchange(true)) return
28 owner
29 .application
30 .mailbox
31 .postControl {
32 closing = None
33 for (index in 0L..<owner.closingLeases.size)
34 if (owner.closingLeases[index] === this) {
35 owner.closingLeases.removeAt(index)
36 break
37 }
38 owner.leases -= 1L
39 }
40 }
43/** Acquires an owner-thread lease before creating a renderer surface. */
44@Unsafe
45public fun Window.acquireNativeWindow(): NativeWindowLease {
46 owner.requireOpen()
47 owner.leases += 1L
48 return NativeWindowLease(owner)