fix(Workspace): ordering

This commit is contained in:
Alex 2019-05-20 20:46:44 +02:00
parent 032ad925af
commit 4accdd4524
1 changed files with 33 additions and 63 deletions

View File

@ -50,10 +50,10 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
});
// adding persistant workspaces (as per the config file)
if (config_["persistant_workspaces"].isObject()) {
const Json::Value & p_workspaces = config_["persistant_workspaces"];
const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames();
auto first_persistant_ws_idx = workspaces_.size();
for (const std::string &p_w_name : p_workspaces_names) {
const Json::Value &p_w = p_workspaces[p_w_name];
auto it =
@ -84,41 +84,11 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
}
}
if (workspaces_order_.empty()) {
// Saving starting order
workspaces_order_.reserve(payload.size() + workspaces_.size() - first_persistant_ws_idx);
for (const Json::Value &workspace : payload) {
workspaces_order_.emplace_back(workspace["name"].asString());
}
for (auto i = first_persistant_ws_idx; i < workspaces_.size() ; ++i) {
workspaces_order_.emplace_back(workspaces_[i]["name"].asString());
}
} else {
// Ordering workspaces as it was before for current output
std::vector<Json::Value> sorted_workspaces;
sorted_workspaces.reserve(workspaces_.size());
auto ws_end = workspaces_.end();
for (const std::string &name_by_order : workspaces_order_) {
auto it =
std::find_if(workspaces_.begin(), ws_end, [&name_by_order](const Json::Value &ws) {
return ws["name"].asString() == name_by_order;
std::sort(workspaces_.begin(),
workspaces_.end(),
[](const Json::Value &lhs, const Json::Value &rhs) {
return lhs["name"].asString() < rhs["name"].asString();
});
if (it != ws_end) {
sorted_workspaces.emplace_back(*it);
--ws_end;
if (ws_end == workspaces_.begin()) { // we've extracted everything
break;
}
ws_end->swap(*it);
}
}
// Adding new workspaces to the output (those were never showed in this output before)
for (int i = 0; workspaces_.size() > sorted_workspaces.size(); ++i) {
workspaces_order_.emplace_back(workspaces_[i]["name"].asString());
sorted_workspaces.emplace_back(workspaces_[i]);
}
workspaces_.swap(sorted_workspaces);
}
dp.emit();