mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-14 15:12:31 +02:00
refactor: sort-by enum hyprland
This commit is contained in:
@ -59,7 +59,14 @@ auto Workspaces::parse_config(const Json::Value &config) -> void {
|
||||
|
||||
auto config_sort_by = config_["sort-by"];
|
||||
if (config_sort_by.isString()) {
|
||||
sort_by = config_sort_by.asString();
|
||||
auto sort_by_str = config_sort_by.asString();
|
||||
try {
|
||||
sort_by_ = enum_parser_.sortStringToEnum(sort_by_str);
|
||||
} catch (const std::invalid_argument &e) {
|
||||
// Handle the case where the string is not a valid enum representation.
|
||||
sort_by_ = util::EnumParser::SORT_METHOD::DEFAULT;
|
||||
g_warning("Invalid string representation for sort-by. Falling back to default sort method.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -427,47 +434,56 @@ void Workspaces::sort_workspaces() {
|
||||
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) {
|
||||
switch (sort_by_) {
|
||||
case util::EnumParser::SORT_METHOD::ID:
|
||||
return is_id_less;
|
||||
}
|
||||
|
||||
// one normal, one special => normal first
|
||||
if ((a->is_special()) ^ (b->is_special())) {
|
||||
return b->is_special();
|
||||
}
|
||||
|
||||
// only one normal, one named
|
||||
if ((a->id() > 0) ^ (b->id() > 0)) {
|
||||
return a->id() > 0;
|
||||
}
|
||||
|
||||
// both special
|
||||
if (a->is_special() && b->is_special()) {
|
||||
// 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)
|
||||
case util::EnumParser::SORT_METHOD::NAME:
|
||||
return is_name_less;
|
||||
}
|
||||
case util::EnumParser::SORT_METHOD::NUMBER:
|
||||
try {
|
||||
return is_number_less;
|
||||
} catch (const std::invalid_argument &) {
|
||||
// Handle the exception if necessary.
|
||||
break;
|
||||
}
|
||||
case util::EnumParser::SORT_METHOD::DEFAULT:
|
||||
default:
|
||||
// Handle the default case here.
|
||||
// normal -> named persistent -> named -> special -> named special
|
||||
|
||||
// sort non-special named workspaces by name (ID <= -1377)
|
||||
return is_name_less;
|
||||
// both normal (includes numbered persistent) => sort by ID
|
||||
if (a->id() > 0 && b->id() > 0) {
|
||||
return is_id_less;
|
||||
}
|
||||
|
||||
// one normal, one special => normal first
|
||||
if ((a->is_special()) ^ (b->is_special())) {
|
||||
return b->is_special();
|
||||
}
|
||||
|
||||
// only one normal, one named
|
||||
if ((a->id() > 0) ^ (b->id() > 0)) {
|
||||
return a->id() > 0;
|
||||
}
|
||||
|
||||
// both special
|
||||
if (a->is_special() && b->is_special()) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// sort non-special named workspaces by name (ID <= -1377)
|
||||
return is_name_less;
|
||||
break;
|
||||
}
|
||||
|
||||
// Return a default value if none of the cases match.
|
||||
return is_name_less; // You can adjust this to your specific needs.
|
||||
});
|
||||
|
||||
for (size_t i = 0; i < workspaces_.size(); ++i) {
|
||||
|
Reference in New Issue
Block a user