Host: Add ConfirmMessageAsync()

This commit is contained in:
Stenzek 2024-12-09 20:24:25 +10:00
parent 42535591bc
commit 1bea8817f1
No known key found for this signature in database
3 changed files with 28 additions and 0 deletions
src
duckstation-qt
duckstation-regtest
util

View File

@ -1917,6 +1917,18 @@ bool Host::ConfirmMessage(std::string_view title, std::string_view message)
QString::fromUtf8(message.data(), message.size()));
}
void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback)
{
QtHost::RunOnUIThread([title = QtUtils::StringViewToQString(title), message = QtUtils::StringViewToQString(message),
callback = std::move(callback)]() mutable {
auto lock = g_main_window->pauseAndLockSystem();
const bool result = (QMessageBox::question(lock.getDialogParent(), title, message) != QMessageBox::No);
callback(result);
});
}
void Host::OpenURL(std::string_view url)
{
QtHost::RunOnUIThread([url = QtUtils::StringViewToQString(url)]() { QtUtils::OpenURL(g_main_window, QUrl(url)); });

View File

@ -150,6 +150,16 @@ bool Host::ConfirmMessage(std::string_view title, std::string_view message)
return true;
}
void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback)
{
if (!title.empty() && !message.empty())
ERROR_LOG("ConfirmMessage: {}: {}", title, message);
else if (!message.empty())
ERROR_LOG("ConfirmMessage: {}", message);
callback(true);
}
void Host::ReportDebuggerMessage(std::string_view message)
{
ERROR_LOG("ReportDebuggerMessage: {}", message);

View File

@ -8,6 +8,7 @@
#include "common/types.h"
#include <ctime>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
@ -44,6 +45,11 @@ void ReportErrorAsync(std::string_view title, std::string_view message);
/// Displays a synchronous confirmation on the UI thread, i.e. blocks the caller.
bool ConfirmMessage(std::string_view title, std::string_view message);
/// Displays an asynchronous confirmation on the UI thread, but does not block the caller.
/// The callback may be executed on a different thread. Use RunOnCPUThread() in the callback to ensure safety.
using ConfirmMessageAsyncCallback = std::function<void(bool)>;
void ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback);
/// Returns the user agent to use for HTTP requests.
std::string GetHTTPUserAgent();