Files
php-qml/framework/qml/src/SingleInstance.h

48 lines
1.5 KiB
C
Raw Normal View History

#pragma once
#include <QLocalServer>
#include <QObject>
#include <QString>
#include <QStringList>
namespace PhpQml::Bridge {
/// Per-OS-user single-instance lock with launch-arg forwarding.
///
/// Owned by the application's `main()`, NOT a QML singleton — the
/// acquire-or-forward decision must run before the QML engine boots,
/// so we cannot rely on lazy QML construction. The application exposes
/// the live instance to QML via `setContextProperty("SingleInstance", &si)`.
///
/// See PLAN.md §3 (Single-instance, Edge cases — Single-instance launch race).
class SingleInstance : public QObject
{
Q_OBJECT
public:
explicit SingleInstance(const QString& applicationId, QObject* parent = nullptr);
/// Returns true if this process is the live instance and should
/// continue starting up. Returns false if another instance was
/// already running; the launch arguments have been forwarded and
/// the caller must exit before creating any QML/window resources.
bool acquireOrForward(const QStringList& launchArgs);
signals:
/// Emitted when a subsequent invocation forwards its launch args
/// to the running instance. Application code is expected to act
/// on this — typically focus the existing window or open a new one.
void launchArgsReceived(const QStringList& args);
private slots:
void onNewConnection();
private:
QString endpointName() const;
QString m_appId;
QLocalServer m_server;
};
} // namespace PhpQml::Bridge