Skip to content

Commit 1618613

Browse files
committed
chore: make hover preview proxy model simpler
简化 HoverPreviewProxyModel, 使 model 本身不与设置项耦合, 过滤规则 在 taskmanager.cpp 控制 Log:
1 parent 9defd55 commit 1618613

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

panels/dock/taskmanager/hoverpreviewproxymodel.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "hoverpreviewproxymodel.h"
66
#include "taskmanager.h"
7-
#include "taskmanagersettings.h"
87

98
#include <QDebug>
109

@@ -13,34 +12,30 @@ namespace dock
1312

1413
HoverPreviewProxyModel::HoverPreviewProxyModel(QObject *parent)
1514
: QSortFilterProxyModel(parent)
16-
, m_settings(TaskManagerSettings::instance())
1715
{
1816
// 设置动态排序,确保模型变化时自动重新过滤
1917
setDynamicSortFilter(true);
2018
}
2119

22-
void HoverPreviewProxyModel::setFilterModelIndex(const QModelIndex &index)
20+
void HoverPreviewProxyModel::setFilter(QString filter, enum FilterMode mode)
2321
{
24-
if (m_filterIndex == index)
25-
return;
22+
m_filter = filter;
23+
m_filterMode = mode;
2624

27-
m_filterIndex = index;
28-
29-
// 触发过滤器重新计算
3025
invalidateFilter();
3126
}
3227

3328
void HoverPreviewProxyModel::clearFilter()
3429
{
35-
setFilterModelIndex(QModelIndex());
30+
setFilter(QString(), FilterByAppId);
3631
}
3732

3833
bool HoverPreviewProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
3934
{
4035
Q_UNUSED(sourceParent)
4136

4237
// 如果没有设置过滤条件,则不显示任何行
43-
if (!m_filterIndex.isValid())
38+
if (m_filter.isEmpty())
4439
return false;
4540

4641
if (!sourceModel())
@@ -56,27 +51,19 @@ bool HoverPreviewProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &
5651
if (winId == 0)
5752
return false;
5853

59-
// 根据 noTaskGrouping 配置采用不同的过滤策略
60-
bool isWindowSplit = (m_settings && m_settings->isWindowSplit());
61-
62-
if (isWindowSplit) {
63-
// noTaskGrouping = true: 精确匹配该模型索引对应的窗口
64-
// 比较窗口ID是否匹配
65-
QVariant filterWinId = m_filterIndex.data(TaskManager::WinIdRole);
66-
uint32_t targetWinId = filterWinId.toUInt();
67-
54+
switch (m_filterMode) {
55+
case FilterByAppId: {
56+
QString currentDesktopId = sourceIndex.data(TaskManager::DesktopIdRole).toString();
57+
return currentDesktopId == m_filter;
58+
}
59+
case FilterByWinId: {
60+
uint32_t targetWinId = m_filter.toUInt();
6861
bool result = (winId == targetWinId && targetWinId != 0);
69-
// 在 WindowSplit 模式下,精确匹配单个窗口
70-
return result;
71-
} else {
72-
// noTaskGrouping = false: 基于 DesktopIdRole 匹配应用的所有窗口
73-
QVariant currentDesktopId = sourceIndex.data(TaskManager::DesktopIdRole);
74-
QVariant filterDesktopId = m_filterIndex.data(TaskManager::DesktopIdRole);
75-
76-
bool result = (currentDesktopId.toString() == filterDesktopId.toString());
77-
// 在 Grouped 模式下,匹配同一应用的所有窗口
7862
return result;
7963
}
64+
}
65+
66+
return false;
8067
}
8168

8269
}

panels/dock/taskmanager/hoverpreviewproxymodel.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,16 @@ class HoverPreviewProxyModel : public QSortFilterProxyModel
2424
public:
2525
explicit HoverPreviewProxyModel(QObject *parent = nullptr);
2626

27+
enum FilterMode {
28+
FilterByAppId,
29+
FilterByWinId,
30+
};
31+
2732
/**
2833
* @brief 设置要预览的模型索引,在 noTaskGrouping 模式下精确匹配该窗口
2934
* @param index 要预览的模型索引,无效索引则清除过滤条件
3035
*/
31-
void setFilterModelIndex(const QModelIndex &index);
32-
33-
/**
34-
* @brief 获取当前过滤的模型索引
35-
* @return 当前过滤的模型索引
36-
*/
37-
QModelIndex filterModelIndex() const
38-
{
39-
return m_filterIndex;
40-
}
36+
void setFilter(QString filter, enum FilterMode mode);
4137

4238
/**
4339
* @brief 清除过滤条件,重置模型状态
@@ -48,8 +44,8 @@ class HoverPreviewProxyModel : public QSortFilterProxyModel
4844
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
4945

5046
private:
51-
QModelIndex m_filterIndex; // 当前过滤的模型索引
52-
TaskManagerSettings *m_settings; // 任务管理器设置,用于获取 noTaskGrouping 状态
47+
FilterMode m_filterMode;
48+
QString m_filter;
5349
};
5450

5551
}

panels/dock/taskmanager/taskmanager.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,13 @@ void TaskManager::requestPreview(const QModelIndex &index, QObject *relativePosi
248248
}
249249

250250
// Set the preview filter condition based on the incoming model index
251-
m_hoverPreviewModel->setFilterModelIndex(index);
251+
if (windowSplit()) {
252+
QString winId = index.data(TaskManager::WinIdRole).toString();
253+
m_hoverPreviewModel->setFilter(winId, HoverPreviewProxyModel::FilterByWinId);
254+
} else {
255+
QString appId = index.data(TaskManager::DesktopIdRole).toString();
256+
m_hoverPreviewModel->setFilter(appId, HoverPreviewProxyModel::FilterByAppId);
257+
}
252258

253259
// Check if there are any windows after filtering
254260
if (m_hoverPreviewModel->rowCount() == 0) {

0 commit comments

Comments
 (0)