Scoop API

scoop:scoop.window:0.1.0

src/native/watch.scoop

Selected production source · src/native/watch.scoop

1package scoop.window.native
3import scoop.window.*
5/** A borrowed native notification, retired asynchronously on the owner thread. */
6public class ApplicationWatch internal constructor(
7 private val owner: ApplicationOwner,
8 internal val identity: Long,
9 private val pointer: Ptr<Unit>,
10 private val ready: () -> Unit
11) {
12 internal var removing: Boolean = false
13 private var onRemoved: (() -> Unit)? = None
14 /** Cancels once; completion follows every queued or active ready callback. */
15 public fun remove(onRemoved: () -> Unit) {
16 owner.checkThread()
17 if (removing)
18 return
19 removing = true
20 this.onRemoved = Some(onRemoved)
21 @Unsafe
22 {
23 nativeWatchRemove(pointer)
24 }
25 }
26 internal fun notifyReady() {
27 if (!removing)
28 ready()
29 }
30 internal fun notifyRemoved() {
31 onRemoved!!();
32 onRemoved = None
33 }
36@Unsafe
37internal fun ApplicationOwner.registerWatch(
38 create: (Long) -> NativeWatchResult,
39 ready: () -> Unit
40): ApplicationWatch {
41 checkThread()
42 if (exiting || native == None)
43 throw WindowException("install notification", WindowErrorKind.InvalidState)
44 if (nextWatch == 9223372036854775807L)
45 throw WindowException("notification identity", WindowErrorKind.NativeFailure)
46 val identity = nextWatch
47 nextWatch += 1L
48 val result = create(identity)
49 val pointer = result.pointer
50 val error = result.error
51 val watch =
52 ApplicationWatch(
53 this,
54 identity,
55 pointer ?:
56 throw WindowException(
57 "install notification",
58 WindowErrorKind.NativeFailure,
59 nativeCode = Some(error)
60 ),
61 ready
62 )
63 watches.add(watch)
64 return watch
67internal fun ApplicationOwner.dispatchWatch(identity: Long, stage: Int) {
68 var index = 0L
69 while (index < watches.size) {
70 val watch = watches[index]
71 if (watch.identity == identity) {
72 if (stage == 2)
73 watch.notifyReady()
74 else
75 if (stage == 3) {
76 watches.removeAt(index)
77 watch.notifyRemoved()
78 mailbox.wake()
79 } else
80 throw WindowException(
81 "unknown notification stage",
82 WindowErrorKind.NativeFailure
83 )
84 return
85 }
86 index += 1L
87 }
90@CLayout
91internal struct NativeWatchResult(val pointer: Ptr<Unit>?, val error: Long)
93@Extern(name = "scoop_window_watch_remove")
94private fun nativeWatchRemove(watch: Ptr<Unit>)