scoop:scoop.desktop:0.1.0
src/notification/center.scoop
Selected production source · src/notification/center.scoop
1
package scoop.desktop.notification3
import scoop.Result4
import scoop.desktop.*5
import scoop.window.*7
/** Prepares the native activation entry before Application starts. */8
public class NotificationLaunch internal constructor(9
internal val owner: NotificationOwner10
): Closeable {11
public val isClosed: Boolean get() {12
owner.checkThread();13
return owner.closed14
}15
public override fun close() {16
owner.close()17
}18
}20
private object NotificationSlot {21
var current: NotificationOwner? = None22
}24
public fun prepareNotifications(applicationId: String): NotificationLaunch {25
notificationLabel(applicationId)26
when (NotificationSlot.current) {27
case Some(val previous) -> {28
previous.checkThread()29
if (!previous.closed)30
throw WindowException(31
"notification launch is already active",32
WindowErrorKind.ResourceBusy33
)34
}35
case None -> {}36
}37
val owner = NotificationOwner(applicationId)38
owner.prepare()39
NotificationSlot.current = Some(owner)40
return NotificationLaunch(owner)41
}43
public fun DesktopApplication.openNotifications(launch: NotificationLaunch): NotificationCenter {44
owner.requireOpen()45
launch.owner.requireOpen()46
if (owner.notifications?.isClosed != None || launch.owner.resource?.isClosing != None)47
throw WindowException(48
"notification center is already attached",49
WindowErrorKind.ResourceBusy50
)51
if (owner.application.applicationId != Some(launch.owner.applicationId))52
throw IllegalArgumentException(Some("notification and application identifiers differ"))53
launch.owner.attach(owner)54
val center = NotificationCenter(launch.owner)55
owner.notifications = Some(center)56
return center57
}59
/** Cancelling an operation only stops its local completion callback. */60
public class NotificationOperation internal constructor(61
private val owner: NotificationOwner,62
internal val id: Long,63
internal var callback: ((NotificationReply) -> Unit)?64
): Closeable {65
public override fun close() {66
owner.checkThread()67
callback = None68
owner.removeOperation(this)69
}70
}72
public class NotificationCenter internal constructor(73
private val owner: NotificationOwner74
): Closeable {75
public val isClosed: Boolean get() {76
owner.checkThread();77
return owner.closed78
}80
public fun settings(81
callback: (Result<NotificationSettings, NotificationError>) -> Unit82
): NotificationOperation = query(false, callback)84
public fun requestPermission(85
callback: (Result<NotificationSettings, NotificationError>) -> Unit86
): NotificationOperation = query(true, callback)88
private fun query(89
permission: Boolean,90
callback: (Result<NotificationSettings, NotificationError>) -> Unit91
): NotificationOperation {92
owner.requireAttached()93
return owner.start(94
{ reply -> callback(settingsReply(reply)) },95
{96
id -> @Unsafe97
{98
return@start notificationSettings(99
owner.pointer!!,100
id,101
if (permission)102
1103
else104
0105
)106
}107
}108
)109
}111
public fun post(112
id: NotificationId,113
content: NotificationContent,114
callback: (Result<NotificationSubmission, NotificationError>) -> Unit115
): NotificationOperation {116
owner.requireAttached()117
notificationLabel(id.value)118
return withNotificationContent(id, content) {119
native -> owner.start(120
{121
reply -> callback(122
if (reply.data.status == 0)123
Result.Success<NotificationSubmission, NotificationError>(124
NotificationSubmission.Submitted125
)126
else127
Result.Failure<NotificationSubmission, NotificationError>(128
replyError(reply)129
)130
)131
},132
{133
request -> @Unsafe134
{135
return@start notificationPost(owner.pointer!!, request, native)136
}137
}138
)139
}140
}142
public fun withdraw(143
id: NotificationId,144
callback: (Result<NotificationWithdrawal, NotificationError>) -> Unit145
): NotificationOperation {146
owner.requireAttached()147
notificationLabel(id.value)148
return owner.start(149
{150
reply -> callback(151
if (reply.data.status == 0)152
Result.Success<NotificationWithdrawal, NotificationError>(153
if (reply.data.detail == 0)154
NotificationWithdrawal.Requested155
else156
NotificationWithdrawal.NotFound157
)158
else159
Result.Failure<NotificationWithdrawal, NotificationError>(replyError(reply))160
)161
},162
{163
request -> @Unsafe164
{165
return@start id.value.withUtf8Bytes {166
text,167
length -> @Unsafe168
{169
return@withUtf8Bytes notificationWithdraw(170
owner.pointer!!,171
request,172
text,173
length174
)175
}176
}177
}178
}179
)180
}182
public fun observe(callback: (NotificationEvent) -> Unit): Closeable {183
owner.requireAttached()184
return owner.observe(callback)185
}187
public override fun close() {188
owner.close()189
}190
}