mirror of
https://invent.kde.org/plasma/layer-shell-qt.git
synced 2025-07-23 07:24:36 -04:00
Add API to choose an initial state of the window
This commit is contained in:
@ -4,16 +4,74 @@
|
||||
* SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <interfaces/shell.h>
|
||||
#include <interfaces/window.h>
|
||||
|
||||
using namespace LayerShellQt;
|
||||
|
||||
QStringList enumsToStringList(QMetaEnum metaEnum)
|
||||
{
|
||||
QStringList ret;
|
||||
ret.reserve(metaEnum.keyCount());
|
||||
for (int i = 0; i < metaEnum.keyCount(); ++i) {
|
||||
ret.append(metaEnum.key(i));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T stringToEnum(QMetaEnum metaEnum, const QString &str)
|
||||
{
|
||||
T ret = {};
|
||||
const auto splitted = str.split(QLatin1Char('|'));
|
||||
for (const auto &value : splitted) {
|
||||
ret |= T(metaEnum.keyToValue(qPrintable(value)));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
LayerShellQt::Shell::useLayerShell();
|
||||
Shell::useLayerShell();
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
const auto layerMetaEnum = QMetaEnum::fromType<Window::Layer>();
|
||||
const auto anchorMetaEnum = QMetaEnum::fromType<Window::Anchor>();
|
||||
|
||||
QCommandLineParser parser;
|
||||
QCommandLineOption marginsOption(QStringLiteral("margins"), QStringLiteral("Window margins"), QStringLiteral("pixels"), QStringLiteral("0"));
|
||||
QCommandLineOption scopeOption(QStringLiteral("scope"), QStringLiteral("Window scope"), QStringLiteral("namespace"), QStringLiteral("normal"));
|
||||
QCommandLineOption anchorsOption(QStringLiteral("anchors"),
|
||||
QStringLiteral("Either ") + enumsToStringList(anchorMetaEnum).join(QLatin1String("|")),
|
||||
QStringLiteral("anchors"),
|
||||
QStringLiteral("AnchorTop|AnchorBottom|AnchorLeft|AnchorRight"));
|
||||
QCommandLineOption layerOption(QStringLiteral("layer"),
|
||||
QStringLiteral("One of ") + enumsToStringList(layerMetaEnum).join(QLatin1String("|")),
|
||||
QStringLiteral("layer"),
|
||||
QStringLiteral("LayerTop"));
|
||||
parser.addOptions({marginsOption, scopeOption, anchorsOption, layerOption});
|
||||
parser.addHelpOption();
|
||||
parser.process(app);
|
||||
|
||||
static int margins = 0;
|
||||
if (parser.isSet(marginsOption)) {
|
||||
margins = parser.value(marginsOption).toInt();
|
||||
Shell::setDefaultMargins({margins, margins, margins, margins});
|
||||
}
|
||||
if (parser.isSet(scopeOption)) {
|
||||
Shell::setDefaultScope(parser.value(scopeOption));
|
||||
}
|
||||
if (parser.isSet(layerOption)) {
|
||||
Shell::setDefaultLayer(Window::Layer(layerMetaEnum.keyToValue(qPrintable(parser.value(layerOption)))));
|
||||
}
|
||||
if (parser.isSet(anchorsOption)) {
|
||||
Shell::setDefaultAnchors(stringToEnum<Window::Anchors>(anchorMetaEnum, parser.value(anchorsOption)));
|
||||
}
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.loadData(
|
||||
"import QtQuick.Controls 2.10\n"
|
||||
@ -28,11 +86,5 @@ int main(int argc, char **argv)
|
||||
,
|
||||
QStringLiteral("bananaland:/potato.qml"));
|
||||
|
||||
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [](QObject *object) {
|
||||
auto layerWindow = LayerShellQt::Window::get(qobject_cast<QWindow *>(object));
|
||||
Q_ASSERT(layerWindow);
|
||||
layerWindow->setMargins({50, 50, 50, 50});
|
||||
});
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
Reference in New Issue
Block a user