From add9e925f4f242e37f6a5e50aaabcf9acbcb4eae Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 2 Feb 2019 00:36:52 +0100 Subject: [PATCH] fix(Workspaces): button order --- Dockerfiles/opensuse | 2 +- include/modules/sway/workspaces.hpp | 5 ++-- src/modules/sway/workspaces.cpp | 41 ++++++++++++++++------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/Dockerfiles/opensuse b/Dockerfiles/opensuse index 7f6c19f..a1406b5 100644 --- a/Dockerfiles/opensuse +++ b/Dockerfiles/opensuse @@ -2,4 +2,4 @@ FROM opensuse/tumbleweed:latest RUN zypper -n up && \ zypper -n install -t pattern devel_C_C++ && \ - zypper -n install git meson clang libinput libinput-devel libwayland-client0 libwayland-cursor0 wayland-protocols-devel wayland-devel Mesa-libEGL-devel Mesa-libGLESv2-devel libgbm-devel libxkbcommon-devel libudev-devel libpixman-1-0-devel gtkmm3-devel jsoncpp-devel \ No newline at end of file + zypper -n install git meson clang libinput-devel libwayland-client0 libwayland-cursor0 wayland-protocols-devel wayland-devel Mesa-libEGL-devel Mesa-libGLESv2-devel libgbm-devel libxkbcommon-devel libudev-devel libpixman-1-0-devel gtkmm3-devel jsoncpp-devel \ No newline at end of file diff --git a/include/modules/sway/workspaces.hpp b/include/modules/sway/workspaces.hpp index f3fe7b6..d244505 100644 --- a/include/modules/sway/workspaces.hpp +++ b/include/modules/sway/workspaces.hpp @@ -19,11 +19,12 @@ class Workspaces : public IModule { operator Gtk::Widget &(); private: void worker(); - void addWorkspace(Json::Value); - std::string getIcon(std::string, Json::Value); + void addWorkspace(const Json::Value&); + std::string getIcon(const std::string&, const Json::Value&); bool handleScroll(GdkEventScroll*); std::string getPrevWorkspace(); std::string getNextWorkspace(); + uint16_t getWorkspaceIndex(const std::string &name); std::string trimWorkspaceName(std::string); const Bar& bar_; diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index 290cbb4..9b77a97 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -17,12 +17,7 @@ void waybar::modules::sway::Workspaces::worker() { thread_ = [this] { try { - // Wait for the name of the output - if (!config_["all-outputs"].asBool() && bar_.output_name.empty()) { - while (bar_.output_name.empty()) { - thread_.sleep_for(std::chrono::milliseconds(150)); - } - } else if (!workspaces_.empty()) { + if (!workspaces_.empty()) { ipc_.handleEvent(); } { @@ -47,8 +42,7 @@ auto waybar::modules::sway::Workspaces::update() -> void auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), [it](auto node) -> bool { return node["name"].asString() == it->first; }); if (ws == workspaces_.end() || - (!config_["all-outputs"].asBool() && - (*ws)["output"].asString() != bar_.output_name)) { + (!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output_name)) { it = buttons_.erase(it); needReorder = true; } else { @@ -82,7 +76,7 @@ auto waybar::modules::sway::Workspaces::update() -> void button.get_style_context()->remove_class("urgent"); } if (needReorder) { - box_.reorder_child(button, node["num"].asInt()); + box_.reorder_child(button, getWorkspaceIndex(node["name"].asString())); } auto icon = getIcon(node["name"].asString(), node); if (config_["format"].isString()) { @@ -101,7 +95,7 @@ auto waybar::modules::sway::Workspaces::update() -> void } } -void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node) +void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) { auto icon = getIcon(node["name"].asString(), node); auto format = config_["format"].isString() @@ -127,7 +121,7 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node) button.signal_scroll_event() .connect(sigc::mem_fun(*this, &Workspaces::handleScroll)); } - box_.reorder_child(button, node["num"].asInt()); + box_.reorder_child(button, getWorkspaceIndex(node["name"].asString())); if (node["focused"].asBool()) { button.get_style_context()->add_class("focused"); } @@ -140,11 +134,10 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node) button.show(); } -std::string waybar::modules::sway::Workspaces::getIcon(std::string name, - Json::Value node) +std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name, + const Json::Value &node) { - std::vector keys = { - name, "urgent", "focused", "visible", "default"}; + std::vector keys = { name, "urgent", "focused", "visible", "default" }; for (auto const& key : keys) { if (key == "focused" || key == "visible" || key == "urgent") { if (config_["format-icons"][key].isString() && node[key].asBool()) { @@ -209,7 +202,7 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) 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 (i > 0) { return workspaces_[i - 1]["name"].asString(); @@ -222,7 +215,7 @@ std::string waybar::modules::sway::Workspaces::getPrevWorkspace() 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 (i + 1U < workspaces_.size()) { return workspaces_[i + 1]["name"].asString(); @@ -233,8 +226,14 @@ std::string waybar::modules::sway::Workspaces::getNextWorkspace() return ""; } -waybar::modules::sway::Workspaces::operator Gtk::Widget &() { - return box_; +uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) +{ + for (uint16_t i = 0; i < workspaces_.size(); i += 1) { + if (workspaces_[i]["name"].asString() == name) { + return i; + } + } + return workspaces_.size(); } std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name) @@ -245,3 +244,7 @@ std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string nam } return name; } + +waybar::modules::sway::Workspaces::operator Gtk::Widget &() { + return box_; +}