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,75 +50,45 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
}); });
// adding persistant workspaces (as per the config file) // adding persistant workspaces (as per the config file)
const Json::Value & p_workspaces = config_["persistant_workspaces"]; if (config_["persistant_workspaces"].isObject()) {
const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames(); 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) {
for (const std::string &p_w_name : p_workspaces_names) { const Json::Value &p_w = p_workspaces[p_w_name];
const Json::Value &p_w = p_workspaces[p_w_name]; auto it =
auto it = std::find_if(payload.begin(), payload.end(), [&p_w_name](const Json::Value &node) {
std::find_if(payload.begin(), payload.end(), [&p_w_name](const Json::Value &node) { return node["name"].asString() == p_w_name;
return node["name"].asString() == p_w_name;
});
if (it != payload.end()) {
continue; // already displayed by some bar
}
if (p_w.isArray() && !p_w.empty()) {
// Adding to target outputs
for (const Json::Value &output : p_w) {
if (output.asString() == bar_.output->name) {
Json::Value v;
v["name"] = p_w_name;
v["target_output"] = bar_.output->name;
workspaces_.emplace_back(std::move(v));
break;
}
}
} else {
// Adding to all outputs
Json::Value v;
v["name"] = p_w_name;
workspaces_.emplace_back(std::move(v));
}
}
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;
}); });
if (it != ws_end) {
sorted_workspaces.emplace_back(*it); if (it != payload.end()) {
--ws_end; continue; // already displayed by some bar
if (ws_end == workspaces_.begin()) { // we've extracted everything }
break;
if (p_w.isArray() && !p_w.empty()) {
// Adding to target outputs
for (const Json::Value &output : p_w) {
if (output.asString() == bar_.output->name) {
Json::Value v;
v["name"] = p_w_name;
v["target_output"] = bar_.output->name;
workspaces_.emplace_back(std::move(v));
break;
}
} }
ws_end->swap(*it); } else {
// Adding to all outputs
Json::Value v;
v["name"] = p_w_name;
workspaces_.emplace_back(std::move(v));
} }
} }
// Adding new workspaces to the output (those were never showed in this output before) std::sort(workspaces_.begin(),
for (int i = 0; workspaces_.size() > sorted_workspaces.size(); ++i) { workspaces_.end(),
workspaces_order_.emplace_back(workspaces_[i]["name"].asString()); [](const Json::Value &lhs, const Json::Value &rhs) {
sorted_workspaces.emplace_back(workspaces_[i]); return lhs["name"].asString() < rhs["name"].asString();
} });
workspaces_.swap(sorted_workspaces);
} }
dp.emit(); dp.emit();