Scoop API

scoop:scoop.window:0.1.0

src/native/application_lease.scoop

Selected production source · src/native/application_lease.scoop

1package scoop.window.native
3import scoop.window.*
5/** Keeps the existing application alive until a separate native resource is retired. */
6public class NativeApplicationLease internal constructor(
7 internal val owner: ApplicationOwner,
8 private var closing: (() -> Unit)?
9): Closeable {
10 private var closed = false
11 private var closeRequested = false
13 public val isClosing: Boolean get() {
14 checkThread()
15 return closed || closeRequested
16 }
18 public fun checkThread() {
19 owner.checkThread()
20 }
22 /** Delivers ordinary callbacks through the same application and asyncio queues. */
23 public fun post(block: () -> Unit): Boolean {
24 checkThread()
25 if (closed || closeRequested)
26 return false
27 return owner.mailbox.post {
28 if (!closed && !closeRequested)
29 block()
30 }
31 }
33 /** Cleanup remains available after the application stops accepting business work. */
34 public fun postControl(block: () -> Unit): Boolean {
35 checkThread()
36 if (closed)
37 return false
38 return owner.mailbox.postControl {
39 if (!closed)
40 block()
41 }
42 }
44 /** Starts native tracking only after the current callback and I/O watch return. */
45 public fun postNative(block: () -> Unit): Boolean {
46 checkThread()
47 if (closed)
48 return false
49 owner.deferNativeOperation {
50 if (!closed)
51 block()
52 }
53 return true
54 }
56 @Unsafe
57 public fun acquireWindow(
58 window: Window,
59 requestClose: (() -> Unit)? = None
60 ): NativeWindowLease {
61 requireOpen()
62 requireWindow(window.owner.application === owner, "window belongs to another application")
63 val lease = window.acquireNativeWindow()
64 when (requestClose) {
65 case Some(val callback) -> lease.onClosing(callback)
66 case None -> {}
67 }
68 return lease
69 }
71 @Unsafe
72 public fun acquireMenu(menu: Menu, requestClose: () -> Unit): NativeMenuLease {
73 requireOpen()
74 requireWindow(menu.owner.application === owner, "menu belongs to another application")
75 return menu.acquireNativeMenu(requestClose)
76 }
78 /** The caller must first unregister and destroy its own native resource. */
79 public override fun close() {
80 checkThread()
81 if (closed)
82 return
83 closed = true
84 closing = None
85 for (index in 0L ..< owner.nativeResources.size)
86 if (owner.nativeResources[index] === this) {
87 owner.nativeResources.removeAt(index)
88 break
89 }
90 owner.mailbox.wake()
91 }
93 internal fun requestClose() {
94 if (closed || closeRequested)
95 return
96 closeRequested = true
97 val callback = closing
98 closing = None
99 owner.mailbox.postControl {
100 if (!closed)
101 callback!!()
102 }
103 }
105 private fun requireOpen() {
106 owner.requireRunning("native application resource")
107 if (closed || closeRequested)
108 throw WindowException("native application resource", WindowErrorKind.InvalidState)
109 }
112/** Registers the real resource's shutdown callback without creating another event loop. */
113@Unsafe
114public fun ActiveApplication.registerNativeResource(
115 requestClose: () -> Unit
116): NativeApplicationLease {
117 owner.requireRunning("register native application resource")
118 val lease = NativeApplicationLease(owner, Some(requestClose))
119 owner.nativeResources.add(lease)
120 return lease