change sort selector to button on Albums 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,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