add selection to tracklist
This commit is contained in:
+47
-7
@@ -3,10 +3,15 @@ package widgets
|
||||
import (
|
||||
"strconv"
|
||||
"supersonic/ui/layouts"
|
||||
"supersonic/ui/os"
|
||||
"supersonic/ui/util"
|
||||
"time"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/driver/desktop"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/dweymouth/go-subsonic"
|
||||
)
|
||||
@@ -18,18 +23,21 @@ type TrackRow struct {
|
||||
prevTrackID string
|
||||
prevIsPlaying bool
|
||||
|
||||
tappedAt int64 // unixMillis
|
||||
|
||||
num *widget.RichText
|
||||
name *widget.RichText
|
||||
artist *widget.RichText
|
||||
dur *widget.RichText
|
||||
|
||||
OnTapped func()
|
||||
OnDoubleTapped func()
|
||||
|
||||
selectionRect *canvas.Rectangle
|
||||
|
||||
container *fyne.Container
|
||||
}
|
||||
|
||||
var _ fyne.DoubleTappable = (*TrackRow)(nil)
|
||||
|
||||
func NewTrackRow(layout *layouts.ColumnsLayout) *TrackRow {
|
||||
t := &TrackRow{}
|
||||
t.ExtendBaseWidget(t)
|
||||
@@ -42,8 +50,11 @@ func NewTrackRow(layout *layouts.ColumnsLayout) *TrackRow {
|
||||
t.dur = widget.NewRichTextWithText("")
|
||||
t.dur.Segments[0].(*widget.TextSegment).Style.Alignment = fyne.TextAlignTrailing
|
||||
|
||||
t.container = container.New(layout,
|
||||
t.num, t.name, t.artist, t.dur)
|
||||
t.selectionRect = canvas.NewRectangle(theme.SelectionColor())
|
||||
t.selectionRect.Hidden = true
|
||||
t.container = container.NewMax(t.selectionRect,
|
||||
container.New(layout,
|
||||
t.num, t.name, t.artist, t.dur))
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -75,9 +86,19 @@ func (t *TrackRow) CreateRenderer() fyne.WidgetRenderer {
|
||||
return widget.NewSimpleRenderer(t.container)
|
||||
}
|
||||
|
||||
func (t *TrackRow) DoubleTapped(*fyne.PointEvent) {
|
||||
if t.OnDoubleTapped != nil {
|
||||
t.OnDoubleTapped()
|
||||
// We implement our own double tapping so that the Tapped behavior
|
||||
// can be triggered instantly.
|
||||
func (t *TrackRow) Tapped(*fyne.PointEvent) {
|
||||
prevTap := t.tappedAt
|
||||
t.tappedAt = time.Now().UnixMilli()
|
||||
if t.tappedAt-prevTap < 300 {
|
||||
if t.OnDoubleTapped != nil {
|
||||
t.OnDoubleTapped()
|
||||
}
|
||||
} else {
|
||||
if t.OnTapped != nil {
|
||||
t.OnTapped()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +108,7 @@ type Tracklist struct {
|
||||
Tracks []*subsonic.Child
|
||||
AutoNumber bool
|
||||
OnPlayTrackAt func(int)
|
||||
SelectionMgr util.ListSelectionManager
|
||||
|
||||
nowPlayingIdx int
|
||||
colLayout *layouts.ColumnsLayout
|
||||
@@ -98,6 +120,7 @@ type Tracklist struct {
|
||||
func NewTracklist(tracks []*subsonic.Child) *Tracklist {
|
||||
t := &Tracklist{Tracks: tracks, nowPlayingIdx: -1}
|
||||
t.ExtendBaseWidget(t)
|
||||
t.SelectionMgr = util.NewListSelectionManager(func() int { return len(t.Tracks) })
|
||||
t.colLayout = layouts.NewColumnsLayout([]float32{35, -1, -1, 60})
|
||||
t.hdr = NewListHeader([]ListColumn{{"#", true}, {"Title", false}, {"Artist", false}, {"Time", true}}, t.colLayout)
|
||||
t.list = widget.NewList(
|
||||
@@ -105,7 +128,9 @@ func NewTracklist(tracks []*subsonic.Child) *Tracklist {
|
||||
func() fyne.CanvasObject { return NewTrackRow(t.colLayout) },
|
||||
func(itemID widget.ListItemID, item fyne.CanvasObject) {
|
||||
tr := item.(*TrackRow)
|
||||
tr.OnTapped = func() { t.onSelectTrack(itemID) }
|
||||
tr.OnDoubleTapped = func() { t.onPlayTrackAt(itemID) }
|
||||
tr.selectionRect.Hidden = !t.SelectionMgr.IsSelected(itemID)
|
||||
i := itemID + 1
|
||||
if !t.AutoNumber {
|
||||
i = -1 // signal that we want to use the track num.
|
||||
@@ -136,3 +161,18 @@ func (t *Tracklist) onPlayTrackAt(idx int) {
|
||||
t.OnPlayTrackAt(idx)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tracklist) onSelectTrack(idx int) {
|
||||
if d, ok := fyne.CurrentApp().Driver().(desktop.Driver); ok {
|
||||
if d.ActiveKeyModifiers()&os.ControlModifier != 0 {
|
||||
t.SelectionMgr.SelectAddOrRemove(idx)
|
||||
} else if (d.ActiveKeyModifiers() & fyne.KeyModifierShift) != 0 {
|
||||
t.SelectionMgr.SelectRange(idx)
|
||||
} else {
|
||||
t.SelectionMgr.Select(idx)
|
||||
}
|
||||
} else {
|
||||
t.SelectionMgr.Select(idx)
|
||||
}
|
||||
t.list.Refresh()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user