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()
store := widget.Store()
archiveDir := acct.AccountConfig().Archive
acct.Messages().Next()
store.Next()
acct.Messages().Scroll()
switch args[1] {
case ARCHIVE_MONTH:

View File

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

View File

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

View File

@ -17,6 +17,7 @@ type MessageStore struct {
// Ordered list of known UIDs
Uids []uint32
selected int
bodyCallbacks map[uint32][]func(io.Reader)
headerCallbacks map[uint32][]func(*types.MessageInfo)
@ -34,6 +35,7 @@ func NewMessageStore(worker *types.Worker,
Deleted: make(map[uint32]interface{}),
DirInfo: *dirInfo,
selected: 0,
bodyCallbacks: make(map[uint32][]func(io.Reader)),
headerCallbacks: make(map[uint32][]func(*types.MessageInfo)),
@ -279,3 +281,42 @@ func (store *MessageStore) Read(uids []uint32, read bool,
Uids: set,
}, 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 {
ui.Invalidatable
conf *config.AercConfig
logger *log.Logger
height int
scroll int
selected int
nmsgs int
spinner *Spinner
store *lib.MessageStore
conf *config.AercConfig
logger *log.Logger
height int
scroll int
nmsgs int
spinner *Spinner
store *lib.MessageStore
}
func NewMessageList(conf *config.AercConfig, logger *log.Logger) *MessageList {
ml := &MessageList{
conf: conf,
logger: logger,
selected: 0,
spinner: NewSpinner(),
conf: conf,
logger: logger,
spinner: NewSpinner(),
}
ml.spinner.OnInvalidate(func(_ ui.Drawable) {
ml.Invalidate()
@ -78,7 +76,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
style := tcell.StyleDefault
// current row
if row == ml.selected-ml.scroll {
if row == ml.store.SelectedIndex()-ml.scroll {
style = style.Reverse(true)
}
// deleted message
@ -139,12 +137,12 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
// for the previously selected UID.
if len(store.Uids) > ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < len(store.Uids)-ml.nmsgs; i++ {
ml.Next()
ml.Scroll()
}
}
if len(store.Uids) < ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < ml.nmsgs-len(store.Uids); i++ {
ml.Prev()
ml.Scroll()
}
}
ml.nmsgs = len(store.Uids)
@ -156,7 +154,6 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
func (ml *MessageList) SetStore(store *lib.MessageStore) {
if ml.Store() != store {
ml.scroll = 0
ml.selected = 0
}
ml.store = store
if store != nil {
@ -180,54 +177,34 @@ func (ml *MessageList) Empty() bool {
func (ml *MessageList) Selected() *types.MessageInfo {
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) {
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
for ml.selected-ml.scroll >= ml.Height() {
for store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1
}
for ml.selected-ml.scroll < 0 {
for store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1
}
}
func (ml *MessageList) nextPrev(delta int) {
func (ml *MessageList) Scroll() {
store := ml.Store()
if store == nil || len(store.Uids) == 0 {
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.selected-ml.scroll >= ml.Height() {
if store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1
} else if ml.selected-ml.scroll < 0 {
} else if store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1
}
}
ml.Invalidate()
}
func (ml *MessageList) Next() {
ml.nextPrev(1)
}
func (ml *MessageList) Prev() {
ml.nextPrev(-1)
}