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:
+7
-4
@@ -64,9 +64,11 @@ type AlbumPageConfig struct {
|
|||||||
TracklistColumns []string
|
TracklistColumns []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shared between Albums and Genre pages
|
||||||
type AlbumsPageConfig struct {
|
type AlbumsPageConfig struct {
|
||||||
SortOrder string
|
SortOrder string // only relevant for Albums page
|
||||||
ShowYears bool
|
ShowYears bool
|
||||||
|
ShuffleMode string // only relevant for genre page
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArtistPageConfig struct {
|
type ArtistPageConfig struct {
|
||||||
@@ -192,8 +194,9 @@ func DefaultConfig(appVersionTag string) *Config {
|
|||||||
TracklistColumns: []string{"Artist", "Time", "Plays", "Favorite", "Rating"},
|
TracklistColumns: []string{"Artist", "Time", "Plays", "Favorite", "Rating"},
|
||||||
},
|
},
|
||||||
AlbumsPage: AlbumsPageConfig{
|
AlbumsPage: AlbumsPageConfig{
|
||||||
SortOrder: string("Recently Added"),
|
SortOrder: string("Recently Added"),
|
||||||
ShowYears: false,
|
ShowYears: false,
|
||||||
|
ShuffleMode: "Tracks",
|
||||||
},
|
},
|
||||||
ArtistPage: ArtistPageConfig{
|
ArtistPage: ArtistPageConfig{
|
||||||
InitialView: "Discography",
|
InitialView: "Discography",
|
||||||
|
|||||||
@@ -289,6 +289,34 @@ func (p *PlaybackManager) PlaySimilarSongs(id string) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PlaybackManager) PlayRandomAlbums(genreName string) error {
|
||||||
|
if p.engine.replayGainCfg.Mode == ReplayGainAuto {
|
||||||
|
p.SetReplayGainMode(player.ReplayGainAlbum)
|
||||||
|
}
|
||||||
|
|
||||||
|
mp := p.engine.sm.Server
|
||||||
|
var options mediaprovider.AlbumFilterOptions
|
||||||
|
if genreName != "" {
|
||||||
|
options = mediaprovider.AlbumFilterOptions{
|
||||||
|
Genres: []string{genreName},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iter := mp.IterateAlbums(mediaprovider.AlbumSortRandom, mediaprovider.NewAlbumFilter(options))
|
||||||
|
insertMode := Replace
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
al := iter.Next()
|
||||||
|
if al, err := mp.GetAlbum(al.ID); err == nil {
|
||||||
|
p.LoadTracks(al.Tracks, insertMode, false)
|
||||||
|
if i == 0 {
|
||||||
|
p.PlayFromBeginning()
|
||||||
|
insertMode = Append
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *PlaybackManager) LoadRadioStation(station *mediaprovider.RadioStation, queueMode InsertQueueMode) {
|
func (p *PlaybackManager) LoadRadioStation(station *mediaprovider.RadioStation, queueMode InsertQueueMode) {
|
||||||
p.cmdQueue.LoadRadioStation(station, queueMode)
|
p.cmdQueue.LoadRadioStation(station, queueMode)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package browsing
|
package browsing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
@@ -64,7 +65,20 @@ func (a *albumsPageAdapter) SaveSortOrder(orderIdx int) {
|
|||||||
a.cfg.SortOrder = a.mp.AlbumSortOrders()[orderIdx]
|
a.cfg.SortOrder = a.mp.AlbumSortOrders()[orderIdx]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *albumsPageAdapter) ActionButton() *widget.Button { return nil }
|
func (a *albumsPageAdapter) ActionButton() fyne.CanvasObject {
|
||||||
|
fn := func() {
|
||||||
|
go func() {
|
||||||
|
if err := a.pm.PlayRandomAlbums(""); err != nil {
|
||||||
|
log.Printf("error playing random albums: %v", err)
|
||||||
|
fyne.Do(func() {
|
||||||
|
a.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play random albums"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
return widget.NewButtonWithIcon(lang.L("Play random"), myTheme.ShuffleIcon, fn)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *albumsPageAdapter) Iter(sortOrderIdx int, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
|
func (a *albumsPageAdapter) Iter(sortOrderIdx int, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
|
||||||
sortOrder := a.mp.AlbumSortOrders()[sortOrderIdx]
|
sortOrder := a.mp.AlbumSortOrders()[sortOrderIdx]
|
||||||
|
|||||||
+11
-28
@@ -51,7 +51,7 @@ type ArtistPage struct {
|
|||||||
|
|
||||||
albumGrid *widgets.GridView
|
albumGrid *widgets.GridView
|
||||||
tracklistCtr *fyne.Container
|
tracklistCtr *fyne.Container
|
||||||
sortButton *widgets.IconButton
|
sortButton *widgets.SortChooserButton
|
||||||
nowPlayingID string
|
nowPlayingID string
|
||||||
header *ArtistPageHeader
|
header *ArtistPageHeader
|
||||||
container *fyne.Container
|
container *fyne.Container
|
||||||
@@ -96,8 +96,16 @@ func newArtistPage(artistID string, cfg *backend.ArtistPageConfig, pool *util.Wi
|
|||||||
viewToggle := widgets.NewToggleText(0, []string{lang.L("Discography"), lang.L("Top Tracks")})
|
viewToggle := widgets.NewToggleText(0, []string{lang.L("Discography"), lang.L("Top Tracks")})
|
||||||
viewToggle.SetActivatedLabel(a.activeView)
|
viewToggle.SetActivatedLabel(a.activeView)
|
||||||
viewToggle.OnChanged = a.onViewChange
|
viewToggle.OnChanged = a.onViewChange
|
||||||
a.sortButton = widgets.NewIconButton(myTheme.SortIcon, a.showAlbumSortMenu)
|
a.sortButton = widgets.NewSortChooserButton(util.LocalizeSlice(discographySorts), func(selIdx int) {
|
||||||
a.sortButton.SetToolTip(lang.L("Sort"))
|
a.cfg.DiscographySort = discographySorts[selIdx]
|
||||||
|
a.showAlbumGrid(true /*reSort*/)
|
||||||
|
})
|
||||||
|
a.sortButton.AlignLeft = true
|
||||||
|
for i, sort := range discographySorts {
|
||||||
|
if sort == a.cfg.DiscographySort {
|
||||||
|
a.sortButton.SetSelectedIndex(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
viewToggleRow := container.NewBorder(nil, nil,
|
viewToggleRow := container.NewBorder(nil, nil,
|
||||||
container.NewHBox(util.NewHSpace(5), viewToggle),
|
container.NewHBox(util.NewHSpace(5), viewToggle),
|
||||||
container.NewHBox(a.sortButton, util.NewHSpace(10)),
|
container.NewHBox(a.sortButton, util.NewHSpace(10)),
|
||||||
@@ -182,31 +190,6 @@ func (a *ArtistPage) playArtistRadio() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ArtistPage) showAlbumSortMenu() {
|
|
||||||
m := fyne.NewMenu("")
|
|
||||||
oneChecked := false
|
|
||||||
for i, s := range util.LocalizeSlice(discographySorts) {
|
|
||||||
_i := i
|
|
||||||
item := fyne.NewMenuItem(s, func() {
|
|
||||||
a.cfg.DiscographySort = discographySorts[_i]
|
|
||||||
a.showAlbumGrid(true /*reSort*/)
|
|
||||||
})
|
|
||||||
if discographySorts[i] == a.cfg.DiscographySort {
|
|
||||||
item.Checked = true
|
|
||||||
oneChecked = true
|
|
||||||
}
|
|
||||||
m.Items = append(m.Items, item)
|
|
||||||
}
|
|
||||||
if !oneChecked {
|
|
||||||
m.Items[0].Checked = true
|
|
||||||
}
|
|
||||||
btnPos := fyne.CurrentApp().Driver().AbsolutePositionForObject(a.sortButton)
|
|
||||||
btnSize := a.sortButton.Size()
|
|
||||||
pop := widget.NewPopUpMenu(m, fyne.CurrentApp().Driver().CanvasForObject(a))
|
|
||||||
menuW := pop.MinSize().Width
|
|
||||||
pop.ShowAtPosition(fyne.NewPos(btnPos.X+btnSize.Width-menuW, btnPos.Y+btnSize.Height))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ArtistPage) getGridViewAlbumsModel() []widgets.GridViewItemModel {
|
func (a *ArtistPage) getGridViewAlbumsModel() []widgets.GridViewItemModel {
|
||||||
if a.artistInfo == nil {
|
if a.artistInfo == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/lang"
|
"fyne.io/fyne/v2/lang"
|
||||||
"fyne.io/fyne/v2/widget"
|
|
||||||
"github.com/dweymouth/supersonic/backend"
|
"github.com/dweymouth/supersonic/backend"
|
||||||
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
||||||
"github.com/dweymouth/supersonic/ui/controller"
|
"github.com/dweymouth/supersonic/ui/controller"
|
||||||
@@ -60,7 +59,7 @@ func (a *artistsPageAdapter) SaveSortOrder(orderIdx int) {
|
|||||||
a.cfg.SortOrder = a.mp.ArtistSortOrders()[orderIdx]
|
a.cfg.SortOrder = a.mp.ArtistSortOrders()[orderIdx]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *artistsPageAdapter) ActionButton() *widget.Button { return nil }
|
func (a *artistsPageAdapter) ActionButton() fyne.CanvasObject { return nil }
|
||||||
|
|
||||||
func (a *artistsPageAdapter) Iter(sortOrderIdx int, filter mediaprovider.ArtistFilter) widgets.GridViewIterator {
|
func (a *artistsPageAdapter) Iter(sortOrderIdx int, filter mediaprovider.ArtistFilter) widgets.GridViewIterator {
|
||||||
sortOrder := a.mp.ArtistSortOrders()[sortOrderIdx]
|
sortOrder := a.mp.ArtistSortOrders()[sortOrderIdx]
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/lang"
|
"fyne.io/fyne/v2/lang"
|
||||||
"fyne.io/fyne/v2/widget"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type genrePageAdapter struct {
|
type genrePageAdapter struct {
|
||||||
@@ -59,19 +58,47 @@ func (g *genrePageAdapter) Route() controller.Route {
|
|||||||
return controller.GenreRoute(g.genre)
|
return controller.GenreRoute(g.genre)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *genrePageAdapter) ActionButton() *widget.Button {
|
func (g *genrePageAdapter) ActionButton() fyne.CanvasObject {
|
||||||
fn := func() {
|
fn := func() {
|
||||||
go func() {
|
go func() {
|
||||||
err := g.pm.PlayRandomSongs(g.genre)
|
var err error
|
||||||
|
if g.cfg.ShuffleMode == "Albums" {
|
||||||
|
err = g.pm.PlayRandomAlbums(g.genre)
|
||||||
|
} else {
|
||||||
|
err = g.pm.PlayRandomSongs(g.genre)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error playing random tracks: %v", err)
|
log.Printf("error playing random tracks: %v", err)
|
||||||
fyne.Do(func() {
|
fyne.Do(func() {
|
||||||
g.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play random tracks"))
|
g.contr.ToastProvider.ShowErrorToast(lang.L("Unable to play random tracks"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
return widget.NewButtonWithIcon(lang.L("Play random"), myTheme.ShuffleIcon, fn)
|
|
||||||
|
var tracks, albums *fyne.MenuItem
|
||||||
|
|
||||||
|
setShuffleMode := func(isAlbums bool) {
|
||||||
|
if isAlbums {
|
||||||
|
g.cfg.ShuffleMode = "Albums"
|
||||||
|
} else {
|
||||||
|
g.cfg.ShuffleMode = "Tracks"
|
||||||
|
}
|
||||||
|
albums.Checked = isAlbums
|
||||||
|
tracks.Checked = !isAlbums
|
||||||
|
}
|
||||||
|
|
||||||
|
tracks = fyne.NewMenuItem(lang.L("Tracks"), func() { setShuffleMode(false) })
|
||||||
|
tracks.Icon = myTheme.TracksIcon
|
||||||
|
albums = fyne.NewMenuItem(lang.L("Albums"), func() { setShuffleMode(true) })
|
||||||
|
albums.Icon = myTheme.AlbumIcon
|
||||||
|
|
||||||
|
isAlbums := g.cfg.ShuffleMode == "Albums"
|
||||||
|
albums.Checked = isAlbums
|
||||||
|
tracks.Checked = !isAlbums
|
||||||
|
|
||||||
|
menu := fyne.NewMenu("", tracks, albums)
|
||||||
|
return widgets.NewOptionButtonWithIcon(lang.L("Play random"), myTheme.ShuffleIcon, menu, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *genrePageAdapter) Iter(sortOrderIdx int, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
|
func (a *genrePageAdapter) Iter(sortOrderIdx int, filter mediaprovider.AlbumFilter) widgets.GridViewIterator {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ type GridViewPage[M, F any] struct {
|
|||||||
searchGridState *widgets.GridViewState
|
searchGridState *widgets.GridViewState
|
||||||
|
|
||||||
title *widget.RichText
|
title *widget.RichText
|
||||||
sortOrder *widget.Select
|
sortOrder *widgets.SortChooserButton
|
||||||
filterBtn widgets.FilterButton[M, F]
|
filterBtn widgets.FilterButton[M, F]
|
||||||
filter mediaprovider.MediaFilter[M, F]
|
filter mediaprovider.MediaFilter[M, F]
|
||||||
searcher *widgets.SearchEntry
|
searcher *widgets.SearchEntry
|
||||||
@@ -58,7 +58,7 @@ type GridViewPageAdapter[M, F any] interface {
|
|||||||
Route() controller.Route
|
Route() controller.Route
|
||||||
|
|
||||||
// Returns the ActionButton for this page, if any
|
// Returns the ActionButton for this page, if any
|
||||||
ActionButton() *widget.Button
|
ActionButton() fyne.CanvasObject
|
||||||
|
|
||||||
// Returns the iterator for the given sortOrder and filter.
|
// Returns the iterator for the given sortOrder and filter.
|
||||||
// (Non-media pages can ignore the filter argument)
|
// (Non-media pages can ignore the filter argument)
|
||||||
@@ -125,15 +125,7 @@ func (g *GridViewPage[M, F]) createTitleAndSort() {
|
|||||||
})
|
})
|
||||||
if s, ok := g.adapter.(SortableGridViewPageAdapter); ok {
|
if s, ok := g.adapter.(SortableGridViewPageAdapter); ok {
|
||||||
sorts, selected := s.SortOrders()
|
sorts, selected := s.SortOrders()
|
||||||
g.sortOrder = widget.NewSelect(sorts, g.onSortOrderChanged)
|
g.sortOrder = widgets.NewSortChooserButton(sorts, g.onSortOrderChanged)
|
||||||
// find longest string
|
|
||||||
l := ""
|
|
||||||
for _, s := range sorts {
|
|
||||||
if len(s) > len(l) {
|
|
||||||
l = s
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.sortOrder.PlaceHolder = l // props up MinSize.Width
|
|
||||||
g.sortOrder.SetSelectedIndex(selected)
|
g.sortOrder.SetSelectedIndex(selected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -213,13 +205,13 @@ func (g *GridViewPage[M, F]) doSearch(query string) {
|
|||||||
g.grid.Reset(g.adapter.SearchIter(query, g.getFilter()))
|
g.grid.Reset(g.adapter.SearchIter(query, g.getFilter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GridViewPage[M, F]) onSortOrderChanged(_ string) {
|
func (g *GridViewPage[M, F]) onSortOrderChanged(idx int) {
|
||||||
if g.grid == nil {
|
if g.grid == nil {
|
||||||
return // callback from initializing
|
return // callback from initializing
|
||||||
}
|
}
|
||||||
|
|
||||||
g.adapter.(SortableGridViewPageAdapter).SaveSortOrder(g.getSortOrderIdx())
|
g.adapter.(SortableGridViewPageAdapter).SaveSortOrder(g.getSortOrderIdx())
|
||||||
g.grid.Reset(g.adapter.Iter(g.getSortOrderIdx(), g.getFilter()))
|
g.grid.Reset(g.adapter.Iter(idx, g.getFilter()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GridViewPage[M, F]) getFilter() mediaprovider.MediaFilter[M, F] {
|
func (g *GridViewPage[M, F]) getFilter() mediaprovider.MediaFilter[M, F] {
|
||||||
|
|||||||
@@ -27,9 +27,10 @@ type IconButton struct {
|
|||||||
IconSize IconButtonSize
|
IconSize IconButtonSize
|
||||||
OnTapped func()
|
OnTapped func()
|
||||||
|
|
||||||
icon fyne.Resource
|
icon fyne.Resource
|
||||||
focused bool
|
focused bool
|
||||||
hovered bool
|
hovered bool
|
||||||
|
disabled bool
|
||||||
|
|
||||||
themed *theme.ThemedResource
|
themed *theme.ThemedResource
|
||||||
img *canvas.Image
|
img *canvas.Image
|
||||||
@@ -38,6 +39,7 @@ type IconButton struct {
|
|||||||
var (
|
var (
|
||||||
_ fyne.Tappable = (*IconButton)(nil)
|
_ fyne.Tappable = (*IconButton)(nil)
|
||||||
_ fyne.Focusable = (*IconButton)(nil)
|
_ fyne.Focusable = (*IconButton)(nil)
|
||||||
|
_ fyne.Disableable = (*IconButton)(nil)
|
||||||
_ desktop.Hoverable = (*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) {
|
func (i *IconButton) Tapped(*fyne.PointEvent) {
|
||||||
if i.OnTapped != nil {
|
if !i.disabled && i.OnTapped != nil {
|
||||||
i.OnTapped()
|
i.OnTapped()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +97,7 @@ func (i *IconButton) FocusLost() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *IconButton) TypedKey(e *fyne.KeyEvent) {
|
func (i *IconButton) TypedKey(e *fyne.KeyEvent) {
|
||||||
if e.Name == fyne.KeySpace {
|
if !i.disabled && e.Name == fyne.KeySpace {
|
||||||
i.Tapped(nil)
|
i.Tapped(nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -87,6 +107,10 @@ func (i *IconButton) TypedRune(r rune) {
|
|||||||
|
|
||||||
func (i *IconButton) MouseIn(e *desktop.MouseEvent) {
|
func (i *IconButton) MouseIn(e *desktop.MouseEvent) {
|
||||||
i.ToolTipWidget.MouseIn(e)
|
i.ToolTipWidget.MouseIn(e)
|
||||||
|
if i.disabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !i.hovered {
|
if !i.hovered {
|
||||||
defer i.Refresh()
|
defer i.Refresh()
|
||||||
}
|
}
|
||||||
@@ -95,6 +119,10 @@ func (i *IconButton) MouseIn(e *desktop.MouseEvent) {
|
|||||||
|
|
||||||
func (i *IconButton) MouseOut() {
|
func (i *IconButton) MouseOut() {
|
||||||
i.ToolTipWidget.MouseOut()
|
i.ToolTipWidget.MouseOut()
|
||||||
|
if i.disabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if i.hovered {
|
if i.hovered {
|
||||||
defer i.Refresh()
|
defer i.Refresh()
|
||||||
}
|
}
|
||||||
@@ -121,7 +149,9 @@ func (i *IconButton) iconSize() fyne.Size {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *IconButton) updateColor() {
|
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
|
i.themed.ColorName = theme.ColorNamePrimary
|
||||||
} else if i.hovered {
|
} else if i.hovered {
|
||||||
i.themed.ColorName = myTheme.ColorNameHoveredIconButton
|
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