Merge pull request #9814 from igorkorsukov/multiinst/double_create
Fixed creation of multiple windows when clicking on 'new project' multiple times
This commit is contained in:
commit
a13cebfdea
8 changed files with 163 additions and 5 deletions
|
@ -84,6 +84,7 @@ set(MODULE_SRC
|
|||
${CMAKE_CURRENT_LIST_DIR}/xmlwriter.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/utils.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/utils.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/defer.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/internal/globalconfiguration.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/internal/globalconfiguration.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/internal/interactive.cpp
|
||||
|
|
44
src/framework/global/defer.h
Normal file
44
src/framework/global/defer.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
* MuseScore-CLA-applies
|
||||
*
|
||||
* MuseScore
|
||||
* Music Composition & Notation
|
||||
*
|
||||
* Copyright (C) 2021 MuseScore BVBA and others
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 3 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef MU_GLOBAL_DEFER_H
|
||||
#define MU_GLOBAL_DEFER_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace mu {
|
||||
struct Defer
|
||||
{
|
||||
Defer(const std::function<void()>& f)
|
||||
: f(f) {}
|
||||
|
||||
~Defer()
|
||||
{
|
||||
if (f) {
|
||||
f();
|
||||
}
|
||||
}
|
||||
|
||||
std::function<void()> f;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // MU_GLOBAL_DEFER_H
|
|
@ -41,6 +41,8 @@ public:
|
|||
// Project opening
|
||||
virtual bool isProjectAlreadyOpened(const io::path& projectPath) const = 0;
|
||||
virtual void activateWindowWithProject(const io::path& projectPath) = 0;
|
||||
virtual bool isHasAppInstanceWithoutProject() const = 0;
|
||||
virtual void activateWindowWithoutProject() = 0;
|
||||
virtual bool openNewAppInstance(const QStringList& args) = 0;
|
||||
|
||||
// Settings
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
#include <QProcess>
|
||||
#include <QCoreApplication>
|
||||
#include <QTimer>
|
||||
#include <QEventLoop>
|
||||
|
||||
#include "uri.h"
|
||||
#include "settings.h"
|
||||
|
@ -35,6 +37,8 @@ using namespace mu::framework;
|
|||
static const mu::UriQuery DEV_SHOW_INFO_URI("musescore://devtools/multiinstances/info?sync=false&modal=false");
|
||||
static const QString METHOD_PROJECT_IS_OPENED("PROJECT_IS_OPENED");
|
||||
static const QString METHOD_ACTIVATE_WINDOW_WITH_PROJECT("ACTIVATE_WINDOW_WITH_PROJECT");
|
||||
static const QString METHOD_IS_WITHOUT_PROJECT("IS_WITHOUT_PROJECT");
|
||||
static const QString METHOD_ACTIVATE_WINDOW_WITHOUT_PROJECT("METHOD_ACTIVATE_WINDOW_WITHOUT_PROJECT");
|
||||
|
||||
static const mu::Uri PREFERENCES_URI("musescore://preferences");
|
||||
static const QString METHOD_PREFERENCES_IS_OPENED("PREFERENCES_IS_OPENED");
|
||||
|
@ -78,7 +82,7 @@ void MultiInstancesProvider::onMsg(const Msg& msg)
|
|||
|
||||
#define CHECK_ARGS_COUNT(c) IF_ASSERT_FAILED(msg.args.count() >= c) { return; }
|
||||
|
||||
// Score opening
|
||||
// Project opening
|
||||
if (msg.type == MsgType::Request && msg.method == METHOD_PROJECT_IS_OPENED) {
|
||||
CHECK_ARGS_COUNT(1);
|
||||
io::path scorePath = io::path(msg.args.at(0));
|
||||
|
@ -91,6 +95,14 @@ void MultiInstancesProvider::onMsg(const Msg& msg)
|
|||
if (isOpened) {
|
||||
mainWindow()->requestShowOnFront();
|
||||
}
|
||||
} else if (msg.type == MsgType::Request && msg.method == METHOD_IS_WITHOUT_PROJECT) {
|
||||
bool isAnyOpened = projectFilesController()->isAnyProjectOpened();
|
||||
m_ipcChannel->response(METHOD_IS_WITHOUT_PROJECT, { QString::number(!isAnyOpened) }, msg.srcID);
|
||||
} else if (msg.method == METHOD_ACTIVATE_WINDOW_WITHOUT_PROJECT) {
|
||||
bool isAnyOpened = projectFilesController()->isAnyProjectOpened();
|
||||
if (!isAnyOpened) {
|
||||
mainWindow()->requestShowOnFront();
|
||||
}
|
||||
}
|
||||
// Settings
|
||||
else if (msg.type == MsgType::Request && msg.method == METHOD_PREFERENCES_IS_OPENED) {
|
||||
|
@ -149,19 +161,81 @@ void MultiInstancesProvider::activateWindowWithProject(const io::path& projectPa
|
|||
m_ipcChannel->broadcast(METHOD_ACTIVATE_WINDOW_WITH_PROJECT, { projectPath.toQString() });
|
||||
}
|
||||
|
||||
bool MultiInstancesProvider::isHasAppInstanceWithoutProject() const
|
||||
{
|
||||
if (!isInited()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
m_ipcChannel->syncRequestToAll(METHOD_IS_WITHOUT_PROJECT, {}, [&ret](const QStringList& args) {
|
||||
IF_ASSERT_FAILED(!args.empty()) {
|
||||
return false;
|
||||
}
|
||||
ret = args.at(0).toInt();
|
||||
if (ret) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
void MultiInstancesProvider::activateWindowWithoutProject()
|
||||
{
|
||||
if (!isInited()) {
|
||||
return;
|
||||
}
|
||||
mainWindow()->requestShowOnBack();
|
||||
m_ipcChannel->broadcast(METHOD_ACTIVATE_WINDOW_WITHOUT_PROJECT, {});
|
||||
}
|
||||
|
||||
bool MultiInstancesProvider::openNewAppInstance(const QStringList& args)
|
||||
{
|
||||
if (!isInited()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<ID> currentApps = m_ipcChannel->instances();
|
||||
|
||||
QString appPath = QCoreApplication::applicationFilePath();
|
||||
bool ok = QProcess::startDetached(appPath, args);
|
||||
if (ok) {
|
||||
LOGI() << "success start: " << appPath << ", args: " << args;
|
||||
} else {
|
||||
LOGE() << "failed start: " << appPath << ", args: " << args;
|
||||
return ok;
|
||||
}
|
||||
|
||||
auto sleep = [](int msec) {
|
||||
QTimer timer;
|
||||
QEventLoop loop;
|
||||
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||||
timer.start(msec);
|
||||
loop.exec();
|
||||
};
|
||||
|
||||
auto waitNewInstance = [this, sleep, currentApps](int waitMs, int count) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
sleep(waitMs);
|
||||
QList<ID> apps = m_ipcChannel->instances();
|
||||
for (const ID& id : apps) {
|
||||
if (!currentApps.contains(id)) {
|
||||
LOGI() << "created new instance with ID: " << id;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
//! NOTE Waiting for a new instance to be created
|
||||
ok = waitNewInstance(100, 50);
|
||||
if (!ok) {
|
||||
LOGE() << "we didn't wait for registration and response from the new instance";
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,8 @@ public:
|
|||
// Project opening
|
||||
bool isProjectAlreadyOpened(const io::path& projectPath) const override;
|
||||
void activateWindowWithProject(const io::path& projectPath) override;
|
||||
bool isHasAppInstanceWithoutProject() const override;
|
||||
void activateWindowWithoutProject() override;
|
||||
bool openNewAppInstance(const QStringList& args) override;
|
||||
|
||||
// Settings
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <QFileOpenEvent>
|
||||
|
||||
#include "translation.h"
|
||||
#include "defer.h"
|
||||
#include "notation/notationerrors.h"
|
||||
#include "projectconfiguration.h"
|
||||
|
||||
|
@ -125,8 +126,8 @@ Ret ProjectFilesController::openProject(const io::path& projectPath_)
|
|||
return make_ret(Ret::Code::Ok);
|
||||
}
|
||||
|
||||
//! Step 4. Check, if a any project already opened in the current window,
|
||||
//! then we open new window
|
||||
//! Step 4. Check, if a any project is already open in the current window,
|
||||
//! then create a new instance
|
||||
if (globalContext()->currentProject()) {
|
||||
QStringList args;
|
||||
args << projectPath.toQString();
|
||||
|
@ -183,11 +184,41 @@ bool ProjectFilesController::isProjectOpened(const io::path& scorePath) const
|
|||
return false;
|
||||
}
|
||||
|
||||
bool ProjectFilesController::isAnyProjectOpened() const
|
||||
{
|
||||
auto project = globalContext()->currentProject();
|
||||
if (project) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ProjectFilesController::newProject()
|
||||
{
|
||||
//! Check, if a any project already opened in the current window,
|
||||
//! then we open new window
|
||||
//! NOTE This method is synchronous,
|
||||
//! but inside `multiInstancesProvider` there can be an event loop
|
||||
//! to wait for the responces from other instances, accordingly,
|
||||
//! the events (like user click) can be executed and this method can be called several times,
|
||||
//! before the end of the current call.
|
||||
//! So we ignore all subsequent calls until the current one completes.
|
||||
if (m_isNewProjectProcessing) {
|
||||
return;
|
||||
}
|
||||
m_isNewProjectProcessing = true;
|
||||
|
||||
Defer defer([this]() {
|
||||
m_isNewProjectProcessing = false;
|
||||
});
|
||||
|
||||
if (globalContext()->currentProject()) {
|
||||
//! Check, if any project is already open in the current window
|
||||
//! and there is already a created instance without a project, then activate it
|
||||
if (multiInstancesProvider()->isHasAppInstanceWithoutProject()) {
|
||||
multiInstancesProvider()->activateWindowWithoutProject();
|
||||
return;
|
||||
}
|
||||
|
||||
//! Otherwise, we will create a new instance
|
||||
QStringList args;
|
||||
args << "--session-type" << "start-with-new";
|
||||
multiInstancesProvider()->openNewAppInstance(args);
|
||||
|
|
|
@ -60,6 +60,7 @@ public:
|
|||
Ret openProject(const io::path& projectPath) override;
|
||||
bool closeOpenedProject() override;
|
||||
bool isProjectOpened(const io::path& scorePath) const override;
|
||||
bool isAnyProjectOpened() const override;
|
||||
void saveProject(const io::path& path = io::path()) override;
|
||||
|
||||
private:
|
||||
|
@ -105,6 +106,8 @@ private:
|
|||
bool isProjectOpened() const;
|
||||
bool isNeedSaveScore() const;
|
||||
bool hasSelection() const;
|
||||
|
||||
bool m_isNewProjectProcessing = false;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
virtual Ret openProject(const io::path& path) = 0;
|
||||
virtual bool closeOpenedProject() = 0;
|
||||
virtual bool isProjectOpened(const io::path& path) const = 0;
|
||||
virtual bool isAnyProjectOpened() const = 0;
|
||||
virtual void saveProject(const io::path& path = io::path()) = 0;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue