File indexing completed on 2024-04-28 05:27:06

0001 /********************************************************************
0002  KWin - the KDE window manager
0003  This file is part of the KDE project.
0004 
0005 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0006 SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
0007 SPDX-FileCopyrightText: 2015 Mika Allan Rauhala <mika.allan.rauhala@gmail.com>
0008 
0009 SPDX-License-Identifier: GPL-2.0-or-later
0010 *********************************************************************/
0011 
0012 #include "decorationpalette.h"
0013 
0014 #include <KColorScheme>
0015 #include <KConfigGroup>
0016 #include <KSharedConfig>
0017 
0018 #include <QFileInfo>
0019 #include <QStandardPaths>
0020 
0021 namespace KWin
0022 {
0023 namespace Decoration
0024 {
0025 DecorationPalette::DecorationPalette(const QString &colorScheme)
0026     : m_colorScheme(QFileInfo(colorScheme).isAbsolute() ? colorScheme : QStandardPaths::locate(QStandardPaths::GenericConfigLocation, colorScheme))
0027 {
0028     if (!m_colorScheme.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)) && colorScheme == QStringLiteral("kdeglobals")) {
0029         // kdeglobals doesn't exist so create it. This is needed to monitor it using QFileSystemWatcher.
0030         auto config = KSharedConfig::openConfig(colorScheme, KConfig::SimpleConfig);
0031         KConfigGroup wmConfig(config, QStringLiteral("WM"));
0032         wmConfig.writeEntry("FakeEntryToKeepThisGroup", true);
0033         config->sync();
0034 
0035         m_colorScheme = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, colorScheme);
0036     }
0037     m_watcher.addPath(m_colorScheme);
0038     connect(&m_watcher, &QFileSystemWatcher::fileChanged, [this]() {
0039         m_watcher.addPath(m_colorScheme);
0040         update();
0041         emit changed();
0042     });
0043 
0044     update();
0045 }
0046 
0047 bool DecorationPalette::isValid() const
0048 {
0049     return m_activeTitleBarColor.isValid();
0050 }
0051 
0052 QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const
0053 {
0054     using KDecoration2::ColorGroup;
0055     using KDecoration2::ColorRole;
0056 
0057     switch (role) {
0058     case ColorRole::Frame:
0059         switch (group) {
0060         case ColorGroup::Active:
0061             return m_activeFrameColor;
0062         case ColorGroup::Inactive:
0063             return m_inactiveFrameColor;
0064         default:
0065             return QColor();
0066         }
0067     case ColorRole::TitleBar:
0068         switch (group) {
0069         case ColorGroup::Active:
0070             return m_activeTitleBarColor;
0071         case ColorGroup::Inactive:
0072             return m_inactiveTitleBarColor;
0073         default:
0074             return QColor();
0075         }
0076     case ColorRole::Foreground:
0077         switch (group) {
0078         case ColorGroup::Active:
0079             return m_activeForegroundColor;
0080         case ColorGroup::Inactive:
0081             return m_inactiveForegroundColor;
0082         case ColorGroup::Warning:
0083             return m_warningForegroundColor;
0084         default:
0085             return QColor();
0086         }
0087     default:
0088         return QColor();
0089     }
0090 }
0091 
0092 QPalette DecorationPalette::palette() const
0093 {
0094     return m_palette;
0095 }
0096 
0097 void DecorationPalette::update()
0098 {
0099     auto config = KSharedConfig::openConfig(m_colorScheme, KConfig::SimpleConfig);
0100     KConfigGroup wmConfig(config, QStringLiteral("WM"));
0101 
0102     if (!wmConfig.exists() && !m_colorScheme.endsWith(QStringLiteral("/kdeglobals"))) {
0103         return;
0104     }
0105 
0106     m_palette = KColorScheme::createApplicationPalette(config);
0107 
0108     m_activeFrameColor = wmConfig.readEntry("frame", m_palette.color(QPalette::Active, QPalette::Window));
0109     m_inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_activeFrameColor);
0110     m_activeTitleBarColor = wmConfig.readEntry("activeBackground", m_palette.color(QPalette::Active, QPalette::Highlight));
0111     m_inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_inactiveFrameColor);
0112     m_activeForegroundColor = wmConfig.readEntry("activeForeground", m_palette.color(QPalette::Active, QPalette::HighlightedText));
0113     m_inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_activeForegroundColor.darker());
0114 
0115     KConfigGroup windowColorsConfig(config, QStringLiteral("Colors:Window"));
0116     m_warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2));
0117 }
0118 
0119 }
0120 }
0121 
0122 #include "moc_decorationpalette.cpp"