feat: hyprland workspaces add sort-by

This commit is contained in:
Austin Horstman 2023-09-08 22:24:28 -05:00
parent 587bd0cd62
commit cbc12e5443
No known key found for this signature in database
3 changed files with 53 additions and 27 deletions

View File

@ -86,6 +86,7 @@ class Workspaces : public AModule, public EventHandler {
bool all_outputs_ = false;
bool show_special_ = false;
bool active_only_ = false;
std::string sort_by = "default";
void fill_persistent_workspaces();
void create_persistent_workspaces();

View File

@ -36,6 +36,14 @@ Addressed by *hyprland/workspaces*
default: false ++
If set to true, only the active workspace will be shown.
*sort-by*: ++
typeof: string ++
default: "default" ++
If set to number, workspaces will sort by number.
If set to name, workspaces will sort by name.
If set to id, workspaces will sort by id.
If none of those, workspaces will sort with default behavior.
# FORMAT REPLACEMENTS
*{id}*: id of workspace assigned by compositor

View File

@ -421,12 +421,27 @@ void Workspace::update(const std::string &format, const std::string &icon) {
void Workspaces::sort_workspaces() {
std::sort(workspaces_.begin(), workspaces_.end(),
[](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
[&](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
// Helper comparisons
auto is_id_less = a->id() < b->id();
auto is_name_less = a->name() < b->name();
auto is_number_less = std::stoi(a->name()) < std::stoi(b->name());
if (sort_by == "number") {
try {
return is_number_less;
} catch (const std::invalid_argument &) {
}
} else if (sort_by == "name") {
return is_name_less;
} else if (sort_by == "id") {
return is_id_less;
} else {
// normal -> named persistent -> named -> special -> named special
// both normal (includes numbered persistent) => sort by ID
if (a->id() > 0 && b->id() > 0) {
return a->id() < b->id();
return is_id_less;
}
// one normal, one special => normal first
@ -445,12 +460,14 @@ void Workspaces::sort_workspaces() {
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 a->name() < b->name();
// both are 0 (not yet named persistents) / both are named specials (-98 <= ID
// <=-1)
return is_name_less;
}
// sort non-special named workspaces by name (ID <= -1377)
return a->name() < b->name();
return is_name_less;
}
});
for (size_t i = 0; i < workspaces_.size(); ++i) {