Merge pull request #4136 from Roc25/special-centered

hyprland workspaces: Add sorting Special Centered
This commit is contained in:
Alexis Rouillard
2025-06-22 08:57:12 +01:00
committed by GitHub
3 changed files with 45 additions and 1 deletions

View File

@ -51,6 +51,7 @@ class Workspaces : public AModule, public EventHandler {
private: private:
void onEvent(const std::string& e) override; void onEvent(const std::string& e) override;
void updateWindowCount(); void updateWindowCount();
void sortSpecialCentered();
void sortWorkspaces(); void sortWorkspaces();
void createWorkspace(Json::Value const& workspace_data, void createWorkspace(Json::Value const& workspace_data,
Json::Value const& clients_data = Json::Value::nullRef); Json::Value const& clients_data = Json::Value::nullRef);
@ -132,12 +133,13 @@ class Workspaces : public AModule, public EventHandler {
// and doesn't share windows across bars (a.k.a `all-outputs` = false) // and doesn't share windows across bars (a.k.a `all-outputs` = false)
std::map<WindowAddress, std::string> m_orphanWindowMap; std::map<WindowAddress, std::string> m_orphanWindowMap;
enum class SortMethod { ID, NAME, NUMBER, DEFAULT }; enum class SortMethod { ID, NAME, NUMBER, SPECIAL_CENTERED, DEFAULT };
util::EnumParser<SortMethod> m_enumParser; util::EnumParser<SortMethod> m_enumParser;
SortMethod m_sortBy = SortMethod::DEFAULT; SortMethod m_sortBy = SortMethod::DEFAULT;
std::map<std::string, SortMethod> m_sortMap = {{"ID", SortMethod::ID}, std::map<std::string, SortMethod> m_sortMap = {{"ID", SortMethod::ID},
{"NAME", SortMethod::NAME}, {"NAME", SortMethod::NAME},
{"NUMBER", SortMethod::NUMBER}, {"NUMBER", SortMethod::NUMBER},
{"SPECIAL-CENTERED", SortMethod::SPECIAL_CENTERED},
{"DEFAULT", SortMethod::DEFAULT}}; {"DEFAULT", SortMethod::DEFAULT}};
std::string m_format; std::string m_format;

View File

@ -81,6 +81,7 @@ Addressed by *hyprland/workspaces*
If set to number, workspaces will sort by number. If set to number, workspaces will sort by number.
If set to name, workspaces will sort by name. If set to name, workspaces will sort by name.
If set to id, workspaces will sort by id. If set to id, workspaces will sort by id.
If set to special-centered, workspaces will sort by default with special workspaces in the center.
If none of those, workspaces will sort with default behavior. If none of those, workspaces will sort with default behavior.
*expand*: ++ *expand*: ++

View File

@ -771,7 +771,45 @@ void Workspaces::setCurrentMonitorId() {
} }
} }
void Workspaces::sortSpecialCentered() {
std::vector<std::unique_ptr<Workspace>> specialWorkspaces;
std::vector<std::unique_ptr<Workspace>> hiddenWorkspaces;
std::vector<std::unique_ptr<Workspace>> normalWorkspaces;
for (auto &workspace : m_workspaces) {
if (workspace->isSpecial()) {
specialWorkspaces.push_back(std::move(workspace));
} else {
if (workspace->button().is_visible()) {
normalWorkspaces.push_back(std::move(workspace));
} else {
hiddenWorkspaces.push_back(std::move(workspace));
}
}
}
m_workspaces.clear();
size_t center = normalWorkspaces.size() / 2;
m_workspaces.insert(m_workspaces.end(),
std::make_move_iterator(normalWorkspaces.begin()),
std::make_move_iterator(normalWorkspaces.begin() + center));
m_workspaces.insert(m_workspaces.end(),
std::make_move_iterator(specialWorkspaces.begin()),
std::make_move_iterator(specialWorkspaces.end()));
m_workspaces.insert(m_workspaces.end(),
std::make_move_iterator(normalWorkspaces.begin() + center),
std::make_move_iterator(normalWorkspaces.end()));
m_workspaces.insert(m_workspaces.end(),
std::make_move_iterator(hiddenWorkspaces.begin()),
std::make_move_iterator(hiddenWorkspaces.end()));
}
void Workspaces::sortWorkspaces() { void Workspaces::sortWorkspaces() {
std::ranges::sort( // std::ranges::sort( //
m_workspaces, [&](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) { m_workspaces, [&](std::unique_ptr<Workspace> &a, std::unique_ptr<Workspace> &b) {
// Helper comparisons // Helper comparisons
@ -829,6 +867,9 @@ void Workspaces::sortWorkspaces() {
// Return a default value if none of the cases match. // Return a default value if none of the cases match.
return isNameLess; // You can adjust this to your specific needs. return isNameLess; // You can adjust this to your specific needs.
}); });
if (m_sortBy == SortMethod::SPECIAL_CENTERED) {
this->sortSpecialCentered();
}
for (size_t i = 0; i < m_workspaces.size(); ++i) { for (size_t i = 0; i < m_workspaces.size(); ++i) {
m_box.reorder_child(m_workspaces[i]->button(), i); m_box.reorder_child(m_workspaces[i]->button(), i);