Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [base/gui/sidebar] adpat windowmanager and sidebar #2030

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ else()
endif()

find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
find_package(DtkCMake REQUIRED)
find_package(Dtk${DTK_VERSION_MAJOR}CMake REQUIRED)
#if no debug, can't out in code define key '__FUNCTION__' and so on
add_definitions(-DQT_MESSAGELOGCONTEXT)

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion include/dfm-gui/applet.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class QQuickItem;
DFMGUI_BEGIN_NAMESPACE

class Containment;
class Panel;

class AppletPrivate;
class Applet : public QObject
Expand Down Expand Up @@ -43,10 +44,11 @@ class Applet : public QObject
Q_SIGNAL void rootObjectChanged(QObject *object);

QUrl currentUrl() const;
void setCurrentUrl(const QUrl &url);
Q_SLOT void setCurrentUrl(const QUrl &url);
Q_SIGNAL void currentUrlChanged(const QUrl &url);

Containment *containment() const;
Panel *panel() const;

QString plugin() const;
QString id() const;
Expand Down
5 changes: 3 additions & 2 deletions include/dfm-gui/appletfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DFMGUI_BEGIN_NAMESPACE

class Applet;
class Containment;
class AppletFactory
{
explicit AppletFactory();
Expand All @@ -19,12 +20,12 @@ class AppletFactory
public:
using AppletPtr = Applet *;
using Key = QString;
using CreateFunc = std::function<AppletPtr(const Key &url)>;
using CreateFunc = std::function<AppletPtr(const Key &url, Containment *parent, QString *error)>;

static AppletFactory *instance();

bool regCreator(const Key &url, CreateFunc creator, QString *errorString = nullptr);
AppletPtr create(const Key &url, QString *errorString = nullptr);
AppletPtr create(const Key &url, Containment *parent = nullptr, QString *errorString = nullptr);

private:
dfmbase::DThreadMap<Key, CreateFunc> constructList {};
Expand Down
7 changes: 4 additions & 3 deletions include/dfm-gui/appletmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class AppletManager : public QObject
Panel *createPanel(const QString &pluginName, const QString &quickId);

QList<QString> allAppletTemplateIdList() const;
Applet *createApplet(const QString &templateId, QObject *parent = nullptr);
Applet *createApplet(const QString &pluginName, const QString &quickId, QObject *parent = nullptr);
Applet *createApplet(const QString &templateId, Containment *parent = nullptr);
Applet *createApplet(const QString &pluginName, const QString &quickId, Containment *parent = nullptr);

QString lastError() const;

static Applet *createAppletFromInfo(const dpf::PluginQuickMetaPtr &metaPtr, QObject *parent = nullptr, QString *errorString = nullptr);
static Applet *createAppletFromInfo(
const dpf::PluginQuickMetaPtr &metaPtr, Containment *parent = nullptr, QString *errorString = nullptr);

private:
QScopedPointer<AppletManagerPrivate> dptr;
Expand Down
8 changes: 8 additions & 0 deletions include/dfm-gui/panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Panel : public Containment
explicit Panel(QObject *parent = nullptr);

QQuickWindow *window() const;
quint64 windId() const;

virtual void loadState();
virtual void saveState();

Q_SIGNALS:
void aboutToOpen();
void aboutToClose();

private:
Q_DECLARE_PRIVATE_D(dptr, Panel);
Expand Down
1 change: 0 additions & 1 deletion include/dfm-gui/sharedqmlengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class SharedQmlEngine : public QObject
QObject *rootObject() const;
QQmlContext *rootContext() const;
QQmlComponent *mainComponent() const;
static QSharedPointer<QQmlEngine> globalEngine();

bool create(Applet *applet, bool async = false);
bool completeCreation(const QVariantMap &args = {});
Expand Down
42 changes: 0 additions & 42 deletions include/dfm-gui/windowhandle.h

This file was deleted.

36 changes: 31 additions & 5 deletions include/dfm-gui/windowmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#define WINDOWMANAGER_H

#include <dfm-gui/dfm_gui_global.h>
#include <dfm-gui/windowhandle.h>
#include <dfm-gui/panel.h>

#include <QObject>

class QQuickWindow;
class QQmlEngine;

DFMGUI_BEGIN_NAMESPACE

class Applet;
class WindowManagerPrivate;
class WindowManager : public QObject
{
Expand All @@ -23,21 +25,45 @@ class WindowManager : public QObject
~WindowManager() override;

public:
using Handle = QPointer<Panel>;

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

WindowHandlePtr createWindow(const QString &pluginName, const QString &quickId,
const QVariantMap &var = {});
void showWindow(const WindowHandlePtr &handle);
Handle createWindow(const QUrl &url, const QString &pluginName,
const QString &quickId, const QVariantMap &var = {});
void showWindow(const Handle &handle) const;
bool activeExistsWindowByUrl(const QUrl &url) const;

quint64 findWindowId(const QObject *itemObject) const;
quint64 findWindowIdFromApplet(Applet *applet) const;
Handle findWindowByUrl(const QUrl &url) const;
Handle findWindowById(quint64 windId) const;
QList<quint64> windowIdList() const;

void resetPreviousActivedWindowId();
quint64 previousActivedWindowId() const;
bool containsCurrentUrl(const QUrl &url, const QQuickWindow *window = nullptr) const;

QString lastError() const;

Q_SIGNALS:
void windowCreated(quint64 windId);
void windowOpened(quint64 windId);
void windowClosed(quint64 windId);
void lastWindowClosed(quint64 windId);
void currentUrlChanged(quint64 windId, const QUrl &url);

private:
QScopedPointer<WindowManagerPrivate> dptr;
Q_DECLARE_PRIVATE_D(dptr, WindowManager);
Q_DISABLE_COPY(WindowManager)
};

using WindowHandle = WindowManager::Handle;

DFMGUI_END_NAMESPACE

#define FMQuickWindowIns DFMGUI_NAMESPACE::WindowManager::instance();
#define FMQuickWindowIns (DFMGUI_NAMESPACE::WindowManager::instance())

#endif // WINDOWMANAGER_H
2 changes: 1 addition & 1 deletion src/dfm-base/dialogs/basedialog/basedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "basedialog.h"

#include "utils/windowutils.h"
#include <dfm-base/utils/windowutils.h>

#include <dtitlebar.h>

Expand Down
3 changes: 2 additions & 1 deletion src/dfm-base/utils/fileinfoasycworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "fileinfoasycworker.h"
#include "fileutils.h"
#include "networkutils.h"
#include "mimetype/dmimedatabase.h"

#include <dfm-base/utils/fileutils.h>

namespace dfmbase {
FileInfoAsycWorker::FileInfoAsycWorker(QObject *parent)
: QObject(parent)
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/filestatisticsjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define FILESTATISTICSJOB_H

#include <dfm-base/dfm_base_global.h>
#include "fileutils.h"
#include <dfm-base/utils/fileutils.h>

#include <QThread>

Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/universalutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "universalutils.h"
#include <dfm-base/utils/universalutils.h>

#include <dfm-base/dfm_event_defines.h>
#include <dfm-base/base/schemefactory.h>
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/windowutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "windowutils.h"
#include <dfm-base/utils/windowutils.h>

#include <QApplication>
#include <QScreen>
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/widgets/dfmstatusbar/basicstatusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "basicstatusbar.h"
#include "private/basicstatusbar_p.h"
#include "utils/fileutils.h"

#include <dfm-base/utils/fileutils.h>
#include <dfm-base/base/application/application.h>

#include <DAnchors>
Expand Down
27 changes: 22 additions & 5 deletions src/dfm-gui/applet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <dfm-gui/applet.h>
#include <dfm-gui/containment.h>
#include <dfm-gui/panel.h>
#include "applet_p.h"
#include "containment_p.h"

Expand All @@ -21,6 +22,8 @@ AppletPrivate::~AppletPrivate()
// 释放关联的 QQuickItem
if (rootObject && QJSEngine::CppOwnership == QJSEngine::objectOwnership(rootObject)) {
Q_ASSERT_X(rootObject->parent() == q_ptr, "AppletItem memory management", "Undefined behaviour, unmanaged QQuickItem");

QObject::disconnect(rootObject, nullptr, q_ptr, nullptr);
rootObject->deleteLater();
}
}
Expand Down Expand Up @@ -119,18 +122,32 @@ QObject *Applet::rootObject() const
*/
Containment *Applet::containment() const
{
Containment *contain = nullptr;
QObject *parent = this->parent();
while (parent) {
if (Containment *contain = qobject_cast<Containment *>(parent)) {
return contain;
}
parent = parent->parent();
}

return nullptr;
}

/*!
* \brief 返回当前 Applet 的顶层 Panel , 会递归向上查找
* \return Panel 指针,若没有被 Panel 管理,返回 nullptr
*/
Panel *Applet::panel() const
{
QObject *parent = this->parent();
while (parent) {
if (Containment *tmp = qobject_cast<Containment *>(parent)) {
contain = tmp;
break;
if (Panel *panel = qobject_cast<Panel *>(parent)) {
return panel;
}
parent = parent->parent();
}

return contain;
return nullptr;
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-gui/applet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AppletPrivate
explicit AppletPrivate(Applet *q);
virtual ~AppletPrivate();

void setRootObject(QObject *item);
virtual void setRootObject(QObject *item);
void dumpAppletTreeImpl(int level = 0);

Applet *q_ptr;
Expand Down
5 changes: 2 additions & 3 deletions src/dfm-gui/appletfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool AppletFactory::regCreator(const AppletFactory::Key &url, CreateFunc creator
return true;
}

AppletFactory::AppletPtr AppletFactory::create(const AppletFactory::Key &url, QString *errorString)
AppletFactory::AppletPtr AppletFactory::create(const AppletFactory::Key &url, Containment *parent, QString *errorString)
{
QString error;
dfmbase::FinallyUtil finally([&]() {
Expand All @@ -60,9 +60,8 @@ AppletFactory::AppletPtr AppletFactory::create(const AppletFactory::Key &url, QS
"before create function";
return nullptr;
}
finally.dismiss();
AppletPtr info = constantFunc(url);

AppletPtr info = constantFunc(url, parent, &error);
return info;
}

Expand Down
Loading
Loading