river/tags: refactor to support special purpose tags

adds the set-tags and toggle-tags setting so it's possible to have
different tags set vs toggled. This enables the use of e.g. sticky tags

Also clean-up the code a bit.
This commit is contained in:
shironeko 2023-01-15 12:16:30 -05:00
parent 6c8e186586
commit 5649c3f552
1 changed files with 44 additions and 45 deletions

View File

@ -53,7 +53,7 @@ static const zriver_command_callback_v1_listener command_callback_listener_impl{
static void handle_global(void *data, struct wl_registry *registry, uint32_t name, static void handle_global(void *data, struct wl_registry *registry, uint32_t name,
const char *interface, uint32_t version) { const char *interface, uint32_t version) {
if (std::strcmp(interface, zriver_status_manager_v1_interface.name) == 0) { if (std::strcmp(interface, zriver_status_manager_v1_interface.name) == 0) {
version = std::min<uint32_t>(version, 2); version = std::min(version, 2u);
if (version < ZRIVER_OUTPUT_STATUS_V1_URGENT_TAGS_SINCE_VERSION) { if (version < ZRIVER_OUTPUT_STATUS_V1_URGENT_TAGS_SINCE_VERSION) {
spdlog::warn("river server does not support urgent tags"); spdlog::warn("river server does not support urgent tags");
} }
@ -62,13 +62,13 @@ static void handle_global(void *data, struct wl_registry *registry, uint32_t nam
} }
if (std::strcmp(interface, zriver_control_v1_interface.name) == 0) { if (std::strcmp(interface, zriver_control_v1_interface.name) == 0) {
version = std::min<uint32_t>(version, 1); version = std::min(version, 1u);
static_cast<Tags *>(data)->control_ = static_cast<struct zriver_control_v1 *>( static_cast<Tags *>(data)->control_ = static_cast<struct zriver_control_v1 *>(
wl_registry_bind(registry, name, &zriver_control_v1_interface, version)); wl_registry_bind(registry, name, &zriver_control_v1_interface, version));
} }
if (std::strcmp(interface, wl_seat_interface.name) == 0) { if (std::strcmp(interface, wl_seat_interface.name) == 0) {
version = std::min<uint32_t>(version, 1); version = std::min(version, 1u);
static_cast<Tags *>(data)->seat_ = static_cast<struct wl_seat *>( static_cast<Tags *>(data)->seat_ = static_cast<struct wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, version)); wl_registry_bind(registry, name, &wl_seat_interface, version));
} }
@ -114,33 +114,39 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
event_box_.add(box_); event_box_.add(box_);
// Default to 9 tags, cap at 32 // Default to 9 tags, cap at 32
const uint32_t num_tags = const int num_tags =
config["num-tags"].isUInt() ? std::min<uint32_t>(32, config_["num-tags"].asUInt()) : 9; config["num-tags"].isUInt() ? std::min<int>(32, config_["num-tags"].asUInt()) : 9;
std::vector<std::string> tag_labels(num_tags); const auto tag_labels = config["tag-labels"];
for (uint32_t tag = 0; tag < num_tags; ++tag) { const auto set_tags = config["set-tags"];
tag_labels[tag] = std::to_string(tag + 1); const auto toggle_tags = config["toggle-tags"];
} for (int tag = 0; tag < num_tags; ++tag) {
const Json::Value custom_labels = config["tag-labels"]; if (tag_labels.isArray() && !tag_labels.empty()) {
if (custom_labels.isArray() && !custom_labels.empty()) { buttons_.emplace_back(tag_labels[tag].asString());
for (uint32_t tag = 0; tag < std::min(num_tags, custom_labels.size()); ++tag) { } else {
tag_labels[tag] = custom_labels[tag].asString(); // default name is the tag value
buttons_.emplace_back(std::to_string(tag + 1));
} }
}
uint32_t i = 1; auto &button = buttons_[tag];
for (const auto &tag_label : tag_labels) {
Gtk::Button &button = buttons_.emplace_back(tag_label);
button.set_relief(Gtk::RELIEF_NONE); button.set_relief(Gtk::RELIEF_NONE);
box_.pack_start(button, false, false, 0); box_.pack_start(button, false, false, 0);
if (!config_["disable-click"].asBool()) { if (!config_["disable-click"].asBool()) {
button.signal_clicked().connect( if (set_tags.isArray() && !set_tags.empty())
sigc::bind(sigc::mem_fun(*this, &Tags::handle_primary_clicked), i)); button.signal_clicked().connect(sigc::bind(
button.signal_button_press_event().connect( sigc::mem_fun(*this, &Tags::handle_primary_clicked), set_tags[tag].asUInt()));
sigc::bind(sigc::mem_fun(*this, &Tags::handle_button_press), i)); else
button.signal_clicked().connect(
sigc::bind(sigc::mem_fun(*this, &Tags::handle_primary_clicked), (1 << tag)));
if (toggle_tags.isArray() && !toggle_tags.empty())
button.signal_button_press_event().connect(sigc::bind(
sigc::mem_fun(*this, &Tags::handle_button_press), toggle_tags[tag].asUInt()));
else
button.signal_button_press_event().connect(
sigc::bind(sigc::mem_fun(*this, &Tags::handle_button_press), (1 << tag)));
} }
button.show(); button.show();
i <<= 1;
} }
struct wl_output *output = gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj()); struct wl_output *output = gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj());
@ -182,45 +188,38 @@ bool Tags::handle_button_press(GdkEventButton *event_button, uint32_t tag) {
} }
void Tags::handle_focused_tags(uint32_t tags) { void Tags::handle_focused_tags(uint32_t tags) {
uint32_t i = 0; for (size_t i = 0; i < buttons_.size(); ++i) {
for (auto &button : buttons_) {
if ((1 << i) & tags) { if ((1 << i) & tags) {
button.get_style_context()->add_class("focused"); buttons_[i].get_style_context()->add_class("focused");
} else { } else {
button.get_style_context()->remove_class("focused"); buttons_[i].get_style_context()->remove_class("focused");
} }
++i;
} }
} }
void Tags::handle_view_tags(struct wl_array *view_tags) { void Tags::handle_view_tags(struct wl_array *view_tags) {
// First clear all occupied state uint32_t tags = 0;
for (auto &button : buttons_) { auto view_tag = reinterpret_cast<uint32_t *>(view_tags->data);
button.get_style_context()->remove_class("occupied"); auto end = view_tag + (view_tags->size / sizeof(uint32_t));
for (; view_tag < end; ++view_tag) {
tags |= *view_tag;
} }
for (size_t i = 0; i < buttons_.size(); ++i) {
// Set tags with a view to occupied if ((1 << i) & tags) {
uint32_t *start = static_cast<uint32_t *>(view_tags->data); buttons_[i].get_style_context()->add_class("occupied");
for (uint32_t *tags = start; tags < start + view_tags->size / sizeof(uint32_t); ++tags) { } else {
uint32_t i = 0; buttons_[i].get_style_context()->remove_class("occupied");
for (auto &button : buttons_) {
if (*tags & (1 << i)) {
button.get_style_context()->add_class("occupied");
}
++i;
} }
} }
} }
void Tags::handle_urgent_tags(uint32_t tags) { void Tags::handle_urgent_tags(uint32_t tags) {
uint32_t i = 0; for (size_t i = 0; i < buttons_.size(); ++i) {
for (auto &button : buttons_) {
if ((1 << i) & tags) { if ((1 << i) & tags) {
button.get_style_context()->add_class("urgent"); buttons_[i].get_style_context()->add_class("urgent");
} else { } else {
button.get_style_context()->remove_class("urgent"); buttons_[i].get_style_context()->remove_class("urgent");
} }
++i;
} }
} }