From 7fbbf96659fd447eb5493fb12167101e36ac2ecb Mon Sep 17 00:00:00 2001 From: Wang Zichong Date: Fri, 12 Jan 2024 11:44:13 +0800 Subject: [PATCH] fix: should clean-up no longer valid items in fullscreen mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 全屏模式中,当图标数量变化时,从图标位置数据源中移除不再有效的图标。 已被卸载的图标将不会再占据位置,因应用卸载导致的图标消失后页面留空 的空白页也会被相应移除。 Issue: https://github.com/linuxdeepin/developer-center/issues/6861 Log: --- src/models/itemspage.cpp | 20 ++++++++++++++++++++ src/models/itemspage.h | 2 ++ src/models/multipageproxymodel.cpp | 11 ++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/models/itemspage.cpp b/src/models/itemspage.cpp index d99bd0d7..f1956073 100644 --- a/src/models/itemspage.cpp +++ b/src/models/itemspage.cpp @@ -4,6 +4,8 @@ #include "itemspage.h" +#include + ItemsPage::ItemsPage(const QString &name, int maxItemCountPerPage, QObject *parent) : QObject(parent) , m_maxItemCountPerPage(maxItemCountPerPage) @@ -157,6 +159,24 @@ bool ItemsPage::removeItem(const QString id, bool removePageIfPageIsEmpty) return false; } +void ItemsPage::removeItemsNotIn(const QSet &itemSet) +{ + for (int i = 0; i < m_pages.count(); i++) { + for (int j = m_pages.at(i).count() - 1; j >= 0; j--) { + const QString & item(m_pages.at(i).at(j)); + if (itemSet.contains(item)) continue; + if (item.startsWith(QLatin1String("internal/"))) continue; + m_pages[i].removeAt(j); + } + } + removeEmptyPages(); +} + +void ItemsPage::removeEmptyPages() +{ + m_pages.removeAll({}); +} + // std::tuple ItemsPage::findItem(const QString &id) const { diff --git a/src/models/itemspage.h b/src/models/itemspage.h index ec600205..97c824cd 100644 --- a/src/models/itemspage.h +++ b/src/models/itemspage.h @@ -34,6 +34,8 @@ class ItemsPage : public QObject void insertItem(const QString id, int page, int pos = 0); void moveItem(int from_page, int from_index, int to_page, int to_index); bool removeItem(const QString id, bool removePageIfPageIsEmpty = true); + void removeItemsNotIn(const QSet & itemSet); + void removeEmptyPages(); std::tuple findItem(const QString & id) const; bool contains(const QString & id) const; diff --git a/src/models/multipageproxymodel.cpp b/src/models/multipageproxymodel.cpp index e17eb68a..2fba2375 100644 --- a/src/models/multipageproxymodel.cpp +++ b/src/models/multipageproxymodel.cpp @@ -283,20 +283,25 @@ std::tuple MultipageProxyModel::findItem(const QString &id, bool void MultipageProxyModel::onSourceModelChanged() { - // add all existing ones if they are not already in + QSet appDesktopIdSet; int appsCount = AppsModel::instance().rowCount(); for (int i = 0; i < appsCount; i++) { QString desktopId(AppsModel::instance().data(AppsModel::instance().index(i, 0), AppItem::DesktopIdRole).toString()); + appDesktopIdSet.insert(desktopId); int folder, page, idx; std::tie(folder, std::ignore, std::ignore) = findItem(desktopId); + // add all existing ones if they are not already in if (folder == -1) { findItem(desktopId); m_topLevel->appendItem(desktopId); } } - // TODO: remove the ones that no longer valid out of m_folders - + m_topLevel->removeItemsNotIn(appDesktopIdSet); + for (int i = 0; i < m_folderModel.rowCount(); i++) { + const QString & folderId = m_folderModel.index(i, 0).data(AppItem::DesktopIdRole).toString(); + m_folders.value(folderId)->removeItemsNotIn(appDesktopIdSet); + } emit dataChanged(index(0, 0), index(rowCount() - 1, 0), { PageRole, IndexInPageRole, FolderIdNumberRole, IconsNameRole