Skip to content

Commit

Permalink
#212: introduces native call to open an external browser on MacOsX
Browse files Browse the repository at this point in the history
  • Loading branch information
rladstaetter committed Apr 10, 2024
1 parent 6f1dd2f commit 8a9c457
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
18 changes: 17 additions & 1 deletion app/src/main/java/app/logorrr/OsxBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@
* Provides interface methods for native code
*/
public class OsxBridge {

/**
* register a file in the macosx permission system
*
* @param path absolute path to the file
*/
public static native void registerPath(String path);

/**
* release a path (if the path is no longer used for example)
*
* @param path absolute path to the file
*/
public static native void releasePath(String path);

/**
* open an url via the native browser
*
* @param url the url to open, for example https://www.logorrr.app/
*/
public static native void openUrl(String url);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package app.logorrr.services.hostservices

import app.logorrr.OsxBridge
import app.logorrr.util.OsUtil
import javafx.application.HostServices

class NativeHostServices(hostServices: => HostServices) extends LogoRRRHostServices {

override def showDocument(url: String): Unit = hostServices.showDocument(url)
override def showDocument(url: String): Unit = {
// hostServices.showDocument doesn't work with Entitlements / Gatekeeper
// delegate to native method
if (OsUtil.isMac) {
OsxBridge.openUrl(url)
} else {
hostServices.showDocument(url)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<!-- to be able to access files see https://developer.apple.com/documentation/professional_video_applications/fcpxml_reference/asset/media-rep/bookmark/enabling_security-scoped_bookmark_and_url_access?language=swift -->
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<!-- to access network (open urls) -->
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
11 changes: 11 additions & 0 deletions native/native-osx/src/main/c/LogoRRRSwift.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import Foundation
import Cocoa

@_cdecl("openUrl")
public func openUrl(path : UnsafePointer<Int8>) {
if let urlString = String(validatingUTF8: path) {
if let url = URL(string: urlString) {
NSWorkspace.shared.open(url)
}
}
}


@_cdecl("releasePath")
public func releasePath(path: UnsafePointer<Int8>) {
Expand Down
16 changes: 12 additions & 4 deletions native/native-osx/src/main/c/OsxBridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

JNIEXPORT void JNICALL Java_app_logorrr_OsxBridge_registerPath(JNIEnv *env, jobject obj, jstring path) {
const char *pathChars = (*env)->GetStringUTFChars(env, path, NULL);
if (pathChars != NULL) {
if (pathChars != NULL) {
// Call the Swift function from C
registerPath(pathChars);

Expand All @@ -14,11 +14,19 @@ JNIEXPORT void JNICALL Java_app_logorrr_OsxBridge_registerPath(JNIEnv *env, jobj
}
}


JNIEXPORT void JNICALL Java_app_logorrr_OsxBridge_releasePath(JNIEnv *env, jobject obj, jstring path) {
const char *pathChars = (*env)->GetStringUTFChars(env, path, NULL);
if (pathChars != NULL) {
if (pathChars != NULL) {
releasePath(pathChars);
(*env)->ReleaseStringUTFChars(env, path, pathChars);
}
}
}

JNIEXPORT void JNICALL Java_app_logorrr_OsxBridge_openUrl(JNIEnv *env, jobject obj, jstring path) {
const char *urlChars = (*env)->GetStringUTFChars(env, path, NULL);
if (urlChars != NULL) {
openUrl(urlChars);
(*env)->ReleaseStringUTFChars(env, path, urlChars);
}
}

8 changes: 8 additions & 0 deletions native/native-osx/src/main/c/app_logorrr_OsxBridge.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion native/native-osx/src/main/c/app_logorrr_OsxBridge_swift.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
void registerPath(const char* path);
void releasePath(const char* path);
void releasePath(const char* path);
void openUrl(const char* url);

0 comments on commit 8a9c457

Please sign in to comment.