Skip to content

Commit

Permalink
[Theme] Make theme as singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Feb 10, 2025
1 parent 1acb9d8 commit ee6aa6c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
33 changes: 21 additions & 12 deletions src/core/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright (C) 2018-2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/


#include "plugin.h"
#include "nemoimageprovider.h"
#include "sizing.h"
Expand All @@ -8,16 +28,5 @@ void QQuickNemoControlsExtensionPlugin::registerTypes(const char* uri)
{
Q_ASSERT(uri == QLatin1String("Nemo"));
qmlRegisterModule(uri, 2, 0);
}

void QQuickNemoControlsExtensionPlugin::initializeEngine(QQmlEngine* engine, const char* uri)
{
Theme* theme = new Theme();

QQmlExtensionPlugin::initializeEngine(engine, uri);
QQmlContext* context = engine->rootContext();
context->setContextProperty("size", theme->size());
context->setContextProperty("Theme", theme);

engine->addImageProvider(QLatin1String("theme"), new NemoImageProvider);
qmlRegisterSingletonType<Theme>(uri, 1, 0, "Theme", &Theme::qmlInstance);
}
20 changes: 19 additions & 1 deletion src/core/plugin.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright (C) 2018-2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

#ifndef QQUICKNEMOCONTROLSEXTENSIONPLUGIN_H
#define QQUICKNEMOCONTROLSEXTENSIONPLUGIN_H

Expand All @@ -9,7 +28,6 @@ class QQuickNemoControlsExtensionPlugin : public QQmlExtensionPlugin {
Q_PLUGIN_METADATA(IID QQmlEngineExtensionInterface_iid FILE "nemo.json")
public:
void registerTypes(const char* uri);
void initializeEngine(QQmlEngine* engine, const char* uri);
};

#endif // QQUICKNEMOCONTROLSEXTENSIONPLUGIN_H
12 changes: 12 additions & 0 deletions src/core/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <QJsonObject>
#include <math.h>

static Theme* themeInstance = 0;

Theme::Theme(QObject* parent)
: QObject(parent)
, m_size(new Sizing)
Expand All @@ -45,6 +47,16 @@ Theme::Theme(QObject* parent)
}
}

QObject *Theme::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
if(!themeInstance) {
themeInstance = new Theme();
}
return themeInstance;
}

bool Theme::loadTheme(QString fileName)
{
QFile themeFile(fileName);
Expand Down
4 changes: 4 additions & 0 deletions src/core/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

#include <MDConfItem>
#include <QObject>
#include <QQmlEngine>

class Sizing;

class Theme : public QObject {
Q_OBJECT
Q_DISABLE_COPY(Theme)

Q_PROPERTY(qreal iconSizeLauncher READ iconSizeLauncher NOTIFY themeUpdated)

Expand Down Expand Up @@ -69,6 +71,8 @@ class Theme : public QObject {

public:
explicit Theme(QObject* parent = nullptr);
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);

Sizing* size() const { return m_size; }

Q_INVOKABLE bool loadTheme(const QString fileName);
Expand Down

0 comments on commit ee6aa6c

Please sign in to comment.