1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibGUI: Traverse into directory upon filename entry in FilePicker

Previously, typing e.g. `/home/anon` in the filename field of the
FilePicker resulted in an error for applications expecting an existing
file to open. Intuitively I expected the file picker to navigate to the
directory I typed there, similar to what we have with the location text
box at the top, so I changed it to do exactly that :^)
This commit is contained in:
Jelle Raaijmakers 2023-08-06 21:12:48 +02:00 committed by Andreas Kling
parent c985a1b2af
commit 9466512847

View file

@ -32,7 +32,6 @@
#include <LibGUI/Widget.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Palette.h>
#include <string.h>
#include <unistd.h>
namespace GUI {
@ -340,11 +339,11 @@ void FilePicker::model_did_update(unsigned)
void FilePicker::on_file_return()
{
auto path = m_filename_textbox->text();
if (!path.starts_with('/')) {
if (!path.starts_with('/'))
path = LexicalPath::join(m_model->root_path(), path).string();
}
bool file_exists = FileSystem::exists(path);
auto stat_or_error = Core::System::stat(path);
bool file_exists = !stat_or_error.is_error();
if (!file_exists && (m_mode == Mode::Open || m_mode == Mode::OpenFolder)) {
(void)MessageBox::try_show_error(this, DeprecatedString::formatted("Opening \"{}\" failed: {}", m_filename_textbox->text(), Error::from_errno(ENOENT)));
@ -360,6 +359,13 @@ void FilePicker::on_file_return()
return;
}
// If the entered filename matches an existing directory, traverse into it
if (file_exists && m_mode != Mode::OpenFolder && S_ISDIR(stat_or_error.value().st_mode)) {
m_filename_textbox->clear();
set_path(path);
return;
}
m_selected_file = path;
done(ExecResult::OK);
}