Scoop API

scoop:scoop.desktop:0.1.0

src/notification/events.scoop

Selected production source · src/notification/events.scoop

1package scoop.desktop.notification
3import scoop.Result
4import scoop.window.WindowErrorKind
5import scoop.window.WindowException
7internal fun replyError(reply: NotificationReply): NotificationError =
8 NotificationError(
9 when (reply.data.status) {
10 1 -> NotificationErrorKind.Unsupported
11 2 -> NotificationErrorKind.PermissionRequired
12 3 -> NotificationErrorKind.Disabled
13 4 -> NotificationErrorKind.NotConfigured
14 5 -> NotificationErrorKind.Unavailable
15 6 -> NotificationErrorKind.InvalidContent
16 else -> NotificationErrorKind.NativeFailure
17 },
18 if (reply.data.code != 0L)
19 Some(reply.data.code)
20 else
21 None,
22 if (reply.message.byteLength != 0L)
23 Some(reply.message)
24 else
25 None
26 )
28internal fun settingsReply(reply: NotificationReply): Result<
29 NotificationSettings,
30 NotificationError
31> {
32 if (reply.data.status != 0)
33 return Result.Failure<NotificationSettings, NotificationError>(replyError(reply))
34 val availability =
35 when (reply.data.detail) {
36 0 -> NotificationAvailability.Available
37 1 -> NotificationAvailability.PermissionRequired
38 2 -> NotificationAvailability.Disabled
39 3 -> NotificationAvailability.NotConfigured
40 else -> NotificationAvailability.ServiceUnavailable
41 }
42 val flags = reply.data.capabilities
43 return Result.Success<NotificationSettings, NotificationError>(
44 NotificationSettings(
45 availability,
46 NotificationCapabilities(
47 flags.and(1) != 0,
48 flags.and(2) != 0,
49 flags.and(4) != 0,
50 flags.and(8) != 0,
51 flags.and(16) != 0,
52 flags.and(32) != 0
53 )
54 )
55 )
58internal fun eventReply(reply: NotificationReply): NotificationEvent {
59 val id = NotificationId(reply.id)
60 return when (reply.data.kind) {
61 5 -> NotificationEvent.Activated(
62 id,
63 if (reply.action.byteLength == 0L)
64 None
65 else
66 Some(NotificationActionId(reply.action)),
67 reply.payload
68 )
69 6 -> NotificationEvent.Closed(
70 id,
71 when (reply.data.detail) {
72 1 -> NotificationCloseReason.Expired
73 2 -> NotificationCloseReason.Dismissed
74 3 -> NotificationCloseReason.Withdrawn
75 else -> NotificationCloseReason.Unknown
76 }
77 )
78 7 -> NotificationEvent.Failed(id, replyError(reply))
79 else -> throw WindowException("invalid notification event", WindowErrorKind.NativeFailure)
80 }