Improve the new item logic

This commit is contained in:
reionwong 2021-08-29 23:58:22 +08:00
parent 357e4dd3ab
commit 06922de9c9
5 changed files with 35 additions and 11 deletions

View file

@ -131,9 +131,23 @@ void ApplicationModel::removeItem(const QString &desktopFile)
bool ApplicationModel::desktopContains(const QString &desktopFile) bool ApplicationModel::desktopContains(const QString &desktopFile)
{ {
if (desktopFile.isEmpty())
return false;
return findItemByDesktop(desktopFile) != nullptr; 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) void ApplicationModel::clicked(const QString &id)
{ {
ApplicationItem *item = findItemById(id); ApplicationItem *item = findItemById(id);
@ -433,7 +447,19 @@ void ApplicationModel::onWindowAdded(quint64 wid)
if (id == "cutefish-launcher") if (id == "cutefish-launcher")
return; 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) { for (ApplicationItem *item : m_appItems) {
if (item->id == id) { if (item->id == id) {
item->wids.append(wid); item->wids.append(wid);
@ -442,7 +468,9 @@ void ApplicationModel::onWindowAdded(quint64 wid)
handleDataChangedFromItem(item); handleDataChangedFromItem(item);
} }
} }
} else { }
// New item needs to be added.
else {
beginInsertRows(QModelIndex(), rowCount(), rowCount()); beginInsertRows(QModelIndex(), rowCount(), rowCount());
ApplicationItem *item = new ApplicationItem; ApplicationItem *item = new ApplicationItem;
item->id = id; item->id = id;
@ -451,8 +479,6 @@ void ApplicationModel::onWindowAdded(quint64 wid)
item->isActive = info.value("active").toBool(); item->isActive = info.value("active").toBool();
item->wids.append(wid); item->wids.append(wid);
QString desktopPath = m_iface->desktopFilePath(wid);
if (!desktopPath.isEmpty()) { if (!desktopPath.isEmpty()) {
QMap<QString, QString> desktopInfo = Utils::instance()->readInfoFromDesktop(desktopPath); QMap<QString, QString> desktopInfo = Utils::instance()->readInfoFromDesktop(desktopPath);
item->iconName = desktopInfo.value("Icon"); item->iconName = desktopInfo.value("Icon");

View file

@ -50,6 +50,7 @@ public:
void addItem(const QString &desktopFile); void addItem(const QString &desktopFile);
void removeItem(const QString &desktopFile); void removeItem(const QString &desktopFile);
bool desktopContains(const QString &desktopFile); bool desktopContains(const QString &desktopFile);
bool isDesktopPinned(const QString &desktopFile);
Q_INVOKABLE void save() { savePinAndUnPinList(); } Q_INVOKABLE void save() { savePinAndUnPinList(); }

View file

@ -104,12 +104,9 @@ void MainWindow::remove(const QString &desktop)
m_appModel->removeItem(desktop); m_appModel->removeItem(desktop);
} }
bool MainWindow::contains(const QString &desktop) bool MainWindow::pinned(const QString &desktop)
{ {
if (desktop.isEmpty()) return m_appModel->isDesktopPinned(desktop);
return false;
return m_appModel->desktopContains(desktop);
} }
void MainWindow::updateSize() void MainWindow::updateSize()

View file

@ -40,7 +40,7 @@ public:
// DBus interface // DBus interface
void add(const QString &desktop); void add(const QString &desktop);
void remove(const QString &desktop); void remove(const QString &desktop);
bool contains(const QString &desktop); bool pinned(const QString &desktop);
Q_INVOKABLE void updateSize(); Q_INVOKABLE void updateSize();

View file

@ -3,7 +3,7 @@
<interface name="org.cutefish.Dock"> <interface name="org.cutefish.Dock">
<method name="add"><arg name="desktopFile" type="s" direction="in"/></method> <method name="add"><arg name="desktopFile" type="s" direction="in"/></method>
<method name="remove"><arg name="desktopFile" type="s" direction="in"/></method> <method name="remove"><arg name="desktopFile" type="s" direction="in"/></method>
<method name="contains"> <method name="pinned">
<arg name="desktopFile" type="s" direction="in"/> <arg name="desktopFile" type="s" direction="in"/>
<arg type="b" direction="out"/> <arg type="b" direction="out"/>
</method> </method>