Adding comments & fixing code style

This commit is contained in:
Lucas Lazare 2019-05-18 12:04:09 -04:00
parent 1a76aa0c8c
commit 8ba3052dd1

View File

@ -49,19 +49,22 @@ 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"]; const Json::Value &p_workspaces = config_["persistant_workspaces"];
const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames(); const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames();
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 = std::find_if(payload.begin(), payload.end(), [&p_w_name](const Json::Value& node) { auto it =
return node["name"].asString() == p_w_name; std::find_if(payload.begin(), payload.end(), [&p_w_name](const Json::Value &node) {
}); return node["name"].asString() == p_w_name;
});
if (it != payload.end()) { if (it != payload.end()) {
continue; // already displayed by some bar continue; // already displayed by some bar
} }
if (p_w.isArray() && !p_w.empty()) { if (p_w.isArray() && !p_w.empty()) {
for (const Json::Value& output : p_w) { // Adding to target outputs
for (const Json::Value &output : p_w) {
if (output.asString() == bar_.output->name) { if (output.asString() == bar_.output->name) {
Json::Value v; Json::Value v;
v["name"] = p_w_name; v["name"] = p_w_name;
@ -70,6 +73,7 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
} }
} }
} else { } else {
// Adding to all outputs
Json::Value v; Json::Value v;
v["name"] = p_w_name; v["name"] = p_w_name;
workspaces_.emplace_back(std::move(v)); workspaces_.emplace_back(std::move(v));
@ -77,30 +81,34 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
} }
if (workspaces_order_.empty()) { if (workspaces_order_.empty()) {
workspaces_order_.reserve(workspaces_.size()); // Saving starting order
for (const Json::Value& workspace : workspaces_) { workspaces_order_.reserve(workspaces_.size());
workspaces_order_.emplace_back(workspace["name"].asString()); for (const Json::Value &workspace : workspaces_) {
} workspaces_order_.emplace_back(workspace["name"].asString());
}
} else { } else {
std::vector<Json::Value> sorted_workspaces; // Ordering workspaces as it was before for current output
sorted_workspaces.reserve(workspaces_.size()); std::vector<Json::Value> sorted_workspaces;
auto ws_end = workspaces_.end(); sorted_workspaces.reserve(workspaces_.size());
for (const std::string& name_by_order : workspaces_order_) { auto ws_end = workspaces_.end();
auto it = std::find_if(workspaces_.begin(), ws_end, [&name_by_order](const Json::Value& ws) { for (const std::string &name_by_order : workspaces_order_) {
return ws["name"].asString() == name_by_order; auto it =
}); std::find_if(workspaces_.begin(), ws_end, [&name_by_order](const Json::Value &ws) {
if (it != ws_end) { return ws["name"].asString() == name_by_order;
sorted_workspaces.emplace_back(*it); });
--ws_end; if (it != ws_end) {
ws_end->swap(*it); sorted_workspaces.emplace_back(*it);
} --ws_end;
} ws_end->swap(*it);
}
}
for (int i = 0 ; workspaces_.size() > sorted_workspaces.size() ; ++i) { // Adding new workspaces to the output (those where never showed in this output before)
workspaces_order_.emplace_back(workspaces_[i]["name"].asString()); for (int i = 0; workspaces_.size() > sorted_workspaces.size(); ++i) {
sorted_workspaces.emplace_back(workspaces_[i]); workspaces_order_.emplace_back(workspaces_[i]["name"].asString());
} sorted_workspaces.emplace_back(workspaces_[i]);
workspaces_.swap(sorted_workspaces); }
workspaces_.swap(sorted_workspaces);
} }
dp.emit(); dp.emit();