Skip to content

Commit

Permalink
Merge pull request #3958 from Jacalz/advanced-desktop-file
Browse files Browse the repository at this point in the history
Specify custom desktop file fields in FyneApp.toml
  • Loading branch information
Jacalz committed Jun 20, 2023
2 parents 20757dd + 36e4b7f commit 07e3214
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
24 changes: 23 additions & 1 deletion cmd/fyne/internal/commands/package-unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"fyne.io/fyne/v2/cmd/fyne/internal/templates"

Expand All @@ -14,6 +15,10 @@ import (
type unixData struct {
Name, Exec, Icon string
Local string
GenericName string
Categories string
Comment string
Keywords string
}

func (p *Packager) packageUNIX() error {
Expand Down Expand Up @@ -52,7 +57,16 @@ func (p *Packager) packageUNIX() error {
desktop := filepath.Join(appsDir, p.Name+".desktop")
deskFile, _ := os.Create(desktop)

tplData := unixData{Name: p.Name, Exec: filepath.Base(p.exe), Icon: p.Name + filepath.Ext(p.icon), Local: local}
tplData := unixData{
Name: p.Name,
Exec: filepath.Base(p.exe),
Icon: p.Name + filepath.Ext(p.icon),
Local: local,
GenericName: p.linuxAndBSDMetadata.GenericName,
Keywords: formatDesktopFileList(p.linuxAndBSDMetadata.Keywords),
Comment: p.linuxAndBSDMetadata.Comment,
Categories: formatDesktopFileList(p.linuxAndBSDMetadata.Categories),
}
err = templates.DesktopFileUNIX.Execute(deskFile, tplData)
if err != nil {
return fmt.Errorf("failed to write desktop entry string: %w", err)
Expand All @@ -77,3 +91,11 @@ func (p *Packager) packageUNIX() error {

return nil
}

func formatDesktopFileList(items []string) string {
if len(items) == 0 {
return ""
}

return strings.Join(items, ";") + ";"
}
13 changes: 13 additions & 0 deletions cmd/fyne/internal/commands/package-unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package commands

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatDesktopFileList(t *testing.T) {
assert.Equal(t, "", formatDesktopFileList([]string{}))
assert.Equal(t, "One;", formatDesktopFileList([]string{"One"}))
assert.Equal(t, "One;Two;", formatDesktopFileList([]string{"One", "Two"}))
}
4 changes: 3 additions & 1 deletion cmd/fyne/internal/commands/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ type Packager struct {
tags, category string
tempDir string

customMetadata keyValueFlag
customMetadata keyValueFlag
linuxAndBSDMetadata metadata.LinuxAndBSD
}

// AddFlags adds the flags for interacting with the package command.
Expand Down Expand Up @@ -378,6 +379,7 @@ func (p *Packager) validate() (err error) {

p.appData.Release = p.release
p.appData.mergeMetadata(data)
p.linuxAndBSDMetadata = data.LinuxAndBSD
}

exeName := calculateExeName(p.srcDir, p.os)
Expand Down
9 changes: 9 additions & 0 deletions cmd/fyne/internal/metadata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type FyneApp struct {
Details AppDetails
Development map[string]string `toml:",omitempty"`
Release map[string]string `toml:",omitempty"`
LinuxAndBSD LinuxAndBSD
}

// AppDetails describes the build information, this group may be OS or arch specific
Expand All @@ -15,3 +16,11 @@ type AppDetails struct {
Version string `toml:",omitempty"`
Build int `toml:",omitempty"`
}

// LinuxAndBSD describes specific metadata for desktop files on Linux and BSD.
type LinuxAndBSD struct {
GenericName string `toml:",omitempty"`
Categories []string `toml:",omitempty"`
Comment string `toml:",omitempty"`
Keywords []string `toml:",omitempty"`
}
2 changes: 1 addition & 1 deletion cmd/fyne/internal/templates/bundled.go

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

7 changes: 7 additions & 0 deletions cmd/fyne/internal/templates/data/app.desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
[Desktop Entry]
Type=Application
Name={{.Name}}
{{- if ne .GenericName ""}}
GenericName={{.GenericName}}{{end}}
Exec={{.Exec}}
Icon={{.Name}}
{{- if ne .Comment ""}}
Comment={{.Comment}}{{end}}
{{- if ne .Categories ""}}
Categories={{.Categories}}{{end}}
{{if ne .Keywords ""}}Keywords={{.Keywords}}{{else}}Keywords=fyne;{{end}}
12 changes: 9 additions & 3 deletions cmd/fyne_demo/FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
Name = "Fyne Demo"
ID = "io.fyne.demo"
Version = "2.3.0"
Build = 10
Build = 11

[Development]
HelperText = "This binary was built with debug symbol"
HelperText = "This binary was built with debug symbols"

[Release]
HelperText = "This binary was built without debug symbol"
HelperText = "This binary was built without debug symbols"

[LinuxAndBSD]
GenericName = "Toolkit Demo"
Categories = ["Development"]
Comment = "A demo of Fyne and its capabilities."
Keywords = ["demo", "fyne"]

0 comments on commit 07e3214

Please sign in to comment.