Merge pull request #557 from dweymouth/feature/album-shuffle
Add ability to shuffle by albums (to Albums page and Genre page)
This commit is contained in:
@@ -27,9 +27,10 @@ type IconButton struct {
|
||||
IconSize IconButtonSize
|
||||
OnTapped func()
|
||||
|
||||
icon fyne.Resource
|
||||
focused bool
|
||||
hovered bool
|
||||
icon fyne.Resource
|
||||
focused bool
|
||||
hovered bool
|
||||
disabled bool
|
||||
|
||||
themed *theme.ThemedResource
|
||||
img *canvas.Image
|
||||
@@ -38,6 +39,7 @@ type IconButton struct {
|
||||
var (
|
||||
_ fyne.Tappable = (*IconButton)(nil)
|
||||
_ fyne.Focusable = (*IconButton)(nil)
|
||||
_ fyne.Disableable = (*IconButton)(nil)
|
||||
_ desktop.Hoverable = (*IconButton)(nil)
|
||||
)
|
||||
|
||||
@@ -56,8 +58,26 @@ func (i *IconButton) SetIcon(icon fyne.Resource) {
|
||||
}
|
||||
}
|
||||
|
||||
func (i *IconButton) Disable() {
|
||||
if !i.disabled {
|
||||
i.disabled = true
|
||||
i.Refresh()
|
||||
}
|
||||
}
|
||||
|
||||
func (i *IconButton) Enable() {
|
||||
if i.disabled {
|
||||
i.disabled = false
|
||||
i.Refresh()
|
||||
}
|
||||
}
|
||||
|
||||
func (i *IconButton) Disabled() bool {
|
||||
return i.disabled
|
||||
}
|
||||
|
||||
func (i *IconButton) Tapped(*fyne.PointEvent) {
|
||||
if i.OnTapped != nil {
|
||||
if !i.disabled && i.OnTapped != nil {
|
||||
i.OnTapped()
|
||||
}
|
||||
}
|
||||
@@ -77,7 +97,7 @@ func (i *IconButton) FocusLost() {
|
||||
}
|
||||
|
||||
func (i *IconButton) TypedKey(e *fyne.KeyEvent) {
|
||||
if e.Name == fyne.KeySpace {
|
||||
if !i.disabled && e.Name == fyne.KeySpace {
|
||||
i.Tapped(nil)
|
||||
}
|
||||
}
|
||||
@@ -87,6 +107,10 @@ func (i *IconButton) TypedRune(r rune) {
|
||||
|
||||
func (i *IconButton) MouseIn(e *desktop.MouseEvent) {
|
||||
i.ToolTipWidget.MouseIn(e)
|
||||
if i.disabled {
|
||||
return
|
||||
}
|
||||
|
||||
if !i.hovered {
|
||||
defer i.Refresh()
|
||||
}
|
||||
@@ -95,6 +119,10 @@ func (i *IconButton) MouseIn(e *desktop.MouseEvent) {
|
||||
|
||||
func (i *IconButton) MouseOut() {
|
||||
i.ToolTipWidget.MouseOut()
|
||||
if i.disabled {
|
||||
return
|
||||
}
|
||||
|
||||
if i.hovered {
|
||||
defer i.Refresh()
|
||||
}
|
||||
@@ -121,7 +149,9 @@ func (i *IconButton) iconSize() fyne.Size {
|
||||
}
|
||||
|
||||
func (i *IconButton) updateColor() {
|
||||
if i.Highlighted || i.focused {
|
||||
if i.disabled {
|
||||
i.themed.ColorName = theme.ColorNameDisabled
|
||||
} else if i.Highlighted || i.focused {
|
||||
i.themed.ColorName = theme.ColorNamePrimary
|
||||
} else if i.hovered {
|
||||
i.themed.ColorName = myTheme.ColorNameHoveredIconButton
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/layout"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
type OptionButton struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Text string
|
||||
Icon fyne.Resource
|
||||
Menu *fyne.Menu
|
||||
OnTapped func()
|
||||
}
|
||||
|
||||
func NewOptionButton(text string, menu *fyne.Menu, onTapped func()) *OptionButton {
|
||||
return NewOptionButtonWithIcon(text, nil, menu, onTapped)
|
||||
}
|
||||
|
||||
func NewOptionButtonWithIcon(text string, icon fyne.Resource, menu *fyne.Menu, onTapped func()) *OptionButton {
|
||||
o := &OptionButton{
|
||||
Menu: menu,
|
||||
Text: text,
|
||||
Icon: icon,
|
||||
OnTapped: onTapped,
|
||||
}
|
||||
o.ExtendBaseWidget(o)
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *OptionButton) CreateRenderer() fyne.WidgetRenderer {
|
||||
return newOptionButtonRenderer(o)
|
||||
}
|
||||
|
||||
var _ fyne.WidgetRenderer = (*optionButtonRenderer)(nil)
|
||||
|
||||
type optionButtonRenderer struct {
|
||||
wid *OptionButton
|
||||
mainBtn *widget.Button
|
||||
auxBtn *widget.Button
|
||||
dividerLine *canvas.Rectangle
|
||||
objects []fyne.CanvasObject
|
||||
}
|
||||
|
||||
func newOptionButtonRenderer(o *OptionButton) *optionButtonRenderer {
|
||||
render := &optionButtonRenderer{wid: o}
|
||||
render.mainBtn = widget.NewButtonWithIcon(o.Text, o.Icon, render.onMainBtnTapped)
|
||||
render.mainBtn.Alignment = widget.ButtonAlignLeading
|
||||
render.auxBtn = widget.NewButtonWithIcon("", theme.MenuDropDownIcon(), render.showMenu)
|
||||
render.auxBtn.Importance = widget.LowImportance
|
||||
|
||||
render.dividerLine = canvas.NewRectangle(o.Theme().Color(theme.ColorNameSeparator,
|
||||
fyne.CurrentApp().Settings().ThemeVariant()))
|
||||
render.dividerLine.SetMinSize(fyne.NewSquareSize(1))
|
||||
divider := container.NewBorder(layout.NewSpacer(), layout.NewSpacer(), nil, nil, render.dividerLine)
|
||||
|
||||
render.objects = []fyne.CanvasObject{
|
||||
container.NewStack(
|
||||
render.mainBtn,
|
||||
container.New(layout.NewCustomPaddedHBoxLayout(0),
|
||||
layout.NewSpacer(), divider, render.auxBtn),
|
||||
),
|
||||
}
|
||||
|
||||
return render
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) showMenu() {
|
||||
if o.wid.Menu == nil {
|
||||
return
|
||||
}
|
||||
|
||||
canv := fyne.CurrentApp().Driver().CanvasForObject(o.wid)
|
||||
pop := widget.NewPopUpMenu(o.wid.Menu, canv)
|
||||
pop.ShowAtRelativePosition(o.auxBtn.Position().Add(
|
||||
fyne.NewPos(0, o.wid.Size().Height)),
|
||||
o.wid,
|
||||
)
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) MinSize() fyne.Size {
|
||||
return o.mainBtn.MinSize().Add(fyne.NewSize(o.auxBtn.MinSize().Width, 0))
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) Layout(s fyne.Size) {
|
||||
o.objects[0].(*fyne.Container).Resize(s)
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) Objects() []fyne.CanvasObject {
|
||||
return o.objects
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) Refresh() {
|
||||
o.mainBtn.Text = o.wid.Text
|
||||
o.mainBtn.Icon = o.wid.Icon
|
||||
o.dividerLine.FillColor = o.wid.Theme().Color(theme.ColorNameSeparator, fyne.CurrentApp().Settings().ThemeVariant())
|
||||
o.objects[0].Refresh()
|
||||
}
|
||||
|
||||
func (o *optionButtonRenderer) Destroy() {}
|
||||
|
||||
func (o *optionButtonRenderer) onMainBtnTapped() {
|
||||
if o.wid.OnTapped != nil {
|
||||
o.wid.OnTapped()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/lang"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/dweymouth/supersonic/ui/theme"
|
||||
)
|
||||
|
||||
type SortChooserButton struct {
|
||||
widget.BaseWidget
|
||||
|
||||
Sorts []string
|
||||
AlignLeft bool
|
||||
|
||||
OnChanged func(selIndex int)
|
||||
|
||||
disabled bool
|
||||
selectedIndex int
|
||||
btn *IconButton
|
||||
}
|
||||
|
||||
func NewSortChooserButton(sorts []string, onChanged func(selIdx int)) *SortChooserButton {
|
||||
s := &SortChooserButton{Sorts: sorts, OnChanged: onChanged}
|
||||
s.ExtendBaseWidget(s)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) SelectedIndex() int {
|
||||
return s.selectedIndex
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) SetSelectedIndex(idx int) {
|
||||
if idx < 0 || idx > len(s.Sorts)-1 {
|
||||
return
|
||||
}
|
||||
|
||||
s.selectedIndex = idx
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) Disable() {
|
||||
if !s.disabled {
|
||||
s.disabled = true
|
||||
s.Refresh()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) Enable() {
|
||||
if s.disabled {
|
||||
s.disabled = false
|
||||
s.Refresh()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) Disabled() bool {
|
||||
return s.disabled
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) Refresh() {
|
||||
if s.btn != nil {
|
||||
if s.disabled {
|
||||
s.btn.Disable()
|
||||
} else {
|
||||
s.btn.Enable()
|
||||
}
|
||||
}
|
||||
s.BaseWidget.Refresh()
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) CreateRenderer() fyne.WidgetRenderer {
|
||||
s.btn = NewIconButton(theme.SortIcon, s.showMenu)
|
||||
s.btn.SetToolTip(lang.L("Sort"))
|
||||
return widget.NewSimpleRenderer(s.btn)
|
||||
}
|
||||
|
||||
func (s *SortChooserButton) showMenu() {
|
||||
m := fyne.NewMenu("")
|
||||
for i, lbl := range s.Sorts {
|
||||
_i := i
|
||||
item := fyne.NewMenuItem(lbl, func() {
|
||||
s.selectedIndex = _i
|
||||
if s.OnChanged != nil {
|
||||
s.OnChanged(_i)
|
||||
}
|
||||
})
|
||||
if i == s.selectedIndex {
|
||||
item.Checked = true
|
||||
}
|
||||
m.Items = append(m.Items, item)
|
||||
}
|
||||
btnPos := fyne.CurrentApp().Driver().AbsolutePositionForObject(s)
|
||||
btnSize := s.Size()
|
||||
pop := widget.NewPopUpMenu(m, fyne.CurrentApp().Driver().CanvasForObject(s))
|
||||
menuW := pop.MinSize().Width
|
||||
if s.AlignLeft {
|
||||
pop.ShowAtPosition(fyne.NewPos(btnPos.X+btnSize.Width-menuW, btnPos.Y+btnSize.Height))
|
||||
} else {
|
||||
pop.ShowAtPosition(fyne.NewPos(btnPos.X, btnPos.Y+btnSize.Height))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user