Skip to content

Commit

Permalink
fix: [gui] add WindowManager init function
Browse files Browse the repository at this point in the history
Add the init function on WindowManager, allows manual
management of sigleton initialization.

Log: Add WindowManager init function.
  • Loading branch information
rb-union authored and Johnson-zs committed Jun 24, 2024
1 parent 33ecafa commit 1d4f00a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/dfm-gui/windowmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class WindowManager : public QObject
using Handle = QPointer<Panel>;

static WindowManager *instance();
void initialize();
QSharedPointer<QQmlEngine> engine() const;

Handle createWindow(const QUrl &url, const QString &pluginName,
Expand Down
1 change: 1 addition & 0 deletions src/apps/dde-file-manager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static bool initQmlEngine()
globalEngine->addImportPath(DFM_QML_MODULE);
#endif

dfmgui::WindowManager::instance()->initialize();
return true;
}

Expand Down
62 changes: 40 additions & 22 deletions src/dfm-gui/windowmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@ WindowManagerPrivate::WindowManagerPrivate(WindowManager *q)
{
}

/*!
* \brief 注册标识为 \a uri 的 QML 模块信息
* 此函数单独抽离,由于 QtCreator 对 std::call_once 结构下识别不佳,不能有效通过 @uri 识别注册模块。
*/
void WindowManagerPrivate::registerType(const char *uri)
{
// 注册,使用 @uri ... 标记,以在 QtCreator 中方便访问
// @uri org.dfm.base
qmlRegisterModule(uri, 1, 0);
qmlRegisterUncreatableType<Applet>(uri, 1, 0, "Applet", "Applet attached");
qmlRegisterExtendedType<Applet, AppletAttached>(uri, 1, 0, "Applet");
qmlRegisterUncreatableType<Containment>(uri, 1, 0, "Containment", "Containment attached");
qmlRegisterExtendedType<Containment, ContainmentAttached>(uri, 1, 0, "Containment");
qmlRegisterUncreatableType<Panel>(uri, 1, 0, "Panel", "Panel attached");
qmlRegisterExtendedType<Panel, PanelAttached>(uri, 1, 0, "Panel");
qmlRegisterType<AppletItem>(uri, 1, 0, "AppletItem");
qmlRegisterType<ContainmentItem>(uri, 1, 0, "ContainmentItem");

// 工具类,生命周期由 WindowManager 管理
QuickUtils *globalUtils = new QuickUtils(q_ptr);
qmlRegisterSingletonInstance<QuickUtils>(uri, 1, 0, "QuickUtils", globalUtils);
}

WindowManager::Handle WindowManagerPrivate::createQuickWindow(const QString &pluginName, const QString &quickId, const QVariantMap &var)
{
// 首次进入初始化
Expand Down Expand Up @@ -252,28 +275,6 @@ void WindowManagerPrivate::aboutToQuit()
WindowManager::WindowManager()
: dptr(new WindowManagerPrivate(this))
{
// 注册,使用 @uri ... 标记,以在 QtCreator 中方便访问
// @uri org.dfm.base
const char *uri { "org.dfm.base" };
qmlRegisterModule(uri, 1, 0);
qmlRegisterUncreatableType<Applet>(uri, 1, 0, "Applet", "Applet attached");
qmlRegisterExtendedType<Applet, AppletAttached>(uri, 1, 0, "Applet");
qmlRegisterUncreatableType<Containment>(uri, 1, 0, "Containment", "Containment attached");
qmlRegisterExtendedType<Containment, ContainmentAttached>(uri, 1, 0, "Containment");
qmlRegisterUncreatableType<Panel>(uri, 1, 0, "Panel", "Panel attached");
qmlRegisterExtendedType<Panel, PanelAttached>(uri, 1, 0, "Panel");
qmlRegisterType<AppletItem>(uri, 1, 0, "AppletItem");
qmlRegisterType<ContainmentItem>(uri, 1, 0, "ContainmentItem");

// 工具类,生命周期由 WindowManager 管理
QuickUtils *globalUtils = new QuickUtils(this);
qmlRegisterSingletonInstance<QuickUtils>(uri, 1, 0, "QuickUtils", globalUtils);

// 退出时清理界面和 QQmlengine
connect(qApp, &QCoreApplication::aboutToQuit, this, [this]() {
Q_D(WindowManager);
d->aboutToQuit();
});
}

WindowManager::~WindowManager() { }
Expand All @@ -284,6 +285,23 @@ WindowManager *WindowManager::instance()
return &manager;
}

/*!
* \return 初始化界面管理,仅会执行一次,执行 `org.dfm.base` QML 模块的注册,以及关键信号的关联
*/
void WindowManager::initialize()
{
static std::once_flag flag;
std::call_once(flag, [this]() {
d_func()->registerType("org.dfm.base");

// 退出时清理界面和 QQmlengine
connect(qApp, &QCoreApplication::aboutToQuit, this, [this]() {
Q_D(WindowManager);
d->aboutToQuit();
});
});
}

/*!
* \brief 返回当前界面使用的全局 QQmlEngine , 可用于对 QQmlEngine 进行初始化设置。
* \warning 不要持久化保留此函数返回的指针,当 qApp 抛出 aboutToQuit() 信号时,会释放此
Expand Down
2 changes: 2 additions & 0 deletions src/dfm-gui/windowmanager_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class WindowManagerPrivate
explicit WindowManagerPrivate(WindowManager *q);
~WindowManagerPrivate() = default;

void registerType(const char *uri);

WindowManager::Handle createQuickWindow(const QString &pluginName, const QString &quickId, const QVariantMap &var);
void connectWindowHandle(const WindowManager::Handle &handle);

Expand Down

0 comments on commit 1d4f00a

Please sign in to comment.