Qt: Support macOS file open events

This commit is contained in:
TellowKrinkle 2025-06-10 19:32:13 -05:00 committed by Ty
parent 0e649bc70b
commit fe71fc6a30
3 changed files with 38 additions and 20 deletions

View File

@ -2168,57 +2168,56 @@ void MainWindow::dragEnterEvent(QDragEnterEvent* event)
}
void MainWindow::dropEvent(QDropEvent* event)
{
if (startFile(getFilenameFromMimeData(event->mimeData())))
event->acceptProposedAction();
}
bool MainWindow::startFile(const QString& filename)
{
const auto mcLock = pauseAndLockVM();
// Check if memcard is busy, deny request if so
if (shouldAbortForMemcardBusy(mcLock))
{
return;
return false;
}
const QString filename(getFilenameFromMimeData(event->mimeData()));
const std::string filename_str(filename.toStdString());
std::string filename_str = filename.toStdString();
if (VMManager::IsSaveStateFileName(filename_str))
{
event->acceptProposedAction();
// can't load a save state without a current VM
if (s_vm_valid)
g_emu_thread->loadState(filename);
else
QMessageBox::critical(this, tr("Load State Failed"), tr("Cannot load a save state without a running VM."));
return;
return true;
}
if (!VMManager::IsLoadableFileName(filename_str))
return;
return false;
// if we're already running, do a disc change, otherwise start
if (!s_vm_valid)
{
event->acceptProposedAction();
doStartFile(std::nullopt, filename);
return;
return true;
}
if (VMManager::IsDiscFileName(filename_str) || VMManager::IsBlockDumpFileName(filename_str))
{
event->acceptProposedAction();
doDiscChange(CDVD_SourceType::Iso, filename);
}
else if (VMManager::IsElfFileName(filename_str))
{
const auto lock = pauseAndLockVM();
event->acceptProposedAction();
if (QMessageBox::question(this, tr("Confirm Reset"),
tr("The new ELF cannot be loaded without resetting the virtual machine. Do you want to reset the virtual machine now?")) !=
QMessageBox::Yes)
if (QMessageBox::Yes != QMessageBox::question(this, tr("Confirm Reset"),
tr("The new ELF cannot be loaded without resetting the virtual machine. Do you want to reset the virtual machine now?")))
{
return;
return true;
}
g_emu_thread->setELFOverride(filename);
@ -2226,17 +2225,15 @@ void MainWindow::dropEvent(QDropEvent* event)
}
else if (VMManager::IsGSDumpFileName(filename_str))
{
event->acceptProposedAction();
if (!GSDumpReplayer::IsReplayingDump())
{
QMessageBox::critical(this, tr("Error"), tr("Cannot change from game to GS dump without shutting down first."));
return;
}
g_emu_thread->changeGSDump(filename);
switchToEmulationView();
}
return true;
}
void MainWindow::moveEvent(QMoveEvent* event)

View File

@ -103,6 +103,9 @@ public:
/// Rescans a single file. NOTE: Happens on UI thread.
void rescanFile(const std::string& path);
/// Start a file from a user action (e.g. dragging a file onto the main window or with macOS open with)
bool startFile(const QString& path);
void doSettings(const char* category = nullptr);
void doGameSettings(const char* category = nullptr);

View File

@ -2327,6 +2327,24 @@ bool QtHost::RunSetupWizard()
return true;
}
class PCSX2MainApplication : public QApplication {
public:
using QApplication::QApplication;
bool event(QEvent* event) override {
if (event->type() == QEvent::FileOpen)
{
QFileOpenEvent* open = static_cast<QFileOpenEvent*>(event);
const QUrl url = open->url();
if (url.isLocalFile())
return g_main_window->startFile(url.toLocalFile());
else
return false; // No URL schemas currently supported
}
return QApplication::event(event);
}
};
int main(int argc, char* argv[])
{
CrashHandler::Install();
@ -2336,7 +2354,7 @@ int main(int argc, char* argv[])
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
QtHost::RegisterTypes();
QApplication app(argc, argv);
PCSX2MainApplication app(argc, argv);
#ifndef _WIN32
if (!PerformEarlyHardwareChecks())