diff options
author | Koni Marti <koni.marti@gmail.com> | 2024-08-07 20:52:36 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-08-20 12:26:47 +0200 |
commit | 054b5edc002008f20210f79e384381560a4f753d (patch) | |
tree | 293de9ab7e517bd16467ea77b17790e710a56362 | |
parent | dedf7b58037651f3d63a78fc25bd9b319cbaa374 (diff) | |
download | aerc-054b5edc002008f20210f79e384381560a4f753d.tar.gz |
store: extract configure logic
Extract a function to configure the message store from its constructor
to reconfigure the store when the data changes.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Tested-by: Inwit <inwit@sindominio.net>
Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r-- | app/account.go | 20 | ||||
-rw-r--r-- | lib/msgstore.go | 102 |
2 files changed, 60 insertions, 62 deletions
diff --git a/app/account.go b/app/account.go index 2f9cdf99..45321755 100644 --- a/app/account.go +++ b/app/account.go @@ -263,15 +263,11 @@ func (acct *AccountView) newStore(name string) *lib.MessageStore { } backend := acct.AccountConfig().Backend store := lib.NewMessageStore(acct.worker, name, - acct.sortCriteria(uiConf), - uiConf.ThreadingEnabled, - uiConf.ForceClientThreads, - uiConf.ClientThreadsDelay, - uiConf.SelectLast, - uiConf.ThreadingBySubject, - uiConf.ReverseOrder, - uiConf.ReverseThreadOrder, - uiConf.SortThreadSiblings, + func() *config.UIConfig { + return config.Ui. + ForAccount(acct.Name()). + ForFolder(name) + }, func(msg *models.MessageInfo) { err := hooks.RunHook(&hooks.MailReceived{ Account: acct.Name(), @@ -335,8 +331,8 @@ func (acct *AccountView) newStore(name string) *lib.MessageStore { } }, acct.updateSplitView, - acct.dirlist.UiConfig(name).ThreadContext, ) + store.Configure(acct.SortCriteria(uiConf)) store.SetMarker(marker.New(store)) return store } @@ -504,7 +500,7 @@ func (acct *AccountView) updateDirCounts(destination string, uids []uint32) { } } -func (acct *AccountView) sortCriteria(uiConf *config.UIConfig) []*types.SortCriterion { +func (acct *AccountView) SortCriteria(uiConf *config.UIConfig) []*types.SortCriterion { if uiConf == nil { return nil } @@ -520,7 +516,7 @@ func (acct *AccountView) sortCriteria(uiConf *config.UIConfig) []*types.SortCrit } func (acct *AccountView) GetSortCriteria() []*types.SortCriterion { - return acct.sortCriteria(acct.UiConfig()) + return acct.SortCriteria(acct.UiConfig()) } func (acct *AccountView) CheckMail() { diff --git a/lib/msgstore.go b/lib/msgstore.go index f4dac36a..5c8b3ef1 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "git.sr.ht/~rjarry/aerc/config" "git.sr.ht/~rjarry/aerc/lib/iterator" "git.sr.ht/~rjarry/aerc/lib/marker" "git.sr.ht/~rjarry/aerc/lib/sort" @@ -23,6 +24,8 @@ type MessageStore struct { Messages map[uint32]*models.MessageInfo Sorting bool + ui func() *config.UIConfig + // ctx is given by the directory lister ctx context.Context @@ -48,14 +51,10 @@ type MessageStore struct { sortCriteria []*types.SortCriterion sortDefault []*types.SortCriterion - threadedView bool - selectLast bool - reverseThreadOrder bool - threadContext bool - threadBySubject bool - sortThreadSiblings bool - buildThreads bool - builder *ThreadBuilder + threadedView bool + threadContext bool + buildThreads bool + builder *ThreadBuilder // Map of uids we've asked the worker to fetch onUpdate func(store *MessageStore) // TODO: multiple onUpdate handlers @@ -77,7 +76,6 @@ type MessageStore struct { triggerFlagChanged func(string) threadBuilderDebounce *time.Timer - threadBuilderDelay time.Duration threadCallback func() // threads mutex protects the store.threads and store.threadCallback @@ -90,26 +88,20 @@ type MessageStore struct { const MagicUid = 0xFFFFFFFF func NewMessageStore(worker *types.Worker, name string, - defaultSortCriteria []*types.SortCriterion, - thread bool, clientThreads bool, clientThreadsDelay time.Duration, - selectLast bool, threadBySubject bool, - reverseOrder bool, reverseThreadOrder bool, sortThreadSiblings bool, + ui func() *config.UIConfig, triggerNewEmail func(*models.MessageInfo), triggerDirectoryChange func(), triggerMailDeleted func(), triggerMailAdded func(string), triggerTagModified func([]string, []string), triggerFlagChanged func(string), onSelect func(*models.MessageInfo), - threadContext bool, ) *MessageStore { - if !worker.Backend.Capabilities().Thread { - clientThreads = true - } - return &MessageStore{ Name: name, Deleted: make(map[uint32]interface{}), Messages: make(map[uint32]*models.MessageInfo), + ui: ui, + ctx: context.Background(), selectedUid: MagicUid, @@ -118,17 +110,6 @@ func NewMessageStore(worker *types.Worker, name string, bodyCallbacks: make(map[uint32][]func(*types.FullMessage)), - threadedView: thread, - buildThreads: clientThreads, - threadContext: threadContext, - threadBySubject: threadBySubject, - selectLast: selectLast, - reverseThreadOrder: reverseThreadOrder, - sortThreadSiblings: sortThreadSiblings, - - sortCriteria: defaultSortCriteria, - sortDefault: defaultSortCriteria, - pendingBodies: make(map[uint32]interface{}), pendingHeaders: make(map[uint32]interface{}), worker: worker, @@ -143,10 +124,28 @@ func NewMessageStore(worker *types.Worker, name string, triggerTagModified: triggerTagModified, triggerFlagChanged: triggerFlagChanged, - threadBuilderDelay: clientThreadsDelay, + onSelect: onSelect, + } +} + +func (store *MessageStore) Configure( + defaultSort []*types.SortCriterion, +) { + uiConf := store.ui() + + store.buildThreads = uiConf.ForceClientThreads || + !store.worker.Backend.Capabilities().Thread + store.iterFactory = iterator.NewFactory(uiConf.ReverseOrder) + + // The following config values can be toggled by the user; + // reset to default values when reloading config + store.threadedView = uiConf.ThreadingEnabled + store.threadContext = uiConf.ThreadContext - iterFactory: iterator.NewFactory(reverseOrder), - onSelect: onSelect, + // update the default sort criteria + store.sortDefault = defaultSort + if store.sortCriteria == nil { + store.sortCriteria = defaultSort } } @@ -285,9 +284,10 @@ func (store *MessageStore) Update(msg types.WorkerMessage) { } case *types.DirectoryThreaded: if store.builder == nil { - store.builder = NewThreadBuilder(store.iterFactory, store.threadBySubject) + store.builder = NewThreadBuilder(store.iterFactory, + store.ui().ThreadingBySubject) } - store.builder.RebuildUids(msg.Threads, store.reverseThreadOrder) + store.builder.RebuildUids(msg.Threads, store.ReverseThreadOrder()) store.uids = store.builder.Uids() store.threads = msg.Threads @@ -430,21 +430,19 @@ func (store *MessageStore) update(threads bool) { store.runThreadBuilder() default: if store.builder == nil { - store.builder = NewThreadBuilder(store.iterFactory, store.threadBySubject) + store.builder = NewThreadBuilder(store.iterFactory, + store.ui().ThreadingBySubject) } store.threadsMutex.Lock() - store.builder.RebuildUids(store.threads, store.reverseThreadOrder) + store.builder.RebuildUids(store.threads, + store.ReverseThreadOrder()) store.threadsMutex.Unlock() } } } -func (store *MessageStore) SetReverseThreadOrder(reverse bool) { - store.reverseThreadOrder = reverse -} - func (store *MessageStore) ReverseThreadOrder() bool { - return store.reverseThreadOrder + return store.ui().ReverseThreadOrder } func (store *MessageStore) SetThreadedView(thread bool) { @@ -484,7 +482,8 @@ func (store *MessageStore) BuildThreads() bool { func (store *MessageStore) runThreadBuilder() { if store.builder == nil { - store.builder = NewThreadBuilder(store.iterFactory, store.threadBySubject) + store.builder = NewThreadBuilder(store.iterFactory, + store.ui().ThreadingBySubject) for _, msg := range store.Messages { store.builder.Update(msg) } @@ -492,23 +491,26 @@ func (store *MessageStore) runThreadBuilder() { if store.threadBuilderDebounce != nil { store.threadBuilderDebounce.Stop() } - store.threadBuilderDebounce = time.AfterFunc(store.threadBuilderDelay, func() { - store.runThreadBuilderNow() - ui.Invalidate() - }) + store.threadBuilderDebounce = time.AfterFunc(store.ui().ClientThreadsDelay, + func() { + store.runThreadBuilderNow() + ui.Invalidate() + }, + ) } // runThreadBuilderNow runs the threadbuilder without any debounce logic func (store *MessageStore) runThreadBuilderNow() { if store.builder == nil { - store.builder = NewThreadBuilder(store.iterFactory, store.threadBySubject) + store.builder = NewThreadBuilder(store.iterFactory, + store.ui().ThreadingBySubject) for _, msg := range store.Messages { store.builder.Update(msg) } } // build new threads - th := store.builder.Threads(store.uids, store.reverseThreadOrder, - store.sortThreadSiblings) + th := store.builder.Threads(store.uids, store.ReverseThreadOrder(), + store.ui().SortThreadSiblings) // save local threads to the message store variable and // run callback if defined (callback should reposition cursor) @@ -766,7 +768,7 @@ func (store *MessageStore) SelectedUid() uint32 { if store.selectedUid == MagicUid && len(store.Uids()) > 0 { iter := store.UidsIterator() idx := iter.StartIndex() - if store.selectLast { + if store.ui().SelectLast { idx = iter.EndIndex() } store.Select(store.Uids()[idx]) |