aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2023-11-18 02:07:06 +0100
committerRobin Jarry <robin@jarry.cc>2023-11-23 00:14:54 +0100
commit85d0936df6861c41a1dddd91a2f3b97afd8e127a (patch)
tree72def6d94ba56f508bdc6cbf051865c000d968dc
parent48b9589d8bbe4ec8982c08a9fe6e59b77734eb80 (diff)
downloadaerc-85d0936df6861c41a1dddd91a2f3b97afd8e127a.tar.gz
dirlist/dirtree: context handling
Add a NewContext() function that extracts and handles the canceling and re-creation of a new context for the directory list. This enables a better control of the context cancellation especially for a directory tree when a virtual node is selected. Before, the previous folder would have been opened if a virtual node was selected. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--app/dirlist.go15
-rw-r--r--app/dirtree.go12
2 files changed, 16 insertions, 11 deletions
diff --git a/app/dirlist.go b/app/dirlist.go
index 708eb824..d260d7c1 100644
--- a/app/dirlist.go
+++ b/app/dirlist.go
@@ -67,15 +67,12 @@ type DirectoryList struct {
func NewDirectoryList(acctConf *config.AccountConfig,
worker *types.Worker,
) DirectoryLister {
- ctx, cancel := context.WithCancel(context.Background())
-
dirlist := &DirectoryList{
acctConf: acctConf,
store: lib.NewDirStore(),
worker: worker,
- ctx: ctx,
- cancel: cancel,
}
+ dirlist.NewContext()
uiConf := dirlist.UiConfig("")
dirlist.spinner = NewSpinner(uiConf)
dirlist.spinner.Start()
@@ -87,6 +84,13 @@ func NewDirectoryList(acctConf *config.AccountConfig,
return dirlist
}
+func (dirlist *DirectoryList) NewContext() {
+ if dirlist.cancel != nil {
+ dirlist.cancel()
+ }
+ dirlist.ctx, dirlist.cancel = context.WithCancel(context.Background())
+}
+
func (dirlist *DirectoryList) UiConfig(dir string) *config.UIConfig {
if dir == "" {
dir = dirlist.Selected()
@@ -181,8 +185,7 @@ func (dirlist *DirectoryList) Open(name string, delay time.Duration,
) {
dirlist.selecting = name
- dirlist.cancel()
- dirlist.ctx, dirlist.cancel = context.WithCancel(context.Background())
+ dirlist.NewContext()
go func(ctx context.Context) {
defer log.PanicHandler()
diff --git a/app/dirtree.go b/app/dirtree.go
index 6a3b34c0..8f8730b4 100644
--- a/app/dirtree.go
+++ b/app/dirtree.go
@@ -256,12 +256,17 @@ func (dt *DirectoryTree) NextPrev(delta int) {
}
}
- dt.listIdx = newIdx
+ dt.selectIndex(newIdx)
+}
+
+func (dt *DirectoryTree) selectIndex(i int) {
+ dt.listIdx = i
if path := dt.getDirectory(dt.list[dt.listIdx]); path != "" {
dt.virtual = false
dt.Select(path)
} else {
dt.virtual = true
+ dt.NewContext()
dt.virtualCb()
}
}
@@ -274,10 +279,7 @@ func (dt *DirectoryTree) CollapseFolder() {
// highlight parent node and select it
for i, t := range dt.list {
if t == node.Parent {
- dt.listIdx = i
- if path := dt.getDirectory(dt.list[dt.listIdx]); path != "" {
- dt.Select(path)
- }
+ dt.selectIndex(i)
}
}
} else {