diff --git a/tracker/gioui/buttons.go b/tracker/gioui/buttons.go index e9ea45d..63f4575 100644 --- a/tracker/gioui/buttons.go +++ b/tracker/gioui/buttons.go @@ -76,7 +76,7 @@ func ActionIcon(gtx C, th *Theme, w *ActionClickable, icon []byte, tip string) T } func TipIcon(th *Theme, w *TipClickable, icon []byte, tip string) TipIconButtonStyle { - iconButtonStyle := IconButton(th, &w.Clickable, widgetForIcon(icon), "") + iconButtonStyle := IconButton(th, &w.Clickable, th.Icon(icon), "") iconButtonStyle.Color = th.Material.Palette.ContrastBg iconButtonStyle.Background = color.NRGBA{} iconButtonStyle.Inset = layout.UniformInset(unit.Dp(6)) @@ -97,7 +97,7 @@ func ToggleIcon(gtx C, th *Theme, w *BoolClickable, offIcon, onIcon []byte, offT for w.Clickable.Clicked(gtx) { w.Bool.Toggle() } - ibStyle := IconButton(th, &w.Clickable, widgetForIcon(icon), "") + ibStyle := IconButton(th, &w.Clickable, th.Icon(icon), "") ibStyle.Background = color.NRGBA{} ibStyle.Inset = layout.UniformInset(unit.Dp(6)) ibStyle.Color = th.Material.Palette.ContrastBg diff --git a/tracker/gioui/iconcache.go b/tracker/gioui/iconcache.go deleted file mode 100644 index b79eadb..0000000 --- a/tracker/gioui/iconcache.go +++ /dev/null @@ -1,22 +0,0 @@ -package gioui - -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 -} diff --git a/tracker/gioui/menu.go b/tracker/gioui/menu.go index 1413a21..d5ccbe1 100644 --- a/tracker/gioui/menu.go +++ b/tracker/gioui/menu.go @@ -96,7 +96,7 @@ func (m *MenuStyle) Layout(gtx C, items ...MenuItem) D { if i == m.Menu.hover-1 && item.Doer.Enabled() { macro = op.Record(gtx.Ops) } - icon := widgetForIcon(item.IconBytes) + icon := m.Theme.Icon(item.IconBytes) iconColor := m.LabelStyle.Color iconInset := layout.Inset{Left: unit.Dp(12), Right: unit.Dp(6)} textLabel := Label(m.Theme, &m.Theme.Menu.Text, item.Text) diff --git a/tracker/gioui/numericupdown.go b/tracker/gioui/numericupdown.go index 0911f76..0dd7097 100644 --- a/tracker/gioui/numericupdown.go +++ b/tracker/gioui/numericupdown.go @@ -47,7 +47,7 @@ type NumericUpDownStyle struct { type NumericUpDown struct { NumberInput *NumberInput Tooltip component.Tooltip - Shaper *text.Shaper + Theme *Theme Font font.Font NumericUpDownStyle } @@ -59,7 +59,7 @@ func NewNumberInput(v tracker.Int) *NumberInput { func NumUpDown(th *Theme, number *NumberInput, tooltip string) NumericUpDown { return NumericUpDown{ NumberInput: number, - Shaper: th.Material.Shaper, + Theme: th, Tooltip: Tooltip(th, tooltip), NumericUpDownStyle: th.NumericUpDown, } @@ -131,12 +131,12 @@ func (s *NumericUpDown) actualLayout(gtx C) D { s.NumberInput.clickDecrease.Add(gtx.Ops) return D{Size: gtx.Constraints.Min} }, - func(gtx C) D { return widgetForIcon(icons.ContentRemove).Layout(gtx, s.IconColor) }, + func(gtx C) D { return s.Theme.Icon(icons.ContentRemove).Layout(gtx, s.IconColor) }, ) }), layout.Flexed(1, func(gtx C) D { paint.ColorOp{Color: s.TextColor}.Add(gtx.Ops) - return widget.Label{Alignment: text.Middle}.Layout(gtx, s.Shaper, s.Font, s.TextSize, strconv.Itoa(s.NumberInput.Int.Value()), op.CallOp{}) + return widget.Label{Alignment: text.Middle}.Layout(gtx, s.Theme.Material.Shaper, s.Font, s.TextSize, strconv.Itoa(s.NumberInput.Int.Value()), op.CallOp{}) }), layout.Rigid(func(gtx C) D { gtx.Constraints = layout.Exact(image.Pt(width, height)) @@ -146,7 +146,7 @@ func (s *NumericUpDown) actualLayout(gtx C) D { s.NumberInput.clickIncrease.Add(gtx.Ops) return D{Size: gtx.Constraints.Min} }, - func(gtx C) D { return widgetForIcon(icons.ContentAdd).Layout(gtx, s.IconColor) }, + func(gtx C) D { return s.Theme.Icon(icons.ContentAdd).Layout(gtx, s.IconColor) }, ) }), ) diff --git a/tracker/gioui/songpanel.go b/tracker/gioui/songpanel.go index 901a2fc..29dc76f 100644 --- a/tracker/gioui/songpanel.go +++ b/tracker/gioui/songpanel.go @@ -286,7 +286,7 @@ func (e *Expander) layoutHeader(gtx C, th *Theme, title string, smallWidget layo icon = icons.NavigationExpandLess } gtx.Constraints.Min = image.Pt(gtx.Dp(unit.Dp(24)), gtx.Dp(unit.Dp(24))) - return widgetForIcon(icon).Layout(gtx, th.SongPanel.Expander.Color) + return th.Icon(icon).Layout(gtx, th.SongPanel.Expander.Color) }), ) }, diff --git a/tracker/gioui/theme.go b/tracker/gioui/theme.go index f4ae234..1077fe3 100644 --- a/tracker/gioui/theme.go +++ b/tracker/gioui/theme.go @@ -107,6 +107,9 @@ type Theme struct { Shadow color.NRGBA } ScrollBar ScrollBarStyle + + // iconCache is used to cache the icons created from iconvg data + iconCache map[*byte]*widget.Icon } type CursorStyle struct { @@ -127,9 +130,19 @@ func NewTheme() (*Theme, error) { ret.Material.Icon.CheckBoxUnchecked = must(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank)) ret.Material.Icon.RadioChecked = must(widget.NewIcon(icons.ToggleRadioButtonChecked)) ret.Material.Icon.RadioUnchecked = must(widget.NewIcon(icons.ToggleRadioButtonUnchecked)) + ret.iconCache = make(map[*byte]*widget.Icon) return &ret, warn } +func (th *Theme) Icon(data []byte) *widget.Icon { + if icon, ok := th.iconCache[&data[0]]; ok { + return icon + } + icon := must(widget.NewIcon(data)) + th.iconCache[&data[0]] = icon + return icon +} + func must[T any](ic T, err error) T { if err != nil { panic(err)