workspace taskbars: Display windows in a consistent order

Use a vector instead of a map for for storing the workspace windows.
This orders the windows by the time they were added to the workspace, instead of sorting by address (which is effectively a random order). The new ordering seems to match the wlr/taskbar module
This commit is contained in:
Pol Rivero
2025-01-02 07:41:24 +01:00
parent e1649b001f
commit 53ca5a4883
4 changed files with 26 additions and 15 deletions

View File

@ -98,7 +98,8 @@ WindowRepr WindowCreationPayload::repr(Workspaces &workspace_manager) {
}
if (std::holds_alternative<ClassAndTitle>(m_window)) {
auto [window_class, window_title] = std::get<ClassAndTitle>(m_window);
return {window_class, window_title, workspace_manager.getRewrite(window_class, window_title)};
return {m_windowAddress, window_class, window_title,
workspace_manager.getRewrite(window_class, window_title)};
}
// Unreachable
spdlog::error("WorkspaceWindow::repr: Unreachable");

View File

@ -53,8 +53,13 @@ void addOrRemoveClass(const Glib::RefPtr<Gtk::StyleContext> &context, bool condi
}
std::optional<WindowRepr> Workspace::closeWindow(WindowAddress const &addr) {
if (m_windowMap.contains(addr)) {
return removeWindow(addr);
auto it = std::ranges::find_if(m_windowMap,
[&addr](const auto &window) { return window.address == addr; });
// If the vector contains the address, remove it and return the window representation
if (it != m_windowMap.end()) {
WindowRepr windowRepr = *it;
m_windowMap.erase(it);
return windowRepr;
}
return std::nullopt;
}
@ -101,7 +106,15 @@ void Workspace::insertWindow(WindowCreationPayload create_window_paylod) {
auto repr = create_window_paylod.repr(m_workspaceManager);
if (!repr.empty()) {
m_windowMap[create_window_paylod.getAddress()] = repr;
auto addr = create_window_paylod.getAddress();
auto it = std::ranges::find_if(
m_windowMap, [&addr](const auto &window) { return window.address == addr; });
// If the vector contains the address, update the window representation, otherwise insert it
if (it != m_windowMap.end()) {
*it = repr;
} else {
m_windowMap.emplace_back(repr);
}
}
}
};
@ -114,12 +127,6 @@ bool Workspace::onWindowOpened(WindowCreationPayload const &create_window_paylod
return false;
}
WindowRepr Workspace::removeWindow(WindowAddress const &addr) {
WindowRepr windowRepr = m_windowMap[addr];
m_windowMap.erase(addr);
return windowRepr;
}
std::string &Workspace::selectIcon(std::map<std::string, std::string> &icons_map) {
spdlog::trace("Selecting icon for workspace {}", name());
if (isUrgent()) {
@ -212,7 +219,7 @@ void Workspace::update(const std::string &workspace_icon) {
bool isNotFirst = false;
for (const auto &[_pid, window_repr] : m_windowMap) {
for (const auto &window_repr : m_windowMap) {
if (isNotFirst) {
windows.append(windowSeparator);
}
@ -238,7 +245,7 @@ void Workspace::updateTaskbar(const std::string &workspace_icon) {
}
}
for (const auto &[_addr, window_repr] : m_windowMap) {
for (const auto &window_repr : m_windowMap) {
auto window_box = Gtk::make_managed<Gtk::Box>(Gtk::ORIENTATION_HORIZONTAL);
window_box->set_tooltip_text(window_repr.window_title);