Host: Allow button title override in ConfirmMessageAsync()

This commit is contained in:
Stenzek 2025-01-27 15:10:34 +10:00
parent 4dd6365a99
commit 128bab29f8
No known key found for this signature in database
3 changed files with 41 additions and 20 deletions

View File

@ -1976,47 +1976,66 @@ bool Host::ConfirmMessage(std::string_view title, std::string_view message)
QString::fromUtf8(message.data(), message.size())); QString::fromUtf8(message.data(), message.size()));
} }
void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback) void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback,
std::string_view yes_text, std::string_view no_text)
{ {
INFO_LOG("ConfirmMessageAsync({}, {})", title, message); INFO_LOG("ConfirmMessageAsync({}, {})", title, message);
// This is a racey read, but whether FSUI is started should be visible on all threads. // This is a racey read, but whether FSUI is started should be visible on all threads.
std::atomic_thread_fence(std::memory_order_acquire); std::atomic_thread_fence(std::memory_order_acquire);
// Default button titles.
if (yes_text.empty())
yes_text = TRANSLATE_SV("QtHost", "Yes");
if (no_text.empty())
no_text = TRANSLATE_SV("QtHost", "No");
// Use FSUI to display the confirmation if it is active. // Use FSUI to display the confirmation if it is active.
if (FullscreenUI::IsInitialized()) if (FullscreenUI::IsInitialized())
{ {
// This.. should not be a thing. // This.. should not be a thing.
if (!g_emu_thread->isCurrentThread()) if (!g_emu_thread->isCurrentThread())
{ {
Host::RunOnCPUThread( Host::RunOnCPUThread([title = std::string(title), message = std::string(message), callback = std::move(callback),
[title = std::string(title), message = std::string(message), callback = std::move(callback)]() mutable { yes_text = std::string(yes_text), no_text = std::string(no_text)]() mutable {
ConfirmMessageAsync(title, message, std::move(callback)); ConfirmMessageAsync(title, message, std::move(callback));
}); });
return; return;
} }
GPUThread::RunOnThread( GPUThread::RunOnThread([title = std::string(title), message = std::string(message), callback = std::move(callback),
[title = std::string(title), message = std::string(message), callback = std::move(callback)]() mutable { yes_text = std::string(yes_text), no_text = std::string(no_text)]() mutable {
if (!FullscreenUI::Initialize()) if (!FullscreenUI::Initialize())
{ {
callback(false); callback(false);
return; return;
} }
ImGuiFullscreen::OpenConfirmMessageDialog(std::move(title), std::move(message), std::move(callback), ImGuiFullscreen::OpenConfirmMessageDialog(std::move(title), std::move(message), std::move(callback),
fmt::format(ICON_FA_CHECK " {}", TRANSLATE_SV("QtHost", "Yes")), fmt::format(ICON_FA_CHECK " {}", yes_text),
fmt::format(ICON_FA_TIMES " {}", TRANSLATE_SV("QtHost", "No"))); fmt::format(ICON_FA_TIMES " {}", no_text));
}); });
} }
else else
{ {
// Otherwise, use the desktop UI. // Otherwise, use the desktop UI.
QtHost::RunOnUIThread([title = QtUtils::StringViewToQString(title), message = QtUtils::StringViewToQString(message), QtHost::RunOnUIThread([title = QtUtils::StringViewToQString(title), message = QtUtils::StringViewToQString(message),
callback = std::move(callback)]() mutable { callback = std::move(callback), yes_text = QtUtils::StringViewToQString(yes_text),
no_text = QtUtils::StringViewToQString(no_text)]() mutable {
auto lock = g_main_window->pauseAndLockSystem(); auto lock = g_main_window->pauseAndLockSystem();
const bool result = (QMessageBox::question(lock.getDialogParent(), title, message) != QMessageBox::No); bool result;
{
QMessageBox msgbox(lock.getDialogParent());
msgbox.setIcon(QMessageBox::Question);
msgbox.setWindowTitle(title);
msgbox.setText(message);
QPushButton* const yes_button = msgbox.addButton(yes_text, QMessageBox::AcceptRole);
msgbox.addButton(no_text, QMessageBox::RejectRole);
msgbox.exec();
result = (msgbox.clickedButton() == yes_button);
}
callback(result); callback(result);
}); });

View File

@ -162,7 +162,8 @@ bool Host::ConfirmMessage(std::string_view title, std::string_view message)
return true; return true;
} }
void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback) void Host::ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback,
std::string_view yes_text, std::string_view no_text)
{ {
if (!title.empty() && !message.empty()) if (!title.empty() && !message.empty())
ERROR_LOG("ConfirmMessage: {}: {}", title, message); ERROR_LOG("ConfirmMessage: {}: {}", title, message);

View File

@ -48,7 +48,8 @@ bool ConfirmMessage(std::string_view title, std::string_view message);
/// Displays an asynchronous confirmation on the UI thread, but does not block the caller. /// 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. /// The callback may be executed on a different thread. Use RunOnCPUThread() in the callback to ensure safety.
using ConfirmMessageAsyncCallback = std::function<void(bool)>; using ConfirmMessageAsyncCallback = std::function<void(bool)>;
void ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback); void ConfirmMessageAsync(std::string_view title, std::string_view message, ConfirmMessageAsyncCallback callback,
std::string_view yes_text = std::string_view(), std::string_view no_text = std::string_view());
/// Returns the user agent to use for HTTP requests. /// Returns the user agent to use for HTTP requests.
std::string GetHTTPUserAgent(); std::string GetHTTPUserAgent();