Ladybird/Qt: Implement the <input type=file> UI

This commit is contained in:
Timothy Flynn 2024-02-25 13:09:26 -05:00 committed by Andreas Kling
parent ab4d4f0711
commit 834f76ef24
Notes: sideshowbarker 2024-07-17 00:37:23 +09:00

View file

@ -14,6 +14,7 @@
#include <AK/TemporaryChange.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
#include <LibGfx/Painter.h>
#include <LibWeb/HTML/SelectedFile.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/SourceHighlighter.h>
#include <LibWebView/URL.h>
@ -231,6 +232,32 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St
m_dialog = nullptr;
};
view().on_request_file_picker = [this](auto allow_multiple_files) {
Vector<Web::HTML::SelectedFile> selected_files;
auto create_selected_file = [&](auto const& qfile_path) {
auto file_path = ak_byte_string_from_qstring(qfile_path);
if (auto file = Web::HTML::SelectedFile::from_file_path(file_path); file.is_error())
warnln("Unable to open file {}: {}", file_path, file.error());
else
selected_files.append(file.release_value());
};
if (allow_multiple_files == Web::HTML::AllowMultipleFiles::Yes) {
auto paths = QFileDialog::getOpenFileNames(this, "Select files", QDir::homePath(), "All Files (*.*)");
selected_files.ensure_capacity(static_cast<size_t>(paths.size()));
for (auto const& path : paths)
create_selected_file(path);
} else {
auto path = QFileDialog::getOpenFileName(this, "Select file", QDir::homePath(), "All Files (*.*)");
create_selected_file(path);
}
view().file_picker_closed(std::move(selected_files));
};
m_select_dropdown = new QMenu("Select Dropdown", this);
QObject::connect(m_select_dropdown, &QMenu::aboutToHide, this, [this]() {
if (!m_select_dropdown->activeAction())