File indexing completed on 2024-05-19 16:34:03

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