// SPDX-License-Identifier: MIT #include "taskselection.h" #include #include #include #include #include #include #include #include using TaskManager::AbstractTasksModel; QVector logicalTaskRows(const TaskManager::TasksModel &model, bool separateLaunchers) { auto rows = logicalTaskRows(static_cast(model), separateLaunchers); if (model.sortMode() != TaskManager::TasksModel::SortManual) { return rows; } // TasksModel keeps its manual window order when setLauncherList changes. // Reorder only launcher-backed slots; leave unpinned tasks and children alone. QVector pinnedSlots; QVector pinnedRows; const auto launcherPosition = [&](int row) { return model.launcherPosition(model.index(row, 0).data(AbstractTasksModel::LauncherUrlWithoutIcon).toUrl()); }; for (int i = 0; i < rows.size(); ++i) { if ((!separateLaunchers || model.index(rows.at(i), 0).data(AbstractTasksModel::IsLauncher).toBool()) && launcherPosition(rows.at(i)) >= 0) { pinnedSlots.append(i); pinnedRows.append(rows.at(i)); } } std::stable_sort(pinnedRows.begin(), pinnedRows.end(), [&](int left, int right) { return launcherPosition(left) < launcherPosition(right); }); for (int i = 0; i < pinnedSlots.size(); ++i) { rows[pinnedSlots.at(i)] = pinnedRows.at(i); } return rows; } QModelIndex taskToActivate(const QAbstractItemModel &model, int row, int cyclePosition) { const QModelIndex task = model.index(row, 0); if (!task.isValid() || model.rowCount(task) == 0) { return task; } const int childCount = model.rowCount(task); const int childRow = ((cyclePosition % childCount) + childCount) % childCount; return model.index(childRow, 0, task); } QVector logicalTaskRows(const QAbstractItemModel &model, bool separateLaunchers) { QVector rows; rows.reserve(model.rowCount()); if (separateLaunchers) { for (int row = 0; row < model.rowCount(); ++row) { rows.append(row); } return rows; } QHash launcherTargets; QSet mergedWindows; for (int launcherRow = 0; launcherRow < model.rowCount(); ++launcherRow) { const QModelIndex launcher = model.index(launcherRow, 0); if (!launcher.data(AbstractTasksModel::IsLauncher).toBool()) { continue; } const QString launcherAppId = launcher.data(AbstractTasksModel::AppId).toString(); const QUrl launcherUrl = launcher.data(AbstractTasksModel::LauncherUrlWithoutIcon).toUrl(); for (int windowRow = 0; windowRow < model.rowCount(); ++windowRow) { const QModelIndex window = model.index(windowRow, 0); if (!window.data(AbstractTasksModel::IsWindow).toBool()) { continue; } const QString windowAppId = window.data(AbstractTasksModel::AppId).toString(); const QUrl windowUrl = window.data(AbstractTasksModel::LauncherUrlWithoutIcon).toUrl(); if ((!launcherAppId.isEmpty() && launcherAppId == windowAppId) || (launcherUrl.isValid() && windowUrl.isValid() && TaskManager::launcherUrlsMatch(launcherUrl, windowUrl, TaskManager::IgnoreQueryItems))) { launcherTargets.insert(launcherRow, windowRow); mergedWindows.insert(windowRow); break; } } } for (int row = 0; row < model.rowCount(); ++row) { if (launcherTargets.contains(row)) { rows.append(launcherTargets.value(row)); } else if (!mergedWindows.contains(row)) { rows.append(row); } } return rows; } int initialCyclePosition(const QAbstractItemModel &model, int row, bool startWithFirst, bool backwards) { const QModelIndex group = model.index(row, 0); if (!group.isValid()) { return 0; } const int childCount = model.rowCount(group); if (childCount == 0) { return 0; } if (!startWithFirst) { for (int child = 0; child < childCount; ++child) { if (model.index(child, 0, group).data(AbstractTasksModel::IsActive).toBool()) { return child + (backwards ? -1 : 1); } } } return backwards ? childCount - 1 : 0; }