Skip to content

Commit

Permalink
add linux onfiledialog event
Browse files Browse the repository at this point in the history
  • Loading branch information
air authored and tishion committed Oct 8, 2023
1 parent b86fece commit 3a72a3a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/details/CCefClientDelegate_DialogHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ CCefClientDelegate::onFileDialog(CefRefPtr<CefBrowser> browser,
const std::vector<CefString>& accept_filters,
CefRefPtr<CefFileDialogCallback> callback)
{
#if defined (Q_OS_LINUX)
QMetaObject::invokeMethod(pCefViewPrivate_, [=]() {
pCefViewPrivate_->onFileDialog(browser, mode, title, default_file_path, accept_filters, callback);
});
return true;
#endif
return false;
}
74 changes: 74 additions & 0 deletions src/details/QCefViewPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#pragma region qt_headers
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QGridLayout>
#include <QInputMethodQueryEvent>
#include <QPainter>
Expand Down Expand Up @@ -581,6 +583,78 @@ QCefViewPrivate::onRunCefContextMenu(QPoint pos, CefRefPtr<CefRunContextMenuCall
qApp->sendEvent(q, e);
}

void
QCefViewPrivate::onFileDialog(CefRefPtr<CefBrowser> browser,
CefBrowserHost::FileDialogMode mode,
const CefString& title,
const CefString& default_file_path,
const std::vector<CefString>& accept_filters,
CefRefPtr<CefFileDialogCallback> callback)
{
Q_Q(QCefView);
QFileDialog dialog;
if (mode == FILE_DIALOG_OPEN) {
dialog.setFileMode(QFileDialog::ExistingFile);
} else if (mode == FILE_DIALOG_OPEN_MULTIPLE) {
dialog.setFileMode(QFileDialog::ExistingFiles);
} else if (mode == FILE_DIALOG_OPEN_FOLDER) {
dialog.setFileMode(QFileDialog::Directory);
} else if (mode == FILE_DIALOG_SAVE) {
dialog.setAcceptMode(QFileDialog::AcceptSave);
} else {
NOTREACHED();
callback->Cancel();
return;
}
QString title_str;
if (!title.empty()) {
title_str = title.ToString().c_str();
} else {
switch (mode) {
case FILE_DIALOG_OPEN:
title_str = "Open File";
break;
case FILE_DIALOG_OPEN_MULTIPLE:
title_str = "Open Files";
break;
case FILE_DIALOG_OPEN_FOLDER:
title_str = "Open Folder";
break;
case FILE_DIALOG_SAVE:
title_str = "Save File";
break;
default:
break;
}
}
dialog.setWindowTitle(title_str);
if (!default_file_path.empty() && mode == FILE_DIALOG_SAVE) {
QDir dir(QString::fromStdString(default_file_path.ToString()));
if (dir.exists()) {
dialog.setDirectory(dir);
} else {
dialog.setDirectory(QDir::homePath());
}
}
if (!accept_filters.empty()) {
QStringList filters;
for (const auto& filter : accept_filters) {
filters << "*" + QString::fromStdString(filter.ToString());
}
dialog.setNameFilter(QString("(%1)").arg(filters.join(" ")));
}
if (dialog.exec()) {
std::vector<CefString> file_paths;
auto selected_files = dialog.selectedFiles();
for (const auto& file : selected_files) {
file_paths.push_back(file.toStdString());
}
callback->Continue(file_paths);
} else {
callback->Cancel();
}
}

void
QCefViewPrivate::onCefContextMenuDismissed()
{
Expand Down
7 changes: 7 additions & 0 deletions src/details/QCefViewPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public slots:

void onRunCefContextMenu(QPoint pos, CefRefPtr<CefRunContextMenuCallback> callback);

void onFileDialog(CefRefPtr<CefBrowser> browser,
CefBrowserHost::FileDialogMode mode,
const CefString& title,
const CefString& default_file_path,
const std::vector<CefString>& accept_filters,
CefRefPtr<CefFileDialogCallback> callback);

void onCefContextMenuDismissed();

bool hasDevTools();
Expand Down

0 comments on commit 3a72a3a

Please sign in to comment.