Move select functionality from msglist to msgstore

Remove msglist Next and Prev commands

Signed-off-by: Kevin Kuehler <kkuehler@brave.com>
This commit is contained in:
Kevin Kuehler 2019-06-10 22:05:55 -07:00 committed by Drew DeVault
parent d1df710328
commit 32f970e053
5 changed files with 67 additions and 46 deletions

View file

@ -34,7 +34,8 @@ func Archive(aerc *widgets.Aerc, args []string) error {
msg := widget.SelectedMessage() msg := widget.SelectedMessage()
store := widget.Store() store := widget.Store()
archiveDir := acct.AccountConfig().Archive archiveDir := acct.AccountConfig().Archive
acct.Messages().Next() store.Next()
acct.Messages().Scroll()
switch args[1] { switch args[1] {
case ARCHIVE_MONTH: case ARCHIVE_MONTH:

View file

@ -31,7 +31,8 @@ func DeleteMessage(aerc *widgets.Aerc, args []string) error {
if isMsgView { if isMsgView {
aerc.RemoveTab(widget) aerc.RemoveTab(widget)
} }
acct.Messages().Next() store.Next()
acct.Messages().Scroll()
store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) { store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:

View file

@ -45,7 +45,8 @@ func Move(aerc *widgets.Aerc, args []string) error {
if isMsgView { if isMsgView {
aerc.RemoveTab(widget) aerc.RemoveTab(widget)
} }
acct.Messages().Next() store.Next()
acct.Messages().Scroll()
store.Move([]uint32{msg.Uid}, args[optind], createParents, func( store.Move([]uint32{msg.Uid}, args[optind], createParents, func(
msg types.WorkerMessage) { msg types.WorkerMessage) {

View file

@ -17,6 +17,7 @@ type MessageStore struct {
// Ordered list of known UIDs // Ordered list of known UIDs
Uids []uint32 Uids []uint32
selected int
bodyCallbacks map[uint32][]func(io.Reader) bodyCallbacks map[uint32][]func(io.Reader)
headerCallbacks map[uint32][]func(*types.MessageInfo) headerCallbacks map[uint32][]func(*types.MessageInfo)
@ -34,6 +35,7 @@ func NewMessageStore(worker *types.Worker,
Deleted: make(map[uint32]interface{}), Deleted: make(map[uint32]interface{}),
DirInfo: *dirInfo, DirInfo: *dirInfo,
selected: 0,
bodyCallbacks: make(map[uint32][]func(io.Reader)), bodyCallbacks: make(map[uint32][]func(io.Reader)),
headerCallbacks: make(map[uint32][]func(*types.MessageInfo)), headerCallbacks: make(map[uint32][]func(*types.MessageInfo)),
@ -279,3 +281,42 @@ func (store *MessageStore) Read(uids []uint32, read bool,
Uids: set, Uids: set,
}, cb) }, cb)
} }
func (store *MessageStore) Selected() *types.MessageInfo {
return store.Messages[store.Uids[len(store.Uids)-store.selected-1]]
}
func (store *MessageStore) SelectedIndex() int {
return store.selected
}
func (store *MessageStore) Select(index int) {
store.selected = index
for ; store.selected < 0; store.selected = len(store.Uids) + store.selected {
/* This space deliberately left blank */
}
if store.selected > len(store.Uids) {
store.selected = len(store.Uids)
}
}
func (store *MessageStore) nextPrev(delta int) {
if len(store.Uids) == 0 {
return
}
store.selected += delta
if store.selected < 0 {
store.selected = 0
}
if store.selected >= len(store.Uids) {
store.selected = len(store.Uids) - 1
}
}
func (store *MessageStore) Next() {
store.nextPrev(1)
}
func (store *MessageStore) Prev() {
store.nextPrev(-1)
}

View file

@ -16,22 +16,20 @@ import (
type MessageList struct { type MessageList struct {
ui.Invalidatable ui.Invalidatable
conf *config.AercConfig conf *config.AercConfig
logger *log.Logger logger *log.Logger
height int height int
scroll int scroll int
selected int nmsgs int
nmsgs int spinner *Spinner
spinner *Spinner store *lib.MessageStore
store *lib.MessageStore
} }
func NewMessageList(conf *config.AercConfig, logger *log.Logger) *MessageList { func NewMessageList(conf *config.AercConfig, logger *log.Logger) *MessageList {
ml := &MessageList{ ml := &MessageList{
conf: conf, conf: conf,
logger: logger, logger: logger,
selected: 0, spinner: NewSpinner(),
spinner: NewSpinner(),
} }
ml.spinner.OnInvalidate(func(_ ui.Drawable) { ml.spinner.OnInvalidate(func(_ ui.Drawable) {
ml.Invalidate() ml.Invalidate()
@ -78,7 +76,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
style := tcell.StyleDefault style := tcell.StyleDefault
// current row // current row
if row == ml.selected-ml.scroll { if row == ml.store.SelectedIndex()-ml.scroll {
style = style.Reverse(true) style = style.Reverse(true)
} }
// deleted message // deleted message
@ -139,12 +137,12 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
// for the previously selected UID. // for the previously selected UID.
if len(store.Uids) > ml.nmsgs && ml.nmsgs != 0 { if len(store.Uids) > ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < len(store.Uids)-ml.nmsgs; i++ { for i := 0; i < len(store.Uids)-ml.nmsgs; i++ {
ml.Next() ml.Scroll()
} }
} }
if len(store.Uids) < ml.nmsgs && ml.nmsgs != 0 { if len(store.Uids) < ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < ml.nmsgs-len(store.Uids); i++ { for i := 0; i < ml.nmsgs-len(store.Uids); i++ {
ml.Prev() ml.Scroll()
} }
} }
ml.nmsgs = len(store.Uids) ml.nmsgs = len(store.Uids)
@ -156,7 +154,6 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
func (ml *MessageList) SetStore(store *lib.MessageStore) { func (ml *MessageList) SetStore(store *lib.MessageStore) {
if ml.Store() != store { if ml.Store() != store {
ml.scroll = 0 ml.scroll = 0
ml.selected = 0
} }
ml.store = store ml.store = store
if store != nil { if store != nil {
@ -180,54 +177,34 @@ func (ml *MessageList) Empty() bool {
func (ml *MessageList) Selected() *types.MessageInfo { func (ml *MessageList) Selected() *types.MessageInfo {
store := ml.Store() store := ml.Store()
return store.Messages[store.Uids[len(store.Uids)-ml.selected-1]] return store.Messages[store.Uids[len(store.Uids)-ml.store.SelectedIndex()-1]]
} }
func (ml *MessageList) Select(index int) { func (ml *MessageList) Select(index int) {
store := ml.Store() store := ml.Store()
store.Select(index)
ml.selected = index
for ; ml.selected < 0; ml.selected = len(store.Uids) + ml.selected {
}
if ml.selected > len(store.Uids) {
ml.selected = len(store.Uids)
}
// I'm too lazy to do the math right now // I'm too lazy to do the math right now
for ml.selected-ml.scroll >= ml.Height() { for store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1 ml.scroll += 1
} }
for ml.selected-ml.scroll < 0 { for store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1 ml.scroll -= 1
} }
} }
func (ml *MessageList) nextPrev(delta int) { func (ml *MessageList) Scroll() {
store := ml.Store() store := ml.Store()
if store == nil || len(store.Uids) == 0 { if store == nil || len(store.Uids) == 0 {
return return
} }
ml.selected += delta
if ml.selected < 0 {
ml.selected = 0
}
if ml.selected >= len(store.Uids) {
ml.selected = len(store.Uids) - 1
}
if ml.Height() != 0 { if ml.Height() != 0 {
if ml.selected-ml.scroll >= ml.Height() { if store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1 ml.scroll += 1
} else if ml.selected-ml.scroll < 0 { } else if store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1 ml.scroll -= 1
} }
} }
ml.Invalidate() ml.Invalidate()
} }
func (ml *MessageList) Next() {
ml.nextPrev(1)
}
func (ml *MessageList) Prev() {
ml.nextPrev(-1)
}