scoop:scoop.desktop:0.1.0
src/notification/owner.scoop
Selected production source · src/notification/owner.scoop
1
package scoop.desktop.notification3
import scoop.desktop.*4
import scoop.platform.concurrent.currentThreadId5
import scoop.window.*6
import scoop.window.native.*8
internal class NotificationOwner(val applicationId: String) {9
private val thread = currentThreadId()10
var pointer: Ptr<Unit>? = None11
var resource: NativeApplicationLease? = None12
var desktop: DesktopOwner? = None13
var closed = false14
private var callback: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None15
private var nextRequest = 1L16
private val operations = ArrayList<NotificationOperation>()17
private val observers = ArrayList<NotificationObserver>()18
private val startup = ArrayList<NotificationEvent>()19
private var observed = false21
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
@Unsafe37
{38
pointer =39
Some(40
applicationId.withUtf8Bytes {41
bytes,42
length -> @Unsafe43
{44
return@withUtf8Bytes desktopCreated(45
notificationCreate(bytes, length),46
"prepare notifications"47
)48
}49
}50
)51
}52
}53
fun attach(target: DesktopOwner) {54
@Unsafe55
{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
165
},66
0,67
ForeignCallbackMode.Reusable68
)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 error77
}78
}79
}80
fun start(81
callback: (NotificationReply) -> Unit,82
submit: (Long) -> Long83
): NotificationOperation {84
val operation = NotificationOperation(this, nextRequest, Some(callback))85
nextRequest += 1L86
operations.add(operation)87
try {88
desktopStatus(submit(operation.id), "notification request")89
} catch (error: Throwable) {90
operation.close()91
throw error92
}93
return operation94
}95
fun removeOperation(operation: NotificationOperation) {96
for (index in 0L ..< operations.size)97
if (operations[index] === operation) {98
operations.removeAt(index);99
return100
}101
}102
fun drain() {103
if (closed)104
return105
@Unsafe106
{107
while (!closed) {108
val event = notificationNextEvent(pointer!!) ?: return109
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.callback125
operation.close()126
completion?.invoke(reply)127
break128
}129
} else {130
val value = eventReply(reply)131
val activation =132
when (value) {133
case Activated(_, _, _) -> true134
else -> false135
}136
if (!observed && activation)137
startup.add(value)138
else139
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 = true150
val pending = startup.toArray()151
startup.clear()152
for (event in pending)153
resource!!.post { observer.deliver(event) }154
}155
return observer156
}157
fun removeObserver(observer: NotificationObserver) {158
for (index in 0L ..< observers.size)159
if (observers[index] === observer) {160
observers.removeAt(index);161
return162
}163
}164
fun close() {165
checkThread()166
if (closed)167
return168
closed = true169
for (operation in operations.toArray())170
operation.close()171
for (observer in observers.toArray())172
observer.close()173
startup.clear()174
@Unsafe175
{176
if (pointer != None)177
notificationDestroy(pointer!!)178
pointer = None179
when (callback) {180
case Some(val value) -> releaseForeignCallback(value)181
case None -> {}182
}183
callback = None184
}185
resource?.close()186
when (desktop) {187
case Some(val value) -> value.notifications = None188
case None -> {}189
}190
}191
}193
internal 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 = None204
owner.removeObserver(this)205
}206
}