Waybar icon indicating copy to clipboard operation
Waybar copied to clipboard

hyprland workspaces: Add sorting Special Centered

Open Roc25 opened this issue 7 months ago • 5 comments

Closes #4135

Roc25 avatar May 20 '25 22:05 Roc25

Doing the for loop you're moving the workspace you're iterating over. Wouldn't that cause problems?

JassonCordones avatar May 21 '25 14:05 JassonCordones

Doring the for loop you're moving the workspace you're iterating over. Wouldn't that cause problems?

I think it's fine because:

  • we have a mutex lock around doUpdate(), which ensures m_workspaces isn't touched by another thread
  • we clear m_workspaces right after we made all workspaces nullptrs, so there's no risk of null ptr dereferencing

But I'm still not a big fan of that method, I'm trying to find a safer way to do it

EDIT: I came up with this but I feel like it isn't much better :laughing:

  std::vector<size_t> normalIdx;
  std::vector<size_t> specialIdx;
  std::vector<size_t> hiddenIdx;

  for (size_t i = 0; i < m_workspaces.size(); ++i) {
    if (m_workspaces[i]->isSpecial()) {
      specialIdx.push_back(i);
    } else if (m_workspaces[i]->button().is_visible()) {
      normalIdx.push_back(i);
    } else {
      hiddenIdx.push_back(i);
    }
  }
  size_t center = normalIdx.size() / 2;

  std::vector<std::unique_ptr<Workspace>> sorted;
  sorted.reserve(m_workspaces.size());

  auto move_by_indexes = [&](const std::vector<size_t>& idxs) {
    for (size_t idx : idxs) {
      sorted.push_back(std::move(m_workspaces[idx]));
    }
  };

  move_by_indexes({normalIdx.begin(), normalIdx.begin() + center});
  move_by_indexes(specialIdx);
  move_by_indexes({normalIdx.begin() + center, normalIdx.end()});
  move_by_indexes(hiddenIdx);

  m_workspaces = std::move(sorted);

zjeffer avatar Jun 05 '25 20:06 zjeffer

Can you update the docs?

zjeffer avatar Jun 05 '25 20:06 zjeffer

I'll check if I can update the docs if I have some spare time during the weekend, but I don't promise anything

JassonCordones avatar Jun 06 '25 19:06 JassonCordones

I also update wiki on github

Roc25 avatar Jun 07 '25 08:06 Roc25