@@ -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) {
374378HyprlandWorkspace*
375379HyprlandIpc::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}
0 commit comments