From 06922de9c9fda87b63eeb2585a61e314fd9a363d Mon Sep 17 00:00:00 2001 From: reionwong Date: Sun, 29 Aug 2021 23:58:22 +0800 Subject: [PATCH] Improve the new item logic --- src/applicationmodel.cpp | 34 ++++++++++++++++++++++++++++++---- src/applicationmodel.h | 1 + src/mainwindow.cpp | 7 ++----- src/mainwindow.h | 2 +- src/org.Cutefish.Dock.xml | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/applicationmodel.cpp b/src/applicationmodel.cpp index 0f563ef..2d860e0 100644 --- a/src/applicationmodel.cpp +++ b/src/applicationmodel.cpp @@ -131,9 +131,23 @@ void ApplicationModel::removeItem(const QString &desktopFile) bool ApplicationModel::desktopContains(const QString &desktopFile) { + if (desktopFile.isEmpty()) + return false; + return findItemByDesktop(desktopFile) != nullptr; } +bool ApplicationModel::isDesktopPinned(const QString &desktopFile) +{ + ApplicationItem *item = findItemByDesktop(desktopFile); + + if (item) { + return item->isPinned; + } + + return false; +} + void ApplicationModel::clicked(const QString &id) { ApplicationItem *item = findItemById(id); @@ -433,7 +447,19 @@ void ApplicationModel::onWindowAdded(quint64 wid) if (id == "cutefish-launcher") return; - if (contains(id)) { + QString desktopPath = m_iface->desktopFilePath(wid); + ApplicationItem *desktopItem = findItemByDesktop(desktopPath); + + // Use desktop find + if (!desktopPath.isEmpty() && desktopItem != nullptr) { + desktopItem->id = id; + desktopItem->wids.append(wid); + // Need to update application active status. + desktopItem->isActive = info.value("active").toBool(); + handleDataChangedFromItem(desktopItem); + } + // Find from id + else if (contains(id)) { for (ApplicationItem *item : m_appItems) { if (item->id == id) { item->wids.append(wid); @@ -442,7 +468,9 @@ void ApplicationModel::onWindowAdded(quint64 wid) handleDataChangedFromItem(item); } } - } else { + } + // New item needs to be added. + else { beginInsertRows(QModelIndex(), rowCount(), rowCount()); ApplicationItem *item = new ApplicationItem; item->id = id; @@ -451,8 +479,6 @@ void ApplicationModel::onWindowAdded(quint64 wid) item->isActive = info.value("active").toBool(); item->wids.append(wid); - QString desktopPath = m_iface->desktopFilePath(wid); - if (!desktopPath.isEmpty()) { QMap desktopInfo = Utils::instance()->readInfoFromDesktop(desktopPath); item->iconName = desktopInfo.value("Icon"); diff --git a/src/applicationmodel.h b/src/applicationmodel.h index 0a64056..0fb505a 100644 --- a/src/applicationmodel.h +++ b/src/applicationmodel.h @@ -50,6 +50,7 @@ public: void addItem(const QString &desktopFile); void removeItem(const QString &desktopFile); bool desktopContains(const QString &desktopFile); + bool isDesktopPinned(const QString &desktopFile); Q_INVOKABLE void save() { savePinAndUnPinList(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ac46468..c3d7c66 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -104,12 +104,9 @@ void MainWindow::remove(const QString &desktop) m_appModel->removeItem(desktop); } -bool MainWindow::contains(const QString &desktop) +bool MainWindow::pinned(const QString &desktop) { - if (desktop.isEmpty()) - return false; - - return m_appModel->desktopContains(desktop); + return m_appModel->isDesktopPinned(desktop); } void MainWindow::updateSize() diff --git a/src/mainwindow.h b/src/mainwindow.h index 8f1dbf9..1b1bab5 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -40,7 +40,7 @@ public: // DBus interface void add(const QString &desktop); void remove(const QString &desktop); - bool contains(const QString &desktop); + bool pinned(const QString &desktop); Q_INVOKABLE void updateSize(); diff --git a/src/org.Cutefish.Dock.xml b/src/org.Cutefish.Dock.xml index f445cbf..8bae41b 100644 --- a/src/org.Cutefish.Dock.xml +++ b/src/org.Cutefish.Dock.xml @@ -3,7 +3,7 @@ - +