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 {
ret := FileDialogStyle{
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"),
FileNameStyle: material.Editor(th, &f.FileName, "Filename"),
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.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.Disabled = white
ret.ExtMain = ".yml"

View File

@ -43,14 +43,7 @@ func (t *Tracker) layoutInstruments(gtx C) D {
for t.NewInstrumentBtn.Clicked() {
t.AddInstrument(true)
}
btnStyle := material.IconButton(t.Theme, t.NewInstrumentBtn, widgetForIcon(icons.ContentAdd))
btnStyle.Background = transparent
btnStyle.Inset = layout.UniformInset(unit.Dp(6))
if t.CanAddInstrument() {
btnStyle.Color = primaryColor
} else {
btnStyle.Color = disabledTextColor
}
btnStyle := IconButton(t.Theme, t.NewInstrumentBtn, icons.ContentAdd, t.CanAddInstrument())
spy, spiedGtx := eventx.Enspy(gtx)
ret := layout.Flex{Axis: layout.Vertical}.Layout(spiedGtx,
layout.Rigid(func(gtx C) D {
@ -91,34 +84,11 @@ func (t *Tracker) layoutInstrumentHeader(gtx C) D {
collapseIcon = icons.NavigationExpandMore
}
instrumentExpandBtnStyle := material.IconButton(t.Theme, t.InstrumentExpandBtn, widgetForIcon(collapseIcon))
instrumentExpandBtnStyle.Background = transparent
instrumentExpandBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
instrumentExpandBtnStyle.Color = primaryColor
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
}
instrumentExpandBtnStyle := IconButton(t.Theme, t.InstrumentExpandBtn, collapseIcon, true)
copyInstrumentBtnStyle := IconButton(t.Theme, t.CopyInstrumentBtn, icons.ContentContentCopy, true)
saveInstrumentBtnStyle := IconButton(t.Theme, t.SaveInstrumentBtn, icons.ContentSave, true)
loadInstrumentBtnStyle := IconButton(t.Theme, t.LoadInstrumentBtn, icons.FileFolderOpen, true)
deleteInstrumentBtnStyle := IconButton(t.Theme, t.DeleteInstrumentBtn, icons.ActionDelete, t.CanDeleteInstrument())
header := func(gtx C) D {
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.Background = transparent
noteOffBtnStyle.Inset = layout.UniformInset(unit.Dp(6))
deleteTrackBtnStyle := material.IconButton(t.Theme, t.DeleteTrackBtn, widgetForIcon(icons.ActionDelete))
deleteTrackBtnStyle.Background = transparent
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
}
deleteTrackBtnStyle := IconButton(t.Theme, t.DeleteTrackBtn, icons.ActionDelete, t.CanDeleteTrack())
newTrackBtnStyle := IconButton(t.Theme, t.NewTrackBtn, icons.ContentAdd, t.CanAddTrack())
in := layout.UniformInset(unit.Dp(1))
octave := func(gtx C) D {
t.OctaveNumberInput.Value = t.Octave()

View File

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