Skip to content

Commit

Permalink
Embed assets into app binary
Browse files Browse the repository at this point in the history
This will fix errors that occur when running the app outside the
project directory.
  • Loading branch information
beansgum committed Sep 8, 2021
1 parent 9f72abb commit 42865b5
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 144 deletions.
35 changes: 2 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package main

import (
"fmt"
"image"
"net/http"
"os"
"path/filepath"
"strings"

"gioui.org/app"

_ "net/http/pprof"

"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui"
_ "github.com/planetdecred/godcr/ui/assets"
"github.com/planetdecred/godcr/wallet"
)

Expand All @@ -33,33 +31,6 @@ func main() {

dcrlibwallet.SetLogLevels(cfg.DebugLevel)

absoluteWdPath, err := ui.GetAbsolutePath()
if err != nil {
panic(err)
}

decredIcons := make(map[string]image.Image)
err = filepath.Walk(filepath.Join(absoluteWdPath, "ui/assets/decredicons"), func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if info.IsDir() || !strings.HasSuffix(path, ".png") {
return nil
}

f, _ := os.Open(path)
img, _, err := image.Decode(f)
if err != nil {
return err
}
split := strings.Split(info.Name(), ".")
decredIcons[split[0]] = img
return nil
})
if err != nil {
log.Warn(err)
}

var confirms int32 = dcrlibwallet.DefaultRequiredConfirmations

if cfg.SpendUnconfirmed {
Expand All @@ -79,9 +50,7 @@ func main() {
os.Exit(0)
}()

collection := fontCollection()

win, appWindow, err := ui.CreateWindow(wal, decredIcons, collection, internalLog)
win, appWindow, err := ui.CreateWindow(wal, internalLog)
if err != nil {
fmt.Printf("Could not initialize window: %s\ns", err)
return
Expand Down
39 changes: 9 additions & 30 deletions font.go → ui/assets/font.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main
package assets

import (
"fmt"
"os"
"path/filepath"
"sync"

"gioui.org/font/opentype"
Expand All @@ -15,8 +13,6 @@ import (
"golang.org/x/image/font/gofont/gomedium"
"golang.org/x/image/font/gofont/gomediumitalic"
"golang.org/x/image/font/gofont/goregular"

"github.com/planetdecred/godcr/ui"
)

var (
Expand All @@ -25,33 +21,33 @@ var (
)

// FontCollection registers the fonts to used in the app
func fontCollection() []text.FontFace {
regularItalic, err := getFontByte("ui/assets/fonts/source_sans_pro_It.otf")
func FontCollection() []text.FontFace {
regularItalic, err := getFontByte("fonts/source_sans_pro_It.otf")
if err != nil {
regularItalic = goitalic.TTF
}

regular, err := getFontByte("ui/assets/fonts/source_sans_pro_regular.otf")
regular, err := getFontByte("fonts/source_sans_pro_regular.otf")
if err != nil {
regular = goregular.TTF
}

semibold, err := getFontByte("ui/assets/fonts/source_sans_pro_semibold.otf")
semibold, err := getFontByte("fonts/source_sans_pro_semibold.otf")
if err != nil {
semibold = gomedium.TTF
}

semiboldItalic, err := getFontByte("ui/assets/fonts/source_sans_pro_semiboldIt.otf")
semiboldItalic, err := getFontByte("fonts/source_sans_pro_semiboldIt.otf")
if err != nil {
semiboldItalic = gomediumitalic.TTF
}

bold, err := getFontByte("ui/assets/fonts/source_sans_pro_bold.otf")
bold, err := getFontByte("fonts/source_sans_pro_bold.otf")
if err != nil {
bold = gobold.TTF
}

boldItalic, err := getFontByte("ui/assets/fonts/source_sans_pro_boldIt.otf")
boldItalic, err := getFontByte("fonts/source_sans_pro_boldIt.otf")
if err != nil {
boldItalic = gobolditalic.TTF
}
Expand Down Expand Up @@ -80,22 +76,5 @@ func register(fnt text.Font, fontByte []byte) {
}

func getFontByte(path string) ([]byte, error) {
absoluteWdPath, err := ui.GetAbsolutePath()
if err != nil {
log.Errorf("failed to get absoluteWdPath: %s", err.Error())
return nil, err
}

source, err := os.Open(filepath.Join(absoluteWdPath, path))
if err != nil {
return nil, err
}

stat, err := source.Stat()
if err != nil {
return nil, err
}
bytes := make([]byte, stat.Size())
source.Read(bytes)
return bytes, nil
return content.ReadFile(path)
}
52 changes: 52 additions & 0 deletions ui/assets/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package assets

import (
"bytes"
"embed"
"image"
"strings"
)

//go:embed *
var content embed.FS

var DecredIcons map[string]image.Image

func init() {
decredIcons, err := Icons()
if err != nil {
panic("Error loading icons")
}

DecredIcons = decredIcons
}

func Icons() (map[string]image.Image, error) {
entries, err := content.ReadDir("decredicons")
if err != nil {
return nil, err
}

decredIcons := make(map[string]image.Image)
for _, entry := range entries {

if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".png") {
continue
}

imgBytes, err := content.ReadFile("decredicons/" + entry.Name())
if err != nil {
return nil, err
}

img, _, err := image.Decode(bytes.NewBuffer(imgBytes))
if err != nil {
return nil, err
}

split := strings.Split(entry.Name(), ".")
decredIcons[split[0]] = img
}

return decredIcons, nil
}
92 changes: 52 additions & 40 deletions ui/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package load

import (
"image"
"errors"

"golang.org/x/exp/shiny/materialdesign/icons"
"golang.org/x/text/language"
Expand All @@ -17,6 +17,7 @@ import (
"gioui.org/widget"

"github.com/planetdecred/dcrlibwallet"
"github.com/planetdecred/godcr/ui/assets"
"github.com/planetdecred/godcr/ui/decredmaterial"
"github.com/planetdecred/godcr/ui/notification"
"github.com/planetdecred/godcr/wallet"
Expand Down Expand Up @@ -86,7 +87,55 @@ type Load struct {
PopToFragment func(pageID string)
}

func NewLoad(th *decredmaterial.Theme, decredIcons map[string]image.Image) *Load {
func NewLoad() (*Load, error) {

wl := &WalletLoad{
Wallet: new(wallet.Wallet),
Account: new(wallet.Account),
Info: new(wallet.MultiWalletInfo),
SyncStatus: new(wallet.SyncStatus),
Transactions: new(wallet.Transactions),
UnspentOutputs: new(wallet.UnspentOutputs),
Tickets: new(*wallet.Tickets),
VspInfo: new(wallet.VSP),
Proposals: new(wallet.Proposals),

SelectedProposal: new(dcrlibwallet.Proposal),
}

r := &Receiver{
AcctMixerStatus: make(chan *wallet.AccountMixer),
SyncedProposal: make(chan *wallet.Proposal),
}

icons := loadIcons()
th := decredmaterial.NewTheme(assets.FontCollection(), assets.DecredIcons, false)
if th == nil {
return nil, errors.New("unexpected error while loading theme")
}
l := &Load{
Theme: th,
Icons: icons,
WL: wl,
Receiver: r,
Toast: notification.NewToast(th),

Printer: message.NewPrinter(language.English),
}

return l, nil
}

func (l *Load) RefreshTheme() {
isDarkModeOn := l.WL.MultiWallet.ReadBoolConfigValueForKey("isDarkModeOn", false)
if isDarkModeOn != l.Theme.DarkMode {
l.Theme.SwitchDarkMode(isDarkModeOn)
}
}

func loadIcons() Icons {
decredIcons := assets.DecredIcons

ic := Icons{
ContentAdd: mustIcon(widget.NewIcon(icons.ContentAdd)),
NavigationCheck: mustIcon(widget.NewIcon(icons.NavigationCheck)),
Expand Down Expand Up @@ -167,42 +216,5 @@ func NewLoad(th *decredmaterial.Theme, decredIcons map[string]image.Image) *Load
DecredSymbolIcon: &widget.Image{Src: paint.NewImageOp(decredIcons["decred_symbol"])},
DecredSymbol2: &widget.Image{Src: paint.NewImageOp(decredIcons["ic_decred02"])},
}

wl := &WalletLoad{
Wallet: new(wallet.Wallet),
Account: new(wallet.Account),
Info: new(wallet.MultiWalletInfo),
SyncStatus: new(wallet.SyncStatus),
Transactions: new(wallet.Transactions),
UnspentOutputs: new(wallet.UnspentOutputs),
Tickets: new(*wallet.Tickets),
VspInfo: new(wallet.VSP),
Proposals: new(wallet.Proposals),

SelectedProposal: new(dcrlibwallet.Proposal),
}

r := &Receiver{
AcctMixerStatus: make(chan *wallet.AccountMixer),
SyncedProposal: make(chan *wallet.Proposal),
}

l := &Load{
Theme: th,
Icons: ic,
WL: wl,
Receiver: r,
Toast: notification.NewToast(th),

Printer: message.NewPrinter(language.English),
}

return l
}

func (l *Load) RefreshTheme() {
isDarkModeOn := l.WL.Wallet.ReadBoolConfigValueForKey("isDarkModeOn")
if isDarkModeOn != l.Theme.DarkMode {
l.Theme.SwitchDarkMode(isDarkModeOn)
}
return ic
}
2 changes: 1 addition & 1 deletion ui/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// Loading lays out the loading widget with a faded background
func (win *Window) Loading(gtx layout.Context) {
layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
lbl := win.theme.H1("Loading")
lbl := win.load.Theme.H1("Loading")
lbl.Alignment = text.Middle
return lbl.Layout(gtx)
})
Expand Down
9 changes: 2 additions & 7 deletions ui/page/receive_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"image"
"image/color"
"path/filepath"
"time"

"github.com/planetdecred/godcr/ui/assets"
"github.com/planetdecred/godcr/ui/load"
"github.com/planetdecred/godcr/ui/modal"
"github.com/planetdecred/godcr/ui/page/components"
Expand Down Expand Up @@ -125,12 +125,7 @@ func (pg *ReceivePage) OnResume() {
}

func (pg *ReceivePage) generateQRForAddress() {
absoluteWdPath, err := GetAbsolutePath()
if err != nil {
log.Error(err.Error())
}

opt := qrcode.WithLogoImageFilePNG(filepath.Join(absoluteWdPath, "ui/assets/decredicons/qrcodeSymbol.png"))
opt := qrcode.WithLogoImage(assets.DecredIcons["qrcodeSymbol"])
qrCode, err := qrcode.New(pg.currentAddress, opt)
if err != nil {
log.Error("Error generating address qrCode: " + err.Error())
Expand Down
17 changes: 0 additions & 17 deletions ui/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ package ui
import (
"fmt"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -155,17 +152,3 @@ func handleSubmitEvent(editors ...*widget.Editor) bool {
}
return submit
}

func GetAbsolutePath() (string, error) {
ex, err := os.Executable()
if err != nil {
return "", fmt.Errorf("error getting executable path: %s", err.Error())
}

exSym, err := filepath.EvalSymlinks(ex)
if err != nil {
return "", fmt.Errorf("error getting filepath after evaluating sym links")
}

return path.Dir(exSym), nil
}
Loading

0 comments on commit 42865b5

Please sign in to comment.