-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoration.cpp
130 lines (108 loc) · 3.44 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
122
123
124
125
126
127
128
129
130
#include "decoration.hpp"
#include <KDecoration3/DecoratedWindow>
#include <KDecoration3/DecorationSettings>
#include <KPluginFactory>
#include <QPainter>
K_PLUGIN_FACTORY_WITH_JSON(ArstotzkaDecorationFactory,
"metadata.json",
registerPlugin<Arstotzka::Decoration>();)
using namespace std;
using namespace KDecoration3;
namespace Arstotzka {
Decoration::Decoration(QObject* parent, const QVariantList& args)
: KDecoration3::Decoration(parent, args) {}
bool
Decoration::init() {
watcher = KConfigWatcher::create(KSharedConfig::openConfig("kdeglobals"));
this->connectEvents();
this->setBorderSizes();
this->updateColors();
return true;
}
void
Decoration::paint(QPainter* painter, const QRectF& repaintRegion) {
if (!painter)
return;
const DecoratedWindow* window = this->window();
const QRectF rect = this->rect();
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(window->isActive() ? active : inactive);
painter->drawRect(rect);
painter->restore();
}
void
Decoration::updateColors() {
const KSharedConfig::Ptr colors = KSharedConfig::openConfig("kdeglobals");
const KConfigGroup group = colors->group("Colors:Window");
active = group.readEntry("DecorationFocus", QColor(255, 0, 0));
inactive = group.readEntry("BackgroundNormal", QColor(0, 0, 0));
}
inline constexpr int
sizeToInt(const BorderSize size) noexcept {
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 DecorationSettings* settings = this->settings().get();
const BorderSize desired = settings->borderSize();
const int base = settings->smallSpacing();
const int size = base * sizeToInt(desired);
const int sides = (BorderSize::NoSides == desired) ? 0 : size;
this->setBorders(QMargins(sides, size, sides, size));
}
const QString General = QStringLiteral("General");
const QByteArray Scheme = QByteArrayLiteral("ColorScheme");
const QByteArray Accent = QByteArrayLiteral("AccentColor");
void
Decoration::connectEvents() {
const DecoratedWindow* window = this->window();
const DecorationSettings* settings = this->settings().get();
const KConfigWatcher* const config = watcher.data();
connect(
window,
&DecoratedWindow::activeChanged,
this,
[this](bool _______) { this->update(); }
);
connect(
settings,
&DecorationSettings::borderSizeChanged,
this,
&Decoration::setBorderSizes
);
connect(
config,
&KConfigWatcher::configChanged,
this,
[this](const KConfigGroup& group, const QByteArrayList& names) {
bool general = group.name() == General;
bool section = names.contains(Scheme) || names.contains(Accent);
if (!general || !section)
return;
this->updateColors();
this->update();
}
);
}
} // namespace Arstotzka
#include "decoration.moc"