Skip to content

Commit 2b39c5d

Browse files
committed
chore: avoid using cell width to calculate target index for taskbar
避免使用固定的任务栏图标宽度来计算当前拖拽的目标项的索引. 这个修改旨在下面两点: 1. 为后续任务栏图标不固定长度做准备(图标+标题) 2. 为后续ItemModel解耦做准备,便于重构分支合入(重构分支使用DelegateModel 的 items 属性维护的顺序表示任务栏上的图标顺序,因而无法直接使用此处 原本的下标计算方式) (下一步是规避使用 ItemModel::moveTo() 接口) Log:
1 parent cf6a59c commit 2b39c5d

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

panels/dock/OverflowContainer.qml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Item {
4747
}
4848
}
4949

50+
function indexAt(x, y) {
51+
return listView.indexAt(x, y)
52+
}
53+
5054
implicitWidth: {
5155
let width = 0
5256
for (let child of listView.contentItem.visibleChildren) {

panels/dock/taskmanager/package/TaskManager.qml

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ ContainmentItem {
3333
implicitWidth: useColumnLayout ? Panel.rootObject.dockSize : (Math.min(remainingSpacesForTaskManager, appContainer.implicitWidth) + forceRelayoutWorkaround)
3434
implicitHeight: useColumnLayout ? (Math.min(remainingSpacesForTaskManager, appContainer.implicitHeight) + forceRelayoutWorkaround) : Panel.rootObject.dockSize
3535

36+
// Find target index by position using ListView's built-in indexAt method instead of relying on fixed width
37+
function findTargetIndexByPosition(dragPosition) {
38+
if (!appContainer || !appContainer.model || appContainer.model.count === 0) {
39+
return 0
40+
}
41+
42+
// Use ListView's built-in indexAt method
43+
let targetIndex = appContainer.indexAt(dragPosition.x, dragPosition.y)
44+
45+
// If no valid index found, return end position
46+
if (targetIndex === -1) {
47+
return appContainer.model.count
48+
}
49+
50+
return targetIndex
51+
}
52+
3653
OverflowContainer {
3754
id: appContainer
3855
anchors.fill: parent
@@ -103,7 +120,8 @@ ContainmentItem {
103120
implicitHeight: useColumnLayout ? visualModel.cellWidth : taskmanager.implicitHeight
104121

105122
onEntered: function(drag) {
106-
visualModel.items.move((drag.source as AppItem).visualIndex, app.visualIndex)
123+
// TODO: this is actually unused, should change the delegateRoot type from DropArea to Item later.
124+
visualModel.items.move(drag.source.DelegateModel.itemsIndex, delegateRoot.DelegateModel.itemsIndex)
107125
}
108126

109127
property int visualIndex: DelegateModel.itemsIndex
@@ -156,21 +174,19 @@ ContainmentItem {
156174

157175
onPositionChanged: function(drag) {
158176
if (launcherDndDesktopId === "") return
159-
let curX = taskmanager.useColumnLayout ? drag.y : drag.x
160-
let cellWidth = visualModel.cellWidth
161-
let curCell = curX / cellWidth
177+
let dragPosition = Qt.point(drag.x, drag.y)
178+
let targetIndex = taskmanager.findTargetIndexByPosition(dragPosition)
162179
let appId = taskmanager.Applet.desktopIdToAppId(launcherDndDesktopId)
163-
taskmanager.Applet.dataModel.moveTo(appId, curCell)
180+
taskmanager.Applet.dataModel.moveTo(appId, targetIndex)
164181
}
165182

166183
onDropped: function(drop) {
167184
Panel.contextDragging = false
168185
if (launcherDndDesktopId === "") return
169-
let curX = taskmanager.useColumnLayout ? drop.y : drop.x
170-
let cellWidth = visualModel.cellWidth
171-
let curCell = curX / cellWidth
186+
let dropPosition = Qt.point(drop.x, drop.y)
187+
let targetIndex = taskmanager.findTargetIndexByPosition(dropPosition)
172188
let appId = taskmanager.Applet.desktopIdToAppId(launcherDndDesktopId)
173-
taskmanager.Applet.dataModel.moveTo(appId, curCell)
189+
taskmanager.Applet.dataModel.moveTo(appId, targetIndex)
174190
resetDndState()
175191
}
176192

0 commit comments

Comments
 (0)