Skip to content

Commit 1a20c39

Browse files
committed
i3/ipc: convert to bindable properties
1 parent 3b2d84c commit 1a20c39

File tree

5 files changed

+96
-170
lines changed

5 files changed

+96
-170
lines changed

src/x11/i3/ipc/connection.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void I3Ipc::setFocusedWorkspace(I3Workspace* workspace) {
190190
if (workspace == this->mFocusedWorkspace) return;
191191

192192
if (this->mFocusedWorkspace != nullptr) {
193-
this->mFocusedWorkspace->setFocus(false);
193+
this->mFocusedWorkspace->bindableFocused().setValue(false);
194194
QObject::disconnect(this->mFocusedWorkspace, nullptr, this, nullptr);
195195
}
196196

@@ -202,7 +202,7 @@ void I3Ipc::setFocusedWorkspace(I3Workspace* workspace) {
202202
}
203203

204204
QObject::connect(workspace, &QObject::destroyed, this, &I3Ipc::onFocusedWorkspaceDestroyed);
205-
workspace->setFocus(true);
205+
workspace->bindableFocused().setValue(true);
206206
this->setFocusedMonitor(workspace->monitor());
207207
}
208208

@@ -213,14 +213,14 @@ void I3Ipc::setFocusedMonitor(I3Monitor* monitor) {
213213
if (monitor == this->mFocusedMonitor) return;
214214

215215
if (this->mFocusedMonitor != nullptr) {
216-
this->mFocusedMonitor->setFocus(false);
216+
this->mFocusedMonitor->bindableFocused().setValue(false);
217217
QObject::disconnect(this->mFocusedMonitor, nullptr, this, nullptr);
218218
}
219219

220220
this->mFocusedMonitor = monitor;
221221

222222
if (monitor != nullptr) {
223-
monitor->setFocus(true);
223+
monitor->bindableFocused().setValue(true);
224224
QObject::connect(monitor, &QObject::destroyed, this, &I3Ipc::onFocusedMonitorDestroyed);
225225
}
226226

@@ -264,8 +264,9 @@ void I3Ipc::handleGetWorkspacesEvent(I3IpcEvent* event) {
264264
auto object = entry.toObject().toVariantMap();
265265
auto name = object["name"].toString();
266266

267-
auto workspaceIter =
268-
std::ranges::find_if(mList, [name](const I3Workspace* m) { return m->name() == name; });
267+
auto workspaceIter = std::ranges::find_if(mList, [name](I3Workspace* m) {
268+
return m->bindableName().value() == name;
269+
});
269270

270271
auto* workspace = workspaceIter == mList.end() ? nullptr : *workspaceIter;
271272
auto existed = workspace != nullptr;
@@ -276,7 +277,7 @@ void I3Ipc::handleGetWorkspacesEvent(I3IpcEvent* event) {
276277

277278
workspace->updateFromObject(object);
278279

279-
if (workspace->focused()) {
280+
if (workspace->bindableFocused().value()) {
280281
this->setFocusedWorkspace(workspace);
281282
}
282283

@@ -290,7 +291,7 @@ void I3Ipc::handleGetWorkspacesEvent(I3IpcEvent* event) {
290291
auto removedWorkspaces = QVector<I3Workspace*>();
291292

292293
for (auto* workspace: mList) {
293-
if (!names.contains(workspace->name())) {
294+
if (!names.contains(workspace->bindableName().value())) {
294295
removedWorkspaces.push_back(workspace);
295296
}
296297
}
@@ -320,8 +321,9 @@ void I3Ipc::handleGetOutputsEvent(I3IpcEvent* event) {
320321
auto object = elem.toObject().toVariantMap();
321322
auto name = object["name"].toString();
322323

323-
auto monitorIter =
324-
std::ranges::find_if(mList, [name](const I3Monitor* m) { return m->name() == name; });
324+
auto monitorIter = std::ranges::find_if(mList, [name](I3Monitor* m) {
325+
return m->bindableName().value() == name;
326+
});
325327

326328
auto* monitor = monitorIter == mList.end() ? nullptr : *monitorIter;
327329
auto existed = monitor != nullptr;
@@ -332,7 +334,7 @@ void I3Ipc::handleGetOutputsEvent(I3IpcEvent* event) {
332334

333335
monitor->updateFromObject(object);
334336

335-
if (monitor->focused()) {
337+
if (monitor->bindableFocused().value()) {
336338
this->setFocusedMonitor(monitor);
337339
}
338340

@@ -346,7 +348,7 @@ void I3Ipc::handleGetOutputsEvent(I3IpcEvent* event) {
346348
auto removedMonitors = QVector<I3Monitor*>();
347349

348350
for (auto* monitor: mList) {
349-
if (!names.contains(monitor->name())) {
351+
if (!names.contains(monitor->bindableName().value())) {
350352
removedMonitors.push_back(monitor);
351353
}
352354
}
@@ -413,7 +415,7 @@ void I3Ipc::handleWorkspaceEvent(I3IpcEvent* event) {
413415

414416
if (!existed) {
415417
this->mWorkspaces.insertObject(workspace);
416-
qCInfo(logI3Ipc) << "Added workspace" << workspace->name() << "to list";
418+
qCInfo(logI3Ipc) << "Added workspace" << workspace->bindableName().value() << "to list";
417419
}
418420
} else if (change == "focus") {
419421
auto oldData = event->mData["old"];
@@ -441,7 +443,7 @@ void I3Ipc::handleWorkspaceEvent(I3IpcEvent* event) {
441443
auto* oldWorkspace = this->findWorkspaceByName(name);
442444

443445
if (oldWorkspace != nullptr) {
444-
qCInfo(logI3Ipc) << "Deleting" << oldWorkspace->id() << name;
446+
qCInfo(logI3Ipc) << "Deleting" << oldWorkspace->bindableId().value() << name;
445447

446448
if (this->mFocusedWorkspace == oldWorkspace) {
447449
this->setFocusedWorkspace(nullptr);
@@ -480,23 +482,25 @@ I3Monitor* I3Ipc::monitorFor(QuickshellScreenInfo* screen) {
480482
I3Workspace* I3Ipc::findWorkspaceByID(qint32 id) {
481483
auto list = this->mWorkspaces.valueList();
482484
auto workspaceIter =
483-
std::ranges::find_if(list, [id](const I3Workspace* m) { return m->id() == id; });
485+
std::ranges::find_if(list, [id](I3Workspace* m) { return m->bindableId().value() == id; });
484486

485487
return workspaceIter == list.end() ? nullptr : *workspaceIter;
486488
}
487489

488490
I3Workspace* I3Ipc::findWorkspaceByName(const QString& name) {
489491
auto list = this->mWorkspaces.valueList();
490-
auto workspaceIter =
491-
std::ranges::find_if(list, [name](const I3Workspace* m) { return m->name() == name; });
492+
auto workspaceIter = std::ranges::find_if(list, [name](I3Workspace* m) {
493+
return m->bindableName().value() == name;
494+
});
492495

493496
return workspaceIter == list.end() ? nullptr : *workspaceIter;
494497
}
495498

496499
I3Monitor* I3Ipc::findMonitorByName(const QString& name) {
497500
auto list = this->mMonitors.valueList();
498-
auto monitorIter =
499-
std::ranges::find_if(list, [name](const I3Monitor* m) { return m->name() == name; });
501+
auto monitorIter = std::ranges::find_if(list, [name](I3Monitor* m) {
502+
return m->bindableName().value() == name;
503+
});
500504

501505
return monitorIter == list.end() ? nullptr : *monitorIter;
502506
}

src/x11/i3/ipc/monitor.cpp

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "monitor.hpp"
22

33
#include <qcontainerfwd.h>
4+
#include <qproperty.h>
45
#include <qstring.h>
56
#include <qtmetamacros.h>
67
#include <qtypes.h>
@@ -9,45 +10,24 @@
910

1011
namespace qs::i3::ipc {
1112

12-
qint32 I3Monitor::id() const { return this->mId; };
13-
QString I3Monitor::name() const { return this->mName; };
14-
bool I3Monitor::power() const { return this->mPower; };
1513
I3Workspace* I3Monitor::focusedWorkspace() const { return this->mFocusedWorkspace; };
16-
qint32 I3Monitor::x() const { return this->mX; };
17-
qint32 I3Monitor::y() const { return this->mY; };
18-
qint32 I3Monitor::width() const { return this->mWidth; };
19-
qint32 I3Monitor::height() const { return this->mHeight; };
20-
qreal I3Monitor::scale() const { return this->mScale; };
21-
bool I3Monitor::focused() const { return this->mFocused; };
2214
QVariantMap I3Monitor::lastIpcObject() const { return this->mLastIpcObject; };
2315

2416
void I3Monitor::updateFromObject(const QVariantMap& obj) {
25-
auto id = obj.value("id").value<qint32>();
26-
auto name = obj.value("name").value<QString>();
27-
auto power = obj.value("power").value<bool>();
2817
auto activeWorkspaceId = obj.value("current_workspace").value<QString>();
2918
auto rect = obj.value("rect").toMap();
30-
auto x = rect.value("x").value<qint32>();
31-
auto y = rect.value("y").value<qint32>();
32-
auto width = rect.value("width").value<qint32>();
33-
auto height = rect.value("height").value<qint32>();
34-
auto scale = obj.value("scale").value<qreal>();
35-
auto focused = obj.value("focused").value<bool>();
3619

37-
if (id != this->mId) {
38-
this->mId = id;
39-
emit this->idChanged();
40-
}
41-
42-
if (name != this->mName) {
43-
this->mName = name;
44-
emit this->nameChanged();
45-
}
46-
47-
if (power != this->mPower) {
48-
this->mPower = power;
49-
this->powerChanged();
50-
}
20+
Qt::beginPropertyUpdateGroup();
21+
this->bId = obj.value("id").value<qint32>();
22+
this->bName = obj.value("name").value<QString>();
23+
this->bPower = obj.value("power").value<bool>();
24+
this->bX = rect.value("x").value<qint32>();
25+
this->bY = rect.value("y").value<qint32>();
26+
this->bWidth = rect.value("width").value<qint32>();
27+
this->bHeight = rect.value("height").value<qint32>();
28+
this->bScale = obj.value("scale").value<qreal>();
29+
this->bFocused = obj.value("focused").value<bool>();
30+
Qt::endPropertyUpdateGroup();
5131

5232
if (activeWorkspaceId != this->mFocusedWorkspaceName) {
5333
auto* workspace = this->ipc->findWorkspaceByName(activeWorkspaceId);
@@ -61,50 +41,15 @@ void I3Monitor::updateFromObject(const QVariantMap& obj) {
6141
emit this->focusedWorkspaceChanged();
6242
};
6343

64-
if (x != this->mX) {
65-
this->mX = x;
66-
emit this->xChanged();
67-
}
68-
69-
if (y != this->mY) {
70-
this->mY = y;
71-
emit this->yChanged();
72-
}
73-
74-
if (width != this->mWidth) {
75-
this->mWidth = width;
76-
emit this->widthChanged();
77-
}
78-
79-
if (height != this->mHeight) {
80-
this->mHeight = height;
81-
emit this->heightChanged();
82-
}
83-
84-
if (scale != this->mScale) {
85-
this->mScale = scale;
86-
emit this->scaleChanged();
87-
}
88-
89-
if (focused != this->mFocused) {
90-
this->mFocused = focused;
91-
emit this->focusedChanged();
92-
}
93-
9444
if (obj != this->mLastIpcObject) {
9545
this->mLastIpcObject = obj;
9646
emit this->lastIpcObjectChanged();
9747
}
9848
}
9949

100-
void I3Monitor::setFocus(bool focused) {
101-
this->mFocused = focused;
102-
emit this->focusedChanged();
103-
}
104-
10550
void I3Monitor::setFocusedWorkspace(I3Workspace* workspace) {
10651
this->mFocusedWorkspace = workspace;
107-
this->mFocusedWorkspaceName = workspace->name();
52+
this->mFocusedWorkspaceName = workspace->bindableName().value();
10853
emit this->focusedWorkspaceChanged();
10954
};
11055

src/x11/i3/ipc/monitor.hpp

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

33
#include <qobject.h>
4+
#include <qproperty.h>
45

56
#include "connection.hpp"
67

@@ -11,25 +12,25 @@ class I3Monitor: public QObject {
1112
Q_OBJECT;
1213
// clang-format off
1314
/// The ID of this monitor
14-
Q_PROPERTY(qint32 id READ id NOTIFY idChanged);
15+
Q_PROPERTY(qint32 id READ default NOTIFY idChanged BINDABLE bindableId);
1516
/// The name of this monitor
16-
Q_PROPERTY(QString name READ name NOTIFY nameChanged);
17+
Q_PROPERTY(QString name READ default NOTIFY nameChanged BINDABLE bindableName);
1718
/// Wether this monitor is turned on or not
18-
Q_PROPERTY(bool power READ power NOTIFY powerChanged);
19+
Q_PROPERTY(bool power READ default NOTIFY powerChanged BINDABLE bindablePower);
1920
/// The current workspace
2021
Q_PROPERTY(qs::i3::ipc::I3Workspace* focusedWorkspace READ focusedWorkspace NOTIFY focusedWorkspaceChanged);
2122
/// The X coordinate of this monitor inside the monitor layout
22-
Q_PROPERTY(qint32 x READ x NOTIFY xChanged);
23+
Q_PROPERTY(qint32 x READ default NOTIFY xChanged BINDABLE bindableX);
2324
/// The Y coordinate of this monitor inside the monitor layout
24-
Q_PROPERTY(qint32 y READ y NOTIFY yChanged);
25+
Q_PROPERTY(qint32 y READ default NOTIFY yChanged BINDABLE bindableY);
2526
/// The width in pixels of this monitor
26-
Q_PROPERTY(qint32 width READ width NOTIFY widthChanged);
27+
Q_PROPERTY(qint32 width READ default NOTIFY widthChanged BINDABLE bindableWidth);
2728
/// The height in pixels of this monitor
28-
Q_PROPERTY(qint32 height READ height NOTIFY heightChanged);
29+
Q_PROPERTY(qint32 height READ default NOTIFY heightChanged BINDABLE bindableHeight);
2930
/// The scaling factor of this monitor, 1 means it runs at native resolution
30-
Q_PROPERTY(qreal scale READ scale NOTIFY scaleChanged);
31+
Q_PROPERTY(qreal scale READ default NOTIFY scaleChanged BINDABLE bindableScale);
3132
/// Whether this monitor is currently in focus
32-
Q_PROPERTY(bool focused READ focused NOTIFY focusedChanged);
33+
Q_PROPERTY(bool focused READ default NOTIFY focusedChanged BINDABLE bindableFocused);
3334
/// Last JSON returned for this monitor, as a JavaScript object.
3435
///
3536
/// This updates every time Quickshell receives an `output` event from i3/Sway
@@ -41,22 +42,23 @@ class I3Monitor: public QObject {
4142
public:
4243
explicit I3Monitor(I3Ipc* ipc): QObject(ipc), ipc(ipc) {}
4344

44-
[[nodiscard]] qint32 id() const;
45-
[[nodiscard]] QString name() const;
46-
[[nodiscard]] bool power() const;
45+
[[nodiscard]] QBindable<qint32> bindableId() { return &this->bId; }
46+
[[nodiscard]] QBindable<QString> bindableName() { return &this->bName; }
47+
[[nodiscard]] QBindable<bool> bindablePower() { return &this->bPower; }
48+
[[nodiscard]] QBindable<qint32> bindableX() { return &this->bX; }
49+
[[nodiscard]] QBindable<qint32> bindableY() { return &this->bY; }
50+
[[nodiscard]] QBindable<qint32> bindableWidth() { return &this->bWidth; }
51+
[[nodiscard]] QBindable<qint32> bindableHeight() { return &this->bHeight; }
52+
[[nodiscard]] QBindable<qreal> bindableScale() { return &this->bScale; }
53+
[[nodiscard]] QBindable<bool> bindableFocused() { return &this->bFocused; }
54+
4755
[[nodiscard]] I3Workspace* focusedWorkspace() const;
48-
[[nodiscard]] qint32 x() const;
49-
[[nodiscard]] qint32 y() const;
50-
[[nodiscard]] qint32 width() const;
51-
[[nodiscard]] qint32 height() const;
52-
[[nodiscard]] qreal scale() const;
53-
[[nodiscard]] bool focused() const;
5456
[[nodiscard]] QVariantMap lastIpcObject() const;
5557

5658
void updateFromObject(const QVariantMap& obj);
5759

5860
void setFocusedWorkspace(I3Workspace* workspace);
59-
void setFocus(bool focus);
61+
6062
signals:
6163
void idChanged();
6264
void nameChanged();
@@ -73,19 +75,19 @@ class I3Monitor: public QObject {
7375
private:
7476
I3Ipc* ipc;
7577

76-
qint32 mId = -1;
77-
QString mName;
78-
bool mPower = false;
79-
qint32 mX = 0;
80-
qint32 mY = 0;
81-
qint32 mWidth = 0;
82-
qint32 mHeight = 0;
83-
qreal mScale = 1;
84-
bool mFocused = false;
8578
QVariantMap mLastIpcObject;
86-
8779
I3Workspace* mFocusedWorkspace = nullptr;
8880
QString mFocusedWorkspaceName; // use for faster change detection
81+
82+
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(I3Monitor, qint32, bId, -1, &I3Monitor::idChanged);
83+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, QString, bName, &I3Monitor::nameChanged);
84+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, bool, bPower, &I3Monitor::powerChanged);
85+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, qint32, bX, &I3Monitor::xChanged);
86+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, qint32, bY, &I3Monitor::yChanged);
87+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, qint32, bWidth, &I3Monitor::widthChanged);
88+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, qint32, bHeight, &I3Monitor::heightChanged);
89+
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(I3Monitor, qreal, bScale, 1, &I3Monitor::scaleChanged);
90+
Q_OBJECT_BINDABLE_PROPERTY(I3Monitor, bool, bFocused, &I3Monitor::focusedChanged);
8991
};
9092

9193
} // namespace qs::i3::ipc

0 commit comments

Comments
 (0)