KWin
Loading...
Searching...
No Matches
decorationpalette.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
6 SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
7 SPDX-FileCopyrightText: 2015 Mika Allan Rauhala <mika.allan.rauhala@gmail.com>
8 SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
9
10 SPDX-License-Identifier: GPL-2.0-or-later
11*/
12
13#include "decorationpalette.h"
14
15#include <KConfigGroup>
16
17#include <QFileInfo>
18#include <QStandardPaths>
19
20namespace KWin
21{
22namespace Decoration
23{
24
25DecorationPalette::DecorationPalette(const QString &colorScheme)
26 : m_colorScheme(colorScheme != QStringLiteral("kdeglobals") ? colorScheme : QString())
27{
28 if (m_colorScheme.isEmpty()) {
29 m_colorSchemeConfig = KSharedConfig::openConfig(m_colorScheme, KConfig::FullConfig);
30 } else {
31 m_colorSchemeConfig = KSharedConfig::openConfig(m_colorScheme, KConfig::SimpleConfig);
32 }
33 m_watcher = KConfigWatcher::create(m_colorSchemeConfig);
34
35 connect(m_watcher.data(), &KConfigWatcher::configChanged, this, &DecorationPalette::update);
36
37 update();
38}
39
41{
42 return true;
43}
44
45QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const
46{
47 using KDecoration2::ColorGroup;
48 using KDecoration2::ColorRole;
49
50 if (m_legacyColors.has_value()) {
51 switch (role) {
52 case ColorRole::Frame:
53 switch (group) {
54 case ColorGroup::Active:
55 return m_legacyColors->activeFrameColor;
56 case ColorGroup::Inactive:
57 return m_legacyColors->inactiveFrameColor;
58 default:
59 return QColor();
60 }
61 case ColorRole::TitleBar:
62 switch (group) {
63 case ColorGroup::Active:
64 return m_legacyColors->activeTitleBarColor;
65 case ColorGroup::Inactive:
66 return m_legacyColors->inactiveTitleBarColor;
67 default:
68 return QColor();
69 }
70 case ColorRole::Foreground:
71 switch (group) {
72 case ColorGroup::Active:
73 return m_legacyColors->activeForegroundColor;
74 case ColorGroup::Inactive:
75 return m_legacyColors->inactiveForegroundColor;
76 case ColorGroup::Warning:
77 return m_legacyColors->warningForegroundColor;
78 default:
79 return QColor();
80 }
81 default:
82 return QColor();
83 }
84 }
85
86 switch (role) {
87 case ColorRole::Frame:
88 switch (group) {
89 case ColorGroup::Active:
90 return m_colors.active.background().color();
91 case ColorGroup::Inactive:
92 return m_colors.inactive.background().color();
93 default:
94 return QColor();
95 }
96 case ColorRole::TitleBar:
97 switch (group) {
98 case ColorGroup::Active:
99 return m_colors.active.background().color();
100 case ColorGroup::Inactive:
101 return m_colors.inactive.background().color();
102 default:
103 return QColor();
104 }
105 case ColorRole::Foreground:
106 switch (group) {
107 case ColorGroup::Active:
108 return m_colors.active.foreground().color();
109 case ColorGroup::Inactive:
110 return m_colors.inactive.foreground().color();
111 case ColorGroup::Warning:
112 return m_colors.inactive.foreground(KColorScheme::ForegroundRole::NegativeText).color();
113 default:
114 return QColor();
115 }
116 default:
117 return QColor();
118 }
119}
120
122{
123 return m_palette;
124}
125
126void DecorationPalette::update()
127{
128 m_colorSchemeConfig->sync();
129 m_palette = KColorScheme::createApplicationPalette(m_colorSchemeConfig);
130
131 if (KColorScheme::isColorSetSupported(m_colorSchemeConfig, KColorScheme::Header)) {
132 m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Header, m_colorSchemeConfig);
133 m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Header, m_colorSchemeConfig);
134 m_legacyColors.reset();
135 } else {
136 KConfigGroup wmConfig(m_colorSchemeConfig, QStringLiteral("WM"));
137
138 if (!wmConfig.exists()) {
139 m_colors.active = KColorScheme(QPalette::Normal, KColorScheme::Window, m_colorSchemeConfig);
140 m_colors.inactive = KColorScheme(QPalette::Inactive, KColorScheme::Window, m_colorSchemeConfig);
141 m_legacyColors.reset();
142 return;
143 }
144
145 m_legacyColors = LegacyColors{};
146 m_legacyColors->activeFrameColor = wmConfig.readEntry("frame", m_palette.color(QPalette::Active, QPalette::Window));
147 m_legacyColors->inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_legacyColors->activeFrameColor);
148 m_legacyColors->activeTitleBarColor = wmConfig.readEntry("activeBackground", m_palette.color(QPalette::Active, QPalette::Highlight));
149 m_legacyColors->inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_legacyColors->inactiveTitleBarColor);
150 m_legacyColors->activeForegroundColor = wmConfig.readEntry("activeForeground", m_palette.color(QPalette::Active, QPalette::HighlightedText));
151 m_legacyColors->inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_legacyColors->activeForegroundColor.darker());
152
153 KConfigGroup windowColorsConfig(m_colorSchemeConfig, QStringLiteral("Colors:Window"));
154 m_legacyColors->warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2));
155 }
156
157 Q_EMIT changed();
158}
159
160}
161}
162
163#include "moc_decorationpalette.cpp"
DecorationPalette(const QString &colorScheme)
QColor color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const