diff --git a/plugins/single_plugins/wsets.cpp b/plugins/single_plugins/wsets.cpp index 66de6b0a2..bf3b0dca8 100644 --- a/plugins/single_plugins/wsets.cpp +++ b/plugins/single_plugins/wsets.cpp @@ -210,19 +210,39 @@ class wayfire_wsets_plugin_t : public wf::plugin_interface_t }); } - void select_workspace(int index) + /** + * Find the workspace set with the given index, or create a new one if it does not exist already. + * In addition, take a reference to it. + */ + void locate_or_create_wset(int64_t index) { - auto wo = wf::get_core().get_active_output(); - if (!wo) + if (available_sets.count(index)) { return; } - if (!available_sets.count(index)) + auto all_wsets = wf::workspace_set_t::get_all(); + auto it = std::find_if(all_wsets.begin(), all_wsets.end(), + [&] (auto wset) { return wset->get_index() == index; }); + + if (it == all_wsets.end()) { available_sets[index] = wf::workspace_set_t::create(index); + } else + { + available_sets[index] = (*it)->shared_from_this(); } + } + void select_workspace(int index) + { + auto wo = wf::get_core().get_active_output(); + if (!wo) + { + return; + } + + locate_or_create_wset(index); if (wo->wset() != available_sets[index]) { LOGC(WSET, "Output ", wo->to_string(), " selecting workspace set id=", index); @@ -260,10 +280,7 @@ class wayfire_wsets_plugin_t : public wf::plugin_interface_t return; } - if (!available_sets.count(index)) - { - available_sets[index] = wf::workspace_set_t::create(index); - } + locate_or_create_wset(index); auto target_wset = available_sets[index]; const auto& old_wset = view->get_wset();