FullscreenUI: Use native file selector on Flatpak

This commit is contained in:
Stenzek
2024-05-12 20:22:37 +10:00
parent 37a76a020a
commit 522c2e3458
7 changed files with 121 additions and 6 deletions

View File

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "qthost.h"
@ -36,6 +36,7 @@
#include "util/audio_stream.h"
#include "util/http_downloader.h"
#include "util/imgui_fullscreen.h"
#include "util/imgui_manager.h"
#include "util/ini_settings_interface.h"
#include "util/input_manager.h"
@ -1566,6 +1567,60 @@ void Host::OnCoverDownloaderOpenRequested()
emit g_emu_thread->onCoverDownloaderOpenRequested();
}
bool Host::ShouldPreferHostFileSelector()
{
#ifdef __linux__
// If running inside a flatpak, we want to use native selectors/portals.
return (std::getenv("container") != nullptr);
#else
return false;
#endif
}
void Host::OpenHostFileSelectorAsync(std::string_view title, bool select_directory, FileSelectorCallback callback,
FileSelectorFilters filters /* = FileSelectorFilters() */,
std::string_view initial_directory /* = std::string_view() */)
{
const bool from_cpu_thread = g_emu_thread->isOnThread();
QString filters_str;
if (!filters.empty())
{
filters_str.append(QStringLiteral("All File Types (%1)")
.arg(QString::fromStdString(StringUtil::JoinString(filters.begin(), filters.end(), " "))));
for (const std::string& filter : filters)
{
filters_str.append(
QStringLiteral(";;%1 Files (%2)")
.arg(
QtUtils::StringViewToQString(std::string_view(filter).substr(filter.starts_with("*.") ? 2 : 0)).toUpper())
.arg(QString::fromStdString(filter)));
}
}
QtHost::RunOnUIThread([title = QtUtils::StringViewToQString(title), select_directory, callback = std::move(callback),
filters_str = std::move(filters_str),
initial_directory = QtUtils::StringViewToQString(initial_directory),
from_cpu_thread]() mutable {
auto lock = g_main_window->pauseAndLockSystem();
QString path;
if (select_directory)
path = QFileDialog::getExistingDirectory(lock.getDialogParent(), title, initial_directory);
else
path = QFileDialog::getOpenFileName(lock.getDialogParent(), title, initial_directory, filters_str);
if (!path.isEmpty())
path = QDir::toNativeSeparators(path);
if (from_cpu_thread)
Host::RunOnCPUThread([callback = std::move(callback), path = path.toStdString()]() { callback(path); });
else
callback(path.toStdString());
});
}
void EmuThread::doBackgroundControllerPoll()
{
System::Internal::IdlePollUpdate();