Scoop API

scoop:scoop.ui.component.desktop:0.1.0

src/notification.scoop

Selected production source · src/notification.scoop

1package scoop.ui.component.desktop
3import scoop.Result
4import scoop.desktop.notification.*
5import scoop.ui.*
7/** Borrows a center and owns only this scope's requests and subscriptions. */
8public class NativeNotificationController internal constructor(
9 private val center: NotificationCenter,
10 private val mounted: () -> Boolean,
11 private val dispatch: (() -> Unit) -> Boolean
12): Closeable {
13 private var closed = false
14 private val registrations = ArrayList<NativeNotificationRegistration>()
16 internal fun checkThread() {
17 center.isClosed
18 }
19 private fun requireOpen() {
20 checkThread()
21 if (closed || !mounted() || center.isClosed)
22 throw IllegalStateException(Some("notification controller is not active"))
23 }
24 internal fun remove(value: NativeNotificationRegistration) {
25 for (index in 0L ..< registrations.size)
26 if (registrations[index] === value) {
27 registrations.removeAt(index)
28 return
29 }
30 }
31 public fun settings(
32 callback: (Result<NotificationSettings, NotificationError>) -> Unit
33 ): Closeable = start({ center.settings(it) }, callback)
35 public fun requestPermission(
36 callback: (Result<NotificationSettings, NotificationError>) -> Unit
37 ): Closeable = start({ center.requestPermission(it) }, callback)
39 public fun post(
40 id: NotificationId,
41 content: NotificationContent,
42 callback: (Result<NotificationSubmission, NotificationError>) -> Unit
43 ): Closeable = start({ center.post(id, content, it) }, callback)
45 public fun withdraw(
46 id: NotificationId,
47 callback: (Result<NotificationWithdrawal, NotificationError>) -> Unit
48 ): Closeable = start({ center.withdraw(id, it) }, callback)
50 private fun <T> start(
51 submit: ((Result<T, NotificationError>) -> Unit) -> NotificationOperation,
52 callback: (Result<T, NotificationError>) -> Unit
53 ): Closeable {
54 requireOpen()
55 val entry = NativeNotificationRegistration(this)
56 registrations.add(entry)
57 try {
58 entry.resource =
59 Some(
60 submit {
61 result -> if (entry.active) {
62 entry.close()
63 if (!closed)
64 dispatch {
65 if (!closed)
66 callback(result)
67 }
68 }
69 }
70 )
71 } catch (error: Throwable) {
72 entry.close()
73 throw error
74 }
75 return entry
76 }
77 public fun observe(callback: (NotificationEvent) -> Unit): Closeable {
78 requireOpen()
79 val entry = NativeNotificationRegistration(this)
80 registrations.add(entry)
81 try {
82 entry.resource =
83 Some(
84 center.observe {
85 event -> if (entry.active && !closed)
86 dispatch {
87 if (entry.active && !closed)
88 callback(event)
89 }
90 }
91 )
92 } catch (error: Throwable) {
93 entry.close()
94 throw error
95 }
96 return entry
97 }
98 public override fun close() {
99 checkThread()
100 if (closed)
101 return
102 closed = true
103 for (entry in registrations.toArray())
104 entry.close()
105 }
108internal class NativeNotificationRegistration(
109 private val owner: NativeNotificationController
110): Closeable {
111 var resource: Closeable? = None
112 var active = true
113 public override fun close() {
114 owner.checkThread()
115 if (!active)
116 return
117 active = false
118 val previous = resource
119 resource = None
120 owner.remove(this)
121 previous?.close()
122 }
125public fun ApplicationScope.notificationController(
126 center: NotificationCenter
127): NativeNotificationController =
128 resource { NativeNotificationController(center, { isMounted }, { action -> dispatch(action) }) }
130public fun ComponentScope.notificationController(
131 center: NotificationCenter
132): NativeNotificationController =
133 resource { NativeNotificationController(center, { isMounted }, { action -> dispatch(action) }) }