refactor(gioui): move common iconbutton code to a function

This commit is contained in:
vsariola 2021-04-20 17:57:36 +03:00
parent 8ba9fb1f00
commit c0a0a5d501
5 changed files with 31 additions and 69 deletions

20
tracker/gioui/buttons.go Normal file
View File

@ -0,0 +1,20 @@
package gioui
import (
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
)
func IconButton(th *material.Theme, w *widget.Clickable, icon []byte, enabled bool) material.IconButtonStyle {
ret := material.IconButton(th, w, widgetForIcon(icon))
ret.Background = transparent
ret.Inset = layout.UniformInset(unit.Dp(6))
if enabled {
ret.Color = primaryColor
} else {
ret.Color = disabledTextColor
}
return ret
}

View File

@ -73,7 +73,7 @@ func OpenFileDialog(th *material.Theme, f *FileDialog) FileDialogStyle {
func commonFileDialog(th *material.Theme, f *FileDialog) FileDialogStyle { func commonFileDialog(th *material.Theme, f *FileDialog) FileDialogStyle {
ret := FileDialogStyle{ ret := FileDialogStyle{
dialog: f, dialog: f,
FolderUpStyle: material.IconButton(th, &f.BtnFolderUp, widgetForIcon(icons.NavigationArrowUpward)), FolderUpStyle: IconButton(th, &f.BtnFolderUp, icons.NavigationArrowUpward, true),
DirEditorStyle: material.Editor(th, &f.Directory, "Directory"), DirEditorStyle: material.Editor(th, &f.Directory, "Directory"),
FileNameStyle: material.Editor(th, &f.FileName, "Filename"), FileNameStyle: material.Editor(th, &f.FileName, "Filename"),
CancelStyle: material.Button(th, &f.BtnCancel, "Cancel"), CancelStyle: material.Button(th, &f.BtnCancel, "Cancel"),
@ -81,9 +81,6 @@ func commonFileDialog(th *material.Theme, f *FileDialog) FileDialogStyle {
} }
ret.CancelStyle.Background = transparent ret.CancelStyle.Background = transparent
ret.CancelStyle.Color = primaryColor ret.CancelStyle.Color = primaryColor
ret.FolderUpStyle.Inset = layout.UniformInset(unit.Dp(1))
ret.FolderUpStyle.Color = primaryColor
ret.FolderUpStyle.Background = transparent
ret.UseAltExtStyle.Color.Enabled = white ret.UseAltExtStyle.Color.Enabled = white
ret.UseAltExtStyle.Color.Disabled = white ret.UseAltExtStyle.Color.Disabled = white
ret.ExtMain = ".yml" ret.ExtMain = ".yml"

View File

@ -43,14 +43,7 @@ func (t *Tracker) layoutInstruments(gtx C) D {
for t.NewInstrumentBtn.Clicked() { for t.NewInstrumentBtn.Clicked() {
t.AddInstrument(true) t.AddInstrument(true)
} }
btnStyle := material.IconButton(t.Theme, t.NewInstrumentBtn, widgetForIcon(icons.ContentAdd)) btnStyle := IconButton(t.Theme, t.NewInstrumentBtn, icons.ContentAdd, t.CanAddInstrument())
btnStyle.Background = transparent
btnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanAddInstrument() {
btnStyle.Color = primaryColor
} else {
btnStyle.Color = disabledTextColor
}
spy, spiedGtx := eventx.Enspy(gtx) spy, spiedGtx := eventx.Enspy(gtx)
ret := layout.Flex{Axis: layout.Vertical}.Layout(spiedGtx, ret := layout.Flex{Axis: layout.Vertical}.Layout(spiedGtx,
layout.Rigid(func(gtx C) D { layout.Rigid(func(gtx C) D {
@ -91,34 +84,11 @@ func (t *Tracker) layoutInstrumentHeader(gtx C) D {
collapseIcon = icons.NavigationExpandMore collapseIcon = icons.NavigationExpandMore
} }
instrumentExpandBtnStyle := material.IconButton(t.Theme, t.InstrumentExpandBtn, widgetForIcon(collapseIcon)) instrumentExpandBtnStyle := IconButton(t.Theme, t.InstrumentExpandBtn, collapseIcon, true)
instrumentExpandBtnStyle.Background = transparent copyInstrumentBtnStyle := IconButton(t.Theme, t.CopyInstrumentBtn, icons.ContentContentCopy, true)
instrumentExpandBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) saveInstrumentBtnStyle := IconButton(t.Theme, t.SaveInstrumentBtn, icons.ContentSave, true)
instrumentExpandBtnStyle.Color = primaryColor loadInstrumentBtnStyle := IconButton(t.Theme, t.LoadInstrumentBtn, icons.FileFolderOpen, true)
deleteInstrumentBtnStyle := IconButton(t.Theme, t.DeleteInstrumentBtn, icons.ActionDelete, t.CanDeleteInstrument())
copyInstrumentBtnStyle := material.IconButton(t.Theme, t.CopyInstrumentBtn, widgetForIcon(icons.ContentContentCopy))
copyInstrumentBtnStyle.Background = transparent
copyInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
copyInstrumentBtnStyle.Color = primaryColor
saveInstrumentBtnStyle := material.IconButton(t.Theme, t.SaveInstrumentBtn, widgetForIcon(icons.ContentSave))
saveInstrumentBtnStyle.Background = transparent
saveInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
saveInstrumentBtnStyle.Color = primaryColor
loadInstrumentBtnStyle := material.IconButton(t.Theme, t.LoadInstrumentBtn, widgetForIcon(icons.FileFolderOpen))
loadInstrumentBtnStyle.Background = transparent
loadInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
loadInstrumentBtnStyle.Color = primaryColor
deleteInstrumentBtnStyle := material.IconButton(t.Theme, t.DeleteInstrumentBtn, widgetForIcon(icons.ActionDelete))
deleteInstrumentBtnStyle.Background = transparent
deleteInstrumentBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanDeleteInstrument() {
deleteInstrumentBtnStyle.Color = primaryColor
} else {
deleteInstrumentBtnStyle.Color = disabledTextColor
}
header := func(gtx C) D { header := func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx, return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,

View File

@ -86,22 +86,8 @@ func (t *Tracker) layoutTracker(gtx layout.Context) layout.Dimensions {
noteOffBtnStyle.Color = primaryColor noteOffBtnStyle.Color = primaryColor
noteOffBtnStyle.Background = transparent noteOffBtnStyle.Background = transparent
noteOffBtnStyle.Inset = layout.UniformInset(unit.Dp(6)) noteOffBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
deleteTrackBtnStyle := material.IconButton(t.Theme, t.DeleteTrackBtn, widgetForIcon(icons.ActionDelete)) deleteTrackBtnStyle := IconButton(t.Theme, t.DeleteTrackBtn, icons.ActionDelete, t.CanDeleteTrack())
deleteTrackBtnStyle.Background = transparent newTrackBtnStyle := IconButton(t.Theme, t.NewTrackBtn, icons.ContentAdd, t.CanAddTrack())
deleteTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanDeleteTrack() {
deleteTrackBtnStyle.Color = primaryColor
} else {
deleteTrackBtnStyle.Color = disabledTextColor
}
newTrackBtnStyle := material.IconButton(t.Theme, t.NewTrackBtn, widgetForIcon(icons.ContentAdd))
newTrackBtnStyle.Background = transparent
newTrackBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanAddTrack() {
newTrackBtnStyle.Color = primaryColor
} else {
newTrackBtnStyle.Color = disabledTextColor
}
in := layout.UniformInset(unit.Dp(1)) in := layout.UniformInset(unit.Dp(1))
octave := func(gtx C) D { octave := func(gtx C) D {
t.OctaveNumberInput.Value = t.Octave() t.OctaveNumberInput.Value = t.Octave()

View File

@ -10,7 +10,6 @@ import (
"gioui.org/op/clip" "gioui.org/op/clip"
"gioui.org/op/paint" "gioui.org/op/paint"
"gioui.org/unit" "gioui.org/unit"
"gioui.org/widget/material"
"github.com/vsariola/sointu/tracker" "github.com/vsariola/sointu/tracker"
"golang.org/x/exp/shiny/materialdesign/icons" "golang.org/x/exp/shiny/materialdesign/icons"
) )
@ -79,14 +78,7 @@ func (t *Tracker) layoutUnitFooter() layout.Widget {
t.DeleteUnit(false) t.DeleteUnit(false)
op.InvalidateOp{}.Add(gtx.Ops) op.InvalidateOp{}.Add(gtx.Ops)
} }
deleteUnitBtnStyle := material.IconButton(t.Theme, t.DeleteUnitBtn, widgetForIcon(icons.ActionDelete)) deleteUnitBtnStyle := IconButton(t.Theme, t.DeleteUnitBtn, icons.ActionDelete, t.CanDeleteUnit())
deleteUnitBtnStyle.Background = transparent
deleteUnitBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanDeleteUnit() {
deleteUnitBtnStyle.Color = primaryColor
} else {
deleteUnitBtnStyle.Color = disabledTextColor
}
text := t.Unit().Type text := t.Unit().Type
if text == "" { if text == "" {
text = "Choose unit type" text = "Choose unit type"
@ -99,10 +91,7 @@ func (t *Tracker) layoutUnitFooter() layout.Widget {
layout.Rigid(func(gtx C) D { layout.Rigid(func(gtx C) D {
var dims D var dims D
if t.Unit().Type != "" { if t.Unit().Type != "" {
clearUnitBtnStyle := material.IconButton(t.Theme, t.ClearUnitBtn, widgetForIcon(icons.ContentClear)) clearUnitBtnStyle := IconButton(t.Theme, t.ClearUnitBtn, icons.ContentClear, true)
clearUnitBtnStyle.Color = primaryColor
clearUnitBtnStyle.Background = transparent
clearUnitBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
dims = clearUnitBtnStyle.Layout(gtx) dims = clearUnitBtnStyle.Layout(gtx)
} }
return D{Size: image.Pt(gtx.Px(unit.Dp(48)), dims.Size.Y)} return D{Size: image.Pt(gtx.Px(unit.Dp(48)), dims.Size.Y)}