File indexing completed on 2024-11-10 04:56:41

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     SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
0009 
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 */
0012 
0013 #include "decorationpalette.h"
0014 
0015 #include <KConfigGroup>
0016 
0017 #include <QFileInfo>
0018 #include <QStandardPaths>
0019 
0020 namespace KWin
0021 {
0022 namespace Decoration
0023 {
0024 
0025 DecorationPalette::DecorationPalette(const QString &colorScheme)
0026     : m_colorScheme(colorScheme != QStringLiteral("kdeglobals") ? colorScheme : QString())
0027 {
0028     if (m_colorScheme.isEmpty()) {
0029         m_colorSchemeConfig = KSharedConfig::openConfig(m_colorScheme, KConfig::FullConfig);
0030     } else {
0031         m_colorSchemeConfig = KSharedConfig::openConfig(m_colorScheme, KConfig::SimpleConfig);
0032     }
0033     m_watcher = KConfigWatcher::create(m_colorSchemeConfig);
0034 
0035     connect(m_watcher.data(), &KConfigWatcher::configChanged, this, &DecorationPalette::update);
0036 
0037     update();
0038 }
0039 
0040 bool DecorationPalette::isValid() const
0041 {
0042     return true;
0043 }
0044 
0045 QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const
0046 {
0047     using KDecoration2::ColorGroup;
0048     using KDecoration2::ColorRole;
0049 
0050     if (m_legacyColors.has_value()) {
0051         switch (role) {
0052         case ColorRole::Frame:
0053             switch (group) {
0054             case ColorGroup::Active:
0055                 return m_legacyColors->activeFrameColor;
0056             case ColorGroup::Inactive:
0057                 return m_legacyColors->inactiveFrameColor;
0058             default:
0059                 return QColor();
0060             }
0061         case ColorRole::TitleBar:
0062             switch (group) {
0063             case ColorGroup::Active:
0064                 return m_legacyColors->activeTitleBarColor;
0065             case ColorGroup::Inactive:
0066                 return m_legacyColors->inactiveTitleBarColor;
0067             default:
0068                 return QColor();
0069             }
0070         case ColorRole::Foreground:
0071             switch (group) {
0072             case ColorGroup::Active:
0073                 return m_legacyColors->activeForegroundColor;
0074             case ColorGroup::Inactive:
0075                 return m_legacyColors->inactiveForegroundColor;
0076             case ColorGroup::Warning:
0077                 return m_legacyColors->warningForegroundColor;
0078             default:
0079                 return QColor();
0080             }
0081         default:
0082             return QColor();
0083         }
0084     }
0085 
0086     switch (role) {
0087     case ColorRole::Frame:
0088         switch (group) {
0089         case ColorGroup::Active:
0090             return m_colors.active.background().color();
0091         case ColorGroup::Inactive:
0092             return m_colors.inactive.background().color();
0093         default:
0094             return QColor();
0095         }
0096     case ColorRole::TitleBar:
0097         switch (group) {
0098         case ColorGroup::Active:
0099             return m_colors.active.background().color();
0100         case ColorGroup::Inactive:
0101             return m_colors.inactive.background().color();
0102         default:
0103             return QColor();
0104         }
0105     case ColorRole::Foreground:
0106         switch (group) {
0107         case ColorGroup::Active:
0108             return m_colors.active.foreground().color();
0109         case ColorGroup::Inactive:
0110             return m_colors.inactive.foreground().color();
0111         case ColorGroup::Warning:
0112             return m_colors.inactive.foreground(KColorScheme::ForegroundRole::NegativeText).color();
0113         default:
0114             return QColor();
0115         }
0116     default:
0117         return QColor();
0118     }
0119 }
0120 
0121 QPalette DecorationPalette::palette() const
0122 {
0123     return m_palette;
0124 }
0125 
0126 void DecorationPalette::update()
0127 {
0128     m_colorSchemeConfig->sync();
0129     m_palette = KColorScheme::createApplicationPalette(m_colorSchemeConfig);
0130 
0131     if (KColorScheme::isColorSetSupported(m_colorSchemeConfig, KColorScheme::Header)) {
0132         m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Header, m_colorSchemeConfig);
0133         m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Header, m_colorSchemeConfig);
0134         m_legacyColors.reset();
0135     } else {
0136         KConfigGroup wmConfig(m_colorSchemeConfig, QStringLiteral("WM"));
0137 
0138         if (!wmConfig.exists()) {
0139             m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Window, m_colorSchemeConfig);
0140             m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Window, m_colorSchemeConfig);
0141             m_legacyColors.reset();
0142             return;
0143         }
0144 
0145         m_legacyColors = LegacyColors{};
0146         m_legacyColors->activeFrameColor = wmConfig.readEntry("frame", m_palette.color(QPalette::Active, QPalette::Window));
0147         m_legacyColors->inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_legacyColors->activeFrameColor);
0148         m_legacyColors->activeTitleBarColor = wmConfig.readEntry("activeBackground", m_palette.color(QPalette::Active, QPalette::Highlight));
0149         m_legacyColors->inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_legacyColors->inactiveTitleBarColor);
0150         m_legacyColors->activeForegroundColor = wmConfig.readEntry("activeForeground", m_palette.color(QPalette::Active, QPalette::HighlightedText));
0151         m_legacyColors->inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_legacyColors->activeForegroundColor.darker());
0152 
0153         KConfigGroup windowColorsConfig(m_colorSchemeConfig, QStringLiteral("Colors:Window"));
0154         m_legacyColors->warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2));
0155     }
0156 
0157     Q_EMIT changed();
0158 }
0159 
0160 }
0161 }
0162 
0163 #include "moc_decorationpalette.cpp"