linux custom protocol + .desktop

This commit is contained in:
Atterpac 2025-05-18 16:48:09 -04:00
commit f143bd6866
4 changed files with 84 additions and 9 deletions

View file

@ -1 +1,21 @@
#!/bin/bash
#!/bin/sh
# Update desktop database for .desktop file changes
# This makes the application appear in application menus and registers its capabilities.
if command -v update-desktop-database >/dev/null 2>&1; then
echo "Updating desktop database..."
update-desktop-database -q /usr/share/applications
else
echo "Warning: update-desktop-database command not found. Desktop file may not be immediately recognized." >&2
fi
# Update MIME database for custom URL schemes (x-scheme-handler)
# This ensures the system knows how to handle your custom protocols.
if command -v update-mime-database >/dev/null 2>&1; then
echo "Updating MIME database..."
update-mime-database -n /usr/share/mime
else
echo "Warning: update-mime-database command not found. Custom URL schemes may not be immediately recognized." >&2
fi
exit 0

View file

@ -0,0 +1,15 @@
[Desktop Entry]
Version=1.0
Name={{.ProductName}}
Comment={{.ProductDescription}}
# The Exec line includes %u to pass the URL to the application
Exec=/usr/local/bin/{{.BinaryName}} %u
Terminal=false
Type=Application
Icon={{.BinaryName}}
Categories=Utility;
StartupWMClass={{.BinaryName}}
{{if .Info.Protocols -}}
MimeType={{range $index, $protocol := .Info.Protocols}}x-scheme-handler/{{$protocol.Scheme}};{{end}}
{{- end}}

View file

@ -28,6 +28,13 @@ depends:
- gtk3
- libwebkit2gtk
# scripts section to ensure desktop database is updated after install
scripts:
postinstall: "./build/linux/nfpm/scripts/postinstall.sh"
# You can also add preremove, postremove if needed
# preremove: "./build/linux/nfpm/scripts/preremove.sh"
# postremove: "./build/linux/nfpm/scripts/postremove.sh"
# replaces:
# - foobar
# provides:
@ -42,9 +49,4 @@ depends:
# conflicts:
# - not-foo
# - not-bar
# changelog: "changelog.yaml"
# scripts:
# preinstall: ./build/linux/nfpm/scripts/preinstall.sh
# postinstall: ./build/linux/nfpm/scripts/postinstall.sh
# preremove: ./build/linux/nfpm/scripts/preremove.sh
# postremove: ./build/linux/nfpm/scripts/postremove.sh
# changelog: "changelog.yaml"

View file

@ -27,7 +27,8 @@ import (
func init() {
// FIXME: This should be handled appropriately in the individual files most likely.
// Set GDK_BACKEND=x11 if currently unset and XDG_SESSION_TYPE is unset, unspecified or x11 to prevent warnings
if os.Getenv("GDK_BACKEND") == "" && (os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") {
if os.Getenv("GDK_BACKEND") == "" &&
(os.Getenv("XDG_SESSION_TYPE") == "" || os.Getenv("XDG_SESSION_TYPE") == "unspecified" || os.Getenv("XDG_SESSION_TYPE") == "x11") {
_ = os.Setenv("GDK_BACKEND", "x11")
}
}
@ -94,6 +95,40 @@ func (a *linuxApp) setApplicationMenu(menu *Menu) {
func (a *linuxApp) run() error {
if len(os.Args) == 2 { // Case: program + 1 argument
arg1 := os.Args[1]
// Check if the argument is likely a URL from a custom protocol invocation
if strings.Contains(arg1, "://") {
a.parent.info("Application launched with argument, potentially a URL from custom protocol", "url", arg1)
eventContext := newApplicationEventContext()
eventContext.setURL(arg1)
applicationEvents <- &ApplicationEvent{
Id: uint(events.Common.ApplicationLaunchedWithUrl),
ctx: eventContext,
}
} else {
// Check if the argument matches any file associations
if a.parent.options.FileAssociations != nil {
for _, association := range a.parent.options.FileAssociations {
if strings.HasSuffix(arg1, association.Extension) {
a.parent.info("File opened via file association", "file", arg1, "extension", association.Extension)
eventContext := newApplicationEventContext()
eventContext.setOpenedWithFile(arg1)
applicationEvents <- &ApplicationEvent{
Id: uint(events.Common.ApplicationOpenedWithFile),
ctx: eventContext,
}
return
}
}
}
a.parent.info("Application launched with single argument (not a URL), potential file open?", "arg", arg1)
}
} else if len(os.Args) > 2 {
// Log if multiple arguments are passed
a.parent.info("Application launched with multiple arguments", "args", os.Args[1:])
}
a.parent.OnApplicationEvent(events.Linux.ApplicationStartup, func(evt *ApplicationEvent) {
// TODO: What should happen here?
})
@ -141,7 +176,10 @@ func (a *linuxApp) monitorThemeChanges() {
defer handlePanic()
conn, err := dbus.ConnectSessionBus()
if err != nil {
a.parent.info("[WARNING] Failed to connect to session bus; monitoring for theme changes will not function:", err)
a.parent.info(
"[WARNING] Failed to connect to session bus; monitoring for theme changes will not function:",
err,
)
return
}
defer conn.Close()