From 14802b4b826f87dc9f7b82ce30cb864b6fb85530 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 23 Mar 2023 14:37:44 -0400 Subject: [PATCH] Implemented history for DirectoryView For some reason DirectoryView won't draw changes all of the time... --- elements/file/directory.go | 53 ++++++++++++++++++++++++++++-------- examples/fileBrowser/main.go | 9 ++++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/elements/file/directory.go b/elements/file/directory.go index 723ebe4..7707aa6 100644 --- a/elements/file/directory.go +++ b/elements/file/directory.go @@ -17,6 +17,11 @@ type fileLayoutEntry struct { Bounds image.Rectangle } +type historyEntry struct { + location string + filesystem ReadDirStatFS +} + // DirectoryView displays a list of files within a particular directory and // file system. type DirectoryView struct { @@ -33,8 +38,8 @@ type DirectoryView struct { onScrollBoundsChange func () - filesystem ReadDirStatFS - location string + history []historyEntry + historyIndex int onChoose func (file string) } @@ -56,8 +61,10 @@ func NewDirectoryView ( } // Location returns the directory's location and filesystem. -func (element *DirectoryView) Location () (string, fs.ReadDirFS) { - return element.location, element.filesystem +func (element *DirectoryView) Location () (string, ReadDirStatFS) { + 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, @@ -70,14 +77,40 @@ func (element *DirectoryView) SetLocation ( within = defaultFS { } } 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() } +// 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. func (element *DirectoryView) Update () error { - entries, err := element.filesystem.ReadDir(element.location) + location, filesystem := element.Location() + entries, err := filesystem.ReadDir(location) // disown all entries for _, file := range element.children { @@ -91,10 +124,8 @@ func (element *DirectoryView) Update () error { element.children = make([]fileLayoutEntry, len(entries)) for index, entry := range entries { - filePath := filepath.Join(element.location, entry.Name()) - file, err := NewFile ( - filePath, - element.filesystem) + filePath := filepath.Join(location, entry.Name()) + file, err := NewFile(filePath, filesystem) if err != nil { continue } file.SetParent(element) file.OnChoose (func () { diff --git a/examples/fileBrowser/main.go b/examples/fileBrowser/main.go index 6eea8e4..12bfe0c 100644 --- a/examples/fileBrowser/main.go +++ b/examples/fileBrowser/main.go @@ -53,6 +53,15 @@ func run () { choose(locationInput.Value()) }) choose(homeDir) + backButton.OnClick (func () { + directoryView.Backward() + }) + forwardButton.OnClick (func () { + directoryView.Forward() + }) + refreshButton.OnClick (func () { + directoryView.Update() + }) controlBar.Adopt(backButton, false) controlBar.Adopt(forwardButton, false)