Commit 79560c57 authored by Noah Davis's avatar Noah Davis 🌵
Browse files

[PlasmaDesktopTheme] Update it to match qqc2-desktop-style and clean up a bit

parent 14cc7c00
/*
SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
SPDX-FileCopyrightText: 2020 Noah Davis <noahadvs@gmail.com>
SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -8,18 +9,16 @@
#include <QQmlEngine>
#include <QQmlContext>
#include <QGuiApplication>
#include <QQuickRenderControl>
#include <QPalette>
#include <QDebug>
#include <QQuickWindow>
#include <QScreen>
#include <KIconLoader>
#include <KColorUtils>
#include <KColorScheme>
#include <KConfigGroup>
#include <QDBusConnection>
#include <cmath>
class IconLoaderSingleton
{
public:
......@@ -44,7 +43,6 @@ public:
explicit StyleSingleton()
: QObject()
, buttonScheme(QPalette::Active, KColorScheme::ColorSet::Button)
, viewScheme(QPalette::Active, KColorScheme::ColorSet::View)
{
connect(qGuiApp, &QGuiApplication::paletteChanged,
this, &StyleSingleton::refresh);
......@@ -55,16 +53,44 @@ public:
QDBusConnection::sessionBus().connect( QString(),
QStringLiteral( "/KGlobalSettings" ),
QStringLiteral( "org.kde.KGlobalSettings" ),
QStringLiteral( "notifyChange" ), this, SIGNAL(configurationChanged()));
QStringLiteral( "notifyChange" ), this, SLOT(notifyWatchersConfigurationChange()));
connect(qGuiApp, &QGuiApplication::fontDatabaseChanged, this, &StyleSingleton::notifyWatchersConfigurationChange);
/* QtTextRendering uses less memory, so use it in low power mode.
*
* For scale factors greater than 2, native rendering doesn't actually do much.
* Does native rendering even work when scaleFactor >= 2?
*
* NativeTextRendering is still distorted sometimes with fractional scale
* factors, despite https://bugreports.qt.io/browse/QTBUG-67007 being closed.
* 1.5x scaling looks generally OK, but there are occasional and difficult to
* reproduce issues with all fractional scale factors.
*/
qreal devicePixelRatio = qGuiApp->devicePixelRatio();
QQuickWindow::TextRenderType defaultTextRenderType = int(devicePixelRatio) == devicePixelRatio ?
QQuickWindow::NativeTextRendering : QQuickWindow::QtTextRendering;
// Allow setting the text rendering type with an environment variable
QByteArrayList validInputs = {"qttextrendering", "qtrendering", "nativetextrendering", "nativerendering"};
QByteArray input = qgetenv("QT_QUICK_DEFAULT_TEXT_RENDER_TYPE").toLower();
if (validInputs.contains(input)) {
if (input == validInputs[0] || input == validInputs[1]) {
defaultTextRenderType = QQuickWindow::QtTextRendering;
} else {
defaultTextRenderType = QQuickWindow::NativeTextRendering;
}
}
QQuickWindow::setTextRenderType(defaultTextRenderType);
}
void refresh()
{
m_cache.clear();
buttonScheme = KColorScheme(QPalette::Active, KColorScheme::ColorSet::Button);
viewScheme = KColorScheme(QPalette::Active, KColorScheme::ColorSet::View);
Q_EMIT paletteChanged();
notifyWatchersPaletteChange();
}
Colors loadColors(Kirigami::PlatformTheme::ColorSet cs, QPalette::ColorGroup group)
......@@ -140,12 +166,23 @@ public:
return ret;
}
void notifyWatchersPaletteChange()
{
for (auto watcher : qAsConst(watchers)) {
watcher->syncColors();
}
}
Q_SLOT void notifyWatchersConfigurationChange()
{
for (auto watcher : qAsConst(watchers)) {
watcher->configurationChanged();
}
}
KColorScheme buttonScheme;
KColorScheme viewScheme;
Q_SIGNALS:
void configurationChanged();
void paletteChanged();
QVector<QPointer<PlasmaDesktopTheme>> watchers;
private:
QHash<QPair<Kirigami::PlatformTheme::ColorSet, QPalette::ColorGroup>, Colors> m_cache;
......@@ -155,87 +192,62 @@ Q_GLOBAL_STATIC_WITH_ARGS(QScopedPointer<StyleSingleton>, s_style, (new StyleSin
PlasmaDesktopTheme::PlasmaDesktopTheme(QObject *parent)
: PlatformTheme(parent)
{
//TODO: MOVE THIS SOMEWHERE ELSE
m_lowPowerHardware = QByteArrayList{"1", "true"}.contains(qgetenv("KIRIGAMI_LOWPOWER_HARDWARE").toLower());
qreal scaleFactor = qGuiApp->devicePixelRatio();
qreal scaleFactorRemainder = fmod(scaleFactor, 1.0);
/* QtTextRendering uses less memory, so use it in low power mode.
*
* For scale factors greater than 2, native rendering doesn't actually do much.
* Does native rendering even work when scaleFactor >= 2?
*
* NativeTextRendering is still distorted sometimes with fractional scale
* factors, despite https://bugreports.qt.io/browse/QTBUG-67007 being closed.
* 1.5x scaling looks generally OK, but there are occasional and difficult to
* reproduce issues with all fractional scale factors.
*/
QQuickWindow::TextRenderType defaultTextRenderType = !m_lowPowerHardware
&& scaleFactor < 2
&& (scaleFactorRemainder == 0.0 /*|| scaleFactor == 1.5*/)
? QQuickWindow::NativeTextRendering : QQuickWindow::QtTextRendering;
// Allow setting the text rendering type with an environment variable
QByteArrayList validInputs = {"qttextrendering", "qtrendering", "nativetextrendering", "nativerendering"};
QByteArray input = qgetenv("QT_QUICK_DEFAULT_TEXT_RENDER_TYPE").toLower();
if (validInputs.contains(input)) {
if (input == validInputs[0] || input == validInputs[1]) {
defaultTextRenderType = QQuickWindow::QtTextRendering;
} else {
defaultTextRenderType = QQuickWindow::NativeTextRendering;
}
setSupportsIconColoring(true);
auto parentItem = qobject_cast<QQuickItem *>(parent);
if (parentItem) {
connect(parentItem, &QQuickItem::enabledChanged, this, &PlasmaDesktopTheme::syncColors);
connect(parentItem, &QQuickItem::windowChanged, this, &PlasmaDesktopTheme::syncWindow);
}
QQuickWindow::setTextRenderType(defaultTextRenderType);
addChangeWatcher(this, std::bind(&PlasmaDesktopTheme::syncColors, this));
//---------------------------
(*s_style)->watchers.append(this);
setSupportsIconColoring(true);
m_parentItem = qobject_cast<QQuickItem *>(parent);
syncWindow();
syncColors();
}
PlasmaDesktopTheme::~PlasmaDesktopTheme()
{
(*s_style)->watchers.removeOne(this);
}
//null in case parent is a normal QObject
if (m_parentItem) {
connect(m_parentItem.data(), &QQuickItem::enabledChanged,
void PlasmaDesktopTheme::syncWindow()
{
if (m_window) {
disconnect(m_window.data(), &QWindow::activeChanged,
this, &PlasmaDesktopTheme::syncColors);
if (m_parentItem && m_parentItem->window()) {
connect(m_parentItem->window(), &QWindow::activeChanged,
this, &PlasmaDesktopTheme::syncColors);
m_window = m_parentItem->window();
}
connect(m_parentItem.data(), &QQuickItem::windowChanged,
this, [this]() {
if (m_window) {
disconnect(m_window.data(), &QWindow::activeChanged,
this, &PlasmaDesktopTheme::syncColors);
}
if (m_parentItem && m_parentItem->window()) {
connect(m_parentItem->window(), &QWindow::activeChanged,
this, &PlasmaDesktopTheme::syncColors);
}
syncColors();
});
}
//TODO: correct? depends from https://codereview.qt-project.org/206889
connect(qGuiApp, &QGuiApplication::fontDatabaseChanged, this, [this]() {setDefaultFont(qApp->font());});
configurationChanged();
QWindow *window = nullptr;
connect(this, &PlasmaDesktopTheme::colorSetChanged,
this, &PlasmaDesktopTheme::syncColors);
connect(this, &PlasmaDesktopTheme::colorGroupChanged,
this, &PlasmaDesktopTheme::syncColors);
auto parentItem = qobject_cast<QQuickItem *>(parent());
if (parentItem) {
QQuickWindow *qw = parentItem->window();
connect(s_style->data(), &StyleSingleton::paletteChanged,
this, &PlasmaDesktopTheme::syncColors);
connect(s_style->data(), &StyleSingleton::configurationChanged,
this, &PlasmaDesktopTheme::configurationChanged);
window = QQuickRenderControl::renderWindowFor(qw);
if (!window) {
window = qw;
}
if (qw) {
connect(qw, &QQuickWindow::sceneGraphInitialized,
this, &PlasmaDesktopTheme::syncWindow);
}
}
m_window = window;
syncColors();
if (window) {
connect(m_window.data(), &QWindow::activeChanged,
this, &PlasmaDesktopTheme::syncColors);
syncColors();
}
}
PlasmaDesktopTheme::~PlasmaDesktopTheme() = default;
void PlasmaDesktopTheme::configurationChanged()
{
KSharedConfigPtr ptr = KSharedConfig::openConfig();
......@@ -249,6 +261,8 @@ void PlasmaDesktopTheme::configurationChanged()
}
return smallFont;
}()));
setDefaultFont(qGuiApp->font());
}
QIcon PlasmaDesktopTheme::iconFromTheme(const QString &name, const QColor &customColor)
......@@ -268,20 +282,20 @@ QIcon PlasmaDesktopTheme::iconFromTheme(const QString &name, const QColor &custo
void PlasmaDesktopTheme::syncColors()
{
QPalette::ColorGroup group = (QPalette::ColorGroup)colorGroup();
if (m_parentItem) {
if (!m_parentItem->isEnabled()) {
auto parentItem = qobject_cast<QQuickItem*>(parent());
if (parentItem) {
if (!parentItem->isEnabled()) {
group = QPalette::Disabled;
//Why also checking the window is exposed?
//in the case of QQuickWidget the window() will never be active
//and the widgets will always have the inactive palette.
// better to always show it active than always show it inactive
} else if (m_parentItem->window() && !m_parentItem->window()->isActive() && m_parentItem->window()->isExposed()) {
} else if (m_window && !m_window->isActive() && m_window->isExposed()) {
group = QPalette::Inactive;
}
}
const auto colors = (*s_style)->loadColors(colorSet(), group);
setPalette(colors.palette);
//foreground
setTextColor(colors.scheme.foreground(KColorScheme::NormalText).color());
......@@ -310,23 +324,14 @@ void PlasmaDesktopTheme::syncColors()
setHoverColor(colors.scheme.decoration(KColorScheme::HoverColor).color());
setFocusColor(colors.scheme.decoration(KColorScheme::FocusColor).color());
//legacy stuff
m_buttonTextColor = (*s_style)->buttonScheme.foreground(KColorScheme::NormalText).color();
m_buttonBackgroundColor = (*s_style)->buttonScheme.background(KColorScheme::NormalBackground).color();
m_buttonHoverColor = (*s_style)->buttonScheme.decoration(KColorScheme::HoverColor).color();
m_buttonFocusColor = (*s_style)->buttonScheme.decoration(KColorScheme::FocusColor).color();
m_viewTextColor = (*s_style)->viewScheme.foreground(KColorScheme::NormalText).color();
m_viewBackgroundColor = (*s_style)->viewScheme.background(KColorScheme::NormalBackground).color();
m_viewHoverColor = (*s_style)->viewScheme.decoration(KColorScheme::HoverColor).color();
m_viewFocusColor = (*s_style)->viewScheme.decoration(KColorScheme::FocusColor).color();
// Breeze QQC2 style colors
const QColor &buttonTextColor = (*s_style)->buttonScheme.foreground(KColorScheme::NormalText).color();
const QColor &buttonBackgroundColor = (*s_style)->buttonScheme.background(KColorScheme::NormalBackground).color();
auto separatorColor = [](const QColor &bg, const QColor &fg, const qreal baseRatio = 0.2) {
return KColorUtils::luma(bg) > 0.5 ? KColorUtils::mix(bg, fg, baseRatio) : KColorUtils::mix(bg, fg, baseRatio/2);
};
m_buttonSeparatorColor = separatorColor(m_buttonBackgroundColor, m_buttonTextColor, 0.3);
m_buttonSeparatorColor = separatorColor(buttonBackgroundColor, buttonTextColor, 0.3);
switch (colorSet()) {
// case ColorSet::View:
......@@ -343,57 +348,6 @@ void PlasmaDesktopTheme::syncColors()
default:
m_separatorColor = separatorColor(backgroundColor(), textColor());
}
emit colorsChanged();
}
QColor PlasmaDesktopTheme::buttonTextColor() const
{
qWarning() << "WARNING: buttonTextColor is deprecated, use textColor with colorSet: Theme.Button instead";
return m_buttonTextColor;
}
QColor PlasmaDesktopTheme::buttonBackgroundColor() const
{
qWarning() << "WARNING: buttonBackgroundColor is deprecated, use backgroundColor with colorSet: Theme.Button instead";
return m_buttonBackgroundColor;
}
QColor PlasmaDesktopTheme::buttonHoverColor() const
{
qWarning() << "WARNING: buttonHoverColor is deprecated, use backgroundColor with colorSet: Theme.Button instead";
return m_buttonHoverColor;
}
QColor PlasmaDesktopTheme::buttonFocusColor() const
{
qWarning() << "WARNING: buttonFocusColor is deprecated, use backgroundColor with colorSet: Theme.Button instead";
return m_buttonFocusColor;
}
QColor PlasmaDesktopTheme::viewTextColor() const
{
qWarning()<<"WARNING: viewTextColor is deprecated, use backgroundColor with colorSet: Theme.View instead";
return m_viewTextColor;
}
QColor PlasmaDesktopTheme::viewBackgroundColor() const
{
qWarning() << "WARNING: viewBackgroundColor is deprecated, use backgroundColor with colorSet: Theme.View instead";
return m_viewBackgroundColor;
}
QColor PlasmaDesktopTheme::viewHoverColor() const
{
qWarning() << "WARNING: viewHoverColor is deprecated, use backgroundColor with colorSet: Theme.View instead";
return m_viewHoverColor;
}
QColor PlasmaDesktopTheme::viewFocusColor() const
{
qWarning() << "WARNING: viewFocusColor is deprecated, use backgroundColor with colorSet: Theme.View instead";
return m_viewFocusColor;
}
// Breeze QQC2 style colors
......@@ -407,4 +361,9 @@ QColor PlasmaDesktopTheme::buttonSeparatorColor() const
return m_buttonSeparatorColor;
}
bool PlasmaDesktopTheme::lowPowerHardware() const
{
return m_lowPowerHardware;
}
#include "plasmadesktoptheme.moc"
/*
SPDX-FileCopyrightText: 2017 Marco Martin <mart@kde.org>
SPDX-FileCopyrightText: 2020 Noah Davis <noahadvs@gmail.com>
SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -16,28 +17,18 @@
class PlasmaDesktopTheme;
class KIconLoader;
class StyleSingleton;
class PlasmaDesktopTheme : public Kirigami::PlatformTheme
{
Q_OBJECT
// colors
Q_PROPERTY(QColor buttonTextColor READ buttonTextColor NOTIFY colorsChanged)
Q_PROPERTY(QColor buttonBackgroundColor READ buttonBackgroundColor NOTIFY colorsChanged)
Q_PROPERTY(QColor buttonHoverColor READ buttonHoverColor NOTIFY colorsChanged)
Q_PROPERTY(QColor buttonFocusColor READ buttonFocusColor NOTIFY colorsChanged)
Q_PROPERTY(QColor viewTextColor READ viewTextColor NOTIFY colorsChanged)
Q_PROPERTY(QColor viewBackgroundColor READ viewBackgroundColor NOTIFY colorsChanged)
Q_PROPERTY(QColor viewHoverColor READ viewHoverColor NOTIFY colorsChanged)
Q_PROPERTY(QColor viewFocusColor READ viewFocusColor NOTIFY colorsChanged)
// Breeze QQC2 style colors
Q_PROPERTY(QColor separatorColor READ separatorColor NOTIFY colorsChanged)
Q_PROPERTY(QColor buttonSeparatorColor READ buttonSeparatorColor NOTIFY colorsChanged)
// Needed to deal with ShadowedRectangle
Q_PROPERTY(bool lowPowerHardware MEMBER m_lowPowerHardware CONSTANT)
Q_PROPERTY(bool lowPowerHardware READ lowPowerHardware CONSTANT FINAL)
public:
explicit PlasmaDesktopTheme(QObject *parent = nullptr);
......@@ -45,52 +36,28 @@ public:
Q_INVOKABLE QIcon iconFromTheme(const QString &name, const QColor &customColor = Qt::transparent) override;
void syncWindow();
void syncColors();
QColor buttonTextColor() const;
QColor buttonBackgroundColor() const;
QColor buttonHoverColor() const;
QColor buttonFocusColor() const;
QColor viewTextColor() const;
QColor viewBackgroundColor() const;
QColor viewHoverColor() const;
QColor viewFocusColor() const;
// Breeze QQC2 style colors
QColor separatorColor() const;
QColor buttonSeparatorColor() const;
Q_SIGNALS:
void colorsChanged();
bool lowPowerHardware() const;
protected Q_SLOTS:
void configurationChanged();
private:
QPointer<QQuickItem> m_parentItem;
friend class StyleSingleton;
QPointer<QWindow> m_window;
//legacy colors
QColor m_buttonTextColor;
QColor m_buttonBackgroundColor;
QColor m_buttonHoverColor;
QColor m_buttonFocusColor;
QColor m_viewTextColor;
QColor m_viewBackgroundColor;
QColor m_viewHoverColor;
QColor m_viewFocusColor;
// Breeze QQC2 style colors
QColor m_separatorColor;
QColor m_buttonSeparatorColor;
// Needed to deal with ShadowedRectangle
bool m_lowPowerHardware = false;
bool m_mobile = true;
bool m_hasTouchScreen = true;
};
#endif // KIRIGAMIPLASMATHEME_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment