diff --git a/panels/notification/bubble/bubblemodel.cpp b/panels/notification/bubble/bubblemodel.cpp index 44382c4fe..567e5a141 100644 --- a/panels/notification/bubble/bubblemodel.cpp +++ b/panels/notification/bubble/bubblemodel.cpp @@ -84,15 +84,51 @@ void BubbleModel::insertBubble(BubbleItem *bubble) endInsertRows(); } -bool BubbleModel::isReplaceBubble(const BubbleItem *bubble) const +int BubbleModel::replaceBubbleIndex(const NotifyEntity &entity) const { - return replaceBubbleIndex(bubble) >= 0; + if (entity.isReplace()) { + for (int i = 0; i < m_bubbles.size(); i++) { + const auto item = m_bubbles[i]; + if (!item) + continue; + + if (item->appName() != entity.appName()) + continue; + + if (item->bubbleId() == entity.bubbleId()) { + return i; + } + } + } + return -1; +} + +void BubbleModel::updateBubbleInPlace(int replaceIndex, const NotifyEntity &entity) +{ + if (replaceIndex < 0 || replaceIndex >= m_bubbles.size() || !entity.isReplace()) + return; + + // dataChanged 通知视图刷新 + // 前提:内容变更不会引起气泡高度变化,否则布局不刷新(应改用 replaceBubble)。 + auto oldBubble = m_bubbles[replaceIndex]; + if (oldBubble) { + oldBubble->setEntity(entity); + Q_EMIT dataChanged(index(replaceIndex), index(replaceIndex)); + } } -BubbleItem *BubbleModel::replaceBubble(BubbleItem *bubble) +void BubbleModel::replaceBubble(int replaceIndex, BubbleItem *bubble) { - Q_ASSERT(isReplaceBubble(bubble)); - const auto replaceIndex = replaceBubbleIndex(bubble); + if (!bubble) { + return; + } + + if (replaceIndex < 0 || replaceIndex >= m_bubbles.size()) { + qWarning() << "replaceBubble called with invalid index:" << replaceIndex; + bubble->deleteLater(); + return; + } + const auto oldBubble = m_bubbles[replaceIndex]; // Use remove + insert instead of dataChanged to force the view @@ -105,7 +141,9 @@ BubbleItem *BubbleModel::replaceBubble(BubbleItem *bubble) m_bubbles.insert(replaceIndex, bubble); endInsertRows(); - return oldBubble; + if (oldBubble) { + oldBubble->deleteLater(); + } } void BubbleModel::clear() @@ -261,22 +299,6 @@ void BubbleModel::clearInvalidBubbles() } } -int BubbleModel::replaceBubbleIndex(const BubbleItem *bubble) const -{ - if (bubble->isReplace()) { - for (int i = 0; i < m_bubbles.size(); i++) { - auto item = m_bubbles[i]; - if (item->appName() != bubble->appName()) - continue; - - if (item->bubbleId() == bubble->bubbleId()) { - return i; - } - } - } - return -1; -} - void BubbleModel::updateBubbleTimeTip() { if (m_bubbles.isEmpty()) { diff --git a/panels/notification/bubble/bubblemodel.h b/panels/notification/bubble/bubblemodel.h index b9b8f6203..1a46b2543 100644 --- a/panels/notification/bubble/bubblemodel.h +++ b/panels/notification/bubble/bubblemodel.h @@ -41,8 +41,9 @@ class BubbleModel : public QAbstractListModel public: void push(BubbleItem *bubble); - BubbleItem *replaceBubble(BubbleItem *bubble); - bool isReplaceBubble(const BubbleItem *bubble) const; + int replaceBubbleIndex(const NotifyEntity &entity) const; + void updateBubbleInPlace(int replaceIndex, const NotifyEntity &entity); + void replaceBubble(int replaceIndex, BubbleItem *bubble); QList items() const; @@ -64,7 +65,6 @@ class BubbleModel : public QAbstractListModel private: void insertBubble(BubbleItem *bubble); void updateBubbleCount(int count); - int replaceBubbleIndex(const BubbleItem *bubble) const; void updateBubbleTimeTip(); void updateContentRowCount(int rowCount); diff --git a/panels/notification/bubble/bubblepanel.cpp b/panels/notification/bubble/bubblepanel.cpp index 469fe67f7..660f3fe1e 100644 --- a/panels/notification/bubble/bubblepanel.cpp +++ b/panels/notification/bubble/bubblepanel.cpp @@ -133,17 +133,21 @@ void BubblePanel::addBubble(qint64 id) return; } + const auto replaceIndex = m_bubbles->replaceBubbleIndex(entity); + if (replaceIndex >= 0 + && entity.hints().value("x-deepin-SilentReplace").toBool()) { + m_bubbles->updateBubbleInPlace(replaceIndex, entity); + return; + } + auto bubble = new BubbleItem(entity); const auto enabled = enablePreview(entity.appId()); bubble->setEnablePreview(enabled); - if (m_bubbles->isReplaceBubble(bubble)) { - auto oldBubble = m_bubbles->replaceBubble(bubble); - if (oldBubble) { - oldBubble->deleteLater(); - } - } else { - m_bubbles->push(bubble); + if (replaceIndex >= 0) { + m_bubbles->replaceBubble(replaceIndex, bubble); + return; } + m_bubbles->push(bubble); } void BubblePanel::closeBubble(qint64 id)