mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
app: add custom scheme support
Now, it's possible to launch one Gio app using a custom URI scheme, such as `gio://some/data`.
This feature is supported on Android, iOS, macOS and Windows, issuing a new transfer.URLEvent,
containing the URL launched. If the program is already open, one transfer.URLEvent will be
sent to the current app.
Limitations:
On Windows, if the program listen to schemes (compiled with `-schemes`), then just a single
instance of the app can be open. In other words, just a single `myprogram.exe` can
be active.
Security:
Deeplinking have the same level of security of clipboard. Any other software can send such
information and read the content, without any restriction. That should not be used to transfer
sensible data, and can't be fully trusted.
Setup/Compiling:
In order to set the custom scheme, you need to use the new `-schemes` flag in `gogio`, using
as `-schemes gio` will listen to `gio://`.
If you are not using gogio you need to defined some values, which varies for each OS:
macOS/iOS - You need to define the following Properly List:
```
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>yourCustomScheme</string>
</array>
</dict>
</array>
```
Windows - You need to compiling using -X argument:
```
-ldflags="-X "gioui.org/app.schemesURI=yourCustomScheme" -H=windowsgui"
```
Android - You need to add IntentFilter in GioActivity:
```
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="yourCustomScheme"></data>
</intent-filter>
```
That assumes that you still using GioActivity and GioAppDelegate, otherwise more
changes are required.
Events are routed to a new app.Events, which are not linked to a specific window.
Signed-off-by: inkeliz <inkeliz@inkeliz.com>
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+233
-3
@@ -5,8 +5,12 @@ package app
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gioui.org/io/transfer"
|
||||
syscall "golang.org/x/sys/windows"
|
||||
"golang.org/x/sys/windows/registry"
|
||||
"image"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -16,8 +20,6 @@ import (
|
||||
"unicode/utf8"
|
||||
"unsafe"
|
||||
|
||||
syscall "golang.org/x/sys/windows"
|
||||
|
||||
"gioui.org/app/internal/windows"
|
||||
"gioui.org/op"
|
||||
"gioui.org/unit"
|
||||
@@ -28,7 +30,6 @@ import (
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/io/transfer"
|
||||
)
|
||||
|
||||
type Win32ViewEvent struct {
|
||||
@@ -56,6 +57,8 @@ type window struct {
|
||||
|
||||
const _WM_WAKEUP = windows.WM_USER + iota
|
||||
|
||||
const copyDataURLType = 0xffffff00
|
||||
|
||||
type gpuAPI struct {
|
||||
priority int
|
||||
initializer func(w *window) (context, error)
|
||||
@@ -81,6 +84,7 @@ var resources struct {
|
||||
}
|
||||
|
||||
func osMain() {
|
||||
processURLEvent(startupURI())
|
||||
select {}
|
||||
}
|
||||
|
||||
@@ -452,6 +456,20 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr
|
||||
case windows.WM_IME_ENDCOMPOSITION:
|
||||
w.w.SetComposingRegion(key.Range{Start: -1, End: -1})
|
||||
return windows.TRUE
|
||||
case windows.WM_COPYDATA:
|
||||
data := (*windows.CopyDataStruct)(unsafe.Pointer(lParam))
|
||||
switch data.DwData {
|
||||
case copyDataURLType:
|
||||
if schemesURI == "" {
|
||||
return windows.TRUE
|
||||
}
|
||||
|
||||
uri := syscall.UTF16PtrToString((*uint16)(unsafe.Pointer(data.LpData)))
|
||||
if processURLEvent(uri) {
|
||||
w.Perform(system.ActionRaise)
|
||||
}
|
||||
return windows.TRUE
|
||||
}
|
||||
}
|
||||
|
||||
return windows.DefWindowProc(hwnd, msg, wParam, lParam)
|
||||
@@ -1058,3 +1076,215 @@ func getPointerButtons(pi windows.PointerInfo) pointer.Buttons {
|
||||
|
||||
return btns
|
||||
}
|
||||
|
||||
// schemesURI is a list of schemes, comma separated, that must be
|
||||
// defined using -X compiler ldflag, that used in gogio.
|
||||
var schemesURI string
|
||||
|
||||
func init() {
|
||||
if schemesURI == "" {
|
||||
return
|
||||
}
|
||||
|
||||
currentSchemes := strings.Split(schemesURI, ",")
|
||||
oldSchemes := registeredSchemes(ID)
|
||||
|
||||
for _, s := range currentSchemes {
|
||||
for i, o := range oldSchemes {
|
||||
if s == o {
|
||||
oldSchemes = append(oldSchemes[:i], oldSchemes[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(oldSchemes) > 0 {
|
||||
go unregisterSchemes(ID, oldSchemes)
|
||||
}
|
||||
|
||||
if len(currentSchemes) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// On Windows, launching the app using a URI will start a new instance of the app,
|
||||
// a new window. That behavior, by default, doesn't align with iOS/Android/macOS, where
|
||||
// the deeplink sends the event to the running app (if any). We are emulating it.
|
||||
if hwnd, _ := windows.FindWindow(ID); hwnd != 0 {
|
||||
if u := startupURI(); u != "" {
|
||||
broadcastURI(hwnd, u)
|
||||
}
|
||||
os.Exit(0)
|
||||
return
|
||||
}
|
||||
|
||||
go registerSchemes(ID, currentSchemes)
|
||||
}
|
||||
|
||||
func startupURI() string {
|
||||
if len(os.Args) == 3 && os.Args[1] == "-gio_launch_url" {
|
||||
return os.Args[2]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func processURLEvent(rawurl string) bool {
|
||||
if rawurl == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
evt, err := newURLEvent(rawurl)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, scheme := range strings.Split(schemesURI, ",") {
|
||||
if strings.EqualFold(scheme, evt.URL.Scheme) {
|
||||
processGlobalEvent(evt)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func broadcastURI(hwnd syscall.Handle, uri string) {
|
||||
data, err := syscall.UTF16FromString(uri)
|
||||
if err != nil {
|
||||
return // Only happens if uri contains NULL character.
|
||||
}
|
||||
|
||||
pinner := new(runtime.Pinner)
|
||||
defer pinner.Unpin()
|
||||
pinner.Pin(unsafe.Pointer(&data[0]))
|
||||
|
||||
msg := &windows.CopyDataStruct{
|
||||
DwData: copyDataURLType,
|
||||
CbData: uint32(len(data) * int(unsafe.Sizeof(data[0]))),
|
||||
LpData: uintptr(unsafe.Pointer(unsafe.SliceData(data))),
|
||||
}
|
||||
pinner.Pin(unsafe.Pointer(msg))
|
||||
|
||||
// SendMessage blocks until the message is processed.
|
||||
windows.SendMessage(hwnd, windows.WM_COPYDATA, 0, uintptr(unsafe.Pointer(msg)))
|
||||
}
|
||||
|
||||
func registeredSchemes(appid string) []string {
|
||||
meta, err := registry.OpenKey(registry.CURRENT_USER, `Software\\`+appid, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer meta.Close()
|
||||
|
||||
schemes, _, _ := meta.GetStringsValue("URISchemes")
|
||||
return schemes
|
||||
}
|
||||
|
||||
func registerSchemes(appid string, schemes []string) error {
|
||||
reg := func(scheme string) error {
|
||||
key, existent, err := registry.CreateKey(registry.CURRENT_USER, `Software\\Classes\\`+scheme, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer key.Close()
|
||||
|
||||
if existent {
|
||||
// Check if the existent key belongs to the current application
|
||||
id, _, err := key.GetStringValue("appid")
|
||||
if err != nil || id != appid {
|
||||
return fmt.Errorf("scheme %s already registered by another application", scheme)
|
||||
}
|
||||
}
|
||||
|
||||
path, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = key.SetStringValue("", "URL:"+scheme+" Protocol"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = key.SetStringValue("URL Protocol", ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = key.SetStringValue("appid", appid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
icon, _, err := registry.CreateKey(key, `DefaultIcon`, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer icon.Close()
|
||||
|
||||
if err = icon.SetStringValue("", `"`+path+`",1`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd, _, err := registry.CreateKey(key, `shell\\open\\command`, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cmd.Close()
|
||||
|
||||
if err = cmd.SetStringValue("", `"`+path+`" -gio_launch_url "%1"`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, scheme := range schemes {
|
||||
if scheme == "" {
|
||||
continue // just in case
|
||||
}
|
||||
if err := reg(scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
meta, _, err := registry.CreateKey(registry.CURRENT_USER, `Software\\`+appid, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer meta.Close()
|
||||
|
||||
if err = meta.SetStringsValue("URISchemes", schemes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func unregisterSchemes(appid string, schemes []string) {
|
||||
classes, err := registry.OpenKey(registry.CURRENT_USER, `Software\\Classes`, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer classes.Close()
|
||||
|
||||
for _, scheme := range schemes {
|
||||
if scheme == "" {
|
||||
continue // just in case
|
||||
}
|
||||
|
||||
key, err := registry.OpenKey(classes, scheme, registry.ALL_ACCESS)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
id, _, err := key.GetStringValue("appid")
|
||||
if err == nil && id != appid {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, k := range []string{`DefaultIcon`, `shell\\open\\command`, `shell\\open`, `shell`} {
|
||||
registry.DeleteKey(key, k)
|
||||
}
|
||||
|
||||
if err := key.Close(); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
registry.DeleteKey(classes, scheme)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user