Merge pull request #1918 from ldelossa/sway-workspaces-alpha-sort

This commit is contained in:
Alex 2023-01-03 11:52:39 +01:00 committed by GitHub
commit a35861b3b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -73,6 +73,10 @@ Addressed by *sway/workspaces*
typeof: bool ++
Whether to disable *workspace_auto_back_and_forth* when clicking on workspaces. If this is set to *true*, clicking on a workspace you are already on won't do anything, even if *workspace_auto_back_and_forth* is enabled in the Sway configuration.
*alphabetical_sort*: ++
typeof: bool ++
Whether to sort workspaces alphabetically. Please note this can make "swaymsg workspace prev/next" move to workspaces inconsistent with the ordering shown in Waybar.
# FORMAT REPLACEMENTS
*{value}*: Name of the workspace, as defined by sway.

View File

@ -130,6 +130,10 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
// In a first pass, the maximum "num" value is computed to enqueue
// unnumbered workspaces behind numbered ones when computing the sort
// attribute.
//
// Note: if the 'alphabetical_sort' option is true, the user is in
// agreement that the "workspace prev/next" commands may not follow
// the order displayed in Waybar.
int max_num = -1;
for (auto &workspace : workspaces_) {
max_num = std::max(workspace["num"].asInt(), max_num);
@ -143,16 +147,19 @@ void Workspaces::onCmd(const struct Ipc::ipc_response &res) {
}
}
std::sort(workspaces_.begin(), workspaces_.end(),
[](const Json::Value &lhs, const Json::Value &rhs) {
[this](const Json::Value &lhs, const Json::Value &rhs) {
auto lname = lhs["name"].asString();
auto rname = rhs["name"].asString();
int l = lhs["sort"].asInt();
int r = rhs["sort"].asInt();
if (l == r) {
if (l == r || config_["alphabetical_sort"].asBool()) {
// In case both integers are the same, lexicographical
// sort. The code above already ensure that this will only
// happend in case of explicitly numbered workspaces.
//
// Additionally, if the config specifies to sort workspaces
// alphabetically do this here.
return lname < rname;
}