| 164 | } |
| 165 | |
| 166 | void WorkspaceManager::sort_workspaces() { |
| 167 | // determine if workspace ID's and names can be sort numerically or literally |
| 168 | |
| 169 | auto is_numeric = [](const std::string& s) { |
| 170 | return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit); |
| 171 | }; |
| 172 | |
| 173 | auto sort_by_workspace_id_numerically = |
| 174 | std::all_of(workspaces_.begin(), workspaces_.end(), |
| 175 | [&](const auto& w) { return is_numeric(w->workspace_id()); }); |
| 176 | |
| 177 | auto sort_by_name_numerically = std::all_of(workspaces_.begin(), workspaces_.end(), |
| 178 | [&](const auto& w) { return is_numeric(w->name()); }); |
| 179 | |
| 180 | // sort based on configuration setting with sort-by-id as fallback |
| 181 | |
| 182 | std::sort(workspaces_.begin(), workspaces_.end(), [&](const auto& w1, const auto& w2) { |
| 183 | if (sort_by_id_ || (!sort_by_name_ && !sort_by_coordinates_)) { |
| 184 | if (w1->workspace_id() == w2->workspace_id()) { |
| 185 | return w1->id() < w2->id(); |
| 186 | } |
| 187 | if (sort_by_workspace_id_numerically) { |
| 188 | // the idea is that phonetic compare can be applied just to numbers |
| 189 | // with same number of digits |
| 190 | return w1->workspace_id().size() < w2->workspace_id().size() || |
| 191 | (w1->workspace_id().size() == w2->workspace_id().size() && |
| 192 | w1->workspace_id() < w2->workspace_id()); |
| 193 | } |
| 194 | return w1->workspace_id() < w2->workspace_id(); |
| 195 | } |
| 196 | |
| 197 | if (sort_by_name_) { |
| 198 | if (w1->name() == w2->name()) { |
| 199 | return w1->id() < w2->id(); |
| 200 | } |
| 201 | if (sort_by_name_numerically) { |
| 202 | // see above about numeric sorting |
| 203 | return w1->name().size() < w2->name().size() || |
| 204 | (w1->name().size() == w2->name().size() && w1->name() < w2->name()); |
| 205 | } |
| 206 | return w1->name() < w2->name(); |
| 207 | } |
| 208 | |
| 209 | if (sort_by_coordinates_) { |
| 210 | if (w1->coordinates() == w2->coordinates()) { |
| 211 | return w1->id() < w2->id(); |
| 212 | } |
| 213 | return w1->coordinates() < w2->coordinates(); |
| 214 | } |
| 215 | |
| 216 | return w1->id() < w2->id(); |
| 217 | }); |
| 218 | } |
| 219 | |
| 220 | void WorkspaceManager::clear_buttons() { |
| 221 | for (const auto& workspace : workspaces_) { |