From 8a84cb609ec51ed1e57ad609ce6192c7fb046741 Mon Sep 17 00:00:00 2001 From: zjeffer <4633209+zjeffer@users.noreply.github.com> Date: Mon, 4 Dec 2023 15:05:00 +0100 Subject: [PATCH 1/2] clang-tidy: added StaticConstantCase CheckOption --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index aa2c544e..b7a33451 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -26,3 +26,4 @@ CheckOptions: - { key: readability-identifier-naming.EnumCase, value: CamelCase } - { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE } - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } + - { key: readability-identifier-naming.StaticConstantCase, value: UPPER_CASE } From 4d5b715dc4d24b9f7d74e0907c991b3be5d68ce5 Mon Sep 17 00:00:00 2001 From: zjeffer <4633209+zjeffer@users.noreply.github.com> Date: Mon, 4 Dec 2023 12:10:16 +0100 Subject: [PATCH 2/2] applied clang-tidy suggestions for hyprland workspaces --- include/modules/hyprland/workspaces.hpp | 214 +++--- src/modules/hyprland/workspaces.cpp | 873 ++++++++++++------------ src/util/enum.cpp | 4 +- 3 files changed, 544 insertions(+), 547 deletions(-) diff --git a/include/modules/hyprland/workspaces.hpp b/include/modules/hyprland/workspaces.hpp index 53bc55fc..a17c2db4 100644 --- a/include/modules/hyprland/workspaces.hpp +++ b/include/modules/hyprland/workspaces.hpp @@ -34,82 +34,82 @@ class WindowCreationPayload { std::string window_class, std::string window_title); WindowCreationPayload(Json::Value const& client_data); - int increment_time_spent_uncreated(); - bool is_empty(Workspaces& workspace_manager); - bool repr_is_ready() const { return std::holds_alternative(window_); } + int incrementTimeSpentUncreated(); + bool isEmpty(Workspaces& workspace_manager); + bool reprIsReady() const { return std::holds_alternative(m_window); } std::string repr(Workspaces& workspace_manager); - std::string workspace_name() const { return workspace_name_; } - WindowAddress addr() const { return window_address_; } + std::string getWorkspaceName() const { return m_workspaceName; } + WindowAddress getAddress() const { return m_windowAddress; } - void move_to_worksace(std::string& new_workspace_name); + void moveToWorksace(std::string& new_workspace_name); private: - void clear_addr(); - void clear_workspace_name(); + void clearAddr(); + void clearWorkspaceName(); using Repr = std::string; using ClassAndTitle = std::pair; - std::variant window_; + std::variant m_window; - WindowAddress window_address_; - std::string workspace_name_; + WindowAddress m_windowAddress; + std::string m_workspaceName; - int time_spent_uncreated_ = 0; + int m_timeSpentUncreated = 0; }; class Workspace { public: explicit Workspace(const Json::Value& workspace_data, Workspaces& workspace_manager, const Json::Value& clients_data = Json::Value::nullRef); - std::string& select_icon(std::map& icons_map); - Gtk::Button& button() { return button_; }; + std::string& selectIcon(std::map& icons_map); + Gtk::Button& button() { return m_button; }; - int id() const { return id_; }; - std::string name() const { return name_; }; - std::string output() const { return output_; }; - bool active() const { return active_; }; - bool is_special() const { return is_special_; }; - bool is_persistent() const { return is_persistent_; }; - bool is_visible() const { return is_visible_; }; - bool is_empty() const { return windows_ == 0; }; - bool is_urgent() const { return is_urgent_; }; + int id() const { return m_id; }; + std::string name() const { return m_name; }; + std::string output() const { return m_output; }; + bool isActive() const { return m_active; }; + bool isSpecial() const { return m_isSpecial; }; + bool isPersistent() const { return m_isPersistent; }; + bool isVisible() const { return m_isVisible; }; + bool isEmpty() const { return m_windows == 0; }; + bool isUrgent() const { return m_isUrgent; }; - bool handle_clicked(GdkEventButton* bt) const; - void set_active(bool value = true) { active_ = value; }; - void set_persistent(bool value = true) { is_persistent_ = value; }; - void set_urgent(bool value = true) { is_urgent_ = value; }; - void set_visible(bool value = true) { is_visible_ = value; }; - void set_windows(uint value) { windows_ = value; }; - void set_name(std::string const& value) { name_ = value; }; - bool contains_window(WindowAddress const& addr) const { return window_map_.contains(addr); } - void insert_window(WindowCreationPayload create_window_paylod); - std::string remove_window(WindowAddress const& addr); - void initialize_window_map(const Json::Value& clients_data); + bool handleClicked(GdkEventButton* bt) const; + void setActive(bool value = true) { m_active = value; }; + void setPersistent(bool value = true) { m_isPersistent = value; }; + void setUrgent(bool value = true) { m_isUrgent = value; }; + void setVisible(bool value = true) { m_isVisible = value; }; + void setWindows(uint value) { m_windows = value; }; + void setName(std::string const& value) { m_name = value; }; + bool containsWindow(WindowAddress const& addr) const { return m_windowMap.contains(addr); } + void insertWindow(WindowCreationPayload create_window_paylod); + std::string removeWindow(WindowAddress const& addr); + void initializeWindowMap(const Json::Value& clients_data); - bool on_window_opened(WindowCreationPayload const& create_window_paylod); - std::optional close_window(WindowAddress const& addr); + bool onWindowOpened(WindowCreationPayload const& create_window_paylod); + std::optional closeWindow(WindowAddress const& addr); void update(const std::string& format, const std::string& icon); private: - Workspaces& workspace_manager_; + Workspaces& m_workspaceManager; - int id_; - std::string name_; - std::string output_; - uint windows_; - bool active_ = false; - bool is_special_ = false; - bool is_persistent_ = false; - bool is_urgent_ = false; - bool is_visible_ = false; + int m_id; + std::string m_name; + std::string m_output; + uint m_windows; + bool m_active = false; + bool m_isSpecial = false; + bool m_isPersistent = false; + bool m_isUrgent = false; + bool m_isVisible = false; - std::map window_map_; + std::map m_windowMap; - Gtk::Button button_; - Gtk::Box content_; - Gtk::Label label_; + Gtk::Button m_button; + Gtk::Box m_content; + Gtk::Label m_label; }; class Workspaces : public AModule, public EventHandler { @@ -119,85 +119,85 @@ 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 active_only() const -> bool { return active_only_; } + auto allOutputs() const -> bool { return m_allOutputs; } + auto showSpecial() const -> bool { return m_showSpecial; } + auto activeOnly() const -> bool { return m_activeOnly; } - auto get_bar_output() const -> std::string { return bar_.output->name; } + auto getBarOutput() const -> std::string { return m_bar.output->name; } - std::string get_rewrite(std::string window_class, std::string window_title); - std::string& get_window_separator() { return format_window_separator_; } - bool is_workspace_ignored(std::string const& workspace_name); + std::string getRewrite(std::string window_class, std::string window_title); + std::string& getWindowSeparator() { return m_formatWindowSeparator; } + bool isWorkspaceIgnored(std::string const& workspace_name); - bool window_rewrite_config_uses_title() const { return any_window_rewrite_rule_uses_title_; } + bool windowRewriteConfigUsesTitle() const { return m_anyWindowRewriteRuleUsesTitle; } private: void onEvent(const std::string& e) override; - void update_window_count(); - void sort_workspaces(); - void create_workspace(Json::Value const& workspace_data, - Json::Value const& clients_data = Json::Value::nullRef); - void remove_workspace(std::string const& name); - void set_urgent_workspace(std::string const& windowaddress); - void parse_config(const Json::Value& config); - void register_ipc(); + void updateWindowCount(); + void sortWorkspaces(); + void createWorkspace(Json::Value const& workspace_data, + Json::Value const& clients_data = Json::Value::nullRef); + void removeWorkspace(std::string const& name); + void setUrgentWorkspace(std::string const& windowaddress); + void parseConfig(const Json::Value& config); + void registerIpc(); // workspace events - void on_workspace_activated(std::string const& payload); - void on_workspace_destroyed(std::string const& payload); - void on_workspace_created(std::string const& payload); - void on_workspace_moved(std::string const& payload); - void on_workspace_renamed(std::string const& payload); + void onWorkspaceActivated(std::string const& payload); + void onWorkspaceDestroyed(std::string const& payload); + void onWorkspaceCreated(std::string const& payload); + void onWorkspaceMoved(std::string const& payload); + void onWorkspaceRenamed(std::string const& payload); // monitor events - void on_monitor_focused(std::string const& payload); + void onMonitorFocused(std::string const& payload); // window events - void on_window_opened(std::string const& payload); - void on_window_closed(std::string const& payload); - void on_window_moved(std::string const& payload); + void onWindowOpened(std::string const& payload); + void onWindowClosed(std::string const& addr); + void onWindowMoved(std::string const& payload); - void on_window_title_event(std::string const& payload); + void onWindowTitleEvent(std::string const& payload); - int window_rewrite_priority_function(std::string const& window_rule); + int windowRewritePriorityFunction(std::string const& window_rule); - bool all_outputs_ = false; - bool show_special_ = false; - bool active_only_ = false; + bool m_allOutputs = false; + bool m_showSpecial = false; + bool m_activeOnly = false; - enum class SORT_METHOD { ID, NAME, NUMBER, DEFAULT }; - util::EnumParser enum_parser_; - SORT_METHOD sort_by_ = SORT_METHOD::DEFAULT; - std::map sort_map_ = {{"ID", SORT_METHOD::ID}, - {"NAME", SORT_METHOD::NAME}, - {"NUMBER", SORT_METHOD::NUMBER}, - {"DEFAULT", SORT_METHOD::DEFAULT}}; + enum class SortMethod { ID, NAME, NUMBER, DEFAULT }; + util::EnumParser m_enumParser; + SortMethod m_sortBy = SortMethod::DEFAULT; + std::map m_sortMap = {{"ID", SortMethod::ID}, + {"NAME", SortMethod::NAME}, + {"NUMBER", SortMethod::NUMBER}, + {"DEFAULT", SortMethod::DEFAULT}}; - void fill_persistent_workspaces(); - void create_persistent_workspaces(); - std::vector persistent_workspaces_to_create_; - bool persistent_created_ = false; + void fillPersistentWorkspaces(); + void createPersistentWorkspaces(); + std::vector m_persistentWorkspacesToCreate; + bool m_persistentCreated = false; - std::string format_; + std::string m_format; - std::map icons_map_; - util::RegexCollection window_rewrite_rules_; - bool any_window_rewrite_rule_uses_title_ = false; - std::string format_window_separator_; + std::map m_iconsMap; + util::RegexCollection m_windowRewriteRules; + bool m_anyWindowRewriteRuleUsesTitle = false; + std::string m_formatWindowSeparator; - bool with_icon_; - uint64_t monitor_id_; - std::string active_workspace_name_; - std::vector> workspaces_; - std::vector workspaces_to_create_; - std::vector workspaces_to_remove_; - std::vector windows_to_create_; + bool m_withIcon; + uint64_t m_monitorId; + std::string m_activeWorkspaceName; + std::vector> m_workspaces; + std::vector m_workspacesToCreate; + std::vector m_workspacesToRemove; + std::vector m_windowsToCreate; - std::vector ignore_workspaces_; + std::vector m_ignoreWorkspaces; - std::mutex mutex_; - const Bar& bar_; - Gtk::Box box_; + std::mutex m_mutex; + const Bar& m_bar; + Gtk::Box m_box; }; } // namespace waybar::modules::hyprland diff --git a/src/modules/hyprland/workspaces.cpp b/src/modules/hyprland/workspaces.cpp index 092f8bd6..7ad87bab 100644 --- a/src/modules/hyprland/workspaces.cpp +++ b/src/modules/hyprland/workspaces.cpp @@ -14,21 +14,21 @@ namespace waybar::modules::hyprland { -int Workspaces::window_rewrite_priority_function(std::string const &window_rule) { +int Workspaces::windowRewritePriorityFunction(std::string const &window_rule) { // Rules that match against title are prioritized // Rules that don't specify if they're matching against either title or class are deprioritized - bool const has_title = window_rule.find("title") != std::string::npos; - bool const has_class = window_rule.find("class") != std::string::npos; + bool const hasTitle = window_rule.find("title") != std::string::npos; + bool const hasClass = window_rule.find("class") != std::string::npos; - if (has_title && has_class) { - any_window_rewrite_rule_uses_title_ = true; + if (hasTitle && hasClass) { + m_anyWindowRewriteRuleUsesTitle = true; return 3; } - if (has_title) { - any_window_rewrite_rule_uses_title_ = true; + if (hasTitle) { + m_anyWindowRewriteRuleUsesTitle = true; return 2; } - if (has_class) { + if (hasClass) { return 1; } return 0; @@ -36,101 +36,100 @@ int Workspaces::window_rewrite_priority_function(std::string const &window_rule) Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config) : AModule(config, "workspaces", id, false, false), - bar_(bar), - box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) { + m_bar(bar), + m_box(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) { modulesReady = true; - parse_config(config); + parseConfig(config); - box_.set_name("workspaces"); + m_box.set_name("workspaces"); if (!id.empty()) { - box_.get_style_context()->add_class(id); + m_box.get_style_context()->add_class(id); } - event_box_.add(box_); + event_box_.add(m_box); - if (!gIPC.get()) { + if (!gIPC) { gIPC = std::make_unique(); } init(); - register_ipc(); + registerIpc(); } -auto Workspaces::parse_config(const Json::Value &config) -> void { - const Json::Value &config_format = config["format"]; +auto Workspaces::parseConfig(const Json::Value &config) -> void { + const Json::Value &configFormat = config["format"]; - format_ = config_format.isString() ? config_format.asString() : "{name}"; - with_icon_ = format_.find("{icon}") != std::string::npos; + m_format = configFormat.isString() ? configFormat.asString() : "{name}"; + m_withIcon = m_format.find("{icon}") != std::string::npos; - if (with_icon_ && icons_map_.empty()) { - Json::Value format_icons = config["format-icons"]; - for (std::string &name : format_icons.getMemberNames()) { - icons_map_.emplace(name, format_icons[name].asString()); + if (m_withIcon && m_iconsMap.empty()) { + Json::Value formatIcons = config["format-icons"]; + for (std::string &name : formatIcons.getMemberNames()) { + m_iconsMap.emplace(name, formatIcons[name].asString()); } - icons_map_.emplace("", ""); + m_iconsMap.emplace("", ""); } - auto config_all_outputs = config_["all-outputs"]; - if (config_all_outputs.isBool()) { - all_outputs_ = config_all_outputs.asBool(); + auto configAllOutputs = config_["all-outputs"]; + if (configAllOutputs.isBool()) { + m_allOutputs = configAllOutputs.asBool(); } - auto config_show_special = config_["show-special"]; - if (config_show_special.isBool()) { - show_special_ = config_show_special.asBool(); + auto configShowSpecial = config_["show-special"]; + if (configShowSpecial.isBool()) { + m_showSpecial = configShowSpecial.asBool(); } - auto config_active_only = config_["active-only"]; - if (config_active_only.isBool()) { - active_only_ = config_active_only.asBool(); + auto configActiveOnly = config_["active-only"]; + if (configActiveOnly.isBool()) { + m_activeOnly = configActiveOnly.asBool(); } - auto config_sort_by = config_["sort-by"]; - if (config_sort_by.isString()) { - auto sort_by_str = config_sort_by.asString(); + auto configSortBy = config_["sort-by"]; + if (configSortBy.isString()) { + auto sortByStr = configSortBy.asString(); try { - sort_by_ = enum_parser_.parseStringToEnum(sort_by_str, sort_map_); + m_sortBy = m_enumParser.parseStringToEnum(sortByStr, m_sortMap); } catch (const std::invalid_argument &e) { // Handle the case where the string is not a valid enum representation. - sort_by_ = SORT_METHOD::DEFAULT; + m_sortBy = SortMethod::DEFAULT; g_warning("Invalid string representation for sort-by. Falling back to default sort method."); } } - Json::Value ignore_workspaces = config["ignore-workspaces"]; - if (ignore_workspaces.isArray()) { - for (Json::Value &workspace_regex : ignore_workspaces) { - if (workspace_regex.isString()) { - std::string rule_string = workspace_regex.asString(); + Json::Value ignoreWorkspaces = config["ignore-workspaces"]; + if (ignoreWorkspaces.isArray()) { + for (Json::Value &workspaceRegex : ignoreWorkspaces) { + if (workspaceRegex.isString()) { + std::string ruleString = workspaceRegex.asString(); try { - const std::regex rule{rule_string, std::regex_constants::icase}; - ignore_workspaces_.emplace_back(rule); + const std::regex rule{ruleString, std::regex_constants::icase}; + m_ignoreWorkspaces.emplace_back(rule); } catch (const std::regex_error &e) { - spdlog::error("Invalid rule {}: {}", rule_string, e.what()); + spdlog::error("Invalid rule {}: {}", ruleString, e.what()); } } else { - spdlog::error("Not a string: '{}'", workspace_regex); + spdlog::error("Not a string: '{}'", workspaceRegex); } } } - const Json::Value &format_window_separator = config["format-window-separator"]; - format_window_separator_ = - format_window_separator.isString() ? format_window_separator.asString() : " "; + const Json::Value &formatWindowSeparator = config["format-window-separator"]; + m_formatWindowSeparator = + formatWindowSeparator.isString() ? formatWindowSeparator.asString() : " "; - const Json::Value &window_rewrite = config["window-rewrite"]; + const Json::Value &windowRewrite = config["window-rewrite"]; - const Json::Value &window_rewrite_default_config = config["window-rewrite-default"]; - std::string window_rewrite_default = - window_rewrite_default_config.isString() ? window_rewrite_default_config.asString() : "?"; + const Json::Value &windowRewriteDefaultConfig = config["window-rewrite-default"]; + std::string windowRewriteDefault = + windowRewriteDefaultConfig.isString() ? windowRewriteDefaultConfig.asString() : "?"; - window_rewrite_rules_ = util::RegexCollection( - window_rewrite, window_rewrite_default, [this](std::string &window_rule) { - return this->window_rewrite_priority_function(window_rule); - }); + m_windowRewriteRules = util::RegexCollection( + windowRewrite, windowRewriteDefault, + [this](std::string &window_rule) { return windowRewritePriorityFunction(window_rule); }); } -auto Workspaces::register_ipc() -> void { +auto Workspaces::registerIpc() -> void { gIPC->registerForIPC("workspace", this); gIPC->registerForIPC("createworkspace", this); gIPC->registerForIPC("destroyworkspace", this); @@ -142,7 +141,7 @@ auto Workspaces::register_ipc() -> void { gIPC->registerForIPC("movewindow", this); gIPC->registerForIPC("urgent", this); - if (window_rewrite_config_uses_title()) { + if (windowRewriteConfigUsesTitle()) { spdlog::info( "Registering for Hyprland's 'windowtitle' events because a user-defined window " "rewrite rule uses the 'title' field."); @@ -152,81 +151,81 @@ auto Workspaces::register_ipc() -> void { auto Workspaces::update() -> void { // remove workspaces that wait to be removed - unsigned int current_remove_workspace_num = 0; - for (const std::string &workspace_to_remove : workspaces_to_remove_) { - remove_workspace(workspace_to_remove); - current_remove_workspace_num++; + unsigned int currentRemoveWorkspaceNum = 0; + for (const std::string &workspaceToRemove : m_workspacesToRemove) { + removeWorkspace(workspaceToRemove); + currentRemoveWorkspaceNum++; } - for (unsigned int i = 0; i < current_remove_workspace_num; i++) { - workspaces_to_remove_.erase(workspaces_to_remove_.begin()); + for (unsigned int i = 0; i < currentRemoveWorkspaceNum; i++) { + m_workspacesToRemove.erase(m_workspacesToRemove.begin()); } // add workspaces that wait to be created - unsigned int current_create_workspace_num = 0; - for (Json::Value const &workspace_to_create : workspaces_to_create_) { - create_workspace(workspace_to_create); - current_create_workspace_num++; + unsigned int currentCreateWorkspaceNum = 0; + for (Json::Value const &workspaceToCreate : m_workspacesToCreate) { + createWorkspace(workspaceToCreate); + currentCreateWorkspaceNum++; } - for (unsigned int i = 0; i < current_create_workspace_num; i++) { - workspaces_to_create_.erase(workspaces_to_create_.begin()); + for (unsigned int i = 0; i < currentCreateWorkspaceNum; i++) { + m_workspacesToCreate.erase(m_workspacesToCreate.begin()); } // get all active workspaces auto monitors = gIPC->getSocket1JsonReply("monitors"); - std::vector visible_workspaces; + std::vector visibleWorkspaces; for (Json::Value &monitor : monitors) { auto ws = monitor["activeWorkspace"]; if (ws.isObject() && (ws["name"].isString())) { - visible_workspaces.push_back(ws["name"].asString()); + visibleWorkspaces.push_back(ws["name"].asString()); } } - for (auto &workspace : workspaces_) { + for (auto &workspace : m_workspaces) { // active - workspace->set_active(workspace->name() == active_workspace_name_); + workspace->setActive(workspace->name() == m_activeWorkspaceName); // disable urgency if workspace is active - if (workspace->name() == active_workspace_name_ && workspace->is_urgent()) { - workspace->set_urgent(false); + if (workspace->name() == m_activeWorkspaceName && workspace->isUrgent()) { + workspace->setUrgent(false); } // visible - workspace->set_visible(std::find(visible_workspaces.begin(), visible_workspaces.end(), - workspace->name()) != visible_workspaces.end()); + workspace->setVisible(std::find(visibleWorkspaces.begin(), visibleWorkspaces.end(), + workspace->name()) != visibleWorkspaces.end()); // set workspace icon - std::string &workspace_icon = icons_map_[""]; - if (with_icon_) { - workspace_icon = workspace->select_icon(icons_map_); + std::string &workspaceIcon = m_iconsMap[""]; + if (m_withIcon) { + workspaceIcon = workspace->selectIcon(m_iconsMap); } - workspace->update(format_, workspace_icon); + workspace->update(m_format, workspaceIcon); } - bool any_window_created = false; - std::vector not_created; + bool anyWindowCreated = false; + std::vector notCreated; - for (auto &window_payload : windows_to_create_) { + for (auto &windowPayload : m_windowsToCreate) { bool created = false; - for (auto &workspace : workspaces_) { - if (workspace->on_window_opened(window_payload)) { + for (auto &workspace : m_workspaces) { + if (workspace->onWindowOpened(windowPayload)) { created = true; - any_window_created = true; + anyWindowCreated = true; break; } } if (!created) { static auto const WINDOW_CREATION_TIMEOUT = 2; - if (window_payload.increment_time_spent_uncreated() < WINDOW_CREATION_TIMEOUT) { - not_created.push_back(window_payload); + if (windowPayload.incrementTimeSpentUncreated() < WINDOW_CREATION_TIMEOUT) { + notCreated.push_back(windowPayload); } } } - if (any_window_created) { + if (anyWindowCreated) { dp.emit(); } - windows_to_create_.clear(); - windows_to_create_ = not_created; + m_windowsToCreate.clear(); + m_windowsToCreate = notCreated; AModule::update(); } @@ -239,8 +238,8 @@ bool isDoubleSpecial(std::string const &workspace_name) { return workspace_name.find("special:special:") != std::string::npos; } -bool Workspaces::is_workspace_ignored(std::string const &name) { - for (auto &rule : ignore_workspaces_) { +bool Workspaces::isWorkspaceIgnored(std::string const &name) { + for (auto &rule : m_ignoreWorkspaces) { if (std::regex_match(name, rule)) { return true; break; @@ -251,394 +250,392 @@ bool Workspaces::is_workspace_ignored(std::string const &name) { } void Workspaces::onEvent(const std::string &ev) { - std::lock_guard lock(mutex_); + std::lock_guard lock(m_mutex); std::string eventName(begin(ev), begin(ev) + ev.find_first_of('>')); std::string payload = ev.substr(eventName.size() + 2); if (eventName == "workspace") { - on_workspace_activated(payload); + onWorkspaceActivated(payload); } else if (eventName == "destroyworkspace") { - on_workspace_destroyed(payload); + onWorkspaceDestroyed(payload); } else if (eventName == "createworkspace") { - on_workspace_created(payload); + onWorkspaceCreated(payload); } else if (eventName == "focusedmon") { - on_monitor_focused(payload); - } else if (eventName == "moveworkspace" && !all_outputs()) { - on_workspace_moved(payload); + onMonitorFocused(payload); + } else if (eventName == "moveworkspace" && !allOutputs()) { + onWorkspaceMoved(payload); } else if (eventName == "openwindow") { - on_window_opened(payload); + onWindowOpened(payload); } else if (eventName == "closewindow") { - on_window_closed(payload); + onWindowClosed(payload); } else if (eventName == "movewindow") { - on_window_moved(payload); + onWindowMoved(payload); } else if (eventName == "urgent") { - set_urgent_workspace(payload); + setUrgentWorkspace(payload); } else if (eventName == "renameworkspace") { - on_workspace_renamed(payload); + onWorkspaceRenamed(payload); } else if (eventName == "windowtitle") { - on_window_title_event(payload); + onWindowTitleEvent(payload); } dp.emit(); } -void Workspaces::on_workspace_activated(std::string const &payload) { - active_workspace_name_ = payload; +void Workspaces::onWorkspaceActivated(std::string const &payload) { + m_activeWorkspaceName = payload; } -void Workspaces::on_workspace_destroyed(std::string const &payload) { +void Workspaces::onWorkspaceDestroyed(std::string const &payload) { if (!isDoubleSpecial(payload)) { - workspaces_to_remove_.push_back(payload); + m_workspacesToRemove.push_back(payload); } } -void Workspaces::on_workspace_created(std::string const &payload) { - const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); +void Workspaces::onWorkspaceCreated(std::string const &payload) { + const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces"); - if (!is_workspace_ignored(payload)) { - for (Json::Value workspace_json : workspaces_json) { - std::string name = workspace_json["name"].asString(); + if (!isWorkspaceIgnored(payload)) { + for (Json::Value workspaceJson : workspacesJson) { + std::string name = workspaceJson["name"].asString(); if (name == payload && - (all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && - (show_special() || !name.starts_with("special")) && !isDoubleSpecial(payload)) { - workspaces_to_create_.push_back(workspace_json); + (allOutputs() || m_bar.output->name == workspaceJson["monitor"].asString()) && + (showSpecial() || !name.starts_with("special")) && !isDoubleSpecial(payload)) { + m_workspacesToCreate.push_back(workspaceJson); break; } } } } -void Workspaces::on_workspace_moved(std::string const &payload) { +void Workspaces::onWorkspaceMoved(std::string const &payload) { std::string workspace = payload.substr(0, payload.find(',')); - std::string new_output = payload.substr(payload.find(',') + 1); - bool should_show = show_special() || !workspace.starts_with("special"); - if (should_show && bar_.output->name == new_output) { // TODO: implement this better - const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (Json::Value workspace_json : workspaces_json) { - std::string name = workspace_json["name"].asString(); - if (name == workspace && bar_.output->name == workspace_json["monitor"].asString()) { - workspaces_to_create_.push_back(workspace_json); + std::string newOutput = payload.substr(payload.find(',') + 1); + bool shouldShow = showSpecial() || !workspace.starts_with("special"); + if (shouldShow && m_bar.output->name == newOutput) { // TODO: implement this better + const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces"); + for (Json::Value workspaceJson : workspacesJson) { + std::string name = workspaceJson["name"].asString(); + if (name == workspace && m_bar.output->name == workspaceJson["monitor"].asString()) { + m_workspacesToCreate.push_back(workspaceJson); break; } } } else { - workspaces_to_remove_.push_back(workspace); + m_workspacesToRemove.push_back(workspace); } } -void Workspaces::on_workspace_renamed(std::string const &payload) { - std::string workspace_id_str = payload.substr(0, payload.find(',')); - int workspace_id = workspace_id_str == "special" ? -99 : std::stoi(workspace_id_str); - std::string new_name = payload.substr(payload.find(',') + 1); - for (auto &workspace : workspaces_) { - if (workspace->id() == workspace_id) { - if (workspace->name() == active_workspace_name_) { - active_workspace_name_ = new_name; +void Workspaces::onWorkspaceRenamed(std::string const &payload) { + std::string workspaceIdStr = payload.substr(0, payload.find(',')); + int workspaceId = workspaceIdStr == "special" ? -99 : std::stoi(workspaceIdStr); + std::string newName = payload.substr(payload.find(',') + 1); + for (auto &workspace : m_workspaces) { + if (workspace->id() == workspaceId) { + if (workspace->name() == m_activeWorkspaceName) { + m_activeWorkspaceName = newName; } - workspace->set_name(new_name); + workspace->setName(newName); break; } } - sort_workspaces(); + sortWorkspaces(); } -void Workspaces::on_monitor_focused(std::string const &payload) { - active_workspace_name_ = payload.substr(payload.find(',') + 1); +void Workspaces::onMonitorFocused(std::string const &payload) { + m_activeWorkspaceName = payload.substr(payload.find(',') + 1); } -void Workspaces::on_window_opened(std::string const &payload) { - update_window_count(); - size_t last_comma_idx = 0; - size_t next_comma_idx = payload.find(','); - std::string window_address = payload.substr(last_comma_idx, next_comma_idx - last_comma_idx); +void Workspaces::onWindowOpened(std::string const &payload) { + updateWindowCount(); + size_t lastCommaIdx = 0; + size_t nextCommaIdx = payload.find(','); + std::string windowAddress = payload.substr(lastCommaIdx, nextCommaIdx - lastCommaIdx); - last_comma_idx = next_comma_idx; - next_comma_idx = payload.find(',', next_comma_idx + 1); - std::string workspace_name = - payload.substr(last_comma_idx + 1, next_comma_idx - last_comma_idx - 1); + lastCommaIdx = nextCommaIdx; + nextCommaIdx = payload.find(',', nextCommaIdx + 1); + std::string workspaceName = payload.substr(lastCommaIdx + 1, nextCommaIdx - lastCommaIdx - 1); - last_comma_idx = next_comma_idx; - next_comma_idx = payload.find(',', next_comma_idx + 1); - std::string window_class = - payload.substr(last_comma_idx + 1, next_comma_idx - last_comma_idx - 1); + lastCommaIdx = nextCommaIdx; + nextCommaIdx = payload.find(',', nextCommaIdx + 1); + std::string windowClass = payload.substr(lastCommaIdx + 1, nextCommaIdx - lastCommaIdx - 1); - std::string window_title = payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx); + std::string windowTitle = payload.substr(nextCommaIdx + 1, payload.length() - nextCommaIdx); - windows_to_create_.emplace_back(workspace_name, window_address, window_class, window_title); + m_windowsToCreate.emplace_back(workspaceName, windowAddress, windowClass, windowTitle); } -void Workspaces::on_window_closed(std::string const &addr) { - update_window_count(); - for (auto &workspace : workspaces_) { - if (workspace->close_window(addr)) { +void Workspaces::onWindowClosed(std::string const &addr) { + updateWindowCount(); + for (auto &workspace : m_workspaces) { + if (workspace->closeWindow(addr)) { break; } } } -void Workspaces::on_window_moved(std::string const &payload) { - update_window_count(); - size_t last_comma_idx = 0; - size_t next_comma_idx = payload.find(','); - std::string window_address = payload.substr(last_comma_idx, next_comma_idx - last_comma_idx); +void Workspaces::onWindowMoved(std::string const &payload) { + updateWindowCount(); + size_t lastCommaIdx = 0; + size_t nextCommaIdx = payload.find(','); + std::string windowAddress = payload.substr(lastCommaIdx, nextCommaIdx - lastCommaIdx); - std::string workspace_name = - payload.substr(next_comma_idx + 1, payload.length() - next_comma_idx); + std::string workspaceName = payload.substr(nextCommaIdx + 1, payload.length() - nextCommaIdx); - std::string window_repr; + std::string windowRepr; // If the window was still queued to be created, just change its destination // and exit - for (auto &window : windows_to_create_) { - if (window.addr() == window_address) { - window.move_to_worksace(workspace_name); + for (auto &window : m_windowsToCreate) { + if (window.getAddress() == windowAddress) { + window.moveToWorksace(workspaceName); return; } } // Take the window's representation from the old workspace... - for (auto &workspace : workspaces_) { - if (auto window_addr = workspace->close_window(window_address); window_addr != std::nullopt) { - window_repr = window_addr.value(); + for (auto &workspace : m_workspaces) { + if (auto windowAddr = workspace->closeWindow(windowAddress); windowAddr != std::nullopt) { + windowRepr = windowAddr.value(); break; } } // ...and add it to the new workspace - if (!window_repr.empty()) { - windows_to_create_.emplace_back(workspace_name, window_address, window_repr); + if (!windowRepr.empty()) { + m_windowsToCreate.emplace_back(workspaceName, windowAddress, windowRepr); } } -void Workspaces::on_window_title_event(std::string const &payload) { - auto window_workspace = - std::find_if(workspaces_.begin(), workspaces_.end(), - [payload](auto &workspace) { return workspace->contains_window(payload); }); +void Workspaces::onWindowTitleEvent(std::string const &payload) { + auto windowWorkspace = + std::find_if(m_workspaces.begin(), m_workspaces.end(), + [payload](auto &workspace) { return workspace->containsWindow(payload); }); - if (window_workspace != workspaces_.end()) { - Json::Value clients_data = gIPC->getSocket1JsonReply("clients"); - std::string json_window_address = fmt::format("0x{}", payload); + if (windowWorkspace != m_workspaces.end()) { + Json::Value clientsData = gIPC->getSocket1JsonReply("clients"); + std::string jsonWindowAddress = fmt::format("0x{}", payload); auto client = - std::find_if(clients_data.begin(), clients_data.end(), [json_window_address](auto &client) { - return client["address"].asString() == json_window_address; + std::find_if(clientsData.begin(), clientsData.end(), [jsonWindowAddress](auto &client) { + return client["address"].asString() == jsonWindowAddress; }); if (!client->empty()) { - (*window_workspace)->insert_window({*client}); + (*windowWorkspace)->insertWindow({*client}); } } } -void Workspaces::update_window_count() { - const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - for (auto &workspace : workspaces_) { - auto workspace_json = std::find_if( - workspaces_json.begin(), workspaces_json.end(), +void Workspaces::updateWindowCount() { + const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces"); + for (auto &workspace : m_workspaces) { + auto workspaceJson = std::find_if( + workspacesJson.begin(), workspacesJson.end(), [&](Json::Value const &x) { return x["name"].asString() == workspace->name(); }); uint32_t count = 0; - if (workspace_json != workspaces_json.end()) { + if (workspaceJson != workspacesJson.end()) { try { - count = (*workspace_json)["windows"].asUInt(); + count = (*workspaceJson)["windows"].asUInt(); } catch (const std::exception &e) { spdlog::error("Failed to update window count: {}", e.what()); } } - workspace->set_windows(count); + workspace->setWindows(count); } } -void Workspace::initialize_window_map(const Json::Value &clients_data) { - window_map_.clear(); +void Workspace::initializeWindowMap(const Json::Value &clients_data) { + m_windowMap.clear(); for (auto client : clients_data) { if (client["workspace"]["id"].asInt() == id()) { - insert_window({client}); + insertWindow({client}); } } } -void Workspace::insert_window(WindowCreationPayload create_window_paylod) { - if (!create_window_paylod.is_empty(workspace_manager_)) { - window_map_[create_window_paylod.addr()] = create_window_paylod.repr(workspace_manager_); +void Workspace::insertWindow(WindowCreationPayload create_window_paylod) { + if (!create_window_paylod.isEmpty(m_workspaceManager)) { + m_windowMap[create_window_paylod.getAddress()] = create_window_paylod.repr(m_workspaceManager); } }; -std::string Workspace::remove_window(WindowAddress const &addr) { - std::string window_repr = window_map_[addr]; - window_map_.erase(addr); - return window_repr; +std::string Workspace::removeWindow(WindowAddress const &addr) { + std::string windowRepr = m_windowMap[addr]; + m_windowMap.erase(addr); + return windowRepr; } -bool Workspace::on_window_opened(WindowCreationPayload const &create_window_paylod) { - if (create_window_paylod.workspace_name() == name()) { - insert_window(create_window_paylod); +bool Workspace::onWindowOpened(WindowCreationPayload const &create_window_paylod) { + if (create_window_paylod.getWorkspaceName() == name()) { + insertWindow(create_window_paylod); return true; } return false; } -std::optional Workspace::close_window(WindowAddress const &addr) { - if (window_map_.contains(addr)) { - return remove_window(addr); +std::optional Workspace::closeWindow(WindowAddress const &addr) { + if (m_windowMap.contains(addr)) { + return removeWindow(addr); } return std::nullopt; } -void Workspaces::create_workspace(Json::Value const &workspace_data, - Json::Value const &clients_data) { +void Workspaces::createWorkspace(Json::Value const &workspace_data, + Json::Value const &clients_data) { // avoid recreating existing workspaces - auto workspace_name = workspace_data["name"].asString(); + auto workspaceName = workspace_data["name"].asString(); auto workspace = std::find_if( - workspaces_.begin(), workspaces_.end(), [&](std::unique_ptr const &w) { - return (workspace_name.starts_with("special:") && workspace_name.substr(8) == w->name()) || - workspace_name == w->name(); + m_workspaces.begin(), m_workspaces.end(), + [workspaceName](std::unique_ptr const &w) { + return (workspaceName.starts_with("special:") && workspaceName.substr(8) == w->name()) || + workspaceName == w->name(); }); - if (workspace != workspaces_.end()) { - if (workspace_data["persistent"].asBool() and !(*workspace)->is_persistent()) { - (*workspace)->set_persistent(); + if (workspace != m_workspaces.end()) { + if (workspace_data["persistent"].asBool() and !(*workspace)->isPersistent()) { + (*workspace)->setPersistent(); } return; } // create new workspace - workspaces_.emplace_back(std::make_unique(workspace_data, *this, clients_data)); - Gtk::Button &new_workspace_button = workspaces_.back()->button(); - box_.pack_start(new_workspace_button, false, false); - sort_workspaces(); - new_workspace_button.show_all(); + m_workspaces.emplace_back(std::make_unique(workspace_data, *this, clients_data)); + Gtk::Button &newWorkspaceButton = m_workspaces.back()->button(); + m_box.pack_start(newWorkspaceButton, false, false); + sortWorkspaces(); + newWorkspaceButton.show_all(); } -void Workspaces::remove_workspace(std::string const &name) { +void Workspaces::removeWorkspace(std::string const &name) { auto workspace = - std::find_if(workspaces_.begin(), workspaces_.end(), [&](std::unique_ptr &x) { + std::find_if(m_workspaces.begin(), m_workspaces.end(), [&](std::unique_ptr &x) { return (name.starts_with("special:") && name.substr(8) == x->name()) || name == x->name(); }); - if (workspace == workspaces_.end()) { + if (workspace == m_workspaces.end()) { // happens when a workspace on another monitor is destroyed return; } - if ((*workspace)->is_persistent()) { - // don't remove persistent workspaces, create_workspace will take care of replacement + if ((*workspace)->isPersistent()) { + // don't remove persistent workspaces, createWorkspace will take care of replacement return; } - box_.remove(workspace->get()->button()); - workspaces_.erase(workspace); + m_box.remove(workspace->get()->button()); + m_workspaces.erase(workspace); } -void Workspaces::fill_persistent_workspaces() { +void Workspaces::fillPersistentWorkspaces() { if (config_["persistent_workspaces"].isObject()) { spdlog::warn( "persistent_workspaces is deprecated. Please change config to use persistent-workspaces."); } if (config_["persistent-workspaces"].isObject() || config_["persistent_workspaces"].isObject()) { - const Json::Value persistent_workspaces = config_["persistent-workspaces"].isObject() - ? config_["persistent-workspaces"] - : config_["persistent_workspaces"]; - const std::vector keys = persistent_workspaces.getMemberNames(); + const Json::Value persistentWorkspaces = config_["persistent-workspaces"].isObject() + ? config_["persistent-workspaces"] + : config_["persistent_workspaces"]; + const std::vector keys = persistentWorkspaces.getMemberNames(); for (const std::string &key : keys) { // only add if either: // 1. key is "*" and this monitor is not already defined in the config // 2. key is the current monitor name - bool can_create = - (key == "*" && std::find(keys.begin(), keys.end(), bar_.output->name) == keys.end()) || - key == bar_.output->name; - const Json::Value &value = persistent_workspaces[key]; + bool canCreate = + (key == "*" && std::find(keys.begin(), keys.end(), m_bar.output->name) == keys.end()) || + key == m_bar.output->name; + const Json::Value &value = persistentWorkspaces[key]; if (value.isInt()) { // value is a number => create that many workspaces for this monitor - if (can_create) { + if (canCreate) { int amount = value.asInt(); spdlog::debug("Creating {} persistent workspaces for monitor {}", amount, - bar_.output->name); + m_bar.output->name); for (int i = 0; i < amount; i++) { - persistent_workspaces_to_create_.emplace_back( - std::to_string(monitor_id_ * amount + i + 1)); + m_persistentWorkspacesToCreate.emplace_back( + std::to_string(m_monitorId * amount + i + 1)); } } } else if (value.isArray() && !value.empty()) { // value is an array => create defined workspaces for this monitor - if (can_create) { + if (canCreate) { for (const Json::Value &workspace : value) { if (workspace.isInt()) { - spdlog::debug("Creating workspace {} on monitor {}", workspace, bar_.output->name); - persistent_workspaces_to_create_.emplace_back(std::to_string(workspace.asInt())); + spdlog::debug("Creating workspace {} on monitor {}", workspace, m_bar.output->name); + m_persistentWorkspacesToCreate.emplace_back(std::to_string(workspace.asInt())); } } } else { // key is the workspace and value is array of monitors to create on for (const Json::Value &monitor : value) { - if (monitor.isString() && monitor.asString() == bar_.output->name) { - persistent_workspaces_to_create_.emplace_back(key); + if (monitor.isString() && monitor.asString() == m_bar.output->name) { + m_persistentWorkspacesToCreate.emplace_back(key); break; } } } } else { // this workspace should be displayed on all monitors - persistent_workspaces_to_create_.emplace_back(key); + m_persistentWorkspacesToCreate.emplace_back(key); } } } } -void Workspaces::create_persistent_workspaces() { - for (const std::string &workspace_name : persistent_workspaces_to_create_) { - Json::Value new_workspace; +void Workspaces::createPersistentWorkspaces() { + for (const std::string &workspaceName : m_persistentWorkspacesToCreate) { + Json::Value newWorkspace; try { // numbered persistent workspaces get the name as ID - new_workspace["id"] = workspace_name == "special" ? -99 : std::stoi(workspace_name); + newWorkspace["id"] = workspaceName == "special" ? -99 : std::stoi(workspaceName); } catch (const std::exception &e) { // named persistent workspaces start with ID=0 - new_workspace["id"] = 0; + newWorkspace["id"] = 0; } - new_workspace["name"] = workspace_name; - new_workspace["monitor"] = bar_.output->name; - new_workspace["windows"] = 0; - new_workspace["persistent"] = true; + newWorkspace["name"] = workspaceName; + newWorkspace["monitor"] = m_bar.output->name; + newWorkspace["windows"] = 0; + newWorkspace["persistent"] = true; - create_workspace(new_workspace); + createWorkspace(newWorkspace); } } void Workspaces::init() { - active_workspace_name_ = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString(); + m_activeWorkspaceName = (gIPC->getSocket1JsonReply("activeworkspace"))["name"].asString(); // get monitor ID from name (used by persistent workspaces) - monitor_id_ = 0; + m_monitorId = 0; auto monitors = gIPC->getSocket1JsonReply("monitors"); - auto current_monitor = std::find_if( + auto currentMonitor = std::find_if( monitors.begin(), monitors.end(), - [this](const Json::Value &m) { return m["name"].asString() == bar_.output->name; }); - if (current_monitor == monitors.end()) { - spdlog::error("Monitor '{}' does not have an ID? Using 0", bar_.output->name); + [this](const Json::Value &m) { return m["name"].asString() == m_bar.output->name; }); + if (currentMonitor == monitors.end()) { + spdlog::error("Monitor '{}' does not have an ID? Using 0", m_bar.output->name); } else { - monitor_id_ = (*current_monitor)["id"].asInt(); + m_monitorId = (*currentMonitor)["id"].asInt(); } - const Json::Value workspaces_json = gIPC->getSocket1JsonReply("workspaces"); - const Json::Value clients_json = gIPC->getSocket1JsonReply("clients"); + const Json::Value workspacesJson = gIPC->getSocket1JsonReply("workspaces"); + const Json::Value clientsJson = gIPC->getSocket1JsonReply("clients"); - for (Json::Value workspace_json : workspaces_json) { - std::string workspace_name = workspace_json["name"].asString(); - if ((all_outputs() || bar_.output->name == workspace_json["monitor"].asString()) && - (!workspace_name.starts_with("special") || show_special()) && - !is_workspace_ignored(workspace_name)) { - create_workspace(workspace_json, clients_json); + for (Json::Value workspaceJson : workspacesJson) { + std::string workspaceName = workspaceJson["name"].asString(); + if ((allOutputs() || m_bar.output->name == workspaceJson["monitor"].asString()) && + (!workspaceName.starts_with("special") || showSpecial()) && + !isWorkspaceIgnored(workspaceName)) { + createWorkspace(workspaceJson, clientsJson); } } - fill_persistent_workspaces(); - create_persistent_workspaces(); + fillPersistentWorkspaces(); + createPersistentWorkspaces(); - update_window_count(); + updateWindowCount(); - sort_workspaces(); + sortWorkspaces(); dp.emit(); } @@ -646,41 +643,41 @@ void Workspaces::init() { Workspaces::~Workspaces() { gIPC->unregisterForIPC(this); // wait for possible event handler to finish - std::lock_guard lg(mutex_); + std::lock_guard lg(m_mutex); } Workspace::Workspace(const Json::Value &workspace_data, Workspaces &workspace_manager, const Json::Value &clients_data) - : workspace_manager_(workspace_manager), - id_(workspace_data["id"].asInt()), - name_(workspace_data["name"].asString()), - output_(workspace_data["monitor"].asString()), // TODO:allow using monitor desc - windows_(workspace_data["windows"].asInt()), - active_(true) { - if (name_.starts_with("name:")) { - name_ = name_.substr(5); - } else if (name_.starts_with("special")) { - name_ = id_ == -99 ? name_ : name_.substr(8); - is_special_ = true; + : m_workspaceManager(workspace_manager), + m_id(workspace_data["id"].asInt()), + m_name(workspace_data["name"].asString()), + m_output(workspace_data["monitor"].asString()), // TODO:allow using monitor desc + m_windows(workspace_data["windows"].asInt()), + m_active(true) { + if (m_name.starts_with("name:")) { + m_name = m_name.substr(5); + } else if (m_name.starts_with("special")) { + m_name = m_id == -99 ? m_name : m_name.substr(8); + m_isSpecial = true; } if (workspace_data.isMember("persistent")) { - is_persistent_ = workspace_data["persistent"].asBool(); + m_isPersistent = workspace_data["persistent"].asBool(); } - button_.add_events(Gdk::BUTTON_PRESS_MASK); - button_.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handle_clicked), - false); + m_button.add_events(Gdk::BUTTON_PRESS_MASK); + m_button.signal_button_press_event().connect(sigc::mem_fun(*this, &Workspace::handleClicked), + false); - button_.set_relief(Gtk::RELIEF_NONE); - content_.set_center_widget(label_); - button_.add(content_); + m_button.set_relief(Gtk::RELIEF_NONE); + m_content.set_center_widget(m_label); + m_button.add(m_content); - initialize_window_map(clients_data); + initializeWindowMap(clients_data); } -void add_or_remove_class(const Glib::RefPtr &context, bool condition, - const std::string &class_name) { +void addOrRemoveClass(const Glib::RefPtr &context, bool condition, + const std::string &class_name) { if (condition) { context->add_class(class_name); } else { @@ -690,76 +687,76 @@ void add_or_remove_class(const Glib::RefPtr &context, bool co void Workspace::update(const std::string &format, const std::string &icon) { // clang-format off - if (this->workspace_manager_.active_only() && \ - !this->active() && \ - !this->is_persistent() && \ - !this->is_visible() && \ - !this->is_special()) { + if (this->m_workspaceManager.activeOnly() && \ + !this->isActive() && \ + !this->isPersistent() && \ + !this->isVisible() && \ + !this->isSpecial()) { // clang-format on - // if active_only is true, hide if not active, persistent, visible or special - button_.hide(); + // if activeOnly is true, hide if not active, persistent, visible or special + m_button.hide(); return; } - button_.show(); + m_button.show(); - auto style_context = button_.get_style_context(); - add_or_remove_class(style_context, active(), "active"); - add_or_remove_class(style_context, is_special(), "special"); - add_or_remove_class(style_context, is_empty(), "empty"); - add_or_remove_class(style_context, is_persistent(), "persistent"); - add_or_remove_class(style_context, is_urgent(), "urgent"); - add_or_remove_class(style_context, is_visible(), "visible"); + auto styleContext = m_button.get_style_context(); + addOrRemoveClass(styleContext, isActive(), "active"); + addOrRemoveClass(styleContext, isSpecial(), "special"); + addOrRemoveClass(styleContext, isEmpty(), "empty"); + addOrRemoveClass(styleContext, isPersistent(), "persistent"); + addOrRemoveClass(styleContext, isUrgent(), "urgent"); + addOrRemoveClass(styleContext, isVisible(), "visible"); std::string windows; - auto window_separator = workspace_manager_.get_window_separator(); + auto windowSeparator = m_workspaceManager.getWindowSeparator(); - bool is_not_first = false; + bool isNotFirst = false; - for (auto &[_pid, window_repr] : window_map_) { - if (is_not_first) { - windows.append(window_separator); + for (auto &[_pid, window_repr] : m_windowMap) { + if (isNotFirst) { + windows.append(windowSeparator); } - is_not_first = true; + isNotFirst = true; windows.append(window_repr); } - label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()), - fmt::arg("name", name()), fmt::arg("icon", icon), - fmt::arg("windows", windows))); + m_label.set_markup(fmt::format(fmt::runtime(format), fmt::arg("id", id()), + fmt::arg("name", name()), fmt::arg("icon", icon), + fmt::arg("windows", windows))); } -void Workspaces::sort_workspaces() { - std::sort(workspaces_.begin(), workspaces_.end(), +void Workspaces::sortWorkspaces() { + std::sort(m_workspaces.begin(), m_workspaces.end(), [&](std::unique_ptr &a, std::unique_ptr &b) { // Helper comparisons - auto is_id_less = a->id() < b->id(); - auto is_name_less = a->name() < b->name(); + auto isIdLess = a->id() < b->id(); + auto isNameLess = a->name() < b->name(); - switch (sort_by_) { - case SORT_METHOD::ID: - return is_id_less; - case SORT_METHOD::NAME: - return is_name_less; - case SORT_METHOD::NUMBER: + switch (m_sortBy) { + case SortMethod::ID: + return isIdLess; + case SortMethod::NAME: + return isNameLess; + case SortMethod::NUMBER: try { return std::stoi(a->name()) < std::stoi(b->name()); } catch (const std::invalid_argument &) { // Handle the exception if necessary. break; } - case SORT_METHOD::DEFAULT: + case SortMethod::DEFAULT: default: // Handle the default case here. // normal -> named persistent -> named -> special -> named special // both normal (includes numbered persistent) => sort by ID if (a->id() > 0 && b->id() > 0) { - return is_id_less; + return isIdLess; } // one normal, one special => normal first - if ((a->is_special()) ^ (b->is_special())) { - return b->is_special(); + if ((a->isSpecial()) ^ (b->isSpecial())) { + return b->isSpecial(); } // only one normal, one named @@ -768,92 +765,92 @@ void Workspaces::sort_workspaces() { } // both special - if (a->is_special() && b->is_special()) { + if (a->isSpecial() && b->isSpecial()) { // if one is -99 => put it last if (a->id() == -99 || b->id() == -99) { return b->id() == -99; } // both are 0 (not yet named persistents) / both are named specials (-98 <= ID // <=-1) - return is_name_less; + return isNameLess; } // sort non-special named workspaces by name (ID <= -1377) - return is_name_less; + return isNameLess; break; } // Return a default value if none of the cases match. - return is_name_less; // You can adjust this to your specific needs. + return isNameLess; // You can adjust this to your specific needs. }); - for (size_t i = 0; i < workspaces_.size(); ++i) { - box_.reorder_child(workspaces_[i]->button(), i); + for (size_t i = 0; i < m_workspaces.size(); ++i) { + m_box.reorder_child(m_workspaces[i]->button(), i); } } -std::string &Workspace::select_icon(std::map &icons_map) { - if (is_urgent()) { - auto urgent_icon_it = icons_map.find("urgent"); - if (urgent_icon_it != icons_map.end()) { - return urgent_icon_it->second; +std::string &Workspace::selectIcon(std::map &icons_map) { + if (isUrgent()) { + auto urgentIconIt = icons_map.find("urgent"); + if (urgentIconIt != icons_map.end()) { + return urgentIconIt->second; } } - if (active()) { - auto active_icon_it = icons_map.find("active"); - if (active_icon_it != icons_map.end()) { - return active_icon_it->second; + if (isActive()) { + auto activeIconIt = icons_map.find("active"); + if (activeIconIt != icons_map.end()) { + return activeIconIt->second; } } - if (is_special()) { - auto special_icon_it = icons_map.find("special"); - if (special_icon_it != icons_map.end()) { - return special_icon_it->second; + if (isSpecial()) { + auto specialIconIt = icons_map.find("special"); + if (specialIconIt != icons_map.end()) { + return specialIconIt->second; } } - auto named_icon_it = icons_map.find(name()); - if (named_icon_it != icons_map.end()) { - return named_icon_it->second; + auto namedIconIt = icons_map.find(name()); + if (namedIconIt != icons_map.end()) { + return namedIconIt->second; } - if (is_visible()) { - auto visible_icon_it = icons_map.find("visible"); - if (visible_icon_it != icons_map.end()) { - return visible_icon_it->second; + if (isVisible()) { + auto visibleIconIt = icons_map.find("visible"); + if (visibleIconIt != icons_map.end()) { + return visibleIconIt->second; } } - if (is_empty()) { - auto empty_icon_it = icons_map.find("empty"); - if (empty_icon_it != icons_map.end()) { - return empty_icon_it->second; + if (isEmpty()) { + auto emptyIconIt = icons_map.find("empty"); + if (emptyIconIt != icons_map.end()) { + return emptyIconIt->second; } } - if (is_persistent()) { - auto persistent_icon_it = icons_map.find("persistent"); - if (persistent_icon_it != icons_map.end()) { - return persistent_icon_it->second; + if (isPersistent()) { + auto persistentIconIt = icons_map.find("persistent"); + if (persistentIconIt != icons_map.end()) { + return persistentIconIt->second; } } - auto default_icon_it = icons_map.find("default"); - if (default_icon_it != icons_map.end()) { - return default_icon_it->second; + auto defaultIconIt = icons_map.find("default"); + if (defaultIconIt != icons_map.end()) { + return defaultIconIt->second; } - return name_; + return m_name; } -bool Workspace::handle_clicked(GdkEventButton *bt) const { +bool Workspace::handleClicked(GdkEventButton *bt) const { if (bt->type == GDK_BUTTON_PRESS) { try { if (id() > 0) { // normal or numbered persistent gIPC->getSocket1Reply("dispatch workspace " + std::to_string(id())); - } else if (!is_special()) { // named + } else if (!isSpecial()) { // named gIPC->getSocket1Reply("dispatch workspace name:" + name()); } else if (id() != -99) { // named special gIPC->getSocket1Reply("dispatch togglespecialworkspace " + name()); @@ -868,92 +865,92 @@ bool Workspace::handle_clicked(GdkEventButton *bt) const { return false; } -void Workspaces::set_urgent_workspace(std::string const &windowaddress) { - const Json::Value clients_json = gIPC->getSocket1JsonReply("clients"); - int workspace_id = -1; +void Workspaces::setUrgentWorkspace(std::string const &windowaddress) { + const Json::Value clientsJson = gIPC->getSocket1JsonReply("clients"); + int workspaceId = -1; - for (Json::Value client_json : clients_json) { - if (client_json["address"].asString().ends_with(windowaddress)) { - workspace_id = client_json["workspace"]["id"].asInt(); + for (Json::Value clientJson : clientsJson) { + if (clientJson["address"].asString().ends_with(windowaddress)) { + workspaceId = clientJson["workspace"]["id"].asInt(); break; } } auto workspace = - std::find_if(workspaces_.begin(), workspaces_.end(), - [&](std::unique_ptr &x) { return x->id() == workspace_id; }); - if (workspace != workspaces_.end()) { - workspace->get()->set_urgent(); + std::find_if(m_workspaces.begin(), m_workspaces.end(), + [workspaceId](std::unique_ptr &x) { return x->id() == workspaceId; }); + if (workspace != m_workspaces.end()) { + workspace->get()->setUrgent(); } } -std::string Workspaces::get_rewrite(std::string window_class, std::string window_title) { - std::string window_repr_key; - if (window_rewrite_config_uses_title()) { - window_repr_key = fmt::format("class<{}> title<{}>", window_class, window_title); +std::string Workspaces::getRewrite(std::string window_class, std::string window_title) { + std::string windowReprKey; + if (windowRewriteConfigUsesTitle()) { + windowReprKey = fmt::format("class<{}> title<{}>", window_class, window_title); } else { - window_repr_key = fmt::format("class<{}>", window_class); + windowReprKey = fmt::format("class<{}>", window_class); } - return window_rewrite_rules_.get(window_repr_key); + return m_windowRewriteRules.get(windowReprKey); } WindowCreationPayload::WindowCreationPayload(std::string workspace_name, WindowAddress window_address, std::string window_repr) - : window_(std::move(window_repr)), - window_address_(std::move(window_address)), - workspace_name_(std::move(workspace_name)) { - clear_addr(); - clear_workspace_name(); + : m_window(std::move(window_repr)), + m_windowAddress(std::move(window_address)), + m_workspaceName(std::move(workspace_name)) { + clearAddr(); + clearWorkspaceName(); } WindowCreationPayload::WindowCreationPayload(std::string workspace_name, WindowAddress window_address, std::string window_class, std::string window_title) - : window_(std::make_pair(std::move(window_class), std::move(window_title))), - window_address_(std::move(window_address)), - workspace_name_(std::move(workspace_name)) { - clear_addr(); - clear_workspace_name(); + : m_window(std::make_pair(std::move(window_class), std::move(window_title))), + m_windowAddress(std::move(window_address)), + m_workspaceName(std::move(workspace_name)) { + clearAddr(); + clearWorkspaceName(); } WindowCreationPayload::WindowCreationPayload(Json::Value const &client_data) - : window_(std::make_pair(client_data["class"].asString(), client_data["title"].asString())), - window_address_(client_data["address"].asString()), - workspace_name_(client_data["workspace"]["name"].asString()) { - clear_addr(); - clear_workspace_name(); + : m_window(std::make_pair(client_data["class"].asString(), client_data["title"].asString())), + m_windowAddress(client_data["address"].asString()), + m_workspaceName(client_data["workspace"]["name"].asString()) { + clearAddr(); + clearWorkspaceName(); } std::string WindowCreationPayload::repr(Workspaces &workspace_manager) { - if (std::holds_alternative(window_)) { - return std::get(window_); + if (std::holds_alternative(m_window)) { + return std::get(m_window); } - if (std::holds_alternative(window_)) { - auto [window_class, window_title] = std::get(window_); - return workspace_manager.get_rewrite(window_class, window_title); + if (std::holds_alternative(m_window)) { + auto [window_class, window_title] = std::get(m_window); + return workspace_manager.getRewrite(window_class, window_title); } // Unreachable spdlog::error("WorkspaceWindow::repr: Unreachable"); throw std::runtime_error("WorkspaceWindow::repr: Unreachable"); } -bool WindowCreationPayload::is_empty(Workspaces &workspace_manager) { - if (std::holds_alternative(window_)) { - return std::get(window_).empty(); +bool WindowCreationPayload::isEmpty(Workspaces &workspace_manager) { + if (std::holds_alternative(m_window)) { + return std::get(m_window).empty(); } - if (std::holds_alternative(window_)) { - auto [window_class, window_title] = std::get(window_); + if (std::holds_alternative(m_window)) { + auto [window_class, window_title] = std::get(m_window); return (window_class.empty() && - (!workspace_manager.window_rewrite_config_uses_title() || window_title.empty())); + (!workspace_manager.windowRewriteConfigUsesTitle() || window_title.empty())); } // Unreachable - spdlog::error("WorkspaceWindow::is_empty: Unreachable"); - throw std::runtime_error("WorkspaceWindow::is_empty: Unreachable"); + spdlog::error("WorkspaceWindow::isEmpty: Unreachable"); + throw std::runtime_error("WorkspaceWindow::isEmpty: Unreachable"); } -int WindowCreationPayload::increment_time_spent_uncreated() { return time_spent_uncreated_++; } +int WindowCreationPayload::incrementTimeSpentUncreated() { return m_timeSpentUncreated++; } -void WindowCreationPayload::clear_addr() { +void WindowCreationPayload::clearAddr() { // substr(2, ...) is necessary because Hyprland's JSON follows this format: // 0x{ADDR} // While Hyprland's IPC follows this format: @@ -961,13 +958,13 @@ void WindowCreationPayload::clear_addr() { static const std::string ADDR_PREFIX = "0x"; static const int ADDR_PREFIX_LEN = ADDR_PREFIX.length(); - if (window_address_.starts_with(ADDR_PREFIX)) { - window_address_ = - window_address_.substr(ADDR_PREFIX_LEN, window_address_.length() - ADDR_PREFIX_LEN); + if (m_windowAddress.starts_with(ADDR_PREFIX)) { + m_windowAddress = + m_windowAddress.substr(ADDR_PREFIX_LEN, m_windowAddress.length() - ADDR_PREFIX_LEN); } } -void WindowCreationPayload::clear_workspace_name() { +void WindowCreationPayload::clearWorkspaceName() { // The workspace name may optionally feature "special:" at the beginning. // If so, we need to remove it because the workspace is saved WITHOUT the // special qualifier. The reasoning is that not all of Hyprland's IPC events @@ -976,14 +973,14 @@ void WindowCreationPayload::clear_workspace_name() { static const std::string SPECIAL_QUALIFIER_PREFIX = "special:"; static const int SPECIAL_QUALIFIER_PREFIX_LEN = SPECIAL_QUALIFIER_PREFIX.length(); - if (workspace_name_.starts_with(SPECIAL_QUALIFIER_PREFIX)) { - workspace_name_ = workspace_name_.substr( - SPECIAL_QUALIFIER_PREFIX_LEN, workspace_name_.length() - SPECIAL_QUALIFIER_PREFIX_LEN); + if (m_workspaceName.starts_with(SPECIAL_QUALIFIER_PREFIX)) { + m_workspaceName = m_workspaceName.substr( + SPECIAL_QUALIFIER_PREFIX_LEN, m_workspaceName.length() - SPECIAL_QUALIFIER_PREFIX_LEN); } } -void WindowCreationPayload::move_to_worksace(std::string &new_workspace_name) { - workspace_name_ = new_workspace_name; +void WindowCreationPayload::moveToWorksace(std::string &new_workspace_name) { + m_workspaceName = new_workspace_name; } } // namespace waybar::modules::hyprland diff --git a/src/util/enum.cpp b/src/util/enum.cpp index a29304c5..dc3eae0c 100644 --- a/src/util/enum.cpp +++ b/src/util/enum.cpp @@ -28,7 +28,7 @@ EnumType EnumParser::parseStringToEnum(const std::string& str, std::map capitalizedEnumMap; std::transform( enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()), - [this](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); }); + [](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); }); // Return enum match of string auto it = capitalizedEnumMap.find(uppercaseStr); @@ -40,6 +40,6 @@ EnumType EnumParser::parseStringToEnum(const std::string& str, // Explicit instantiations for specific EnumType types you intend to use // Add explicit instantiations for all relevant EnumType types -template struct EnumParser; +template struct EnumParser; } // namespace waybar::util