-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoration.cpp
121 lines (99 loc) · 3.36 KB
/
decoration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "decoration.hpp"
#include <QPainter>
#include <KDecoration2/DecoratedClient>
#include <KDecoration2/DecorationSettings>
#include <KPluginFactory>
#include <KSharedConfig>
K_PLUGIN_FACTORY_WITH_JSON(ArstotzkaDecorationFactory,
"metadata.json",
registerPlugin<Arstotzka::Decoration>();)
using namespace std;
using namespace KDecoration2;
namespace Arstotzka {
Decoration::Decoration(QObject *parent, const QVariantList &args)
: KDecoration2::Decoration(parent, args) {
}
bool Decoration::init() {
configWatcher = KConfigWatcher::create(KSharedConfig::openConfig("kdeglobals"));
setBorderSizes();
connectEvents();
updateColors();
return true;
}
void Decoration::paint(QPainter *painter, const QRect &repaintRegion) {
if (!painter) { return; }
DecoratedClient *client = this->client();
QRect windowRect = rect();
painter->save();
painter->setPen(Qt::NoPen);
if (client->isActive()) {
painter->setBrush(activeColor);
} else {
painter->setBrush(inactiveColor);
}
painter->drawRect(windowRect);
painter->restore();
}
void Decoration::updateColors() {
KSharedConfig::Ptr colorsConfig = KSharedConfig::openConfig("kdeglobals");
KConfigGroup group = colorsConfig->group("Colors:Window");
activeColor = group.readEntry("DecorationFocus", QColor(255, 0, 0));
inactiveColor = group.readEntry("BackgroundNormal", QColor(0, 0, 0));
}
inline constexpr int sizeToInt(const BorderSize size) {
switch (size) {
case BorderSize::Oversized:
return 10;
case BorderSize::VeryHuge:
return 6;
case BorderSize::Huge:
return 5;
case BorderSize::VeryLarge:
return 4;
case BorderSize::Large:
return 3;
case BorderSize::NoSides:
case BorderSize::Normal:
return 2;
case BorderSize::None:
return 0;
case BorderSize::Tiny:
default:
return 1;
}
}
void Decoration::setBorderSizes() {
const DecoratedClient *client = this->client();
const shared_ptr<DecorationSettings> config = settings();
const BorderSize desired = config->borderSize();
const int base = config->smallSpacing();
const int size = base * sizeToInt(desired);
const int sides = (desired == BorderSize::NoSides) ? 0 : size;
setBorders(QMargins(sides, size, sides, size));
}
void Decoration::connectEvents() {
DecoratedClient *clientPtr = this->client();
DecorationSettings *settingsPtr = settings().get();
connect(clientPtr,
&DecoratedClient::activeChanged,
this,
[this](bool thisIsLongToPreventFmtCollapse) { this->update(); });
connect(settingsPtr,
&DecorationSettings::borderSizeChanged,
this,
&Decoration::setBorderSizes);
connect(configWatcher.data(),
&KConfigWatcher::configChanged,
this,
[this](const KConfigGroup &group, const QByteArrayList &names) {
if (group.name() != QStringLiteral("General")) { return; }
if (false // I want nice alignment
|| names.contains(QByteArrayLiteral("ColorScheme"))
|| names.contains(QByteArrayLiteral("AccentColor"))) {
updateColors();
this->update();
}
});
}
}
#include "decoration.moc"