Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions man/waybar-mango-workspaces.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Addressed by *mango/workspaces*
default: "OVERVIEW" ++
Label shown on the overview button when the overview is active.

*num-tags*: ++
typeof: uint ++
The number of tags that should be displayed.

*expand*: ++
typeof: bool ++
default: false ++
Expand Down
38 changes: 23 additions & 15 deletions src/modules/mango/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void Workspaces::doUpdate() {
const auto& tags = monitor["tags"];

bool overview_mode = false;

if (monitor.isMember("active_tags") && monitor["active_tags"].isArray()) {
const auto& active_tags = monitor["active_tags"];
if (active_tags.size() == 1 && active_tags[0].asInt() == 0) {
Expand Down Expand Up @@ -90,9 +91,9 @@ void Workspaces::doUpdate() {

for (auto btn_it = buttons_.begin(); btn_it != buttons_.end();) {
uint64_t id = btn_it->first;
bool found = std::any_of(tags.begin(), tags.end(), [id](const Json::Value& tag) {
return tag["index"].asUInt64() == id;
});

bool found = std::ranges::any_of(
tags, [id](const Json::Value& tag) { return tag["index"].asUInt64() == id; });

if (!found) {
box_.remove(btn_it->second);
Expand All @@ -102,20 +103,27 @@ void Workspaces::doUpdate() {
}
}

for (const auto& tag : tags) {
uint64_t idx = tag["index"].asUInt64();
auto btn_it = buttons_.find(idx);
Gtk::Button& button = (btn_it == buttons_.end()) ? addButton(idx) : btn_it->second;
updateButtonState(button, tag, monitor);
const auto num_tags = std::min<uint64_t>(
config_["num-tags"].isUInt64() ? config_["num-tags"].asUInt64() : tags.size(), tags.size());

std::vector<const Json::Value*> ordered_tags;
ordered_tags.reserve(num_tags);

for (Json::ArrayIndex i = 0; i < num_tags; ++i) {
const auto& tag = tags[i];
ordered_tags.push_back(&tag);
}

std::vector<uint64_t> indices;
for (const auto& tag : tags) indices.push_back(tag["index"].asUInt64());
std::sort(indices.begin(), indices.end());
int pos = 0;
for (uint64_t idx : indices) {
box_.reorder_child(buttons_[idx], pos + 1);
pos++;
std::ranges::sort(ordered_tags, [](const Json::Value* a, const Json::Value* b) {
return (*a)["index"].asUInt64() < (*b)["index"].asUInt64();
});

for (const auto* tag : ordered_tags) {
uint64_t idx = (*tag)["index"].asUInt64();

auto btn_it = buttons_.find(idx);
Gtk::Button& button = (btn_it == buttons_.end()) ? addButton(idx) : btn_it->second;
updateButtonState(button, *tag, monitor);
}
}
}
Expand Down
Loading