-
Notifications
You must be signed in to change notification settings - Fork 18
Package to open browser url from gio app #17
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
Open
g45t345rt
wants to merge
1
commit into
gioui:main
Choose a base branch
from
g45t345rt:main
base: main
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.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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,9 @@ | ||
## Open browser | ||
|
||
Cross platform open url for [Gio](https://gioui.org) applications. | ||
|
||
## Usage | ||
|
||
```go | ||
browser.OpenUrl("https://gioui.org/") | ||
``` |
This file contains hidden or 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,38 @@ | ||
//go:build android | ||
// +build android | ||
|
||
package browser | ||
|
||
/* | ||
#cgo LDFLAGS: -landroid | ||
|
||
#include <jni.h> | ||
#include <stdlib.h> | ||
*/ | ||
|
||
import ( | ||
"gioui.org/app" | ||
"git.wow.st/gmp/jni" | ||
) | ||
|
||
//go:generate javac -source 8 -target 8 -bootclasspath $ANDROID_HOME/platforms/android-30/android.jar -d $TEMP/browser_browser_android/classes browser_android.java | ||
//go:generate jar cf browser_android.jar -C $TEMP/browser_browser_android/classes . | ||
|
||
func OpenUrl(url string) error { | ||
err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { | ||
class, err := jni.LoadClass(env, jni.ClassLoaderFor(env, jni.Object(app.AppContext())), "org/gioui/x/browser/browser_android") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
methodId := jni.GetStaticMethodID(env, class, "openUrl", "(Landroid/content/Context;Ljava/lang/String;)V") | ||
err = jni.CallStaticVoidMethod(env, class, methodId, jni.Value(app.AppContext()), jni.Value(jni.JavaString(env, url))) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return err | ||
} |
Binary file not shown.
This file contains hidden or 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,14 @@ | ||
package org.gioui.x.browser; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
|
||
public class browser_android { | ||
public static void openUrl(Context context, String url) { | ||
Uri webpage = Uri.parse(url); | ||
Intent intent = new Intent(Intent.ACTION_VIEW, webpage); | ||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
context.startActivity(intent); | ||
} | ||
} |
This file contains hidden or 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,13 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
package browser | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
func OpenUrl(url string) error { | ||
cmd := exec.Command("open", url) | ||
return cmd.Run() | ||
} |
This file contains hidden or 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,32 @@ | ||
//go:build ios | ||
// +build ios | ||
|
||
package browser | ||
|
||
/* | ||
#cgo CFLAGS: -Werror -xobjective-c -fmodules -fobjc-arc | ||
extern static CFTypeRef newNSString(unichar *chars, NSUInteger length); | ||
extern static void openUrl(CFTypeRef str); | ||
*/ | ||
|
||
import "C" | ||
import ( | ||
"unicode/utf16" | ||
"unsafe" | ||
) | ||
|
||
func stringToNSString(str string) C.CFTypeRef { | ||
u16 := utf16.Encode([]rune(str)) | ||
var chars *C.unichar | ||
if len(u16) > 0 { | ||
chars = (*C.unichar)(unsafe.Pointer(&u16[0])) | ||
} | ||
return C.newNSString(chars, C.NSUInteger(len(u16))) | ||
} | ||
|
||
func OpenUrl(url string) error { | ||
cstr := stringToNSString(url) | ||
defer C.CFRelease(cstr) | ||
C.openUrl(cstr) | ||
return nil | ||
} |
This file contains hidden or 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,22 @@ | ||
#include <Foundation/Foundation.h> | ||
#include <UIKit/UIKit.h> | ||
#include <stdint.h> | ||
|
||
static CFTypeRef newNSString(unichar *chars, NSUInteger length) { | ||
@autoreleasepool { | ||
NSString *s = [NSString string]; | ||
if (length > 0) { | ||
s = [NSString stringWithCharacters:chars length:length]; | ||
} | ||
return CFBridgingRetain(s); | ||
} | ||
} | ||
|
||
static void openUrl(CFTypeRef str) { | ||
@autoreleasepool { | ||
NSString *s = (__bridge NSString *)str; | ||
NSURL *url = [NSURL URLWithString:s]; | ||
UIApplication *application = [UIApplication sharedApplication]; | ||
[application openURL:url options:@{} completionHandler:nil]; | ||
} | ||
} |
This file contains hidden or 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,13 @@ | ||
//go:build js | ||
// +build js | ||
|
||
package browser | ||
|
||
import ( | ||
"syscall/js" | ||
) | ||
|
||
func OpenUrl(url string) error { | ||
js.Global().Call("open", url, "_blank", "") | ||
return nil | ||
} | ||
This file contains hidden or 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,14 @@ | ||
//go:build (linux && !android) || freebsd || openbsd | ||
// +build linux,!android freebsd openbsd | ||
|
||
package browser | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
func OpenUrl(url string) error { | ||
// xdg-open is part of the freedesktop.org suite and should be available on all distro | ||
cmd := exec.Command("xdg-open", url) | ||
return cmd.Run() | ||
} |
This file contains hidden or 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,13 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package browser | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
func OpenUrl(url string) error { | ||
cmd := exec.Command("cmd", "/c", "start", url) | ||
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. Given that we can avoid spawning a |
||
return cmd.Run() | ||
} |
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.
How widely did you test this across browser platforms? I'm curious whether @inkeliz's sophisticated hacks are needed here.
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.
In Safari, the open function must be invoked inside onClick event. However, Gio gets and releases this event.
If
open
is called without direct user interaction, outside of onClick, it may not always work. It's inconsistent, so sometimes it works, but often it doesn't.The "hack" requires the user to click anywhere in the page to open, it's not perfect, but works as fallback.