forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-darwin.go
104 lines (88 loc) · 2.64 KB
/
package-darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package commands
import (
"fmt"
"image"
"image/color"
"os"
"path/filepath"
"strings"
"fyne.io/fyne/v2/cmd/fyne/internal/templates"
"github.com/fogleman/gg"
"github.com/nfnt/resize"
"github.com/jackmordaunt/icns/v2"
)
type darwinData struct {
Name, ExeName string
AppID string
Version string
Build int
Category string
}
func (p *Packager) packageDarwin() (err error) {
appDir := util.EnsureSubDir(p.dir, p.Name+".app")
exeName := filepath.Base(p.exe)
contentsDir := util.EnsureSubDir(appDir, "Contents")
info := filepath.Join(contentsDir, "Info.plist")
infoFile, err := os.Create(info)
if err != nil {
return fmt.Errorf("failed to create plist template: %w", err)
}
defer func() {
if r := infoFile.Close(); r != nil && err == nil {
err = r
}
}()
tplData := darwinData{Name: p.Name, ExeName: exeName, AppID: p.AppID, Version: p.AppVersion, Build: p.AppBuild,
Category: strings.ToLower(p.category)}
if err := templates.InfoPlistDarwin.Execute(infoFile, tplData); err != nil {
return fmt.Errorf("failed to write plist template: %w", err)
}
macOSDir := util.EnsureSubDir(contentsDir, "MacOS")
binName := filepath.Join(macOSDir, exeName)
if err := util.CopyExeFile(p.exe, binName); err != nil {
return fmt.Errorf("failed to copy executable: %w", err)
}
resDir := util.EnsureSubDir(contentsDir, "Resources")
icnsPath := filepath.Join(resDir, "icon.icns")
img, err := os.Open(p.icon)
if err != nil {
return fmt.Errorf("failed to open source image \"%s\": %w", p.icon, err)
}
defer img.Close()
srcImg, _, err := image.Decode(img)
if err != nil {
return fmt.Errorf("failed to decode source image: %w", err)
}
dest, err := os.Create(icnsPath)
if err != nil {
return fmt.Errorf("failed to open destination file: %w", err)
}
defer func() {
if r := dest.Close(); r != nil && err == nil {
err = r
}
}()
if !p.rawIcon {
srcImg = processMacOSIcon(srcImg)
}
if err := icns.Encode(dest, srcImg); err != nil {
return fmt.Errorf("failed to encode icns: %w", err)
}
return nil
}
func processMacOSIcon(in image.Image) image.Image {
size := 1024
border := 100
radius := 185.4
innerSize := int(float64(size) - float64(border*2)) // how many pixels inside border
sized := resize.Resize(uint(innerSize), uint(innerSize), in, resize.Lanczos3)
dc := gg.NewContext(size, size)
dc.DrawRoundedRectangle(float64(border), float64(border), float64(innerSize), float64(innerSize), radius)
dc.SetColor(color.Black)
dc.Fill()
mask := dc.AsMask()
dc = gg.NewContext(size, size)
_ = dc.SetMask(mask) // ignore error if size was not equal, as it is
dc.DrawImage(sized, border, border)
return dc.Image()
}