From 52983c7188830931826c18f5df05b4b0aeb2f59e Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sat, 15 Jul 2023 17:43:22 +0000 Subject: [PATCH 01/14] workspaces.cpp --- src/modules/hyprland/workspaces.cpp | 145 ++++++++++++++++++++-------- 1 file changed, 107 insertions(+), 38 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index e169f916..20881fe9 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -28,6 +28,16 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value icons_map_.emplace("", ""); } + auto config_all_outputs = config_["all-outputs"]; + if (config_all_outputs.isBool()) { + all_outputs_ = config_all_outputs.asBool(); + } + + auto config_show_special = config_["show-special"]; + if (config_show_special.isBool()) { + show_special_ = config_show_special.asBool(); + } + box_.set_name("workspaces"); if (!id.empty()) { box_.get_style_context()->add_class(id); @@ -43,10 +53,12 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value gIPC->registerForIPC("workspace", this); gIPC->registerForIPC("createworkspace", this); gIPC->registerForIPC("destroyworkspace", this); + gIPC->registerForIPC("focusedmon", this); + gIPC->registerForIPC("moveworkspace", this); } auto Workspaces::update() -> void { - for (int &workspace_to_remove : workspaces_to_remove_) { + for (std::string workspace_to_remove : workspaces_to_remove_) { remove_workspace(workspace_to_remove); } @@ -56,19 +68,14 @@ auto Workspaces::update() -> void { create_workspace(workspace_to_create); } - workspaces_to_create_.clear(); - - for (std::unique_ptr &workspace : workspaces_) { - workspace->set_active(workspace->id() == active_workspace_id); - + for (auto &workspace : workspaces_) { + workspace->set_active(workspace->name() == active_workspace_name); std::string &workspace_icon = icons_map_[""]; if (with_icon_) { workspace_icon = workspace->select_icon(icons_map_); } - workspace->update(format_, workspace_icon); } - AModule::update(); } @@ -76,35 +83,58 @@ void Workspaces::onEvent(const std::string &ev) { std::lock_guard lock(mutex_); std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>')); std::string payload = ev.substr(eventName.size() + 2); + if (eventName == "workspace") { - std::from_chars(payload.data(), payload.data() + payload.size(), active_workspace_id); + active_workspace_name = payload; + } else if (eventName == "destroyworkspace") { - int deleted_workspace_id; - std::from_chars(payload.data(), payload.data() + payload.size(), deleted_workspace_id); - workspaces_to_remove_.push_back(deleted_workspace_id); + workspaces_to_remove_.push_back(payload); + } else if (eventName == "createworkspace") { - int new_workspace_id; - std::from_chars(payload.data(), payload.data() + payload.size(), new_workspace_id); - workspaces_to_create_.push_back(new_workspace_id); + const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); + for (auto &workspace_json : workspaces_json) { + if (workspace_json["name"].asString() == payload && + (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && + (workspace_json["name"].asString().find("special:") != 0 || show_special())) + create_workspace(workspace_json); + } + + } else if (eventName == "focusedmon") { + active_workspace_name = payload.substr(payload.find(",") + 1); + + } else if (eventName == "moveworkspace") { + std::string workspace = payload.substr(0, payload.find(",")); + std::string new_monitor = payload.substr(payload.find(",") + 1); + if (all_outputs()) { + if (bar_.output->name == new_monitor) { // TODO: implement this better + const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); + for (auto &workspace_json : workspaces_json) { + if (workspace_json["name"].asString() == workspace && + bar_.output->name == workspace_json["monitor"].asString()) + create_workspace(workspace_json); + } + } else { + workspaces_to_remove_.push_back(workspace); + } + } } dp.emit(); } -void Workspaces::create_workspace(int id) { - workspaces_.push_back(std::make_unique(id)); +void Workspaces::create_workspace(const Json::Value &value) { + workspaces_.push_back(std::make_unique(value)); Gtk::Button &new_workspace_button = workspaces_.back()->button(); box_.pack_start(new_workspace_button, false, false); sort_workspaces(); new_workspace_button.show_all(); } -void Workspaces::remove_workspace(int id) { +void Workspaces::remove_workspace(std::string name) { auto workspace = std::find_if(workspaces_.begin(), workspaces_.end(), - [&](std::unique_ptr &x) { return x->id() == id; }); + [&](std::unique_ptr &x) { return x->name() == name; }); if (workspace == workspaces_.end()) { - spdlog::warn("Can't find workspace with id {}", id); return; } @@ -113,16 +143,13 @@ void Workspaces::remove_workspace(int id) { } void Workspaces::init() { - const auto activeWorkspace = WorkspaceDto::parse(gIPC->getSocket1JsonReply("activeworkspace")); - active_workspace_id = activeWorkspace.id; + active_workspace_name = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString(); + const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); for (const Json::Value &workspace_json : workspaces_json) { - workspaces_.push_back( - std::make_unique(Workspace(WorkspaceDto::parse(workspace_json)))); - } - - for (auto &workspace : workspaces_) { - box_.pack_start(workspace->button(), false, false); + if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && + (workspace_json["name"].asString().find("special") != 0 || show_special())) + create_workspace(workspace_json); } sort_workspaces(); @@ -136,13 +163,27 @@ Workspaces::~Workspaces() { std::lock_guard lg(mutex_); } -WorkspaceDto WorkspaceDto::parse(const Json::Value &value) { - return WorkspaceDto{value["id"].asInt()}; -} +Workspace::Workspace(const Json::Value &value) { + id_ = value["id"].asInt(); + name_ = value["name"].asString(); + monitor_ = value["monitor"].asString(); // TODO:allow using monitor desc + windows_ = value["id"].asInt(); + active_ = 1; + is_special_ = 0; -Workspace::Workspace(WorkspaceDto dto) : Workspace(dto.id){}; + if (name_.find("name:") == 0) { + name_ = name_.substr(5); + } else if (name_.find("special") == 0) { + name_ = id_ == -99 ? name_ : name_.substr(13); + is_special_ = 1; + } + // spdlog::info("\nNew workspace:\n\tid:({})\n\tname:({})\n\tmonitor:({})\n\tspecial:({})\n", id_, + // name_, monitor_, is_special_ ? "yes" : "no"); //make less noisy + + button_.add_events(Gdk::BUTTON_PRESS_MASK); + button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handle_clicked), + false); -Workspace::Workspace(int id) : id_(id) { button_.set_relief(Gtk::RELIEF_NONE); content_.set_center_widget(label_); button_.add(content_); @@ -161,14 +202,29 @@ void Workspace::update(const std::string &format, const std::string &icon) { Glib::RefPtr style_context = button_.get_style_context(); add_or_remove_class(style_context, active(), "active"); - label_.set_markup( - fmt::format(fmt::runtime(format), fmt::arg("id", id()), fmt::arg("icon", icon))); + label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()), + fmt::arg("name", name()), fmt::arg("icon", icon))); } void Workspaces::sort_workspaces() { std::sort(workspaces_.begin(), workspaces_.end(), - [](std::unique_ptr &lhs, std::unique_ptr &rhs) { - return lhs->id() < rhs->id(); + [](std::unique_ptr &a, std::unique_ptr &b) { + // normal -> named -> special -> named special + if (a->id() > 0 && b->id() > 0) { + return a->id() < b->id(); + } + if (a->id() < 0 && b->id() < 0) { + if ((a->is_special()) ^ (a->is_special())) { + return a->id() > b->id(); + } else { + return a->id() < b->id(); + } + } + if ((a->id() > 0) ^ (b->id() > 0)) { + return a->id() > b->id(); + } + spdlog::error("huh!!?"); + return false; }); for (size_t i = 0; i < workspaces_.size(); ++i) { @@ -193,7 +249,20 @@ std::string &Workspace::select_icon(std::map &icons_ma if (default_icon_it != icons_map.end()) { return default_icon_it->second; } - return icons_map[""]; } + +auto Workspace::handle_clicked(GdkEventButton *bt) -> bool { + if (id() > 0) { // normal + system(("hyprctl dispatch workspace " + std::to_string(id()) + " &> /dev/null").c_str()); + } else if (!is_special()) { // named normal + system(("hyprctl dispatch workspace name:" + name() + " &> /dev/null").c_str()); + } else if (id() != -99) { // named special + system(("hyprctl dispatch togglespecialworkspace name:" + name() + " &> /dev/null").c_str()); + } else { // special + system("hyprctl dispatch togglespecialworkspace special &> /dev/null"); + } + return 1; +} + } // namespace waybar::modules::hyprland From 495b63d7dcfd2c67b66b67ed24d77bc54305de6c Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sat, 15 Jul 2023 17:44:57 +0000 Subject: [PATCH 02/14] workspaces.hpp --- include/modules/hyprland/workspaces.hpp | 41 ++++++++++++++++--------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 500bbe36..12590fcd 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -9,27 +9,30 @@ namespace waybar::modules::hyprland { -struct WorkspaceDto { - int id; - - static WorkspaceDto parse(const Json::Value& value); -}; - class Workspace { public: - Workspace(int id); - Workspace(WorkspaceDto dto); - int id() const { return id_; }; - int active() const { return active_; }; + Workspace(const Json::Value& value); std::string& select_icon(std::map& icons_map); - void set_active(bool value = true) { active_ = value; }; Gtk::Button& button() { return button_; }; + int id() const { return id_; }; + std::string name() const { return name_; }; + std::string monitor() const { return monitor_; }; + int active() const { return active_; }; + bool is_special() const { return is_special_; }; + + auto handle_clicked(GdkEventButton* bt) -> bool; + void set_active(bool value) { active_ = value; }; + void update(const std::string& format, const std::string& icon); private: int id_; + std::string name_; + std::string monitor_; + int windows_; bool active_; + bool is_special_; Gtk::Button button_; Gtk::Box content_; @@ -43,19 +46,27 @@ class Workspaces : public AModule, public EventHandler { void update() override; void init(); + auto all_outputs() const -> bool { return all_outputs_; } + auto show_special() const -> bool { return show_special_; } + + auto get_bar_output() const -> std::string { return bar_.output->name; } + private: void onEvent(const std::string&) override; void sort_workspaces(); - void create_workspace(int id); - void remove_workspace(int id); + void create_workspace(const Json::Value& value); + void remove_workspace(std::string name); + + bool all_outputs_ = false; + bool show_special_ = false; std::string format_; std::map icons_map_; bool with_icon_; - int active_workspace_id; + std::string active_workspace_name; std::vector> workspaces_; std::vector workspaces_to_create_; - std::vector workspaces_to_remove_; + std::vector workspaces_to_remove_; std::mutex mutex_; const Bar& bar_; Gtk::Box box_; From 75e21c485340ecd5f396b26aed06c7efe82ed30e Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:36:55 +0000 Subject: [PATCH 03/14] Update waybar-hyprland-workspaces.5.scd --- man/waybar-hyprland-workspaces.5.scd | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/man/waybar-hyprland-workspaces.5.scd b/man/waybar-hyprland-workspaces.5.scd index 0678fb22..88ca775b 100644 --- a/man/waybar-hyprland-workspaces.5.scd +++ b/man/waybar-hyprland-workspaces.5.scd @@ -25,6 +25,8 @@ Addressed by *hyprland/workspaces* *{id}*: id of workspace assigned by compositor +*{name}*: workspace name assigned by compositor + *{icon}*: Icon, as defined in *format-icons*. # ICONS @@ -48,7 +50,9 @@ Additional to workspace name matching, the following *format-icons* can be set. "active": "", "default": "" }, - "sort-by-number": true + "sort-by-number": true, + "all-outputs": false, + "show-special": false } ``` From f8a9a970b2f7adc66625547816b6d5695c036f05 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sat, 15 Jul 2023 18:43:46 +0000 Subject: [PATCH 04/14] removes "sort-by-number" --- man/waybar-hyprland-workspaces.5.scd | 1 - 1 file changed, 1 deletion(-) diff --git a/man/waybar-hyprland-workspaces.5.scd b/man/waybar-hyprland-workspaces.5.scd index 88ca775b..95a5911f 100644 --- a/man/waybar-hyprland-workspaces.5.scd +++ b/man/waybar-hyprland-workspaces.5.scd @@ -50,7 +50,6 @@ Additional to workspace name matching, the following *format-icons* can be set. "active": "", "default": "" }, - "sort-by-number": true, "all-outputs": false, "show-special": false } From f3df15650a6fc60710c54ef94c3d284f7132069e Mon Sep 17 00:00:00 2001 From: zjeffer <4633209+zjeffer@users.noreply.github.com> Date: Sat, 15 Jul 2023 23:48:12 +0200 Subject: [PATCH 05/14] use IPC for click events, clang-tidy fixes --- src/modules/hyprland/workspaces.cpp | 37 ++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 20881fe9..34eeffc6 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -163,13 +163,13 @@ Workspaces::~Workspaces() { std::lock_guard lg(mutex_); } -Workspace::Workspace(const Json::Value &value) { - id_ = value["id"].asInt(); - name_ = value["name"].asString(); - monitor_ = value["monitor"].asString(); // TODO:allow using monitor desc - windows_ = value["id"].asInt(); - active_ = 1; - is_special_ = 0; +Workspace::Workspace(const Json::Value &value) + : id_(value["id"].asInt()), + name_(value["name"].asString()), + monitor_(value["monitor"].asString()), // TODO:allow using monitor desc + windows_(value["id"].asInt()) { + active_ = true; + is_special_ = false; if (name_.find("name:") == 0) { name_ = name_.substr(5); @@ -253,16 +253,21 @@ std::string &Workspace::select_icon(std::map &icons_ma } auto Workspace::handle_clicked(GdkEventButton *bt) -> bool { - if (id() > 0) { // normal - system(("hyprctl dispatch workspace " + std::to_string(id()) + " &> /dev/null").c_str()); - } else if (!is_special()) { // named normal - system(("hyprctl dispatch workspace name:" + name() + " &> /dev/null").c_str()); - } else if (id() != -99) { // named special - system(("hyprctl dispatch togglespecialworkspace name:" + name() + " &> /dev/null").c_str()); - } else { // special - system("hyprctl dispatch togglespecialworkspace special &> /dev/null"); + try { + if (id() > 0) { // normal + gIPC->getSocket1Reply("dispatch workspace " + std::to_string(id())); + } else if (!is_special()) { // named normal + gIPC->getSocket1Reply("dispatch workspace name" + name()); + } else if (id() != -99) { // named special + gIPC->getSocket1Reply("dispatch togglespecialworkspace name" + name()); + } else { // special + gIPC->getSocket1Reply("dispatch togglespecialworkspace special"); + } + return true; + } catch (const std::exception &e) { + spdlog::error("Failed to dispatch workspace: {}", e.what()); } - return 1; + return false; } } // namespace waybar::modules::hyprland From 7200b16520d85977566db92d3bfec8200ac73e46 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:02:39 +0000 Subject: [PATCH 06/14] documentation --- man/waybar-hyprland-workspaces.5.scd | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/man/waybar-hyprland-workspaces.5.scd b/man/waybar-hyprland-workspaces.5.scd index 95a5911f..6e67e188 100644 --- a/man/waybar-hyprland-workspaces.5.scd +++ b/man/waybar-hyprland-workspaces.5.scd @@ -21,6 +21,16 @@ Addressed by *hyprland/workspaces* typeof: array ++ Based on the workspace id and state, the corresponding icon gets selected. See *icons*. +*show-special*: ++ + typeof: bool ++ + default: false ++ + If set to true special workspaces will be shown. + +*all-outputs*: ++ + typeof: bool ++ + default: false ++ + If set to false workspaces group will be shown only in assigned output. Otherwise all workspace groups are shown. + # FORMAT REPLACEMENTS *{id}*: id of workspace assigned by compositor From ca0122c3cb8578443631b9853812cf7c5966d54c Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:18:41 +0000 Subject: [PATCH 07/14] workspaces.cpp --- src/modules/hyprland/workspaces.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 34eeffc6..b2a1d092 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -102,20 +102,20 @@ void Workspaces::onEvent(const std::string &ev) { } else if (eventName == "focusedmon") { active_workspace_name = payload.substr(payload.find(",") + 1); - } else if (eventName == "moveworkspace") { + } else if (eventName == "moveworkspace" && !all_outputs()) { std::string workspace = payload.substr(0, payload.find(",")); - std::string new_monitor = payload.substr(payload.find(",") + 1); - if (all_outputs()) { - if (bar_.output->name == new_monitor) { // TODO: implement this better - const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (auto &workspace_json : workspaces_json) { - if (workspace_json["name"].asString() == workspace && - bar_.output->name == workspace_json["monitor"].asString()) - create_workspace(workspace_json); + std::string new_output = payload.substr(payload.find(",") + 1); + if (bar_.output->name == new_output) { // TODO: implement this better + const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); + for (auto &workspace_json : workspaces_json) { + if (workspace_json["name"].asString() == workspace && + bar_.output->name == workspace_json["monitor"].asString()) { + create_workspace(workspace_json); + break; } - } else { - workspaces_to_remove_.push_back(workspace); } + } else { + workspaces_to_remove_.push_back(workspace); } } @@ -166,7 +166,7 @@ Workspaces::~Workspaces() { Workspace::Workspace(const Json::Value &value) : id_(value["id"].asInt()), name_(value["name"].asString()), - monitor_(value["monitor"].asString()), // TODO:allow using monitor desc + output_(value["monitor"].asString()), // TODO:allow using monitor desc windows_(value["id"].asInt()) { active_ = true; is_special_ = false; @@ -177,8 +177,6 @@ Workspace::Workspace(const Json::Value &value) name_ = id_ == -99 ? name_ : name_.substr(13); is_special_ = 1; } - // spdlog::info("\nNew workspace:\n\tid:({})\n\tname:({})\n\tmonitor:({})\n\tspecial:({})\n", id_, - // name_, monitor_, is_special_ ? "yes" : "no"); //make less noisy button_.add_events(Gdk::BUTTON_PRESS_MASK); button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handle_clicked), From 4f81e55e41931e75557e96a3d60afadfc91dbcd6 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:20:30 +0000 Subject: [PATCH 08/14] workspaces.hpp --- include/modules/hyprland/workspaces.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 12590fcd..a2910f51 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -17,19 +17,19 @@ class Workspace { int id() const { return id_; }; std::string name() const { return name_; }; - std::string monitor() const { return monitor_; }; + std::string output() const { return output_; }; int active() const { return active_; }; bool is_special() const { return is_special_; }; auto handle_clicked(GdkEventButton* bt) -> bool; - void set_active(bool value) { active_ = value; }; + void set_active(bool value = true) { active_ = value; }; void update(const std::string& format, const std::string& icon); private: int id_; std::string name_; - std::string monitor_; + std::string output_; int windows_; bool active_; bool is_special_; From 5f0fa71f3204bd2ff1b905c36e05555e11a12ccd Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:43:25 +0000 Subject: [PATCH 09/14] moves createWorkspace to update() --- src/modules/hyprland/workspaces.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index b2a1d092..392f457e 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -64,10 +64,12 @@ auto Workspaces::update() -> void { workspaces_to_remove_.clear(); - for (int &workspace_to_create : workspaces_to_create_) { + for (Json::Value &workspace_to_create : workspaces_to_create_) { create_workspace(workspace_to_create); } + workspaces_to_create_.clear(); + for (auto &workspace : workspaces_) { workspace->set_active(workspace->name() == active_workspace_name); std::string &workspace_icon = icons_map_[""]; @@ -92,7 +94,7 @@ void Workspaces::onEvent(const std::string &ev) { } else if (eventName == "createworkspace") { const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (auto &workspace_json : workspaces_json) { + for (Json::Value workspace_json : workspaces_json) { if (workspace_json["name"].asString() == payload && (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && (workspace_json["name"].asString().find("special:") != 0 || show_special())) @@ -107,10 +109,10 @@ void Workspaces::onEvent(const std::string &ev) { std::string new_output = payload.substr(payload.find(",") + 1); if (bar_.output->name == new_output) { // TODO: implement this better const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (auto &workspace_json : workspaces_json) { + for (Json::Value workspace_json : workspaces_json) { if (workspace_json["name"].asString() == workspace && bar_.output->name == workspace_json["monitor"].asString()) { - create_workspace(workspace_json); + workspaces_to_create_.push_back(workspace_json); break; } } @@ -122,7 +124,7 @@ void Workspaces::onEvent(const std::string &ev) { dp.emit(); } -void Workspaces::create_workspace(const Json::Value &value) { +void Workspaces::create_workspace(Json::Value &value) { workspaces_.push_back(std::make_unique(value)); Gtk::Button &new_workspace_button = workspaces_.back()->button(); box_.pack_start(new_workspace_button, false, false); @@ -146,7 +148,7 @@ void Workspaces::init() { active_workspace_name = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString(); const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (const Json::Value &workspace_json : workspaces_json) { + for (Json::Value workspace_json : workspaces_json) { if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && (workspace_json["name"].asString().find("special") != 0 || show_special())) create_workspace(workspace_json); From 6d24b22b21ccf779ace5032629fa1b2bd450dc74 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:43:54 +0000 Subject: [PATCH 10/14] moves createWorkspace to update() --- include/modules/hyprland/workspaces.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index a2910f51..47df01f8 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -54,7 +54,7 @@ class Workspaces : public AModule, public EventHandler { private: void onEvent(const std::string&) override; void sort_workspaces(); - void create_workspace(const Json::Value& value); + void create_workspace(Json::Value& value); void remove_workspace(std::string name); bool all_outputs_ = false; @@ -65,7 +65,7 @@ class Workspaces : public AModule, public EventHandler { bool with_icon_; std::string active_workspace_name; std::vector> workspaces_; - std::vector workspaces_to_create_; + std::vector workspaces_to_create_; std::vector workspaces_to_remove_; std::mutex mutex_; const Bar& bar_; From 2bfc0e1da6f015b6c359c468385eb830cfdc6130 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 01:49:46 +0000 Subject: [PATCH 11/14] moves createWorkspace to update() --- src/modules/hyprland/workspaces.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 392f457e..0002900a 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -97,8 +97,10 @@ void Workspaces::onEvent(const std::string &ev) { for (Json::Value workspace_json : workspaces_json) { if (workspace_json["name"].asString() == payload && (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && - (workspace_json["name"].asString().find("special:") != 0 || show_special())) - create_workspace(workspace_json); + (workspace_json["name"].asString().find("special:") != 0 || show_special())) { + workspaces_to_create_.push_back(workspace_json); + break; + } } } else if (eventName == "focusedmon") { From 2d0fdaeec6b62d4a245b4660a181b3b2c981494a Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Sun, 16 Jul 2023 18:22:14 +0000 Subject: [PATCH 12/14] special fix --- src/modules/hyprland/workspaces.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 0002900a..aad8eac9 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -263,7 +263,7 @@ auto Workspace::handle_clicked(GdkEventButton *bt) -> bool { } else if (id() != -99) { // named special gIPC->getSocket1Reply("dispatch togglespecialworkspace name" + name()); } else { // special - gIPC->getSocket1Reply("dispatch togglespecialworkspace special"); + gIPC->getSocket1Reply("dispatch togglespecialworkspace"); } return true; } catch (const std::exception &e) { From 2721e19ee6abe8c90cd938a74d6b84c9c66694c6 Mon Sep 17 00:00:00 2001 From: zjeffer <4633209+zjeffer@users.noreply.github.com> Date: Sun, 16 Jul 2023 14:47:14 +0200 Subject: [PATCH 13/14] small improvements --- include/modules/hyprland/workspaces.hpp | 4 ++-- src/modules/hyprland/workspaces.cpp | 32 ++++++++++++------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 47df01f8..b32dc41d 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -11,7 +11,7 @@ namespace waybar::modules::hyprland { class Workspace { public: - Workspace(const Json::Value& value); + Workspace(const Json::Value& workspace_data); std::string& select_icon(std::map& icons_map); Gtk::Button& button() { return button_; }; @@ -42,7 +42,7 @@ class Workspace { class Workspaces : public AModule, public EventHandler { public: Workspaces(const std::string&, const waybar::Bar&, const Json::Value&); - virtual ~Workspaces(); + ~Workspaces() override; void update() override; void init(); diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 0002900a..231a852e 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -44,7 +44,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value } event_box_.add(box_); modulesReady = true; - if (!gIPC.get()) { + if (!gIPC) { gIPC = std::make_unique(); } @@ -104,11 +104,11 @@ void Workspaces::onEvent(const std::string &ev) { } } else if (eventName == "focusedmon") { - active_workspace_name = payload.substr(payload.find(",") + 1); + active_workspace_name = payload.substr(payload.find(',') + 1); } else if (eventName == "moveworkspace" && !all_outputs()) { - std::string workspace = payload.substr(0, payload.find(",")); - std::string new_output = payload.substr(payload.find(",") + 1); + std::string workspace = payload.substr(0, payload.find(',')); + std::string new_output = payload.substr(payload.find(',') + 1); if (bar_.output->name == new_output) { // TODO: implement this better const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); for (Json::Value workspace_json : workspaces_json) { @@ -167,19 +167,18 @@ Workspaces::~Workspaces() { std::lock_guard lg(mutex_); } -Workspace::Workspace(const Json::Value &value) - : id_(value["id"].asInt()), - name_(value["name"].asString()), - output_(value["monitor"].asString()), // TODO:allow using monitor desc - windows_(value["id"].asInt()) { - active_ = true; - is_special_ = false; - +Workspace::Workspace(const Json::Value &workspace_data) + : id_(workspace_data["id"].asInt()), + name_(workspace_data["name"].asString()), + output_(workspace_data["monitor"].asString()), // TODO:allow using monitor desc + windows_(workspace_data["id"].asInt()), + active_(true), + is_special_(false) { if (name_.find("name:") == 0) { name_ = name_.substr(5); } else if (name_.find("special") == 0) { name_ = id_ == -99 ? name_ : name_.substr(13); - is_special_ = 1; + is_special_ = true; } button_.add_events(Gdk::BUTTON_PRESS_MASK); @@ -191,7 +190,7 @@ Workspace::Workspace(const Json::Value &value) button_.add(content_); }; -void add_or_remove_class(Glib::RefPtr context, bool condition, +void add_or_remove_class(const Glib::RefPtr &context, bool condition, const std::string &class_name) { if (condition) { context->add_class(class_name); @@ -201,7 +200,7 @@ void add_or_remove_class(Glib::RefPtr context, bool condition } void Workspace::update(const std::string &format, const std::string &icon) { - Glib::RefPtr style_context = button_.get_style_context(); + auto style_context = button_.get_style_context(); add_or_remove_class(style_context, active(), "active"); label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()), @@ -218,9 +217,8 @@ void Workspaces::sort_workspaces() { if (a->id() < 0 && b->id() < 0) { if ((a->is_special()) ^ (a->is_special())) { return a->id() > b->id(); - } else { - return a->id() < b->id(); } + return a->id() < b->id(); } if ((a->id() > 0) ^ (b->id() > 0)) { return a->id() > b->id(); From 24d56023fd969741bf4448b87fc596555a6463af Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Mon, 17 Jul 2023 22:38:58 +0000 Subject: [PATCH 14/14] last fixes --- src/modules/hyprland/workspaces.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index b33c3472..f7b87bfa 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -97,7 +97,7 @@ void Workspaces::onEvent(const std::string &ev) { for (Json::Value workspace_json : workspaces_json) { if (workspace_json["name"].asString() == payload && (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && - (workspace_json["name"].asString().find("special:") != 0 || show_special())) { + (show_special() || workspace_json["name"].asString().find("special:") != 0)) { workspaces_to_create_.push_back(workspace_json); break; } @@ -223,7 +223,6 @@ void Workspaces::sort_workspaces() { if ((a->id() > 0) ^ (b->id() > 0)) { return a->id() > b->id(); } - spdlog::error("huh!!?"); return false; });