Scoop API

scoop:scoop.desktop:0.1.0

src/notification/center.scoop

Selected production source · src/notification/center.scoop

1package scoop.desktop.notification
3import scoop.Result
4import scoop.desktop.*
5import scoop.window.*
7/** Prepares the native activation entry before Application starts. */
8public class NotificationLaunch internal constructor(
9 internal val owner: NotificationOwner
10): Closeable {
11 public val isClosed: Boolean get() {
12 owner.checkThread();
13 return owner.closed
14 }
15 public override fun close() {
16 owner.close()
17 }
20private object NotificationSlot {
21 var current: NotificationOwner? = None
24public 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.ResourceBusy
33 )
34 }
35 case None -> {}
36 }
37 val owner = NotificationOwner(applicationId)
38 owner.prepare()
39 NotificationSlot.current = Some(owner)
40 return NotificationLaunch(owner)
43public 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.ResourceBusy
50 )
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 center
59/** Cancelling an operation only stops its local completion callback. */
60public 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 = None
68 owner.removeOperation(this)
69 }
72public class NotificationCenter internal constructor(
73 private val owner: NotificationOwner
74): Closeable {
75 public val isClosed: Boolean get() {
76 owner.checkThread();
77 return owner.closed
78 }
80 public fun settings(
81 callback: (Result<NotificationSettings, NotificationError>) -> Unit
82 ): NotificationOperation = query(false, callback)
84 public fun requestPermission(
85 callback: (Result<NotificationSettings, NotificationError>) -> Unit
86 ): NotificationOperation = query(true, callback)
88 private fun query(
89 permission: Boolean,
90 callback: (Result<NotificationSettings, NotificationError>) -> Unit
91 ): NotificationOperation {
92 owner.requireAttached()
93 return owner.start(
94 { reply -> callback(settingsReply(reply)) },
95 {
96 id -> @Unsafe
97 {
98 return@start notificationSettings(
99 owner.pointer!!,
100 id,
101 if (permission)
102 1
103 else
104 0
105 )
106 }
107 }
108 )
109 }
111 public fun post(
112 id: NotificationId,
113 content: NotificationContent,
114 callback: (Result<NotificationSubmission, NotificationError>) -> Unit
115 ): 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.Submitted
125 )
126 else
127 Result.Failure<NotificationSubmission, NotificationError>(
128 replyError(reply)
129 )
130 )
131 },
132 {
133 request -> @Unsafe
134 {
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>) -> Unit
145 ): 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.Requested
155 else
156 NotificationWithdrawal.NotFound
157 )
158 else
159 Result.Failure<NotificationWithdrawal, NotificationError>(replyError(reply))
160 )
161 },
162 {
163 request -> @Unsafe
164 {
165 return@start id.value.withUtf8Bytes {
166 text,
167 length -> @Unsafe
168 {
169 return@withUtf8Bytes notificationWithdraw(
170 owner.pointer!!,
171 request,
172 text,
173 length
174 )
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 }