FileManager: Change the cwd when opening a directory

The `open()` function of DirectoryView should change the current working
directory, so that the "Go to Location" menu item can process relative
paths correctly. Update other functions in DirectoryView to use `open()`
when opening a directory.
This commit is contained in:
TheFightingCatfish 2021-08-11 23:43:52 +08:00 committed by Andreas Kling
parent ce66c40160
commit d2af27d2d0
3 changed files with 17 additions and 20 deletions

View file

@ -355,17 +355,20 @@ void DirectoryView::add_path_to_history(String path)
m_path_history_position = m_path_history.size() - 1;
}
void DirectoryView::open(String const& path)
bool DirectoryView::open(String const& path)
{
auto real_path = Core::File::real_path_for(path);
if (real_path.is_null() || !Core::File::is_directory(path))
return false;
chdir(real_path.characters());
if (model().root_path() == real_path) {
model().invalidate();
return;
refresh();
} else {
set_active_widget(&current_view());
model().set_root_path(real_path);
}
set_active_widget(&current_view());
model().set_root_path(real_path);
return true;
}
void DirectoryView::set_status_message(StringView const& message)
@ -376,8 +379,7 @@ void DirectoryView::set_status_message(StringView const& message)
void DirectoryView::open_parent_directory()
{
auto path = String::formatted("{}/..", model().root_path());
model().set_root_path(path);
open("..");
}
void DirectoryView::refresh()
@ -387,19 +389,13 @@ void DirectoryView::refresh()
void DirectoryView::open_previous_directory()
{
if (m_path_history_position > 0) {
set_active_widget(&current_view());
m_path_history_position--;
model().set_root_path(m_path_history[m_path_history_position]);
}
if (m_path_history_position > 0)
open(m_path_history[--m_path_history_position]);
}
void DirectoryView::open_next_directory()
{
if (m_path_history_position < m_path_history.size() - 1) {
set_active_widget(&current_view());
m_path_history_position++;
model().set_root_path(m_path_history[m_path_history_position]);
}
if (m_path_history_position < m_path_history.size() - 1)
open(m_path_history[++m_path_history_position]);
}
void DirectoryView::update_statusbar()

View file

@ -49,7 +49,7 @@ public:
virtual ~DirectoryView() override;
void open(String const& path);
bool open(String const& path);
String path() const { return model().root_path(); }
void open_parent_directory();
void open_previous_directory();

View file

@ -495,7 +495,8 @@ int run_in_windowed_mode(String initial_location, String entry_focused_on_init)
progressbar.set_frame_thickness(1);
location_textbox.on_return_pressed = [&] {
directory_view.open(location_textbox.text());
if (directory_view.open(location_textbox.text()))
location_textbox.set_focus(false);
};
auto refresh_tree_view = [&] {