From 94665128476439a3d86102c66fc041b2a5e5e81c Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Sun, 6 Aug 2023 21:12:48 +0200 Subject: [PATCH] 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 :^) --- Userland/Libraries/LibGUI/FilePicker.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibGUI/FilePicker.cpp b/Userland/Libraries/LibGUI/FilePicker.cpp index 368570ff8a..9e974e9b06 100644 --- a/Userland/Libraries/LibGUI/FilePicker.cpp +++ b/Userland/Libraries/LibGUI/FilePicker.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include 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); }