refactor(tracker): change so that all icon initialization is a lazy & cache in iconcache

This commit is contained in:
vsariola 2021-02-01 17:57:13 +02:00
parent c667ffb4e1
commit 3cf2fc70a8
4 changed files with 30 additions and 48 deletions

22
tracker/iconcache.go Normal file
View File

@ -0,0 +1,22 @@
package tracker
import (
"log"
"gioui.org/widget"
)
var iconCache = map[*byte]*widget.Icon{}
// widgetForIcon returns a widget for IconVG data, but caching the results
func widgetForIcon(icon []byte) *widget.Icon {
if widget, ok := iconCache[&icon[0]]; ok {
return widget
}
widget, err := widget.NewIcon(icon)
if err != nil {
log.Fatal(err)
}
iconCache[&icon[0]] = widget
return widget
}

View File

@ -11,6 +11,7 @@ import (
"gioui.org/unit" "gioui.org/unit"
"gioui.org/widget" "gioui.org/widget"
"gioui.org/widget/material" "gioui.org/widget/material"
"golang.org/x/exp/shiny/materialdesign/icons"
) )
type C = layout.Context type C = layout.Context
@ -25,7 +26,7 @@ func (t *Tracker) updateInstrumentScroll() {
} }
func (t *Tracker) layoutInstruments() layout.Widget { func (t *Tracker) layoutInstruments() layout.Widget {
btnStyle := material.IconButton(t.Theme, t.NewInstrumentBtn, addIcon) btnStyle := material.IconButton(t.Theme, t.NewInstrumentBtn, widgetForIcon(icons.ContentAdd))
btnStyle.Background = transparent btnStyle.Background = transparent
btnStyle.Inset = layout.UniformInset(unit.Dp(6)) btnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.song.Patch.TotalVoices() < 32 { if t.song.Patch.TotalVoices() < 32 {
@ -57,7 +58,7 @@ func (t *Tracker) layoutInstrumentHeader() layout.Widget {
return layout.Dimensions{Size: gtx.Constraints.Min} return layout.Dimensions{Size: gtx.Constraints.Min}
} }
header := func(gtx C) D { header := func(gtx C) D {
deleteInstrumentBtnStyle := material.IconButton(t.Theme, t.DeleteInstrumentBtn, deleteIcon) deleteInstrumentBtnStyle := material.IconButton(t.Theme, t.DeleteInstrumentBtn, widgetForIcon(icons.ActionDelete))
deleteInstrumentBtnStyle.Background = transparent deleteInstrumentBtnStyle.Background = transparent
deleteInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) deleteInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if len(t.song.Patch.Instruments) > 1 { if len(t.song.Patch.Instruments) > 1 {

View File

@ -3,7 +3,6 @@ package tracker
import ( import (
"image" "image"
"image/color" "image/color"
"log"
"gioui.org/layout" "gioui.org/layout"
"gioui.org/op" "gioui.org/op"
@ -12,50 +11,9 @@ import (
"gioui.org/unit" "gioui.org/unit"
"gioui.org/widget" "gioui.org/widget"
"gioui.org/widget/material" "gioui.org/widget/material"
"golang.org/x/exp/shiny/materialdesign/icons" "golang.org/x/exp/shiny/materialdesign/icons"
) )
var upIcon *widget.Icon
var downIcon *widget.Icon
var addIcon *widget.Icon
var loadIcon *widget.Icon
var saveIcon *widget.Icon
var clearIcon *widget.Icon
var deleteIcon *widget.Icon
func init() {
var err error
upIcon, err = widget.NewIcon(icons.NavigationArrowUpward)
if err != nil {
log.Fatal(err)
}
downIcon, err = widget.NewIcon(icons.NavigationArrowDownward)
if err != nil {
log.Fatal(err)
}
addIcon, err = widget.NewIcon(icons.ContentAdd)
if err != nil {
log.Fatal(err)
}
loadIcon, err = widget.NewIcon(icons.FileFolder)
if err != nil {
log.Fatal(err)
}
saveIcon, err = widget.NewIcon(icons.ContentSave)
if err != nil {
log.Fatal(err)
}
clearIcon, err = widget.NewIcon(icons.ContentClear)
if err != nil {
log.Fatal(err)
}
deleteIcon, err = widget.NewIcon(icons.ActionDelete)
if err != nil {
log.Fatal(err)
}
}
func smallButton(icStyle material.IconButtonStyle) material.IconButtonStyle { func smallButton(icStyle material.IconButtonStyle) material.IconButtonStyle {
icStyle.Size = unit.Dp(14) icStyle.Size = unit.Dp(14)
icStyle.Inset = layout.UniformInset(unit.Dp(1)) icStyle.Inset = layout.UniformInset(unit.Dp(1))
@ -178,7 +136,7 @@ func (t *Tracker) layoutTracks(gtx layout.Context) layout.Dimensions {
subtractOctaveBtnStyle.Color = primaryColor subtractOctaveBtnStyle.Color = primaryColor
subtractOctaveBtnStyle.Background = transparent subtractOctaveBtnStyle.Background = transparent
subtractOctaveBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) subtractOctaveBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
newTrackBtnStyle := material.IconButton(t.Theme, t.NewTrackBtn, addIcon) newTrackBtnStyle := material.IconButton(t.Theme, t.NewTrackBtn, widgetForIcon(icons.ContentAdd))
newTrackBtnStyle.Background = transparent newTrackBtnStyle.Background = transparent
newTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) newTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.song.TotalTrackVoices() < t.song.Patch.TotalVoices() { if t.song.TotalTrackVoices() < t.song.Patch.TotalVoices() {

View File

@ -9,6 +9,7 @@ import (
"gioui.org/op/paint" "gioui.org/op/paint"
"gioui.org/unit" "gioui.org/unit"
"gioui.org/widget/material" "gioui.org/widget/material"
"golang.org/x/exp/shiny/materialdesign/icons"
) )
func (t *Tracker) layoutSongPanel(gtx C) D { func (t *Tracker) layoutSongPanel(gtx C) D {
@ -36,17 +37,17 @@ func (t *Tracker) layoutSongButtons(gtx C) D {
t.SaveSongFile() t.SaveSongFile()
} }
newBtnStyle := material.IconButton(t.Theme, t.NewSongFileBtn, clearIcon) newBtnStyle := material.IconButton(t.Theme, t.NewSongFileBtn, widgetForIcon(icons.ContentClear))
newBtnStyle.Background = transparent newBtnStyle.Background = transparent
newBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) newBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
newBtnStyle.Color = primaryColor newBtnStyle.Color = primaryColor
loadBtnStyle := material.IconButton(t.Theme, t.LoadSongFileBtn, loadIcon) loadBtnStyle := material.IconButton(t.Theme, t.LoadSongFileBtn, widgetForIcon(icons.FileFolder))
loadBtnStyle.Background = transparent loadBtnStyle.Background = transparent
loadBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) loadBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
loadBtnStyle.Color = primaryColor loadBtnStyle.Color = primaryColor
saveBtnStyle := material.IconButton(t.Theme, t.SaveSongFileBtn, saveIcon) saveBtnStyle := material.IconButton(t.Theme, t.SaveSongFileBtn, widgetForIcon(icons.ContentSave))
saveBtnStyle.Background = transparent saveBtnStyle.Background = transparent
saveBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) saveBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
saveBtnStyle.Color = primaryColor saveBtnStyle.Color = primaryColor