fix: 修复从任务栏拖出托盘应用后托盘不弹出的问题

1.当图标只剩下一个的时候,遍历获取展开图标错误
2.在系统图标删除之前,需要将内置的插件返回的widget的parent设置为nullptr,防止在系统图标删除的时候把插件的widget给删除了
3、在拖动图标的过程中,不从原来的系统中删除原来的图标,再释放后,再将原来的图标删除

Log:
Influence: 从任务栏拖动微信或企业微信,观察托盘是否弹出
Bug: https://pms.uniontech.com/bug-view-171497.html
Change-Id: Iacbfe3406112e92a68d268beaaea3c1a3c3afe7c
This commit is contained in:
donghualin 2022-12-01 20:49:27 +08:00
parent 4c4d06310b
commit 077d6f056f
10 changed files with 86 additions and 46 deletions

View File

@ -318,8 +318,6 @@ void DockTrayWindow::initConnection()
connect(m_model, &TrayModel::rowCountChanged, this, &DockTrayWindow::onUpdateComponentSize);
connect(m_model, &TrayModel::rowCountChanged, m_trayView, &TrayGridView::onUpdateEditorView);
connect(m_model, &TrayModel::requestRefreshEditor, m_trayView, &TrayGridView::onUpdateEditorView);
connect(m_trayView, &TrayGridView::requestRemove, m_model, &TrayModel::removeRow);
connect(m_trayView, &TrayGridView::requestRemove, this, &DockTrayWindow::onUpdateComponentSize);
connect(m_trayView, &TrayGridView::dragFinished, this, [ this ] {
// 如果拖拽结束,则隐藏托盘
Q_EMIT m_delegate->requestDrag(false);

View File

@ -151,7 +151,7 @@ void TrayDelegate::setEditorData(QWidget *editor, const QModelIndex &index) cons
{
BaseTrayWidget *widget = static_cast<BaseTrayWidget *>(editor);
if (widget) {
widget->setNeedShow(true);
widget->setNeedShow(!index.data(TrayModel::Blank).toBool());
}
}
@ -222,7 +222,7 @@ ExpandIconWidget *TrayDelegate::expandWidget()
if (!dataModel)
return nullptr;
for (int i = 0; i < dataModel->rowCount() - 1; i++) {
for (int i = 0; i < dataModel->rowCount(); i++) {
QModelIndex index = dataModel->index(i, 0);
ExpandIconWidget *widget = qobject_cast<ExpandIconWidget *>(m_listView->indexWidget(index));
if (widget)

View File

@ -388,6 +388,26 @@ const QModelIndex TrayGridView::getIndexFromPos(QPoint currentPoint) const
return QModelIndex();
}
bool TrayGridView::mouseInDock()
{
QPoint mousePosition = QCursor::pos();
QRect dockRect(topLevelWidget()->pos(), topLevelWidget()->size());
switch (m_positon) {
case Dock::Position::Bottom: {
return mousePosition.y() > dockRect.top();
}
case Dock::Position::Left: {
return mousePosition.x() < dockRect.right();
}
case Dock::Position::Top: {
return mousePosition.y() < dockRect.bottom();
}
case Dock::Position::Right: {
return mousePosition.x() > dockRect.left();
}
}
}
void TrayGridView::handleDropEvent(QDropEvent *e)
{
setState(DListView::NoState);
@ -492,7 +512,7 @@ bool TrayGridView::beginDrag(Qt::DropActions supportedActions)
listModel->setDragKey(modelIndex.data(TrayModel::Role::KeyRole).toString());
listModel->setDragingIndex(modelIndex);
// 删除当前的图标
WinInfo winInfo = listModel->takeIndex(modelIndex);
WinInfo winInfo = listModel->getWinInfo(modelIndex);
Qt::DropAction dropAct = drag->exec(supportedActions);
@ -504,12 +524,15 @@ bool TrayGridView::beginDrag(Qt::DropActions supportedActions)
if (listModel->isIconTray()) {
// 如果当前是从托盘区域释放,按照原来的流程走
QPropertyAnimation *posAni = new QPropertyAnimation(pixLabel, "pos", pixLabel);
connect(posAni, &QPropertyAnimation::finished, [ this, listModel, pixLabel, modelIndex, winInfo ] () {
connect(posAni, &QPropertyAnimation::finished, [ this, listModel, pixLabel, winInfo ] () {
pixLabel->hide();
pixLabel->deleteLater();
listModel->setDragKey(QString());
listModel->insertRow(modelIndex.row(), winInfo);
clearDragModelIndex();
QModelIndex dropIndex = indexAt(m_dropPos);
// 拖转完成后,将拖动的图标插入到新的位置
//listModel->moveToIndex(winInfo, dropIndex.row());
listModel->dropSwap(dropIndex.row());
listModel->setExpandVisible(!TrayModel::getIconModel()->isEmpty());
m_dropPos = QPoint();
@ -518,7 +541,7 @@ bool TrayGridView::beginDrag(Qt::DropActions supportedActions)
onUpdateEditorView();
Q_EMIT dragFinished();
});
// 拖拽完成后将当前拖拽的item从原来的列表中移除后来会根据实际情况将item插入到特定的列表中
posAni->setEasingCurve(QEasingCurve::Linear);
posAni->setDuration(m_aniDuringTime);
posAni->setStartValue((QCursor::pos() - QPoint(0, pixLabel->height() / 2)));
@ -528,19 +551,25 @@ bool TrayGridView::beginDrag(Qt::DropActions supportedActions)
Q_EMIT dragFinished();
} else {
// 如果当前是从任务栏区域释放,则将释放后的图标放到托盘
listModel->setDragKey(QString());
clearDragModelIndex();
TrayModel *trayModel = TrayModel::getIconModel();
trayModel->addRow(winInfo);
if (!mouseInDock()) {
listModel->removeWinInfo(winInfo);
trayModel->addRow(winInfo);
trayModel->saveConfig(-1, winInfo);
}
// 如果是任务栏的的托盘区,则更新是否显示展开入口
listModel->setExpandVisible(trayModel->rowCount() > 0, false);
m_dragPos = QPoint();
m_dropPos = QPoint();
trayModel->saveConfig(-1, winInfo);
Q_EMIT dragFinished();
}
} else {
// 拖拽完成后将当前拖拽的item从原来的列表中移除后来会根据实际情况将item插入到特定的列表中
listModel->removeWinInfo(winInfo);
// 这里是将图标从一个区域移动到另外一个区域
listModel->setDragKey(QString());
clearDragModelIndex();
if (listModel->isIconTray()) {

View File

@ -51,7 +51,6 @@ public Q_SLOTS:
void onUpdateEditorView();
Q_SIGNALS:
void requestRemove(const QString &);
void dragLeaved();
void dragEntered();
void dragFinished();
@ -76,6 +75,7 @@ private:
void initUi();
void createAnimation(const int pos, const bool moveNext, const bool isLastAni);
const QModelIndex getIndexFromPos(QPoint currentPoint) const;
bool mouseInDock();
private:
QEasingCurve::Type m_aniCurveType;

View File

@ -93,19 +93,16 @@ void TrayModel::dropSwap(int newPos)
if (!m_dragModelIndex.isValid())
return;
removeRows(m_dragModelIndex.row(), 1, QModelIndex());
dropInsert(newPos);
int row = m_dragModelIndex.row();
emit QAbstractItemModel::dataChanged(m_dragModelIndex, m_dropModelIndex);
}
if (row < m_winInfos.size())
m_dragInfo = m_winInfos.takeAt(row);
void TrayModel::dropInsert(int newPos)
{
beginInsertRows(QModelIndex(), newPos, newPos);
WinInfo name = m_dragInfo;
m_winInfos.insert(newPos, name);
// 更新输入法的位置
endInsertRows();
emit QAbstractItemModel::dataChanged(m_dragModelIndex, m_dropModelIndex);
requestRefreshEditor();
}
void TrayModel::clearDragDropIndex()
@ -115,7 +112,6 @@ void TrayModel::clearDragDropIndex()
m_dragModelIndex = m_dropModelIndex = QModelIndex();
Q_EMIT requestRefreshEditor();
emit QAbstractItemModel::dataChanged(startIndex, endIndex);
emit QAbstractItemModel::dataChanged(endIndex, startIndex);
}
@ -125,7 +121,6 @@ void TrayModel::setDragingIndex(const QModelIndex index)
m_dragModelIndex = index;
m_dropModelIndex = index;
Q_EMIT requestRefreshEditor();
emit QAbstractListModel::dataChanged(index, index);
}
@ -278,7 +273,6 @@ QVariant TrayModel::data(const QModelIndex &index, int role) const
bool TrayModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(count);
Q_UNUSED(parent);
if (m_winInfos.size() - 1 < row)
return false;
@ -353,20 +347,13 @@ void TrayModel::clear()
Q_EMIT rowCountChanged();
}
WinInfo TrayModel::takeIndex(const QModelIndex &index)
WinInfo TrayModel::getWinInfo(const QModelIndex &index)
{
int row = index.row();
if (row < 0 || row >= m_winInfos.size())
return WinInfo();
WinInfo win = m_winInfos[row];
beginResetModel();
m_winInfos.removeAt(row);
endResetModel();
Q_EMIT rowCountChanged();
return win;
return m_winInfos[row];
}
void TrayModel::onXEmbedTrayAdded(quint32 winId)
@ -462,6 +449,21 @@ void TrayModel::saveConfig(int index, const WinInfo &winInfo)
SETTINGCONFIG->setValue(DOCKQUICKTRAYNAME, m_fixedTrayNames);
}
void TrayModel::removeWinInfo(WinInfo winInfo)
{
for (const WinInfo &info : m_winInfos) {
if (winInfo == info) {
int index = m_winInfos.indexOf(info);
beginRemoveRows(QModelIndex(), index, index);
m_winInfos.removeOne(info);
endRemoveRows();
Q_EMIT rowCountChanged();
break;
}
}
}
bool TrayModel::inTrayConfig(const QString itemKey) const
{
if (m_isTrayIcon) {
@ -640,7 +642,7 @@ void TrayModel::onIndicatorAdded(const QString &indicatorName)
const QString &itemKey = IndicatorTrayItem::toIndicatorKey(indicatorName);
for (const WinInfo &info : m_winInfos) {
if (info.key == itemKey)
if (info.itemKey == itemKey)
return;
}
@ -648,7 +650,7 @@ void TrayModel::onIndicatorAdded(const QString &indicatorName)
WinInfo info;
info.type = Incicator;
info.key = itemKey;
info.itemKey = IndicatorTrayItem::toIndicatorKey(indicatorName);
info.itemKey = itemKey;
m_winInfos.append(info);
sortItems();
@ -754,7 +756,7 @@ void TrayModel::onSettingChanged(const QString &key, const QVariant &value)
void TrayModel::removeRow(const QString &itemKey)
{
for (const WinInfo &info : m_winInfos) {
if (info.key == itemKey) {
if (info.itemKey == itemKey) {
int index = m_winInfos.indexOf(info);
beginRemoveRows(QModelIndex(), index, index);
m_winInfos.removeOne(info);

View File

@ -91,7 +91,6 @@ public:
static TrayModel *getIconModel();
void dropSwap(int newPos);
void dropInsert(int newPos);
void clearDragDropIndex();
void setDragingIndex(const QModelIndex index);
@ -111,8 +110,9 @@ public:
bool isEmpty() const;
void clear();
WinInfo takeIndex(const QModelIndex &index);
WinInfo getWinInfo(const QModelIndex &index);
void saveConfig(int index, const WinInfo &winInfo);
void removeWinInfo(WinInfo winInfo);
Q_SIGNALS:
void requestUpdateIcon(quint32);
@ -121,7 +121,6 @@ Q_SIGNALS:
void requestRefreshEditor();
public Q_SLOTS:
void removeRow(const QString &itemKey);
void addRow(WinInfo info);
void insertRow(int index, WinInfo info);
@ -152,6 +151,7 @@ protected:
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
private:
void removeRow(const QString &itemKey);
bool exist(const QString &itemKey);
QString fileNameByServiceName(const QString &serviceName) const;
bool isTypeWriting(const QString &servicePath) const;

View File

@ -171,7 +171,6 @@ TrayGridWidget *ExpandIconWidget::popupTrayView()
connect(trayDelegate, &TrayDelegate::removeRow, trayView, [ = ](const QModelIndex &index) {
trayView->model()->removeRow(index.row(),index.parent());
});
connect(trayView, &TrayGridView::requestRemove, trayModel, &TrayModel::removeRow);
connect(trayModel, &TrayModel::requestOpenEditor, trayView, [ trayView ](const QModelIndex &index) {
trayView->openPersistentEditor(index);
});
@ -335,7 +334,7 @@ ExpandIconWidget *TrayGridWidget::expandWidget() const
if (!dataModel)
return nullptr;
for (int i = 0; i < dataModel->rowCount() - 1; i++) {
for (int i = 0; i < dataModel->rowCount(); i++) {
QModelIndex index = dataModel->index(i, 0);
ExpandIconWidget *widget = qobject_cast<ExpandIconWidget *>(m_referGridView->indexWidget(index));
if (widget)

View File

@ -47,9 +47,11 @@ SystemPluginItem::SystemPluginItem(PluginsItemInterface *const pluginInter, cons
{
qDebug() << "load tray plugins item: " << m_pluginInter->pluginName() << itemKey << m_centralWidget;
m_centralWidget->setParent(this);
m_centralWidget->setVisible(true);
m_centralWidget->installEventFilter(this);
if (m_centralWidget) {
m_centralWidget->setParent(this);
m_centralWidget->setVisible(true);
m_centralWidget->installEventFilter(this);
}
QBoxLayout *hLayout = new QHBoxLayout(this);
hLayout->addWidget(m_centralWidget);
@ -89,6 +91,8 @@ SystemPluginItem::SystemPluginItem(PluginsItemInterface *const pluginInter, cons
m_popupAdjustDelayTimer->setInterval(10);
m_popupAdjustDelayTimer->setSingleShot(true);
installEventFilter(this);
connect(m_popupTipsDelayTimer, &QTimer::timeout, this, &SystemPluginItem::showHoverTips);
connect(m_popupAdjustDelayTimer, &QTimer::timeout, this, &SystemPluginItem::updatePopupPosition, Qt::QueuedConnection);
connect(m_contextMenu, &QMenu::triggered, this, &SystemPluginItem::menuActionClicked);
@ -188,6 +192,14 @@ bool SystemPluginItem::event(QEvent *event)
return BaseTrayWidget::event(event);
}
bool SystemPluginItem::eventFilter(QObject *watched, QEvent *event)
{
if (watched == this && event->type() == QEvent::DeferredDelete)
m_centralWidget->setParent(nullptr);
return BaseTrayWidget::eventFilter(watched, event);
}
void SystemPluginItem::enterEvent(QEvent *event)
{
if (checkGSettingsControl()) {

View File

@ -67,6 +67,7 @@ signals:
protected:
bool event(QEvent *event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
void enterEvent(QEvent *event) override;
void leaveEvent(QEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;

View File

@ -270,7 +270,6 @@ void TrayManagerWindow::initUi()
void TrayManagerWindow::initConnection()
{
connect(m_trayView, &TrayGridView::requestRemove, m_model, &TrayModel::removeRow);
connect(m_model, &TrayModel::rowCountChanged, this, &TrayManagerWindow::onTrayCountChanged);
connect(m_model, &TrayModel::rowCountChanged, m_trayView, &TrayGridView::onUpdateEditorView);
connect(m_model, &TrayModel::requestRefreshEditor, m_trayView, &TrayGridView::onUpdateEditorView);