Changeset View
Changeset View
Standalone View
Standalone View
kded/kwin_bridge/dummydecorationbridge.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2020 Mikhail Zolotukhin <zomial@protonmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QCoreApplication> | ||||
22 | #include <QMouseEvent> | ||||
23 | | ||||
24 | #include <KDecoration2/Decoration> | ||||
25 | #include <KDecoration2/DecorationSettings> | ||||
26 | #include <KDecoration2/DecoratedClient> | ||||
27 | #include <KDecoration2/Private/DecoratedClientPrivate> | ||||
28 | #include <KDecoration2/Private/DecorationSettingsPrivate> | ||||
29 | #include <KPluginFactory> | ||||
30 | #include <KPluginLoader> | ||||
31 | #include <KPluginMetaData> | ||||
32 | | ||||
33 | #include "dummydecorationbridge.h" | ||||
34 | #include "dummydecorationsettings.h" | ||||
35 | #include "dummydecoratedclient.h" | ||||
36 | | ||||
37 | namespace KDecoration2 | ||||
38 | { | ||||
39 | | ||||
40 | DummyDecorationBridge::DummyDecorationBridge(const QString &decorationTheme, QObject *parent) | ||||
41 | : DecorationBridge(parent) | ||||
42 | , m_factory() | ||||
43 | , m_decoration() | ||||
44 | { | ||||
45 | const QString pluginPath = windowDecorationPluginPath(decorationTheme); | ||||
46 | KPluginLoader loader {pluginPath}; | ||||
47 | m_factory = loader.factory(); | ||||
48 | | ||||
49 | QVariantMap args({ {QStringLiteral("bridge"), QVariant::fromValue(this)} }); | ||||
50 | m_decoration = m_factory->create<KDecoration2::Decoration>(m_factory, QVariantList({args})); | ||||
51 | | ||||
52 | auto decorationSettings = QSharedPointer<KDecoration2::DecorationSettings>::create(this); | ||||
53 | m_decoration->setSettings(decorationSettings); | ||||
54 | m_decoration->init(); | ||||
55 | } | ||||
56 | | ||||
57 | std::unique_ptr< KDecoration2::DecorationSettingsPrivate > DummyDecorationBridge::settings(KDecoration2::DecorationSettings *parent) | ||||
58 | { | ||||
59 | return std::unique_ptr<DummyDecorationSettings>(new DummyDecorationSettings(parent)); | ||||
60 | } | ||||
61 | | ||||
62 | void DummyDecorationBridge::update(KDecoration2::Decoration *decoration, const QRect &geometry) | ||||
63 | { | ||||
64 | Q_UNUSED(decoration) | ||||
65 | Q_UNUSED(geometry) | ||||
66 | } | ||||
67 | | ||||
68 | std::unique_ptr< KDecoration2::DecoratedClientPrivate > DummyDecorationBridge::createClient(KDecoration2::DecoratedClient *client, KDecoration2::Decoration *decoration) | ||||
69 | { | ||||
70 | return std::unique_ptr<DummyDecoratedClient>(new DummyDecoratedClient(client, decoration)); | ||||
71 | } | ||||
72 | | ||||
73 | void DummyDecorationBridge::paintButton(QPainter &painter, const QString &buttonType, const QString &buttonState) | ||||
74 | { | ||||
75 | if (buttonState == "active") { | ||||
76 | paintButtonInPressedState(painter, buttonType); | ||||
77 | } else if (buttonState == "hover") { | ||||
78 | paintButtonInHoveredState(painter, buttonType); | ||||
79 | } else { | ||||
80 | paintButtonInNormalState(painter, buttonType); | ||||
81 | } | ||||
82 | } | ||||
83 | | ||||
84 | QString DummyDecorationBridge::windowDecorationPluginPath(const QString &decorationTheme) const | ||||
85 | { | ||||
86 | const auto decorationPlugins = KPluginLoader::findPlugins(QStringLiteral("org.kde.kdecoration2")); | ||||
87 | | ||||
88 | QString defaultPluginPath; | ||||
89 | | ||||
90 | for (const auto &pluginMetaData : decorationPlugins) { | ||||
91 | if (pluginMetaData.name() == QStringLiteral("Breeze")) { | ||||
92 | defaultPluginPath = pluginMetaData.fileName(); | ||||
93 | } | ||||
94 | | ||||
95 | if (pluginMetaData.name() == decorationTheme) { | ||||
96 | return pluginMetaData.fileName(); | ||||
97 | } | ||||
98 | } | ||||
99 | return defaultPluginPath; | ||||
100 | } | ||||
101 | | ||||
102 | void DummyDecorationBridge::paintButtonInNormalState(QPainter &painter, const QString &buttonType) | ||||
103 | { | ||||
104 | std::unique_ptr<KDecoration2::DecorationButton> button {m_factory->create<KDecoration2::DecorationButton>( | ||||
105 | QStringLiteral("button"), | ||||
106 | m_decoration, | ||||
107 | QVariantList({ | ||||
108 | QVariant::fromValue(strToButtonType(buttonType)), | ||||
109 | QVariant::fromValue(m_decoration) | ||||
110 | }) | ||||
111 | )}; | ||||
112 | | ||||
113 | if (button == nullptr) { | ||||
114 | return; | ||||
115 | } | ||||
116 | | ||||
117 | // TODO What to pass to the second arg? | ||||
118 | button->paint(&painter, {0, 0, 20, 20}); | ||||
119 | } | ||||
120 | | ||||
121 | void DummyDecorationBridge::paintButtonInHoveredState(QPainter &painter, const QString &buttonType) | ||||
122 | { | ||||
123 | std::unique_ptr<KDecoration2::DecorationButton> button {m_factory->create<KDecoration2::DecorationButton>( | ||||
124 | QStringLiteral("button"), | ||||
125 | m_decoration, | ||||
126 | QVariantList({ | ||||
127 | QVariant::fromValue(strToButtonType(buttonType)), | ||||
128 | QVariant::fromValue(m_decoration) | ||||
129 | }) | ||||
130 | )}; | ||||
131 | | ||||
132 | if (button == nullptr) { | ||||
133 | return; | ||||
134 | } | ||||
135 | | ||||
136 | QHoverEvent event { | ||||
137 | QEvent::HoverEnter, | ||||
138 | {5, 5}, | ||||
139 | {4, 4}, | ||||
140 | Qt::NoModifier | ||||
141 | }; | ||||
142 | QCoreApplication::instance()->sendEvent(m_decoration, &event); | ||||
143 | QCoreApplication::instance()->sendEvent(button.get(), &event); | ||||
144 | | ||||
145 | // TODO What to pass to the second arg? | ||||
146 | button->paint(&painter, {0, 0, 20, 20}); | ||||
147 | } | ||||
148 | | ||||
149 | void DummyDecorationBridge::paintButtonInPressedState(QPainter &painter, const QString &buttonType) | ||||
150 | { | ||||
151 | std::unique_ptr<KDecoration2::DecorationButton> button {m_factory->create<KDecoration2::DecorationButton>( | ||||
152 | QStringLiteral("button"), | ||||
153 | m_decoration, | ||||
154 | QVariantList({ | ||||
155 | QVariant::fromValue(strToButtonType(buttonType)), | ||||
156 | QVariant::fromValue(m_decoration) | ||||
157 | }) | ||||
158 | )}; | ||||
159 | | ||||
160 | if (button == nullptr) { | ||||
161 | return; | ||||
162 | } | ||||
163 | | ||||
164 | QMouseEvent event { | ||||
165 | QEvent::MouseButtonPress, | ||||
166 | {5, 5}, | ||||
167 | Qt::LeftButton, | ||||
168 | Qt::LeftButton, | ||||
169 | Qt::NoModifier | ||||
170 | }; | ||||
171 | QCoreApplication::instance()->sendEvent(m_decoration, &event); | ||||
172 | QCoreApplication::instance()->sendEvent(button.get(), &event); | ||||
173 | | ||||
174 | // TODO What to pass to the second arg? | ||||
175 | button->paint(&painter, {0, 0, 20, 20}); | ||||
176 | } | ||||
177 | | ||||
178 | KDecoration2::DecorationButtonType DummyDecorationBridge::strToButtonType(const QString &type) const | ||||
179 | { | ||||
180 | if (type == QStringLiteral("minimize")) { | ||||
181 | return KDecoration2::DecorationButtonType::Minimize; | ||||
182 | } else if (type == QStringLiteral("maximize")) { | ||||
183 | return KDecoration2::DecorationButtonType::Maximize; | ||||
184 | } else { | ||||
185 | return KDecoration2::DecorationButtonType::Close; | ||||
186 | } | ||||
187 | } | ||||
188 | | ||||
189 | } |