Skip to content
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

Support reading .ico icon files if passed to packaging #3008

Merged
merged 10 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/fyne/internal/commands/package-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"runtime"

"fyne.io/fyne/v2/cmd/fyne/internal/templates"
ico "github.com/Kodeworks/golang-image-ico"
ico "github.com/biessek/golang-ico"
"github.com/josephspurrier/goversioninfo"
"golang.org/x/sys/execabs"
)
Expand Down
40 changes: 39 additions & 1 deletion cmd/fyne/internal/commands/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import (
"errors"
"flag"
"fmt"
"image"
_ "image/jpeg" // import image encodings
_ "image/png" // import image encodings
"image/png" // import image encodings
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"

_ "github.com/biessek/golang-ico" // import image encodings
"github.com/urfave/cli/v2"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
Expand Down Expand Up @@ -347,6 +349,13 @@ func (p *Packager) validate() error {
if !util.Exists(p.icon) {
return errors.New("Missing application icon at \"" + p.icon + "\"")
}
if strings.ToLower(filepath.Ext(p.icon)) != ".png" {
tmp, err := normaliseIcon(p.icon)
if err != nil {
return err
}
p.icon = tmp
}

p.appID, err = validateAppID(p.appID, p.os, p.name, p.release)
if err != nil {
Expand Down Expand Up @@ -412,6 +421,35 @@ func mergeMetadata(p *appData, data *metadata.FyneApp) {
}
}

// normaliseIcon takes a non-png image file and converts it to PNG for use in packaging.
// Successful conversion will return a path to the new file.
// Any errors that occur will be returned with an empty string for new path.
func normaliseIcon(path string) (string, error) {
// convert icon
img, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("failed to open source image: %w", err)
}
defer img.Close()
srcImg, _, err := image.Decode(img)
if err != nil {
return "", fmt.Errorf("failed to decode source image: %w", err)
}

tmpPath := filepath.Join(os.TempDir(), "fyne-ico-tmp.png")
out, err := os.Create(tmpPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to remove the file once it is not necessary anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea.
I don't think we have a hook for the processed completing at this time.
Can you think of a clean way to do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doPackage use defer p.removeBuild(files). Maybe make doPackage and validate return a []string of files that need to be cleaned and set a defer in the caller to those function?

Copy link
Member

@Jacalz Jacalz May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could create a folder in the temp directory using https://pkg.go.dev/io/ioutil#TempDir and save everything there when we create temporary files. We then simply make sure to remove the whole folder before exiting the command (we could probably even hook up the interup signal https://www.tutorialspoint.com/how-to-handle-signals-in-golang so that it removes it before exiting using ctrl+c).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using ioutil.TempFile() instead?

Suggested change
tmpPath := filepath.Join(os.TempDir(), "fyne-ico-tmp.png")
out, err := os.Create(tmpPath)
out, err := ioutil.TempFile("", "fyne-ico-tmp.png")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used that originally, but it's not in Go 1.14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about that? It seems to exist in Go 1.14 if you look at this: https://pkg.go.dev/io/ioutil@go1.14#TempFile. The os.CreateTemp stuff was added in Go 1.16 but this does the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch, I must have got the two confused sorry.

if err != nil {
return "", fmt.Errorf("failed to open image output file: %w", err)
}

err = png.Encode(out, srcImg)
if err != nil {
return "", fmt.Errorf("failed to encode icon: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable out isn't closed if we get an error here.

}

return tmpPath, out.Close()
}

func validateAppID(appID, os, name string, release bool) (string, error) {
// old darwin compatibility
if os == "darwin" {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.14
require (
fyne.io/systray v1.9.1-0.20220515191230-b479fb2893c9
github.com/BurntSushi/toml v1.0.0
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9
github.com/akavel/rsrc v0.8.0 // indirect
github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3
github.com/fsnotify/fsnotify v1.5.1
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe
Expand All @@ -19,6 +19,7 @@ require (
github.com/gopherjs/gopherjs v1.17.2
github.com/jackmordaunt/icns/v2 v2.2.1
github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/lucor/goinfo v0.0.0-20210802170112-c078a2b0f08b
github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9 h1:1ltqoej5GtaWF8jaiA49HwsZD459jqm9YFz9ZtMFpQA=
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670 h1:FQPKKjDhzG0T4ew6dm6MGrXb4PRAi8ZmTuYuxcF62BM=
github.com/biessek/golang-ico v0.0.0-20180326222316-d348d9ea4670/go.mod h1:iRWAFbKXMMkVQyxZ1PfGlkBr1TjATx1zy2MRprV7A3Q=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
Expand Down Expand Up @@ -204,6 +204,8 @@ github.com/josephspurrier/goversioninfo v0.0.0-20200309025242-14b0ab84c6ca/go.mo
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e h1:LvL4XsI70QxOGHed6yhQtAU34Kx3Qq2wwBzGFKY8zKk=
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"runtime"
"sync"

ico "github.com/Kodeworks/golang-image-ico"
ico "github.com/biessek/golang-ico"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/animation"
Expand Down
24 changes: 0 additions & 24 deletions vendor/github.com/Kodeworks/golang-image-ico/.gitignore

This file was deleted.

27 changes: 0 additions & 27 deletions vendor/github.com/Kodeworks/golang-image-ico/LICENSE

This file was deleted.

6 changes: 0 additions & 6 deletions vendor/github.com/Kodeworks/golang-image-ico/README.md

This file was deleted.

Binary file not shown.
Binary file removed vendor/github.com/Kodeworks/golang-image-ico/new.ico
Binary file not shown.
72 changes: 0 additions & 72 deletions vendor/github.com/Kodeworks/golang-image-ico/writer.go

This file was deleted.

15 changes: 15 additions & 0 deletions vendor/github.com/biessek/golang-ico/.gitignore

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

8 changes: 8 additions & 0 deletions vendor/github.com/biessek/golang-ico/README.md

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