Merge pull request #3448 from khaneliman/sway

sway/workspaces: remove deprecated field and ignore empty rewrite rules
This commit is contained in:
Alexis Rouillard 2024-07-17 22:36:58 +02:00 committed by GitHub
commit d061d2259e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 30 deletions

View File

@ -19,7 +19,7 @@ namespace waybar::modules::sway {
class Workspaces : public AModule, public sigc::trackable { class Workspaces : public AModule, public sigc::trackable {
public: public:
Workspaces(const std::string&, const waybar::Bar&, const Json::Value&); Workspaces(const std::string&, const waybar::Bar&, const Json::Value&);
virtual ~Workspaces() = default; ~Workspaces() override = default;
auto update() -> void override; auto update() -> void override;
private: private:
@ -38,10 +38,10 @@ class Workspaces : public AModule, public sigc::trackable {
Gtk::Button& addButton(const Json::Value&); Gtk::Button& addButton(const Json::Value&);
void onButtonReady(const Json::Value&, Gtk::Button&); void onButtonReady(const Json::Value&, Gtk::Button&);
std::string getIcon(const std::string&, const Json::Value&); std::string getIcon(const std::string&, const Json::Value&);
const std::string getCycleWorkspace(std::vector<Json::Value>::iterator, bool prev) const; std::string getCycleWorkspace(std::vector<Json::Value>::iterator, bool prev) const;
uint16_t getWorkspaceIndex(const std::string& name) const; uint16_t getWorkspaceIndex(const std::string& name) const;
std::string trimWorkspaceName(std::string); static std::string trimWorkspaceName(std::string);
bool handleScroll(GdkEventScroll*) override; bool handleScroll(GdkEventScroll* /*unused*/) override;
const Bar& bar_; const Bar& bar_;
std::vector<Json::Value> workspaces_; std::vector<Json::Value> workspaces_;

View File

@ -87,6 +87,7 @@ warp-on-scroll: ++
Regex rules to map window class to an icon or preferred method of representation for a workspace's window. Regex rules to map window class to an icon or preferred method of representation for a workspace's window.
Keys are the rules, while the values are the methods of representation. Keys are the rules, while the values are the methods of representation.
Rules may specify `class<...>`, `title<...>`, or both in order to fine-tune the matching. Rules may specify `class<...>`, `title<...>`, or both in order to fine-tune the matching.
You may assign an empty value to a rule to have it ignored from generating any representation in workspaces.
*window-rewrite-default*: *window-rewrite-default*:
typeof: string ++ typeof: string ++

View File

@ -11,15 +11,14 @@ namespace waybar::modules::sway {
// Helper function to assign a number to a workspace, just like sway. In fact // Helper function to assign a number to a workspace, just like sway. In fact
// this is taken quite verbatim from `sway/ipc-json.c`. // this is taken quite verbatim from `sway/ipc-json.c`.
int Workspaces::convertWorkspaceNameToNum(std::string name) { int Workspaces::convertWorkspaceNameToNum(std::string name) {
if (isdigit(name[0])) { if (isdigit(name[0]) != 0) {
errno = 0; errno = 0;
char *endptr = NULL; char *endptr = nullptr;
long long parsed_num = strtoll(name.c_str(), &endptr, 10); long long parsed_num = strtoll(name.c_str(), &endptr, 10);
if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name.c_str()) { if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name.c_str()) {
return -1; return -1;
} else {
return (int)parsed_num;
} }
return (int)parsed_num;
} }
return -1; return -1;
} }
@ -47,7 +46,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
bar_(bar), bar_(bar),
box_(bar.orientation, 0) { box_(bar.orientation, 0) {
if (config["format-icons"]["high-priority-named"].isArray()) { if (config["format-icons"]["high-priority-named"].isArray()) {
for (auto &it : config["format-icons"]["high-priority-named"]) { for (const auto &it : config["format-icons"]["high-priority-named"]) {
high_priority_named_.push_back(it.asString()); high_priority_named_.push_back(it.asString());
} }
} }
@ -70,7 +69,7 @@ Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value
m_windowRewriteRules = waybar::util::RegexCollection( m_windowRewriteRules = waybar::util::RegexCollection(
windowRewrite, m_windowRewriteDefault, windowRewrite, m_windowRewriteDefault,
[this](std::string &window_rule) { return windowRewritePriorityFunction(window_rule); }); [](std::string &window_rule) { return windowRewritePriorityFunction(window_rule); });
ipc_.subscribe(R"(["workspace"])"); ipc_.subscribe(R"(["workspace"])");
ipc_.subscribe(R"(["window"])"); ipc_.subscribe(R"(["window"])");
ipc_.signal_event.connect(sigc::mem_fun(*this, &Workspaces::onEvent)); ipc_.signal_event.connect(sigc::mem_fun(*this, &Workspaces::onEvent));
@ -125,18 +124,10 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
std::copy(output["floating_nodes"].begin(), output["floating_nodes"].end(), std::copy(output["floating_nodes"].begin(), output["floating_nodes"].end(),
std::back_inserter(workspaces_)); std::back_inserter(workspaces_));
} }
if (config_["persistent_workspaces"].isObject()) {
spdlog::warn(
"persistent_workspaces is deprecated. Please change config to use "
"persistent-workspaces.");
}
// adding persistent workspaces (as per the config file) // adding persistent workspaces (as per the config file)
if (config_["persistent-workspaces"].isObject() || if (config_["persistent-workspaces"].isObject()) {
config_["persistent_workspaces"].isObject()) { const Json::Value &p_workspaces = config_["persistent-workspaces"];
const Json::Value &p_workspaces = config_["persistent-workspaces"].isObject()
? config_["persistent-workspaces"]
: config_["persistent_workspaces"];
const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames(); const std::vector<std::string> p_workspaces_names = p_workspaces.getMemberNames();
for (const std::string &p_w_name : p_workspaces_names) { for (const std::string &p_w_name : p_workspaces_names) {
@ -270,14 +261,18 @@ void Workspaces::updateWindows(const Json::Value &node, std::string &windows) {
node["name"].isString()) { node["name"].isString()) {
std::string title = g_markup_escape_text(node["name"].asString().c_str(), -1); std::string title = g_markup_escape_text(node["name"].asString().c_str(), -1);
std::string windowClass = node["app_id"].asString(); std::string windowClass = node["app_id"].asString();
// Only add window rewrites that can be looked up
if (!windowClass.empty()) {
std::string windowReprKey = fmt::format("class<{}> title<{}>", windowClass, title); std::string windowReprKey = fmt::format("class<{}> title<{}>", windowClass, title);
std::string window = m_windowRewriteRules.get(windowReprKey); std::string window = m_windowRewriteRules.get(windowReprKey);
// allow result to have formatting // allow result to have formatting
window = window = fmt::format(fmt::runtime(window), fmt::arg("name", title),
fmt::format(fmt::runtime(window), fmt::arg("name", title), fmt::arg("class", windowClass)); fmt::arg("class", windowClass));
windows.append(window); windows.append(window);
windows.append(m_formatWindowSeperator); windows.append(m_formatWindowSeperator);
} }
}
for (const Json::Value &child : node["nodes"]) { for (const Json::Value &child : node["nodes"]) {
updateWindows(child, windows); updateWindows(child, windows);
} }
@ -422,7 +417,7 @@ std::string Workspaces::getIcon(const std::string &name, const Json::Value &node
} }
bool Workspaces::handleScroll(GdkEventScroll *e) { bool Workspaces::handleScroll(GdkEventScroll *e) {
if (gdk_event_get_pointer_emulated((GdkEvent *)e)) { if (gdk_event_get_pointer_emulated((GdkEvent *)e) != 0) {
/** /**
* Ignore emulated scroll events on window * Ignore emulated scroll events on window
*/ */
@ -472,8 +467,7 @@ bool Workspaces::handleScroll(GdkEventScroll *e) {
return true; return true;
} }
const std::string Workspaces::getCycleWorkspace(std::vector<Json::Value>::iterator it, std::string Workspaces::getCycleWorkspace(std::vector<Json::Value>::iterator it, bool prev) const {
bool prev) const {
if (prev && it == workspaces_.begin() && !config_["disable-scroll-wraparound"].asBool()) { if (prev && it == workspaces_.begin() && !config_["disable-scroll-wraparound"].asBool()) {
return (*(--workspaces_.end()))["name"].asString(); return (*(--workspaces_.end()))["name"].asString();
} }