Scoop API

scoop:scoop.desktop:0.1.0

src/notification/owner.scoop

Selected production source · src/notification/owner.scoop

1package scoop.desktop.notification
3import scoop.desktop.*
4import scoop.platform.concurrent.currentThreadId
5import scoop.window.*
6import scoop.window.native.*
8internal class NotificationOwner(val applicationId: String) {
9 private val thread = currentThreadId()
10 var pointer: Ptr<Unit>? = None
11 var resource: NativeApplicationLease? = None
12 var desktop: DesktopOwner? = None
13 var closed = false
14 private var callback: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None
15 private var nextRequest = 1L
16 private val operations = ArrayList<NotificationOperation>()
17 private val observers = ArrayList<NotificationObserver>()
18 private val startup = ArrayList<NotificationEvent>()
19 private var observed = false
21 fun checkThread() {
22 if (thread != currentThreadId())
23 throw IllegalStateException(Some("notification requires its owner thread"))
24 }
25 fun requireOpen() {
26 checkThread()
27 if (closed)
28 throw WindowException("notification resource is closed", WindowErrorKind.InvalidState)
29 }
30 fun requireAttached() {
31 requireOpen()
32 if (resource?.isClosing != Some(false))
33 throw WindowException("notification center is not active", WindowErrorKind.InvalidState)
34 }
35 fun prepare() {
36 @Unsafe
37 {
38 pointer =
39 Some(
40 applicationId.withUtf8Bytes {
41 bytes,
42 length -> @Unsafe
43 {
44 return@withUtf8Bytes desktopCreated(
45 notificationCreate(bytes, length),
46 "prepare notifications"
47 )
48 }
49 }
50 )
51 }
52 }
53 fun attach(target: DesktopOwner) {
54 @Unsafe
55 {
56 desktop = Some(target)
57 resource = Some(target.application.registerNativeResource { close() })
58 try {
59 val bridge =
60 foreignCallback<(Ptr<Unit>, Int) -> Int>(
61 {
62 event: Int ->
63 resource!!.post { drain() };
64 1
65 },
66 0,
67 ForeignCallbackMode.Reusable
68 )
69 callback = Some(bridge)
70 desktopStatus(
71 notificationAttach(pointer!!, bridge.function, bridge.context),
72 "attach notifications"
73 )
74 } catch (error: Throwable) {
75 close()
76 throw error
77 }
78 }
79 }
80 fun start(
81 callback: (NotificationReply) -> Unit,
82 submit: (Long) -> Long
83 ): NotificationOperation {
84 val operation = NotificationOperation(this, nextRequest, Some(callback))
85 nextRequest += 1L
86 operations.add(operation)
87 try {
88 desktopStatus(submit(operation.id), "notification request")
89 } catch (error: Throwable) {
90 operation.close()
91 throw error
92 }
93 return operation
94 }
95 fun removeOperation(operation: NotificationOperation) {
96 for (index in 0L ..< operations.size)
97 if (operations[index] === operation) {
98 operations.removeAt(index);
99 return
100 }
101 }
102 fun drain() {
103 if (closed)
104 return
105 @Unsafe
106 {
107 while (!closed) {
108 val event = notificationNextEvent(pointer!!) ?: return
109 val reply =
110 try {
111 NotificationReply(
112 notificationEventData(event),
113 notificationText(event, 0),
114 notificationText(event, 1),
115 notificationText(event, 2),
116 notificationText(event, 3)
117 )
118 } finally {
119 notificationEventDestroy(event)
120 }
121 if (reply.data.request != 0L) {
122 for (operation in operations.toArray())
123 if (operation.id == reply.data.request) {
124 val completion = operation.callback
125 operation.close()
126 completion?.invoke(reply)
127 break
128 }
129 } else {
130 val value = eventReply(reply)
131 val activation =
132 when (value) {
133 case Activated(_, _, _) -> true
134 else -> false
135 }
136 if (!observed && activation)
137 startup.add(value)
138 else
139 for (observer in observers.toArray())
140 resource!!.post { observer.deliver(value) }
141 }
142 }
143 }
144 }
145 fun observe(callback: (NotificationEvent) -> Unit): NotificationObserver {
146 val observer = NotificationObserver(this, Some(callback))
147 observers.add(observer)
148 if (!observed) {
149 observed = true
150 val pending = startup.toArray()
151 startup.clear()
152 for (event in pending)
153 resource!!.post { observer.deliver(event) }
154 }
155 return observer
156 }
157 fun removeObserver(observer: NotificationObserver) {
158 for (index in 0L ..< observers.size)
159 if (observers[index] === observer) {
160 observers.removeAt(index);
161 return
162 }
163 }
164 fun close() {
165 checkThread()
166 if (closed)
167 return
168 closed = true
169 for (operation in operations.toArray())
170 operation.close()
171 for (observer in observers.toArray())
172 observer.close()
173 startup.clear()
174 @Unsafe
175 {
176 if (pointer != None)
177 notificationDestroy(pointer!!)
178 pointer = None
179 when (callback) {
180 case Some(val value) -> releaseForeignCallback(value)
181 case None -> {}
182 }
183 callback = None
184 }
185 resource?.close()
186 when (desktop) {
187 case Some(val value) -> value.notifications = None
188 case None -> {}
189 }
190 }
193internal class NotificationObserver(
194 private val owner: NotificationOwner,
195 private var callback: ((NotificationEvent) -> Unit)?
196): Closeable {
197 fun deliver(event: NotificationEvent) {
198 if (!owner.closed)
199 callback?.invoke(event)
200 }
201 public override fun close() {
202 owner.checkThread()
203 callback = None
204 owner.removeObserver(this)
205 }