diff options
author | Christopher Dumas <christopherdumas@gmail.com> | 2019-05-23 14:15:57 -0700 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-24 00:24:28 +0200 |
commit | 00075b1c8a8b0d51b0cc565f22cfba39414bd8c3 (patch) | |
tree | 51068cab9c8e3f363d0a3152be5fe0a46e998518 /Applications/FileManager | |
parent | fba57d6ba3f0b5fc56d01874df2f37a0d21a32c3 (diff) | |
download | serenity-00075b1c8a8b0d51b0cc565f22cfba39414bd8c3.zip |
Added functionality to make back and forward buttons work in FileManager. Also
fixed it so that directories don't get double-opened (first when they are
opened, and second when the selection changes to match in the file tree view)
Diffstat (limited to 'Applications/FileManager')
-rw-r--r-- | Applications/FileManager/DirectoryView.cpp | 29 | ||||
-rw-r--r-- | Applications/FileManager/DirectoryView.h | 8 | ||||
-rw-r--r-- | Applications/FileManager/main.cpp | 18 |
3 files changed, 50 insertions, 5 deletions
diff --git a/Applications/FileManager/DirectoryView.cpp b/Applications/FileManager/DirectoryView.cpp index 5265943129..cf8187ecd4 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 dec41cf1d1..58ff16d628 100644 --- a/Applications/FileManager/DirectoryView.h +++ b/Applications/FileManager/DirectoryView.h @@ -1,5 +1,6 @@ #pragma once +#include <AK/Vector.h> #include <LibGUI/GTableView.h> #include <LibGUI/GItemView.h> #include <LibGUI/GStackWidget.h> @@ -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<GDirectoryModel> m_model; + int m_path_history_position{ 0 }; + Vector<String> 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 e5634214a6..effa2eafaa 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<GMenuBar>(); @@ -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<GMenu>("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) { |