Skip to content

Commit 1b1f279

Browse files
authored
Merge branch 'quickshell-mirror:master' into color-quantization
2 parents c9009a0 + fb343ab commit 1b1f279

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

src/wayland/hyprland/ipc/connection.cpp

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ void HyprlandIpc::onEvent(HyprlandIpcEvent* event) {
333333
auto* monitor = this->findMonitorByName(name, true);
334334
this->setFocusedMonitor(monitor);
335335
monitor->setActiveWorkspace(workspace);
336+
qCDebug(logHyprlandIpc) << "Monitor" << name << "focused with workspace" << workspace->id();
336337
} else if (event->name == "workspacev2") {
337338
auto args = event->parseView(2);
338339
auto id = args.at(0).toInt();
@@ -341,6 +342,8 @@ void HyprlandIpc::onEvent(HyprlandIpcEvent* event) {
341342
if (this->mFocusedMonitor != nullptr) {
342343
auto* workspace = this->findWorkspaceByName(name, true, id);
343344
this->mFocusedMonitor->setActiveWorkspace(workspace);
345+
qCDebug(logHyprlandIpc) << "Workspace" << id << "activated on"
346+
<< this->mFocusedMonitor->name();
344347
}
345348
} else if (event->name == "moveworkspacev2") {
346349
auto args = event->parseView(3);
@@ -351,6 +354,7 @@ void HyprlandIpc::onEvent(HyprlandIpcEvent* event) {
351354
auto* workspace = this->findWorkspaceByName(name, true, id);
352355
auto* monitor = this->findMonitorByName(monitorName, true);
353356

357+
qCDebug(logHyprlandIpc) << "Workspace" << id << "moved to monitor" << monitorName;
354358
workspace->setMonitor(monitor);
355359
} else if (event->name == "renameworkspace") {
356360
auto args = event->parseView(2);
@@ -374,15 +378,28 @@ void HyprlandIpc::onEvent(HyprlandIpcEvent* event) {
374378
HyprlandWorkspace*
375379
HyprlandIpc::findWorkspaceByName(const QString& name, bool createIfMissing, qint32 id) {
376380
const auto& mList = this->mWorkspaces.valueList();
381+
HyprlandWorkspace* workspace = nullptr;
377382

378-
auto workspaceIter =
379-
std::ranges::find_if(mList, [name](const HyprlandWorkspace* m) { return m->name() == name; });
383+
if (id != -1) {
384+
auto workspaceIter =
385+
std::ranges::find_if(mList, [&](const HyprlandWorkspace* m) { return m->id() == id; });
386+
387+
workspace = workspaceIter == mList.end() ? nullptr : *workspaceIter;
388+
}
380389

381-
if (workspaceIter != mList.end()) {
382-
return *workspaceIter;
390+
if (!workspace) {
391+
auto workspaceIter =
392+
std::ranges::find_if(mList, [&](const HyprlandWorkspace* m) { return m->name() == name; });
393+
394+
workspace = workspaceIter == mList.end() ? nullptr : *workspaceIter;
395+
}
396+
397+
if (workspace) {
398+
return workspace;
383399
} else if (createIfMissing) {
384400
qCDebug(logHyprlandIpc) << "Workspace" << name
385-
<< "requested before creation, performing early init";
401+
<< "requested before creation, performing early init with id" << id;
402+
386403
auto* workspace = new HyprlandWorkspace(this);
387404
workspace->updateInitial(id, name);
388405
this->mWorkspaces.insertObject(workspace);
@@ -400,24 +417,34 @@ void HyprlandIpc::refreshWorkspaces(bool canCreate) {
400417
this->requestingWorkspaces = false;
401418
if (!success) return;
402419

403-
qCDebug(logHyprlandIpc) << "parsing workspaces response";
420+
qCDebug(logHyprlandIpc) << "Parsing workspaces response";
404421
auto json = QJsonDocument::fromJson(resp).array();
405422

406423
const auto& mList = this->mWorkspaces.valueList();
407-
auto names = QVector<QString>();
424+
auto ids = QVector<quint32>();
408425

409426
for (auto entry: json) {
410427
auto object = entry.toObject().toVariantMap();
411-
auto name = object.value("name").toString();
412428

413-
auto workspaceIter = std::ranges::find_if(mList, [name](const HyprlandWorkspace* m) {
414-
return m->name() == name;
415-
});
429+
auto id = object.value("id").toInt();
430+
431+
auto workspaceIter =
432+
std::ranges::find_if(mList, [&](const HyprlandWorkspace* m) { return m->id() == id; });
433+
434+
// Only fall back to name-based filtering as a last resort, for workspaces where
435+
// no ID has been determined yet.
436+
if (workspaceIter == mList.end()) {
437+
auto name = object.value("name").toString();
438+
439+
workspaceIter = std::ranges::find_if(mList, [&](const HyprlandWorkspace* m) {
440+
return m->id() == -1 && m->name() == name;
441+
});
442+
}
416443

417444
auto* workspace = workspaceIter == mList.end() ? nullptr : *workspaceIter;
418445
auto existed = workspace != nullptr;
419446

420-
if (workspace == nullptr) {
447+
if (!existed) {
421448
if (!canCreate) continue;
422449
workspace = new HyprlandWorkspace(this);
423450
}
@@ -428,20 +455,22 @@ void HyprlandIpc::refreshWorkspaces(bool canCreate) {
428455
this->mWorkspaces.insertObject(workspace);
429456
}
430457

431-
names.push_back(name);
458+
ids.push_back(id);
432459
}
433460

434-
auto removedWorkspaces = QVector<HyprlandWorkspace*>();
461+
if (canCreate) {
462+
auto removedWorkspaces = QVector<HyprlandWorkspace*>();
435463

436-
for (auto* workspace: mList) {
437-
if (!names.contains(workspace->name())) {
438-
removedWorkspaces.push_back(workspace);
464+
for (auto* workspace: mList) {
465+
if (!ids.contains(workspace->id())) {
466+
removedWorkspaces.push_back(workspace);
467+
}
439468
}
440-
}
441469

442-
for (auto* workspace: removedWorkspaces) {
443-
this->mWorkspaces.removeObject(workspace);
444-
delete workspace;
470+
for (auto* workspace: removedWorkspaces) {
471+
this->mWorkspaces.removeObject(workspace);
472+
delete workspace;
473+
}
445474
}
446475
});
447476
}

src/wayland/hyprland/ipc/connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class HyprlandIpc: public QObject {
8181
[[nodiscard]] ObjectModel<HyprlandWorkspace>* workspaces();
8282

8383
// No byId because these preemptively create objects. The given id is set if created.
84-
HyprlandWorkspace* findWorkspaceByName(const QString& name, bool createIfMissing, qint32 id = 0);
84+
HyprlandWorkspace* findWorkspaceByName(const QString& name, bool createIfMissing, qint32 id = -1);
8585
HyprlandMonitor* findMonitorByName(const QString& name, bool createIfMissing, qint32 id = -1);
8686

8787
// canCreate avoids making ghost workspaces when the connection races

src/wayland/hyprland/ipc/monitor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ void HyprlandMonitor::setActiveWorkspace(HyprlandWorkspace* workspace) {
117117
this->mActiveWorkspace = workspace;
118118

119119
if (workspace != nullptr) {
120+
workspace->setMonitor(this);
121+
120122
QObject::connect(
121123
workspace,
122124
&QObject::destroyed,

src/wayland/hyprland/ipc/workspace.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,22 @@ void HyprlandWorkspace::updateInitial(qint32 id, QString name) {
3535
}
3636

3737
void HyprlandWorkspace::updateFromObject(QVariantMap object) {
38-
auto id = object.value("id").value<qint32>();
3938
auto name = object.value("name").value<QString>();
4039
auto monitorId = object.value("monitorID").value<qint32>();
4140
auto monitorName = object.value("monitor").value<QString>();
4241

43-
if (id != this->mId) {
44-
this->mId = id;
42+
auto initial = this->mId = -1;
43+
44+
// ID cannot be updated after creation
45+
if (initial) {
46+
this->mId = object.value("id").value<qint32>();
4547
emit this->idChanged();
4648
}
4749

48-
if (name != this->mName) {
49-
this->mName = std::move(name);
50+
// No events we currently handle give a workspace id but not a name,
51+
// so we shouldn't set this if it isn't an initial query
52+
if (initial && name != this->mName) {
53+
this->mName = name;
5054
emit this->nameChanged();
5155
}
5256

0 commit comments

Comments
 (0)