GH-1645 reimplement open/close instance window based on settings

This commit is contained in:
Petr Mrázek 2016-11-01 01:25:04 +01:00
parent 4b03dfcbd7
commit fe68d59460
8 changed files with 122 additions and 79 deletions

View File

@ -90,10 +90,8 @@ InstanceWindow::InstanceWindow(InstancePtr instance, QWidget *parent)
{ {
auto launchTask = m_instance->getLaunchTask(); auto launchTask = m_instance->getLaunchTask();
on_InstanceLaunchTask_changed(launchTask); on_InstanceLaunchTask_changed(launchTask);
connect(m_instance.get(), &BaseInstance::launchTaskChanged, connect(m_instance.get(), &BaseInstance::launchTaskChanged, this, &InstanceWindow::on_InstanceLaunchTask_changed);
this, &InstanceWindow::on_InstanceLaunchTask_changed); connect(m_instance.get(), &BaseInstance::runningStatusChanged, this, &InstanceWindow::on_RunningState_changed);
connect(m_instance.get(), &BaseInstance::runningStatusChanged,
this, &InstanceWindow::on_RunningState_changed);
} }
// set up instance destruction detection // set up instance destruction detection
@ -128,22 +126,7 @@ void InstanceWindow::setKillButton(bool kill)
void InstanceWindow::on_InstanceLaunchTask_changed(std::shared_ptr<LaunchTask> proc) void InstanceWindow::on_InstanceLaunchTask_changed(std::shared_ptr<LaunchTask> proc)
{ {
if(m_proc)
{
disconnect(m_proc.get(), &LaunchTask::succeeded, this, &InstanceWindow::onSucceeded);
disconnect(m_proc.get(), &LaunchTask::failed, this, &InstanceWindow::onFailed);
disconnect(m_proc.get(), &LaunchTask::requestProgress, this, &InstanceWindow::onProgressRequested);
}
m_proc = proc; m_proc = proc;
if(m_proc)
{
// Set up signal connections
connect(m_proc.get(), &LaunchTask::succeeded, this, &InstanceWindow::onSucceeded);
connect(m_proc.get(), &LaunchTask::failed, this, &InstanceWindow::onFailed);
connect(m_proc.get(), &LaunchTask::requestProgress, this, &InstanceWindow::onProgressRequested);
}
} }
void InstanceWindow::on_RunningState_changed(bool running) void InstanceWindow::on_RunningState_changed(bool running)
@ -202,34 +185,6 @@ void InstanceWindow::on_btnKillMinecraft_clicked()
} }
} }
void InstanceWindow::onSucceeded()
{
if (m_instance->settings()->get("AutoCloseConsole").toBool() && m_container->prepareToClose())
{
this->close();
return;
}
// Raise Window
if (MMC->settings()->get("RaiseConsole").toBool())
{
show();
raise();
activateWindow();
}
}
void InstanceWindow::onFailed(QString reason)
{
}
void InstanceWindow::onProgressRequested(Task* task)
{
ProgressDialog progDialog(this);
progDialog.setSkipButton(true, tr("Abort"));
m_proc->proceed();
progDialog.execWithTask(task);
}
QString InstanceWindow::instanceId() QString InstanceWindow::instanceId()
{ {
return m_instance->id(); return m_instance->id();

View File

@ -47,10 +47,6 @@ slots:
void on_closeButton_clicked(); void on_closeButton_clicked();
void on_btnKillMinecraft_clicked(); void on_btnKillMinecraft_clicked();
void onSucceeded();
void onFailed(QString reason);
void onProgressRequested(Task *task);
void on_InstanceLaunchTask_changed(std::shared_ptr<LaunchTask> proc); void on_InstanceLaunchTask_changed(std::shared_ptr<LaunchTask> proc);
void on_RunningState_changed(bool running); void on_RunningState_changed(bool running);
void on_instanceStatusChanged(BaseInstance::Status, BaseInstance::Status newStatus); void on_instanceStatusChanged(BaseInstance::Status, BaseInstance::Status newStatus);

View File

@ -206,11 +206,15 @@ void LaunchController::launchInstance()
} }
auto console = qobject_cast<InstanceWindow *>(m_parentWidget); auto console = qobject_cast<InstanceWindow *>(m_parentWidget);
if(!console) if(!console && m_showConsole)
{ {
MMC->showInstanceWindow(m_instance); MMC->showInstanceWindow(m_instance);
} }
connect(m_launcher.get(), &LaunchTask::readyForLaunch, this, &LaunchController::readyForLaunch); connect(m_launcher.get(), &LaunchTask::readyForLaunch, this, &LaunchController::readyForLaunch);
connect(m_launcher.get(), &LaunchTask::succeeded, this, &LaunchController::onSucceeded);
connect(m_launcher.get(), &LaunchTask::failed, this, &LaunchController::onFailed);
connect(m_launcher.get(), &LaunchTask::requestProgress, this, &LaunchController::onProgressRequested);
m_launcher->prependStep(std::make_shared<TextPrint>(m_launcher.get(), "MultiMC version: " + BuildConfig.printableVersionString() + "\n\n", MessageLevel::MultiMC)); m_launcher->prependStep(std::make_shared<TextPrint>(m_launcher.get(), "MultiMC version: " + BuildConfig.printableVersionString() + "\n\n", MessageLevel::MultiMC));
m_launcher->start(); m_launcher->start();
@ -221,7 +225,6 @@ void LaunchController::readyForLaunch()
if (!m_profiler) if (!m_profiler)
{ {
m_launcher->proceed(); m_launcher->proceed();
emitSucceeded();
return; return;
} }
@ -247,7 +250,6 @@ void LaunchController::readyForLaunch()
msg.setModal(true); msg.setModal(true);
msg.exec(); msg.exec();
m_launcher->proceed(); m_launcher->proceed();
emitSucceeded();
}); });
connect(profilerInstance, &BaseProfiler::abortLaunch, [this](const QString & message) connect(profilerInstance, &BaseProfiler::abortLaunch, [this](const QString & message)
{ {
@ -263,3 +265,21 @@ void LaunchController::readyForLaunch()
}); });
profilerInstance->beginProfiling(m_launcher); profilerInstance->beginProfiling(m_launcher);
} }
void LaunchController::onSucceeded()
{
emitSucceeded();
}
void LaunchController::onFailed(QString reason)
{
emitFailed(reason);
}
void LaunchController::onProgressRequested(Task* task)
{
ProgressDialog progDialog(m_parentWidget);
progDialog.setSkipButton(true, tr("Abort"));
m_launcher->proceed();
progDialog.execWithTask(task);
}

View File

@ -17,6 +17,10 @@ public:
{ {
m_instance = instance; m_instance = instance;
} }
InstancePtr instance()
{
return m_instance;
}
void setOnline(bool online) void setOnline(bool online)
{ {
m_online = online; m_online = online;
@ -29,6 +33,14 @@ public:
{ {
m_parentWidget = widget; m_parentWidget = widget;
} }
void setShowConsole(bool showConsole)
{
m_showConsole = showConsole;
}
QString id()
{
return m_instance->id();
}
private: private:
void login(); void login();
@ -37,9 +49,14 @@ private:
private slots: private slots:
void readyForLaunch(); void readyForLaunch();
void onSucceeded();
void onFailed(QString reason);
void onProgressRequested(Task *task);
private: private:
BaseProfilerFactory *m_profiler = nullptr; BaseProfilerFactory *m_profiler = nullptr;
bool m_online = true; bool m_online = true;
bool m_showConsole = false;
InstancePtr m_instance; InstancePtr m_instance;
QWidget * m_parentWidget = nullptr; QWidget * m_parentWidget = nullptr;
InstanceWindow *m_console = nullptr; InstanceWindow *m_console = nullptr;

View File

@ -547,7 +547,6 @@ void MultiMC::initGlobalSettings()
// Console // Console
m_settings->registerSetting("ShowConsole", true); m_settings->registerSetting("ShowConsole", true);
m_settings->registerSetting("RaiseConsole", true);
m_settings->registerSetting("AutoCloseConsole", true); m_settings->registerSetting("AutoCloseConsole", true);
m_settings->registerSetting("LogPrePostOutput", true); m_settings->registerSetting("LogPrePostOutput", true);
@ -1072,25 +1071,32 @@ void MultiMC::launch(InstancePtr instance, bool online, BaseProfilerFactory *pro
{ {
if(instance->canLaunch()) if(instance->canLaunch())
{ {
m_launchController.reset(new LaunchController()); auto & extras = m_instanceExtras[instance->id()];
m_launchController->setInstance(instance); auto & window = extras.window;
m_launchController->setOnline(online); if(window)
m_launchController->setProfiler(profiler);
auto windowIter = m_instanceWindows.find(instance->id());
if(windowIter != m_instanceWindows.end())
{ {
auto window = *windowIter;
if(!window->saveAll()) if(!window->saveAll())
{ {
return; return;
} }
m_launchController->setParentWidget(window);
} }
if(m_mainWindow) auto & controller = extras.controller;
controller.reset(new LaunchController());
controller->setInstance(instance);
controller->setOnline(online);
controller->setProfiler(profiler);
controller->setShowConsole(instance->settings()->get("ShowConsole").toBool());
if(window)
{ {
m_launchController->setParentWidget(m_mainWindow); controller->setParentWidget(window);
} }
m_launchController->start(); else if(m_mainWindow)
{
controller->setParentWidget(m_mainWindow);
}
connect(controller.get(), &LaunchController::succeeded, this, &MultiMC::controllerSucceeded);
connect(controller.get(), &LaunchController::failed, this, &MultiMC::controllerFailed);
controller->start();
} }
else if (instance->isRunning()) else if (instance->isRunning())
{ {
@ -1098,6 +1104,37 @@ void MultiMC::launch(InstancePtr instance, bool online, BaseProfilerFactory *pro
} }
} }
void MultiMC::controllerSucceeded()
{
auto controller = qobject_cast<LaunchController *>(QObject::sender());
if(!controller)
return;
auto id = controller->id();
auto & extras = m_instanceExtras[id];
// on success, do...
if (controller->instance()->settings()->get("AutoCloseConsole").toBool())
{
if(extras.window)
{
extras.window->close();
}
}
extras.controller.reset();
}
void MultiMC::controllerFailed(const QString& error)
{
Q_UNUSED(error);
auto controller = qobject_cast<LaunchController *>(QObject::sender());
if(!controller)
return;
auto id = controller->id();
auto & extras = m_instanceExtras[id];
// on failure, do... nothing
extras.controller.reset();
}
MainWindow * MultiMC::showMainWindow() MainWindow * MultiMC::showMainWindow()
{ {
if(m_mainWindow) if(m_mainWindow)
@ -1123,34 +1160,43 @@ InstanceWindow *MultiMC::showInstanceWindow(InstancePtr instance, QString page)
if(!instance) if(!instance)
return nullptr; return nullptr;
auto id = instance->id(); auto id = instance->id();
InstanceWindow * window = nullptr; auto & extras = m_instanceExtras[id];
auto & window = extras.window;
auto iter = m_instanceWindows.find(id); if(window)
if(iter != m_instanceWindows.end())
{ {
window = *iter;
window->raise(); window->raise();
window->activateWindow(); window->activateWindow();
} }
else else
{ {
window = new InstanceWindow(instance); window = new InstanceWindow(instance);
m_instanceWindows[id] = window;
connect(window, &InstanceWindow::isClosing, this, &MultiMC::on_windowClose); connect(window, &InstanceWindow::isClosing, this, &MultiMC::on_windowClose);
} }
if(!page.isEmpty()) if(!page.isEmpty())
{ {
window->selectPage(page); window->selectPage(page);
} }
m_openWindows ++;
if(extras.controller)
{
extras.controller->setParentWidget(window);
}
return window; return window;
} }
void MultiMC::on_windowClose() void MultiMC::on_windowClose()
{ {
m_openWindows--;
auto instWindow = qobject_cast<InstanceWindow *>(QObject::sender()); auto instWindow = qobject_cast<InstanceWindow *>(QObject::sender());
if(instWindow) if(instWindow)
{ {
m_instanceWindows.remove(instWindow->instanceId()); auto & extras = m_instanceExtras[instWindow->instanceId()];
extras.window = nullptr;
if(extras.controller)
{
extras.controller->setParentWidget(m_mainWindow);
}
return; return;
} }
auto mainWindow = qobject_cast<MainWindow *>(QObject::sender()); auto mainWindow = qobject_cast<MainWindow *>(QObject::sender());
@ -1159,7 +1205,7 @@ void MultiMC::on_windowClose()
m_mainWindow = nullptr; m_mainWindow = nullptr;
} }
// quit when there are no more windows. // quit when there are no more windows.
if(m_instanceWindows.isEmpty() && !m_mainWindow) if(m_openWindows == 0)
{ {
quit(); quit();
} }

View File

@ -154,6 +154,9 @@ private slots:
void messageReceived(const QString & message); void messageReceived(const QString & message);
void controllerSucceeded();
void controllerFailed(const QString & error);
private: private:
void initLogger(); void initLogger();
void initIcons(); void initIcons();
@ -167,7 +170,6 @@ private:
private: private:
QDateTime startTime; QDateTime startTime;
unique_qobject_ptr<LaunchController> m_launchController;
std::shared_ptr<QTranslator> m_qt_translator; std::shared_ptr<QTranslator> m_qt_translator;
std::shared_ptr<QTranslator> m_mmc_translator; std::shared_ptr<QTranslator> m_mmc_translator;
std::shared_ptr<SettingsObject> m_settings; std::shared_ptr<SettingsObject> m_settings;
@ -189,16 +191,24 @@ private:
QMap<QString, std::shared_ptr<BaseDetachedToolFactory>> m_tools; QMap<QString, std::shared_ptr<BaseDetachedToolFactory>> m_tools;
QString m_rootPath; QString m_rootPath;
Status m_status = MultiMC::Failed; Status m_status = MultiMC::Failed;
// used on Windows to attach the standard IO streams // used on Windows to attach the standard IO streams
bool consoleAttached = false; bool consoleAttached = false;
// map from instance ID to its window // FIXME: attach to instances instead.
QMap<QString, InstanceWindow *> m_instanceWindows; struct InstanceXtras
{
InstanceWindow * window = nullptr;
unique_qobject_ptr<LaunchController> controller;
};
std::map<QString, InstanceXtras> m_instanceExtras;
size_t m_openWindows = 0;
// main window, if any // main window, if any
MainWindow * m_mainWindow = nullptr; MainWindow * m_mainWindow = nullptr;
// peer MultiMC instance connector - used to implement single instance MultiMC and signalling
LocalPeer * m_peerInstance = nullptr; LocalPeer * m_peerInstance = nullptr;
public: public:
QString m_instanceIdToLaunch; QString m_instanceIdToLaunch;

View File

@ -94,7 +94,7 @@ bool PageContainer::selectPage(QString pageId)
{ {
index = m_proxyModel->mapFromSource(m_model->index(page->listIndex)); index = m_proxyModel->mapFromSource(m_model->index(page->listIndex));
} }
else if(!index.isValid())
{ {
index = m_proxyModel->index(0, 0); index = m_proxyModel->index(0, 0);
} }

View File

@ -52,7 +52,6 @@ void WonkoClient::runTask(Task *pTask) {
void WonkoClient::initGlobalSettings() void WonkoClient::initGlobalSettings()
{ {
m_settings->registerSetting("ShowConsole", true); m_settings->registerSetting("ShowConsole", true);
m_settings->registerSetting("RaiseConsole", true);
m_settings->registerSetting("AutoCloseConsole", true); m_settings->registerSetting("AutoCloseConsole", true);
m_settings->registerSetting("LogPrePostOutput", true); m_settings->registerSetting("LogPrePostOutput", true);
// Window Size // Window Size