diff --git a/src/selector.cpp b/src/selector.cpp index 5dcaed3..30be9f6 100644 --- a/src/selector.cpp +++ b/src/selector.cpp @@ -191,10 +191,8 @@ int Selector::exec(int startSelection) { // ...fall through... case InputManager::LEFT: if (showDirectories) { - dir = parentDir(dir); - selected = 0; + selected = goToParentDir(fl); firstElement = 0; - prepare(fl); } break; @@ -204,14 +202,19 @@ int Selector::exec(int startSelection) { file = fl[selected]; close = true; } else { - dir = dir+fl[selected]; - char *buf = realpath(dir.c_str(), NULL); - dir = (string) buf + '/'; - free(buf); + string subdir = fl[selected]; + if (subdir == "..") { + selected = goToParentDir(fl); + } else { + dir += subdir; + char *buf = realpath(dir.c_str(), NULL); + dir = (string) buf + '/'; + free(buf); - selected = 0; + prepare(fl); + selected = 0; + } firstElement = 0; - prepare(fl); } } break; @@ -235,3 +238,13 @@ bool Selector::prepare(FileLister& fl) { return opened; } + +int Selector::goToParentDir(FileLister& fl) { + string oldDir = dir; + dir = parentDir(dir); + prepare(fl); + string oldName = oldDir.substr(dir.size(), oldDir.size() - dir.size() - 1); + auto& subdirs = fl.getDirectories(); + auto it = find(subdirs.begin(), subdirs.end(), oldName); + return it == subdirs.end() ? 0 : it - subdirs.begin(); +} diff --git a/src/selector.h b/src/selector.h index 2ed9cb1..ae2b1f0 100644 --- a/src/selector.h +++ b/src/selector.h @@ -37,6 +37,12 @@ private: bool prepare(FileLister& fl); + /** + * Changes 'dir' to its parent directory. + * Returns the index of the old dir in the parent, or 0 if unknown. + */ + int goToParentDir(FileLister& fl); + public: Selector(GMenu2X *gmenu2x, LinkApp& link, const std::string &selectorDir = "");