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)
This commit is contained in:
Christopher Dumas 2019-05-23 14:15:57 -07:00 committed by Andreas Kling
parent fba57d6ba3
commit 00075b1c8a
Notes: sideshowbarker 2024-07-19 13:58:47 +09:00
3 changed files with 50 additions and 5 deletions

View file

@ -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]);
}
}

View file

@ -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 };

View file

@ -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) {