diff --git a/Applications/FileManager/DirectoryView.cpp b/Applications/FileManager/DirectoryView.cpp index 52659431291..cf8187ecd45 100644 --- a/Applications/FileManager/DirectoryView.cpp +++ b/Applications/FileManager/DirectoryView.cpp @@ -108,8 +108,18 @@ void DirectoryView::set_view_mode(ViewMode mode) ASSERT_NOT_REACHED(); } +void DirectoryView::add_path_to_history(const String& path) +{ + if (m_path_history_position < m_path_history.size()) + m_path_history.resize(m_path_history_position + 1); + + m_path_history.append(path); + m_path_history_position = m_path_history.size() - 1; +} + void DirectoryView::open(const String& path) { + add_path_to_history(path); model().open(path); } @@ -121,10 +131,27 @@ void DirectoryView::set_status_message(const String& message) void DirectoryView::open_parent_directory() { - model().open(String::format("%s/..", model().path().characters())); + auto path = String::format("%s/..", model().path().characters()); + add_path_to_history(path); + model().open(path); } void DirectoryView::refresh() { model().update(); } + +void DirectoryView::open_previous_directory() +{ + if (m_path_history_position > 0) { + m_path_history_position--; + model().open(m_path_history[m_path_history_position]); + } +} +void DirectoryView::open_next_directory() +{ + if (m_path_history_position < m_path_history.size() - 1) { + m_path_history_position++; + model().open(m_path_history[m_path_history_position]); + } +} diff --git a/Applications/FileManager/DirectoryView.h b/Applications/FileManager/DirectoryView.h index dec41cf1d14..58ff16d628b 100644 --- a/Applications/FileManager/DirectoryView.h +++ b/Applications/FileManager/DirectoryView.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -14,6 +15,10 @@ public: void open(const String& path); String path() const { return model().path(); } void open_parent_directory(); + void open_previous_directory(); + void open_next_directory(); + int path_history_size() const { return m_path_history.size(); } + int path_history_position() const { return m_path_history_position; } void refresh(); @@ -36,6 +41,9 @@ private: ViewMode m_view_mode { Invalid }; Retained m_model; + int m_path_history_position{ 0 }; + Vector m_path_history; + void add_path_to_history(const String& path); GTableView* m_table_view { nullptr }; GItemView* m_item_view { nullptr }; diff --git a/Applications/FileManager/main.cpp b/Applications/FileManager/main.cpp index e5634214a6a..effa2eafaa6 100644 --- a/Applications/FileManager/main.cpp +++ b/Applications/FileManager/main.cpp @@ -76,7 +76,10 @@ int main(int argc, char** argv) }; file_system_model->on_selection_changed = [&] (auto& index) { - directory_view->open(file_system_model->path(index)); + auto path = file_system_model->path(index); + if (directory_view->path() == path) + return; + directory_view->open(path); }; auto open_parent_directory_action = GAction::create("Open parent directory", { Mod_Alt, Key_Up }, GraphicsBitmap::load_from_file("/res/icons/16x16/open-parent-directory.png"), [directory_view] (const GAction&) { @@ -126,12 +129,14 @@ int main(int argc, char** argv) dbgprintf("'Delete' action activated!\n"); }); - auto go_back_action = GAction::create("Go Back", GraphicsBitmap::load_from_file("/res/icons/16x16/go-back.png"), [] (const GAction&) { + auto go_back_action = GAction::create("Go Back", GraphicsBitmap::load_from_file("/res/icons/16x16/go-back.png"), [directory_view] (const GAction&) { dbgprintf("'Go Back' action activated!\n"); + directory_view->open_previous_directory(); }); - auto go_forward_action = GAction::create("Go Forward", GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [] (const GAction&) { + auto go_forward_action = GAction::create("Go Forward", GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), [directory_view] (const GAction&) { dbgprintf("'Go Forward' action activated!\n"); + directory_view->open_next_directory(); }); auto menubar = make(); @@ -158,6 +163,7 @@ int main(int argc, char** argv) go_menu->add_action(go_back_action.copy_ref()); go_menu->add_action(go_forward_action.copy_ref()); go_menu->add_action(open_parent_directory_action.copy_ref()); + menubar->add_menu(move(go_menu)); auto help_menu = make("Help"); help_menu->add_action(GAction::create("About", [] (const GAction&) { @@ -180,12 +186,16 @@ int main(int argc, char** argv) main_toolbar->add_action(*view_as_icons_action); main_toolbar->add_action(*view_as_table_action); - directory_view->on_path_change = [window, location_textbox, &file_system_model, tree_view] (const String& new_path) { + directory_view->on_path_change = [window, location_textbox, &file_system_model, tree_view, &go_forward_action, &go_back_action, directory_view] (const String& new_path) { window->set_title(String::format("FileManager: %s", new_path.characters())); location_textbox->set_text(new_path); file_system_model->set_selected_index(file_system_model->index(new_path)); tree_view->scroll_into_view(file_system_model->selected_index(), Orientation::Vertical); tree_view->update(); + + go_forward_action->set_enabled(directory_view->path_history_position() + < directory_view->path_history_size() - 1); + go_back_action->set_enabled(directory_view->path_history_position() > 0); }; directory_view->on_status_message = [statusbar] (String message) {