Skip to content

Commit a77f9b0

Browse files
committed
wayland/shortcuts-inhibit: code review feedback
1 parent 9654b19 commit a77f9b0

File tree

4 files changed

+44
-64
lines changed

4 files changed

+44
-64
lines changed

src/wayland/shortcuts_inhibit/inhibitor.cpp

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <private/qwaylandwindow_p.h>
44
#include <qlogging.h>
55
#include <qobject.h>
6-
#include <qtmetamacros.h>
76

87
#include "../../window/proxywindow.hpp"
98
#include "../../window/windowinterface.hpp"
@@ -24,35 +23,18 @@ ShortcutsInhibitor::~ShortcutsInhibitor() {
2423
auto* manager = impl::ShortcutsInhibitManager::instance();
2524
if (!manager) return;
2625

27-
QObject::disconnect(this->inhibitor, nullptr, this, nullptr);
2826
manager->unrefShortcutsInhibitor(this->inhibitor);
2927
}
3028

3129
QObject* ShortcutsInhibitor::window() const { return this->bWindowObject; }
3230

33-
void ShortcutsInhibitor::setWindow(QObject* window) {
34-
if (window == this->bWindowObject) return;
35-
36-
auto* proxyWindow = qobject_cast<ProxyWindowBase*>(window);
37-
38-
if (proxyWindow == nullptr) {
39-
if (auto* iface = qobject_cast<WindowInterface*>(window)) {
40-
proxyWindow = iface->proxyWindow();
41-
}
42-
}
43-
44-
this->bWindowObject = proxyWindow ? window : nullptr;
45-
}
46-
47-
bool ShortcutsInhibitor::isActive() const {
48-
return this->inhibitor ? this->inhibitor->isActive() : false;
49-
}
31+
void ShortcutsInhibitor::setWindow(QObject* window) { this->bWindowObject = window; }
5032

5133
void ShortcutsInhibitor::boundWindowChanged() {
5234
auto* window = this->bBoundWindow.value();
5335
auto* proxyWindow = qobject_cast<ProxyWindowBase*>(window);
5436

55-
if (proxyWindow == nullptr) {
37+
if (!proxyWindow) {
5638
if (auto* iface = qobject_cast<WindowInterface*>(window)) {
5739
proxyWindow = iface->proxyWindow();
5840
}
@@ -90,8 +72,6 @@ void ShortcutsInhibitor::boundWindowChanged() {
9072

9173
this->onWindowVisibilityChanged();
9274
}
93-
94-
emit this->windowChanged();
9575
}
9676

9777
void ShortcutsInhibitor::onWindowDestroyed() {
@@ -107,6 +87,7 @@ void ShortcutsInhibitor::onWindowVisibilityChanged() {
10787
if (!window->handle()) window->create();
10888

10989
auto* waylandWindow = dynamic_cast<QWaylandWindow*>(window->handle());
90+
if (!waylandWindow) return;
11091
if (waylandWindow == this->mWaylandWindow) return;
11192
this->mWaylandWindow = waylandWindow;
11293

@@ -147,19 +128,15 @@ void ShortcutsInhibitor::onWaylandSurfaceCreated() {
147128
}
148129

149130
if (this->inhibitor) {
150-
QObject::disconnect(this->inhibitor, nullptr, this, nullptr);
151-
manager->unrefShortcutsInhibitor(this->inhibitor);
152-
this->inhibitor = nullptr;
131+
qFatal("ShortcutsInhibitor: inhibitor already exists when creating surface");
153132
}
133+
154134
this->inhibitor = manager->createShortcutsInhibitor(this->mWaylandWindow);
155135

156136
if (this->inhibitor) {
157-
QObject::connect(
158-
this->inhibitor,
159-
&impl::ShortcutsInhibitor::activeChanged,
160-
this,
161-
&ShortcutsInhibitor::activeChanged
162-
);
137+
this->bActive.setBinding([this]() {
138+
return this->inhibitor ? this->inhibitor->bindableActive().value() : false;
139+
});
163140
}
164141
}
165142

@@ -169,12 +146,8 @@ void ShortcutsInhibitor::onWaylandSurfaceDestroyed() {
169146
auto* manager = impl::ShortcutsInhibitManager::instance();
170147
if (!manager) return;
171148

172-
QObject::disconnect(this->inhibitor, nullptr, this, nullptr);
173-
auto wasActive = this->inhibitor->isActive();
174149
manager->unrefShortcutsInhibitor(this->inhibitor);
175150
this->inhibitor = nullptr;
176-
177-
if (wasActive) { emit this->activeChanged(); }
178151
}
179152

180153
} // namespace qs::wayland::shortcuts_inhibit

src/wayland/shortcuts_inhibit/inhibitor.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ShortcutsInhibitor: public QObject {
3838
/// The compositor may deactivate the inhibitor if the user requests normal shortcuts to be restored.
3939
/// When inactive, there is no way to programmatically reactivate it - the user must do so through
4040
/// compositor-specific mechanisms.
41-
Q_PROPERTY(bool active READ isActive NOTIFY activeChanged);
41+
Q_PROPERTY(bool active READ default NOTIFY activeChanged BINDABLE bindableActive);
4242
// clang-format on
4343

4444
public:
@@ -50,7 +50,7 @@ class ShortcutsInhibitor: public QObject {
5050
void setWindow(QObject* window);
5151

5252
[[nodiscard]] QBindable<bool> bindableEnabled() { return &this->bEnabled; }
53-
[[nodiscard]] bool isActive() const;
53+
[[nodiscard]] QBindable<bool> bindableActive() { return &this->bActive; }
5454

5555
signals:
5656
void enabledChanged();
@@ -75,6 +75,7 @@ private slots:
7575
Q_OBJECT_BINDABLE_PROPERTY(ShortcutsInhibitor, bool, bEnabled, &ShortcutsInhibitor::enabledChanged);
7676
Q_OBJECT_BINDABLE_PROPERTY(ShortcutsInhibitor, QObject*, bWindowObject, &ShortcutsInhibitor::windowChanged);
7777
Q_OBJECT_BINDABLE_PROPERTY(ShortcutsInhibitor, QObject*, bBoundWindow, &ShortcutsInhibitor::boundWindowChanged);
78+
Q_OBJECT_BINDABLE_PROPERTY(ShortcutsInhibitor, bool, bActive, &ShortcutsInhibitor::activeChanged);
7879
// clang-format on
7980
};
8081

src/wayland/shortcuts_inhibit/proto.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <private/qwaylandwindow_p.h>
77
#include <qlogging.h>
88
#include <qloggingcategory.h>
9-
#include <qtmetamacros.h>
9+
#include <qpair.h>
1010
#include <qwaylandclientextension.h>
1111

1212
#include "../../core/logcat.hpp"
@@ -40,40 +40,46 @@ ShortcutsInhibitManager::createShortcutsInhibitor(QtWaylandClient::QWaylandWindo
4040
auto* wlSurface = surface->surface();
4141

4242
if (this->inhibitors.contains(wlSurface)) {
43-
auto* inhibitor = this->inhibitors.value(wlSurface);
44-
this->refCounts[inhibitor]++;
45-
qCDebug(logShortcutsInhibit) << "Reusing existing inhibitor" << inhibitor << "for surface"
46-
<< wlSurface << "- refcount:" << this->refCounts[inhibitor];
47-
return inhibitor;
43+
auto& pair = this->inhibitors[wlSurface];
44+
pair.second++;
45+
qCDebug(logShortcutsInhibit) << "Reusing existing inhibitor" << pair.first << "for surface"
46+
<< wlSurface << "- refcount:" << pair.second;
47+
return pair.first;
4848
}
4949

5050
auto* inhibitor =
5151
new ShortcutsInhibitor(this->inhibit_shortcuts(wlSurface, inputDevice->object()), wlSurface);
52-
this->inhibitors.insert(wlSurface, inhibitor);
53-
this->refCounts.insert(inhibitor, 1);
52+
this->inhibitors.insert(wlSurface, qMakePair(inhibitor, 1));
5453
qCDebug(logShortcutsInhibit) << "Created inhibitor" << inhibitor << "for surface" << wlSurface;
5554
return inhibitor;
5655
}
5756

5857
void ShortcutsInhibitManager::refShortcutsInhibitor(ShortcutsInhibitor* inhibitor) {
59-
if (inhibitor && this->refCounts.contains(inhibitor)) {
60-
this->refCounts[inhibitor]++;
61-
qCDebug(logShortcutsInhibit) << "Incremented refcount for inhibitor" << inhibitor
62-
<< "- refcount:" << this->refCounts[inhibitor];
63-
}
58+
if (!inhibitor) return;
59+
60+
auto* surface = inhibitor->surface();
61+
if (!this->inhibitors.contains(surface)) return;
62+
63+
auto& pair = this->inhibitors[surface];
64+
pair.second++;
65+
qCDebug(logShortcutsInhibit) << "Incremented refcount for inhibitor" << inhibitor
66+
<< "- refcount:" << pair.second;
6467
}
6568

6669
void ShortcutsInhibitManager::unrefShortcutsInhibitor(ShortcutsInhibitor* inhibitor) {
67-
if (!inhibitor || !this->refCounts.contains(inhibitor)) return;
70+
if (!inhibitor) return;
71+
72+
auto* surface = inhibitor->surface();
73+
if (!this->inhibitors.contains(surface)) return;
6874

69-
this->refCounts[inhibitor]--;
75+
auto& pair = this->inhibitors[surface];
76+
pair.second--;
7077
qCDebug(logShortcutsInhibit) << "Decremented refcount for inhibitor" << inhibitor
71-
<< "- refcount:" << this->refCounts[inhibitor];
78+
<< "- refcount:" << pair.second;
7279

73-
if (this->refCounts[inhibitor] <= 0) {
80+
if (pair.second <= 0) {
7481
qCDebug(logShortcutsInhibit) << "Refcount reached 0, destroying inhibitor" << inhibitor;
75-
this->inhibitors.remove(inhibitor->surface());
76-
this->refCounts.remove(inhibitor);
82+
this->inhibitors.remove(surface);
7783
delete inhibitor;
7884
}
7985
}
@@ -85,14 +91,12 @@ ShortcutsInhibitor::~ShortcutsInhibitor() {
8591

8692
void ShortcutsInhibitor::zwp_keyboard_shortcuts_inhibitor_v1_active() {
8793
qCDebug(logShortcutsInhibit) << "Inhibitor became active" << this;
88-
this->mActive = true;
89-
emit this->activeChanged();
94+
this->bActive = true;
9095
}
9196

9297
void ShortcutsInhibitor::zwp_keyboard_shortcuts_inhibitor_v1_inactive() {
9398
qCDebug(logShortcutsInhibit) << "Inhibitor became inactive" << this;
94-
this->mActive = false;
95-
emit this->activeChanged();
99+
this->bActive = false;
96100
}
97101

98102
} // namespace qs::wayland::shortcuts_inhibit::impl

src/wayland/shortcuts_inhibit/proto.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <private/qwaylandwindow_p.h>
4+
#include <qpair.h>
5+
#include <qproperty.h>
46
#include <qtclasshelpermacros.h>
57
#include <qwayland-keyboard-shortcuts-inhibit-unstable-v1.h>
68
#include <qwaylandclientextension.h>
@@ -24,8 +26,7 @@ class ShortcutsInhibitManager
2426
static ShortcutsInhibitManager* instance();
2527

2628
private:
27-
QHash<wl_surface*, ShortcutsInhibitor*> inhibitors;
28-
QHash<ShortcutsInhibitor*, int> refCounts;
29+
QHash<wl_surface*, QPair<ShortcutsInhibitor*, int>> inhibitors;
2930
};
3031

3132
class ShortcutsInhibitor
@@ -41,7 +42,8 @@ class ShortcutsInhibitor
4142
~ShortcutsInhibitor() override;
4243
Q_DISABLE_COPY_MOVE(ShortcutsInhibitor);
4344

44-
[[nodiscard]] bool isActive() const { return this->mActive; }
45+
[[nodiscard]] QBindable<bool> bindableActive() { return &this->bActive; }
46+
[[nodiscard]] bool isActive() const { return this->bActive; }
4547
[[nodiscard]] wl_surface* surface() const { return this->mSurface; }
4648

4749
signals:
@@ -52,8 +54,8 @@ class ShortcutsInhibitor
5254
void zwp_keyboard_shortcuts_inhibitor_v1_inactive() override;
5355

5456
private:
55-
bool mActive = false;
5657
wl_surface* mSurface;
58+
Q_OBJECT_BINDABLE_PROPERTY(ShortcutsInhibitor, bool, bActive, &ShortcutsInhibitor::activeChanged);
5759
};
5860

5961
} // namespace qs::wayland::shortcuts_inhibit::impl

0 commit comments

Comments
 (0)