-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native Notifications [WIP] #462
Draft
VeselovAlex
wants to merge
3
commits into
master
Choose a base branch
from
notifications-wip
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
kotlin.code.style=official | ||
kotlin.mpp.enableGranularSourceSetsMetadata=true | ||
kotlin.native.enableDependencyPropagation=false | ||
kotlin.native.binary.memoryModel=experimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>APPL</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleVersion</key> | ||
<string>1</string> | ||
<key>UILaunchStoryboardName</key> | ||
<string></string> | ||
<key>NSHighResolutionCapable</key> | ||
<string>True</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
package org.jetbrains.skiko.sample | ||
|
||
import kotlinx.coroutines.* | ||
import platform.AppKit.* | ||
|
||
import org.jetbrains.skiko.* | ||
import platform.Foundation.NSMakeRect | ||
import platform.Foundation.NSSelectorFromString | ||
import platform.darwin.NSObject | ||
import org.jetbrains.skiko.notifications.Notification | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if notifications deserve their own package. |
||
import platform.Foundation.* | ||
import platform.darwin.* | ||
|
||
fun makeApp(skiaLayer: SkiaLayer) = Clocks(skiaLayer) | ||
fun makeApp(skiaLayer: SkiaLayer) = object : Clocks(skiaLayer) { | ||
override fun onKeyboardEvent(event: SkikoKeyboardEvent) { | ||
super.onKeyboardEvent(event) | ||
if (event.kind == SkikoKeyboardEventKind.DOWN) when (event.key) { | ||
SkikoKey.KEY_N -> CoroutineScope(SkikoDispatchers.Main).launch { | ||
Notification( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, why |
||
title = "Hello", | ||
body = "It works!" | ||
).send() | ||
println("After notification sent") | ||
} | ||
else -> {} | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
val app = NSApplication.sharedApplication() | ||
|
@@ -20,7 +35,7 @@ fun main() { | |
appMenuItem.setSubmenu(appMenu) | ||
appMenu.addItemWithTitle("About $appName", NSSelectorFromString("orderFrontStandardAboutPanel:"), "a") | ||
appMenu.addItemWithTitle("Quit $appName", NSSelectorFromString("terminate:"), "q") | ||
|
||
app.delegate = object: NSObject(), NSApplicationDelegateProtocol { | ||
override fun applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication): Boolean { | ||
return true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
skiko/src/awtMain/kotlin/org/jetbrains/skiko/notifications/Expects.awt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
TODO("Not implemented yet") | ||
} |
6 changes: 6 additions & 0 deletions
6
skiko/src/commonMain/kotlin/org/jetbrains/skiko/notifications/Exceptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
sealed class NotificationError(override val message: String) : Throwable(message) | ||
|
||
class PermissionNotGrantedError : NotificationError("Permission not granted for notification") | ||
class NotificationsNotSupportedError : NotificationError("Notifications are not supported for this platform") |
3 changes: 3 additions & 0 deletions
3
skiko/src/commonMain/kotlin/org/jetbrains/skiko/notifications/Expects.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal expect suspend fun sendNotification(notification: Notification) |
7 changes: 7 additions & 0 deletions
7
skiko/src/commonMain/kotlin/org/jetbrains/skiko/notifications/Notification.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
class Notification(var title: String, var body: String) { | ||
var iconPath: String? = null | ||
|
||
suspend fun send() = sendNotification(this) | ||
} |
44 changes: 44 additions & 0 deletions
44
skiko/src/darwinMain/kotlin/org/jetbrains/skiko/notifications/Expects.darwin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
import kotlinx.coroutines.withContext | ||
import org.jetbrains.skiko.SkikoDispatchers | ||
import platform.Foundation.NSError | ||
import platform.Foundation.NSUUID | ||
import platform.UserNotifications.* | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
@Suppress("NAME_SHADOWING") | ||
internal actual suspend fun sendNotification(notification: Notification) = withContext(SkikoDispatchers.IO) { | ||
val nc = UNUserNotificationCenter.currentNotificationCenter() | ||
val options = UNAuthorizationOptionAlert or UNAuthorizationOptionSound or UNAuthorizationOptionBadge | ||
|
||
val sent = suspendCoroutine<Boolean> { cont -> | ||
nc.requestAuthorizationWithOptions(options) { granted: Boolean, err: NSError? -> | ||
if (granted) { | ||
with(notification) { | ||
val request = UNNotificationRequest.requestWithIdentifier(id, content, null) | ||
nc.addNotificationRequest(request) { err: NSError? -> | ||
err?.let { println("Sent with error: $it") } | ||
cont.resume(err == null) | ||
} | ||
} | ||
} else { | ||
println("No auth: $err") | ||
cont.resume(false) | ||
} | ||
} | ||
} | ||
println("Sent: $sent") | ||
} | ||
|
||
private val Notification.content by lazy { | ||
UNMutableNotificationContent().apply { | ||
setBody(body) | ||
setTitle(title) | ||
} | ||
} | ||
|
||
private val Notification.id by lazy { | ||
NSUUID().UUIDString | ||
} |
29 changes: 29 additions & 0 deletions
29
skiko/src/jsMain/kotlin/org/jetbrains/skiko/notifications/Expects.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
import kotlinx.browser.window | ||
import kotlinx.coroutines.await | ||
import org.w3c.notifications.DENIED | ||
import org.w3c.notifications.GRANTED | ||
import org.w3c.notifications.NotificationOptions | ||
import org.w3c.notifications.NotificationPermission | ||
import org.w3c.notifications.Notification as WebNotification | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
if (window.asDynamic()["Notification"] == undefined) { | ||
throw NotificationsNotSupportedError() | ||
} | ||
|
||
val permission = when (WebNotification.permission) { | ||
NotificationPermission.GRANTED -> NotificationPermission.GRANTED | ||
NotificationPermission.DENIED -> NotificationPermission.DENIED | ||
else -> WebNotification.requestPermission().await() | ||
} | ||
|
||
if (permission == NotificationPermission.GRANTED) { | ||
WebNotification(notification.title, NotificationOptions( | ||
body = notification.body | ||
)) | ||
} else { | ||
throw PermissionNotGrantedError() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
skiko/src/linuxMain/kotlin/org/jetbrains/skiko/notifications/Expects.linux.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jetbrains.skiko.notifications | ||
|
||
internal actual suspend fun sendNotification(notification: Notification) { | ||
TODO("Not implemented yet") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporarily opened to add custom key binding for macOS