quickshell/src/core/qmlglobal.cpp

77 lines
2.2 KiB
C++
Raw Normal View History

2024-02-04 12:58:58 +00:00
#include "qmlglobal.hpp"
#include <qcontainerfwd.h>
#include <qcoreapplication.h>
#include <qguiapplication.h>
#include <qlogging.h>
2024-02-04 12:58:58 +00:00
#include <qobject.h>
#include <qqmlcontext.h>
#include <qqmlengine.h>
2024-02-04 12:58:58 +00:00
#include <qqmllist.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "qmlscreen.hpp"
#include "rootwrapper.hpp"
2024-02-04 12:58:58 +00:00
2024-02-12 10:16:22 +00:00
QuickShellGlobal::QuickShellGlobal(QObject* parent): QObject(parent) {
2024-02-04 12:58:58 +00:00
auto* app = QCoreApplication::instance();
auto* guiApp = qobject_cast<QGuiApplication*>(app);
if (guiApp != nullptr) {
// clang-format off
2024-02-12 10:16:22 +00:00
QObject::connect(guiApp, &QGuiApplication::primaryScreenChanged, this, &QuickShellGlobal::updateScreens);
QObject::connect(guiApp, &QGuiApplication::screenAdded, this, &QuickShellGlobal::updateScreens);
QObject::connect(guiApp, &QGuiApplication::screenRemoved, this, &QuickShellGlobal::updateScreens);
2024-02-04 12:58:58 +00:00
// clang-format on
this->updateScreens();
}
}
2024-02-12 10:16:22 +00:00
qsizetype QuickShellGlobal::screensCount(QQmlListProperty<QuickShellScreenInfo>* prop) {
return static_cast<QuickShellGlobal*>(prop->object)->mScreens.size(); // NOLINT
2024-02-04 12:58:58 +00:00
}
QuickShellScreenInfo*
QuickShellGlobal::screenAt(QQmlListProperty<QuickShellScreenInfo>* prop, qsizetype i) {
2024-02-12 10:16:22 +00:00
return static_cast<QuickShellGlobal*>(prop->object)->mScreens.at(i); // NOLINT
2024-02-04 12:58:58 +00:00
}
2024-02-12 10:16:22 +00:00
QQmlListProperty<QuickShellScreenInfo> QuickShellGlobal::screens() {
return QQmlListProperty<QuickShellScreenInfo>(
2024-02-04 12:58:58 +00:00
this,
nullptr,
2024-02-12 10:16:22 +00:00
&QuickShellGlobal::screensCount,
&QuickShellGlobal::screenAt
2024-02-04 12:58:58 +00:00
);
}
2024-02-12 10:16:22 +00:00
void QuickShellGlobal::reload(bool hard) {
auto* rootobj = QQmlEngine::contextForObject(this)->engine()->parent();
auto* root = qobject_cast<RootWrapper*>(rootobj);
if (root == nullptr) {
qWarning() << "cannot find RootWrapper for reload, ignoring request";
return;
}
root->reloadGraph(hard);
}
2024-02-12 10:16:22 +00:00
void QuickShellGlobal::updateScreens() {
2024-02-04 12:58:58 +00:00
auto screens = QGuiApplication::screens();
this->mScreens.resize(screens.size());
for (auto i = 0; i < screens.size(); i++) {
if (this->mScreens[i] != nullptr) {
this->mScreens[i]->screen = nullptr;
this->mScreens[i]->setParent(nullptr); // delete if not owned by the js engine
}
2024-02-12 10:16:22 +00:00
this->mScreens[i] = new QuickShellScreenInfo(this, screens[i]);
2024-02-04 12:58:58 +00:00
}
emit this->screensChanged();
}