From 6963a5de8e3c7ad7a279601d30b78f87beceb5ce Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Mon, 6 Feb 2023 17:18:27 -0800 Subject: [PATCH] add selection to tracklist --- go.mod | 2 +- go.sum | 4 +- ui/mainwindow.go | 9 +- ui/{ => os}/keymodifiers_darwin.go | 6 +- ui/{ => os}/keymodifiers_default.go | 2 +- ui/util/listselectionmanager.go | 154 ++++++++++++++++++++++++++++ ui/widgets/tracklist.go | 54 ++++++++-- 7 files changed, 214 insertions(+), 17 deletions(-) rename ui/{ => os}/keymodifiers_darwin.go (73%) rename ui/{ => os}/keymodifiers_default.go (92%) create mode 100644 ui/util/listselectionmanager.go diff --git a/go.mod b/go.mod index 67534a0..86d6d4f 100644 --- a/go.mod +++ b/go.mod @@ -43,4 +43,4 @@ require ( honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect ) -replace fyne.io/fyne/v2 v2.2.4 => github.com/dweymouth/fyne/v2 v2.2.5-0.20230119024415-238e09217d09 +replace fyne.io/fyne/v2 v2.2.4 => github.com/dweymouth/fyne/v2 v2.2.5-0.20230207011038-d2ef06e09a93 diff --git a/go.sum b/go.sum index 9145ccc..e97a078 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,8 @@ github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7h github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dweymouth/fyne/v2 v2.2.5-0.20230119024415-238e09217d09 h1:huJvmT36/E7MsuAVILCK7VPFd0UiPQDpphn215e1rCo= -github.com/dweymouth/fyne/v2 v2.2.5-0.20230119024415-238e09217d09/go.mod h1:MBoGuHzLLSXdQOWFAwWhIhYTEMp33zqtGCReSWhaQTA= +github.com/dweymouth/fyne/v2 v2.2.5-0.20230207011038-d2ef06e09a93 h1:4GGNszUwZhcneapbaBO+rbHtmCjKjZXHKPCqFXQPg4E= +github.com/dweymouth/fyne/v2 v2.2.5-0.20230207011038-d2ef06e09a93/go.mod h1:MBoGuHzLLSXdQOWFAwWhIhYTEMp33zqtGCReSWhaQTA= github.com/dweymouth/go-subsonic v0.0.0-20221214005741-bd8048fa1863 h1:bOWMpFJ9zY839T1EngQ5nxVCiMlatPtpGkg/yHK6szg= github.com/dweymouth/go-subsonic v0.0.0-20221214005741-bd8048fa1863/go.mod h1:fUez6NFiEJiQTZizZ1BThZr5GJXAbigzGYjEPNm4tdI= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= diff --git a/ui/mainwindow.go b/ui/mainwindow.go index adcc4c9..0111a6b 100644 --- a/ui/mainwindow.go +++ b/ui/mainwindow.go @@ -4,6 +4,7 @@ import ( "supersonic/backend" "supersonic/res" "supersonic/ui/browsing" + "supersonic/ui/os" "supersonic/ui/widgets" "fyne.io/fyne/v2" @@ -14,10 +15,10 @@ import ( ) var ( - ShortcutBack = desktop.CustomShortcut{KeyName: fyne.KeyLeft, Modifier: AltModifier} - ShortcutForward = desktop.CustomShortcut{KeyName: fyne.KeyRight, Modifier: AltModifier} - ShortcutReload = desktop.CustomShortcut{KeyName: fyne.KeyR, Modifier: ControlModifier} - ShortcutSearch = desktop.CustomShortcut{KeyName: fyne.KeyF, Modifier: ControlModifier} + ShortcutBack = desktop.CustomShortcut{KeyName: fyne.KeyLeft, Modifier: os.AltModifier} + ShortcutForward = desktop.CustomShortcut{KeyName: fyne.KeyRight, Modifier: os.AltModifier} + ShortcutReload = desktop.CustomShortcut{KeyName: fyne.KeyR, Modifier: os.ControlModifier} + ShortcutSearch = desktop.CustomShortcut{KeyName: fyne.KeyF, Modifier: os.ControlModifier} ) type MainWindow struct { diff --git a/ui/keymodifiers_darwin.go b/ui/os/keymodifiers_darwin.go similarity index 73% rename from ui/keymodifiers_darwin.go rename to ui/os/keymodifiers_darwin.go index a6b20a1..2d3423b 100644 --- a/ui/keymodifiers_darwin.go +++ b/ui/os/keymodifiers_darwin.go @@ -1,8 +1,10 @@ //go:build darwin -package ui +package os -import "fyne.io/fyne/v2" +import ( + "fyne.io/fyne/v2" +) var ( ControlModifier = fyne.KeyModifierSuper diff --git a/ui/keymodifiers_default.go b/ui/os/keymodifiers_default.go similarity index 92% rename from ui/keymodifiers_default.go rename to ui/os/keymodifiers_default.go index 24b75e2..20ffcf7 100644 --- a/ui/keymodifiers_default.go +++ b/ui/os/keymodifiers_default.go @@ -1,6 +1,6 @@ //go:build !darwin -package ui +package os import "fyne.io/fyne/v2" diff --git a/ui/util/listselectionmanager.go b/ui/util/listselectionmanager.go new file mode 100644 index 0000000..64216bd --- /dev/null +++ b/ui/util/listselectionmanager.go @@ -0,0 +1,154 @@ +package util + +type ListSelectionManager struct { + lastSelectedRow int + numSelected int + selected BitSet + len func() int +} + +func NewListSelectionManager(lenFn func() int) ListSelectionManager { + return ListSelectionManager{lastSelectedRow: -1, len: lenFn} +} + +// If the given row is not selected, reset the selection +// and select only the given row. +func (l *ListSelectionManager) Select(row int) { + if row < 0 || l.selected.IsSet(uint(row)) { + return + } + l.UnselectAll() + l.selectAdd(row) +} + +// If it is not selected, add the given row to the selection. +// If it is selected, unselet it. +func (l *ListSelectionManager) SelectAddOrRemove(row int) { + if row < 0 { + return + } + if l.selected.IsSet(uint(row)) { + // find new last selected row + if row == l.lastSelectedRow { + for i := row - 1; i >= -1; i++ { + if i == -1 { + l.lastSelectedRow = i + } else if l.selected.IsSet(uint(i)) { + l.lastSelectedRow = i + break + } + } + } + l.selected.Unset(uint(row)) + l.numSelected -= 1 + return + } + + l.selectAdd(row) +} + +func (l *ListSelectionManager) selectAdd(row int) { + l.numSelected += 1 + l.selected.Set(uint(row)) + if row > l.lastSelectedRow { + l.lastSelectedRow = row + } +} + +// Select a range between the given row and the furthest-down +// row that is currently selected (which may be above the given row) +// Note: this is modeled after what, as far as I can tell, is Gmail's selection behavior +func (l *ListSelectionManager) SelectRange(row int) { + if row < 0 || l.selected.IsSet(uint(row)) { + return + } + if l.numSelected == 0 { + l.selectAdd(row) + return + } + m := maxInt(row, l.lastSelectedRow) + for i := minInt(l.lastSelectedRow, row); i <= m; i++ { + l.selectAdd(i) + } +} + +func (l *ListSelectionManager) SelectAll() { + for i := 0; i < l.len(); i++ { + l.selectAdd(i) + } +} + +func (l *ListSelectionManager) UnselectAll() { + l.selected = nil + l.numSelected = 0 + l.lastSelectedRow = -1 +} + +func (l *ListSelectionManager) IsSelected(row int) bool { + return row >= 0 && l.selected.IsSet(uint(row)) +} + +func (l *ListSelectionManager) GetSelection() []int { + var sel []int + for i := 0; i < l.len(); i++ { + if l.selected.IsSet(uint(i)) { + sel = append(sel, i) + } + if len(sel) == l.numSelected { + break + } + } + return sel +} + +func (l *ListSelectionManager) AreAllSelected() bool { + return l.numSelected == l.len() +} + +func minInt(a, b int) int { + if a < b { + return a + } + return b +} + +func maxInt(a, b int) int { + if a > b { + return a + } + return b +} + +// BitSet implementation from +// https://stackoverflow.com/questions/2311373/how-to-implement-bitset-with-go + +const uint64size = 64 + +// BitSet is a set of bits that can be set, cleared and queried. +type BitSet []uint64 + +// Set ensures that the given bit is set in the BitSet. +func (s *BitSet) Set(i uint) { + if len(*s) < int(i/uint64size+1) { + r := make([]uint64, i/uint64size+1) + copy(r, *s) + *s = r + } + (*s)[i/uint64size] |= 1 << (i % uint64size) +} + +// Unset ensures that the given bit is cleared (not set) in the BitSet. +func (s *BitSet) Unset(i uint) { + if len(*s) >= int(i/uint64size+1) { + (*s)[i/uint64size] &^= 1 << (i % uint64size) + } +} + +// IsSet returns true if the given bit is set, false if it is cleared. +func (s *BitSet) IsSet(i uint) bool { + idx := i / uint64size + if idx >= uint(len(*s)) { + return false + } + return (*s)[i/uint64size]&(1<<(i%uint64size)) != 0 +} diff --git a/ui/widgets/tracklist.go b/ui/widgets/tracklist.go index d95011b..29e3b65 100644 --- a/ui/widgets/tracklist.go +++ b/ui/widgets/tracklist.go @@ -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() +}