Skip to content

Commit

Permalink
Closes #905: Favorite folders
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Dec 2, 2024
1 parent 97aa92f commit 00dcacd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
8 changes: 5 additions & 3 deletions model/gurps/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/richardwilkes/toolbox/errs"
"github.com/richardwilkes/toolbox/txt"
"github.com/richardwilkes/toolbox/xio"
xfs "github.com/richardwilkes/toolbox/xio/fs"
"github.com/rjeczalik/notify"
)

Expand Down Expand Up @@ -122,8 +121,11 @@ func (l *Library) SetPath(newPath string) error {
func (l *Library) CleanupFavorites() {
var favs []string
for _, one := range l.Favorites {
if xfs.FileIsReadable(filepath.Join(l.PathOnDisk, one)) {
favs = append(favs, one)
path := filepath.Join(l.PathOnDisk, one)
if fi, err := os.Stat(path); err == nil {
if mode := fi.Mode(); (mode.IsDir() || mode.IsRegular()) && mode.Perm()&0o400 != 0 {
favs = append(favs, one)
}
}
}
slices.Sort(favs)
Expand Down
4 changes: 3 additions & 1 deletion model/gurps/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func GlobalSettings() *Settings {
func (s *Settings) Save() error {
cutoff := time.Now().Add(-time.Hour * 24 * 120).Unix()
for k, v := range s.LibraryExplorer.Nodes {
if v.LastUsed < cutoff {
if v.LastUsed < cutoff ||
// Also prune out old keys before we had dirs in the favorites list
(!strings.HasPrefix(k, "F@") && !strings.HasPrefix(k, "_@")) {
delete(s.LibraryExplorer.Nodes, k)
}
}
Expand Down
30 changes: 16 additions & 14 deletions ux/navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,17 @@ func (n *Navigator) favoriteSelection() {
if n.table.HasSelection() {
changed := false
selection := n.table.SelectedRows(true)
seen := make(map[string]bool)
for _, row := range selection {
if row.IsFile() {
changed = true
if i := slices.Index(row.library.Favorites, row.path); i != -1 {
row.library.Favorites = slices.Delete(row.library.Favorites, i, i+1)
} else {
row.library.Favorites = append(row.library.Favorites, row.path)
}
if seen[row.path] || row.IsLibrary() || row.IsFavorites() {
continue
}
changed = true
seen[row.path] = true
if i := slices.Index(row.library.Favorites, row.path); i != -1 {
row.library.Favorites = slices.Delete(row.library.Favorites, i, i+1)
} else {
row.library.Favorites = append(row.library.Favorites, row.path)
}
}
if changed {
Expand Down Expand Up @@ -465,7 +468,7 @@ func (n *Navigator) renameSelection() {
}

func (n *Navigator) fixupFavoritePath(row *NavigatorNode, oldPath, newPath string) {
if row.IsFile() {
if row.IsFile() || row.IsDirectory() {
prefix := row.library.PathOnDisk + string([]rune{filepath.Separator})
oldPath = strings.TrimPrefix(oldPath, prefix)
if i := slices.Index(row.library.Favorites, oldPath); i != -1 {
Expand Down Expand Up @@ -780,15 +783,14 @@ func (n *Navigator) selectionChanged() {
downloadEnabled = len(releases) != 0 && releases[0].HasUpdate()
}
} else {
if row.IsFavorites() {
renameEnabled = false
deleteEnabled = false
}
hasOther = true
configEnabled = false
downloadEnabled = false
if row.IsFile() {
favoriteEnabled = true
favoriteEnabled = true
if row.IsFavorites() {
renameEnabled = false
deleteEnabled = false
favoriteEnabled = false
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions ux/navigator_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ func NewLibraryNode(nav *Navigator, lib *gurps.Library) *NavigatorNode {

// NewDirectoryNode creates a new DirectoryNode.
func NewDirectoryNode(nav *Navigator, lib *gurps.Library, dirPath string, parent *NavigatorNode) *NavigatorNode {
pathForID := "@" + filepath.Join(lib.Path(), dirPath)
root := parent
for root.parent != nil {
root = root.parent
}
if root.IsFavorites() {
pathForID = "F" + pathForID
} else {
pathForID = "_" + pathForID
}
n := &NavigatorNode{
id: gurps.IDForNavNode(filepath.Join(lib.Path(), dirPath), kinds.NavigatorDirectory),
id: gurps.IDForNavNode(pathForID, kinds.NavigatorDirectory),
path: dirPath,
nav: nav,
library: lib,
Expand Down Expand Up @@ -299,7 +309,12 @@ func (n *NavigatorNode) Refresh() {
}
slices.SortFunc(favs, func(a, b *fav) int { return txt.NaturalCmp(a.path, b.path, true) })
for _, one := range favs {
n.children = append(n.children, NewFileNode(one.library, one.path, n))
p := filepath.Join(one.library.Path(), one.path)
if xfs.IsDir(p) {
n.children = append(n.children, NewDirectoryNode(n.nav, one.library, one.path, n))
} else {
n.children = append(n.children, NewFileNode(one.library, one.path, n))
}
}
case n.IsLibrary():
n.children = n.refreshChildren(".", n)
Expand Down

0 comments on commit 00dcacd

Please sign in to comment.