Changeset View
Changeset View
Standalone View
Standalone View
kded/kwin_bridge/decorationpalette.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright 2014 Martin Gräßlin <mgraesslin@kde.org> | ||||
6 | Copyright 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr> | ||||
7 | Copyright 2015 Mika Allan Rauhala <mika.allan.rauhala@gmail.com> | ||||
8 | | ||||
9 | This program is free software; you can redistribute it and/or modify | ||||
10 | it under the terms of the GNU General Public License as published by | ||||
11 | the Free Software Foundation; either version 2 of the License, or | ||||
12 | (at your option) any later version. | ||||
13 | | ||||
14 | This program is distributed in the hope that it will be useful, | ||||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
17 | GNU General Public License for more details. | ||||
18 | | ||||
19 | You should have received a copy of the GNU General Public License | ||||
20 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
21 | *********************************************************************/ | ||||
22 | | ||||
23 | #include "decorationpalette.h" | ||||
24 | | ||||
25 | #include <KConfigGroup> | ||||
26 | #include <KSharedConfig> | ||||
27 | #include <KColorScheme> | ||||
28 | | ||||
29 | #include <QPalette> | ||||
30 | #include <QFileInfo> | ||||
31 | #include <QStandardPaths> | ||||
32 | | ||||
33 | namespace KWin | ||||
34 | { | ||||
35 | namespace Decoration | ||||
36 | { | ||||
37 | | ||||
38 | DecorationPalette::DecorationPalette(const QString &colorScheme) | ||||
39 | : m_colorScheme(QFileInfo(colorScheme).isAbsolute() | ||||
40 | ? colorScheme | ||||
41 | : QStandardPaths::locate(QStandardPaths::GenericConfigLocation, colorScheme)) | ||||
42 | { | ||||
43 | if (!m_colorScheme.startsWith(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)) && colorScheme == QStringLiteral("kdeglobals")) { | ||||
44 | // kdeglobals doesn't exist so create it. This is needed to monitor it using QFileSystemWatcher. | ||||
45 | auto config = KSharedConfig::openConfig(colorScheme, KConfig::SimpleConfig); | ||||
46 | KConfigGroup wmConfig(config, QStringLiteral("WM")); | ||||
47 | wmConfig.writeEntry("FakeEntryToKeepThisGroup", true); | ||||
48 | config->sync(); | ||||
49 | | ||||
50 | m_colorScheme = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, colorScheme); | ||||
51 | } | ||||
52 | m_watcher.addPath(m_colorScheme); | ||||
53 | connect(&m_watcher, &QFileSystemWatcher::fileChanged, [this]() { | ||||
54 | m_watcher.addPath(m_colorScheme); | ||||
55 | update(); | ||||
56 | emit changed(); | ||||
57 | }); | ||||
58 | | ||||
59 | update(); | ||||
60 | } | ||||
61 | | ||||
62 | bool DecorationPalette::isValid() const | ||||
63 | { | ||||
64 | return m_activeTitleBarColor.isValid(); | ||||
65 | } | ||||
66 | | ||||
67 | QColor DecorationPalette::color(KDecoration2::ColorGroup group, KDecoration2::ColorRole role) const | ||||
68 | { | ||||
69 | using KDecoration2::ColorRole; | ||||
70 | using KDecoration2::ColorGroup; | ||||
71 | | ||||
72 | switch (role) { | ||||
73 | case ColorRole::Frame: | ||||
74 | switch (group) { | ||||
75 | case ColorGroup::Active: | ||||
76 | return m_activeFrameColor; | ||||
77 | case ColorGroup::Inactive: | ||||
78 | return m_inactiveFrameColor; | ||||
79 | default: | ||||
80 | return QColor(); | ||||
81 | } | ||||
82 | case ColorRole::TitleBar: | ||||
83 | switch (group) { | ||||
84 | case ColorGroup::Active: | ||||
85 | return m_activeTitleBarColor; | ||||
86 | case ColorGroup::Inactive: | ||||
87 | return m_inactiveTitleBarColor; | ||||
88 | default: | ||||
89 | return QColor(); | ||||
90 | } | ||||
91 | case ColorRole::Foreground: | ||||
92 | switch (group) { | ||||
93 | case ColorGroup::Active: | ||||
94 | return m_activeForegroundColor; | ||||
95 | case ColorGroup::Inactive: | ||||
96 | return m_inactiveForegroundColor; | ||||
97 | case ColorGroup::Warning: | ||||
98 | return m_warningForegroundColor; | ||||
99 | default: | ||||
100 | return QColor(); | ||||
101 | } | ||||
102 | default: | ||||
103 | return QColor(); | ||||
104 | } | ||||
105 | } | ||||
106 | | ||||
107 | QPalette DecorationPalette::palette() const | ||||
108 | { | ||||
109 | return m_palette; | ||||
110 | } | ||||
111 | | ||||
112 | void DecorationPalette::update() | ||||
113 | { | ||||
114 | auto config = KSharedConfig::openConfig(m_colorScheme, KConfig::SimpleConfig); | ||||
115 | KConfigGroup wmConfig(config, QStringLiteral("WM")); | ||||
116 | | ||||
117 | if (!wmConfig.exists() && !m_colorScheme.endsWith(QStringLiteral("/kdeglobals"))) { | ||||
118 | // qCWarning(KWIN_DECORATIONS) << "Invalid color scheme" << m_colorScheme << "lacks WM group"; | ||||
119 | return; | ||||
120 | } | ||||
121 | | ||||
122 | m_palette = KColorScheme::createApplicationPalette(config); | ||||
123 | | ||||
124 | m_activeFrameColor = wmConfig.readEntry("frame", m_palette.color(QPalette::Active, QPalette::Window)); | ||||
125 | m_inactiveFrameColor = wmConfig.readEntry("inactiveFrame", m_activeFrameColor); | ||||
126 | m_activeTitleBarColor = wmConfig.readEntry("activeBackground", m_palette.color(QPalette::Active, QPalette::Highlight)); | ||||
127 | m_inactiveTitleBarColor = wmConfig.readEntry("inactiveBackground", m_inactiveFrameColor); | ||||
128 | m_activeForegroundColor = wmConfig.readEntry("activeForeground", m_palette.color(QPalette::Active, QPalette::HighlightedText)); | ||||
129 | m_inactiveForegroundColor = wmConfig.readEntry("inactiveForeground", m_activeForegroundColor.darker()); | ||||
130 | | ||||
131 | KConfigGroup windowColorsConfig(config, QStringLiteral("Colors:Window")); | ||||
132 | m_warningForegroundColor = windowColorsConfig.readEntry("ForegroundNegative", QColor(237, 21, 2)); | ||||
133 | | ||||
134 | } | ||||
135 | | ||||
136 | } | ||||
137 | } |