Fix clicking and scrolling through workspaces

The way waybar used the workspace "num", clicking a workspace called "1:
something" resulted in going to a newly created workspace called "1",
because the workspace ipc command expects the workspace name, not its number.
This commit is contained in:
David96 2018-11-05 20:16:19 +01:00
parent d6af63d84a
commit 168415440f
2 changed files with 24 additions and 24 deletions

View File

@ -20,8 +20,8 @@ class Workspaces : public IModule {
void addWorkspace(Json::Value); void addWorkspace(Json::Value);
std::string getIcon(std::string, Json::Value); std::string getIcon(std::string, Json::Value);
bool handleScroll(GdkEventScroll*); bool handleScroll(GdkEventScroll*);
int getPrevWorkspace(); std::string getPrevWorkspace();
int getNextWorkspace(); std::string getNextWorkspace();
Bar& bar_; Bar& bar_;
const Json::Value& config_; const Json::Value& config_;
@ -30,7 +30,7 @@ class Workspaces : public IModule {
util::JsonParser parser_; util::JsonParser parser_;
std::mutex mutex_; std::mutex mutex_;
bool scrolling_; bool scrolling_;
std::unordered_map<int, Gtk::Button> buttons_; std::unordered_map<std::string, Gtk::Button> buttons_;
Json::Value workspaces_; Json::Value workspaces_;
Ipc ipc_; Ipc ipc_;
}; };

View File

@ -41,7 +41,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
for (auto it = buttons_.begin(); it != buttons_.end();) { for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
[it](auto node) -> bool { return node["num"].asInt() == it->first; }); [it](auto node) -> bool { return node["name"].asString() == it->first; });
if (ws == workspaces_.end()) { if (ws == workspaces_.end()) {
it = buttons_.erase(it); it = buttons_.erase(it);
needReorder = true; needReorder = true;
@ -54,7 +54,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
&& bar_.output_name != node["output"].asString()) { && bar_.output_name != node["output"].asString()) {
continue; continue;
} }
auto it = buttons_.find(node["num"].asInt()); auto it = buttons_.find(node["name"].asString());
if (it == buttons_.end()) { if (it == buttons_.end()) {
addWorkspace(node); addWorkspace(node);
needReorder = true; needReorder = true;
@ -103,7 +103,7 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
fmt::arg("name", node["name"].asString()), fmt::arg("name", node["name"].asString()),
fmt::arg("index", node["num"].asString())) fmt::arg("index", node["num"].asString()))
: icon; : icon;
auto pair = buttons_.emplace(node["num"].asInt(), format); auto pair = buttons_.emplace(node["name"].asString(), format);
auto &button = pair.first->second; auto &button = pair.first->second;
box_.pack_start(button, false, false, 0); box_.pack_start(button, false, false, 0);
button.set_relief(Gtk::RELIEF_NONE); button.set_relief(Gtk::RELIEF_NONE);
@ -158,73 +158,73 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
return false; return false;
} }
scrolling_ = true; scrolling_ = true;
int id = -1; std::string name;
uint16_t idx = 0; uint16_t idx = 0;
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
for (; idx < workspaces_.size(); idx += 1) { for (; idx < workspaces_.size(); idx += 1) {
if (workspaces_[idx]["focused"].asBool()) { if (workspaces_[idx]["focused"].asBool()) {
id = workspaces_[idx]["num"].asInt(); name = workspaces_[idx]["name"].asString();
break; break;
} }
} }
} }
if (id == -1) { if (name.empty()) {
scrolling_ = false; scrolling_ = false;
return false; return false;
} }
if (e->direction == GDK_SCROLL_UP) { if (e->direction == GDK_SCROLL_UP) {
id = getNextWorkspace(); name = getNextWorkspace();
} }
if (e->direction == GDK_SCROLL_DOWN) { if (e->direction == GDK_SCROLL_DOWN) {
id = getPrevWorkspace(); name = getPrevWorkspace();
} }
if (e->direction == GDK_SCROLL_SMOOTH) { if (e->direction == GDK_SCROLL_SMOOTH) {
gdouble delta_x, delta_y; gdouble delta_x, delta_y;
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e), gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e),
&delta_x, &delta_y); &delta_x, &delta_y);
if (delta_y < 0) { if (delta_y < 0) {
id = getNextWorkspace(); name = getNextWorkspace();
} else if (delta_y > 0) { } else if (delta_y > 0) {
id = getPrevWorkspace(); name = getPrevWorkspace();
} }
} }
{ if (!name.empty()) {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
if (id == workspaces_[idx]["num"].asInt()) { if (name == workspaces_[idx]["name"].asString()) {
scrolling_ = false; scrolling_ = false;
return false; return false;
} }
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", id)); ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
std::this_thread::sleep_for(std::chrono::milliseconds(150)); std::this_thread::sleep_for(std::chrono::milliseconds(150));
} }
return true; return true;
} }
int waybar::modules::sway::Workspaces::getPrevWorkspace() std::string waybar::modules::sway::Workspaces::getPrevWorkspace()
{ {
for (uint16_t i = 0; i != workspaces_.size(); i += 1) { for (uint16_t i = 0; i != workspaces_.size(); i += 1) {
if (workspaces_[i]["focused"].asBool()) { if (workspaces_[i]["focused"].asBool()) {
if (i > 0) { if (i > 0) {
return workspaces_[i - 1]["num"].asInt(); return workspaces_[i - 1]["name"].asString();
} }
return workspaces_[workspaces_.size() - 1]["num"].asInt(); return workspaces_[workspaces_.size() - 1]["name"].asString();
} }
} }
return -1; return "";
} }
int waybar::modules::sway::Workspaces::getNextWorkspace() std::string waybar::modules::sway::Workspaces::getNextWorkspace()
{ {
for (uint16_t i = 0; i != workspaces_.size(); i += 1) { for (uint16_t i = 0; i != workspaces_.size(); i += 1) {
if (workspaces_[i]["focused"].asBool()) { if (workspaces_[i]["focused"].asBool()) {
if (i + 1U < workspaces_.size()) { if (i + 1U < workspaces_.size()) {
return workspaces_[i + 1]["num"].asInt(); return workspaces_[i + 1]["name"].asString();
} }
return workspaces_[0]["num"].asInt(); return workspaces_[0]["String"].asString();
} }
} }
return -1; return "";
} }
waybar::modules::sway::Workspaces::operator Gtk::Widget &() { waybar::modules::sway::Workspaces::operator Gtk::Widget &() {