| 925 | } |
| 926 | |
| 927 | void Workspaces::sortWorkspaces() { |
| 928 | std::ranges::sort( // |
| 929 | m_workspaces, [&](std::unique_ptr<Workspace>& a, std::unique_ptr<Workspace>& b) { |
| 930 | // Helper comparisons |
| 931 | auto isIdLess = a->id() < b->id(); |
| 932 | auto isNameLess = a->name() < b->name(); |
| 933 | |
| 934 | switch (m_sortBy) { |
| 935 | case SortMethod::ID: |
| 936 | return isIdLess; |
| 937 | case SortMethod::NAME: |
| 938 | return isNameLess; |
| 939 | case SortMethod::NUMBER: |
| 940 | try { |
| 941 | return std::stoi(a->name()) < std::stoi(b->name()); |
| 942 | } catch (const std::exception& e) { |
| 943 | // Handle the exception if necessary. |
| 944 | break; |
| 945 | } |
| 946 | case SortMethod::DEFAULT: |
| 947 | default: |
| 948 | // Handle the default case here. |
| 949 | // normal -> named persistent -> named -> special -> named special |
| 950 | |
| 951 | // both normal (includes numbered persistent) => sort by ID |
| 952 | if (a->id() > 0 && b->id() > 0) { |
| 953 | return isIdLess; |
| 954 | } |
| 955 | |
| 956 | // one normal, one special => normal first |
| 957 | if ((a->isSpecial()) ^ (b->isSpecial())) { |
| 958 | return b->isSpecial(); |
| 959 | } |
| 960 | |
| 961 | // only one normal, one named |
| 962 | if ((a->id() > 0) ^ (b->id() > 0)) { |
| 963 | return a->id() > 0; |
| 964 | } |
| 965 | |
| 966 | // both special |
| 967 | if (a->isSpecial() && b->isSpecial()) { |
| 968 | // if one is -99 => put it last |
| 969 | if (a->id() == -99 || b->id() == -99) { |
| 970 | return b->id() == -99; |
| 971 | } |
| 972 | // both are 0 (not yet named persistents) / named specials |
| 973 | // (-98 <= ID <= -1) |
| 974 | return isNameLess; |
| 975 | } |
| 976 | |
| 977 | // sort non-special named workspaces by name (ID <= -1377) |
| 978 | return isNameLess; |
| 979 | break; |
| 980 | } |
| 981 | |
| 982 | // Return a default value if none of the cases match. |
| 983 | return isNameLess; // You can adjust this to your specific needs. |
| 984 | }); |
nothing calls this directly
no test coverage detected