Implemented history for DirectoryView

For some reason DirectoryView won't draw changes all of the time...
This commit is contained in:
Sasha Koshka 2023-03-23 14:37:44 -04:00
parent f74f6a43f8
commit 14802b4b82
2 changed files with 51 additions and 11 deletions

View File

@ -17,6 +17,11 @@ type fileLayoutEntry struct {
Bounds image.Rectangle Bounds image.Rectangle
} }
type historyEntry struct {
location string
filesystem ReadDirStatFS
}
// DirectoryView displays a list of files within a particular directory and // DirectoryView displays a list of files within a particular directory and
// file system. // file system.
type DirectoryView struct { type DirectoryView struct {
@ -33,8 +38,8 @@ type DirectoryView struct {
onScrollBoundsChange func () onScrollBoundsChange func ()
filesystem ReadDirStatFS history []historyEntry
location string historyIndex int
onChoose func (file string) onChoose func (file string)
} }
@ -56,8 +61,10 @@ func NewDirectoryView (
} }
// Location returns the directory's location and filesystem. // Location returns the directory's location and filesystem.
func (element *DirectoryView) Location () (string, fs.ReadDirFS) { func (element *DirectoryView) Location () (string, ReadDirStatFS) {
return element.location, element.filesystem if len(element.history) < 1 { return "", nil }
current := element.history[element.historyIndex]
return current.location, current.filesystem
} }
// SetLocation sets the directory's location and filesystem. If within is nil, // SetLocation sets the directory's location and filesystem. If within is nil,
@ -70,14 +77,40 @@ func (element *DirectoryView) SetLocation (
within = defaultFS { } within = defaultFS { }
} }
element.scroll = image.Point { } element.scroll = image.Point { }
element.location = location
element.filesystem = within if element.history != nil {
element.historyIndex ++
}
element.history = append (
element.history[:element.historyIndex],
historyEntry { location, within })
return element.Update() return element.Update()
} }
// Backward goes back a directory in history
func (element *DirectoryView) Backward () (bool, error) {
if element.historyIndex > 1 {
element.historyIndex --
return true, element.Update()
} else {
return false, nil
}
}
// Forward goes forward a directory in history
func (element *DirectoryView) Forward () (bool, error) {
if element.historyIndex < len(element.history) - 1 {
element.historyIndex ++
return true, element.Update()
} else {
return false, nil
}
}
// Update refreshes the directory's contents. // Update refreshes the directory's contents.
func (element *DirectoryView) Update () error { func (element *DirectoryView) Update () error {
entries, err := element.filesystem.ReadDir(element.location) location, filesystem := element.Location()
entries, err := filesystem.ReadDir(location)
// disown all entries // disown all entries
for _, file := range element.children { for _, file := range element.children {
@ -91,10 +124,8 @@ func (element *DirectoryView) Update () error {
element.children = make([]fileLayoutEntry, len(entries)) element.children = make([]fileLayoutEntry, len(entries))
for index, entry := range entries { for index, entry := range entries {
filePath := filepath.Join(element.location, entry.Name()) filePath := filepath.Join(location, entry.Name())
file, err := NewFile ( file, err := NewFile(filePath, filesystem)
filePath,
element.filesystem)
if err != nil { continue } if err != nil { continue }
file.SetParent(element) file.SetParent(element)
file.OnChoose (func () { file.OnChoose (func () {

View File

@ -53,6 +53,15 @@ func run () {
choose(locationInput.Value()) choose(locationInput.Value())
}) })
choose(homeDir) choose(homeDir)
backButton.OnClick (func () {
directoryView.Backward()
})
forwardButton.OnClick (func () {
directoryView.Forward()
})
refreshButton.OnClick (func () {
directoryView.Update()
})
controlBar.Adopt(backButton, false) controlBar.Adopt(backButton, false)
controlBar.Adopt(forwardButton, false) controlBar.Adopt(forwardButton, false)