|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +import QtQuick 2.15 |
| 6 | +import QtQuick.Window 2.15 |
| 7 | +import QtWayland.Compositor |
| 8 | + |
| 9 | +import org.deepin.ds 1.0 |
| 10 | +import org.deepin.ds.dock 1.0 |
| 11 | +import org.deepin.dtk 1.0 as D |
| 12 | + |
| 13 | +AppletDockItem { |
| 14 | + id: root |
| 15 | + |
| 16 | + readonly property bool fashionMode: Panel.fashionMode |
| 17 | + readonly property int dockSize: Panel.rootObject.dockSize |
| 18 | + readonly property int hoverInset: 5 |
| 19 | + readonly property real taskbarRadius: Panel.rootObject.fashionDock.backgroundRadius |
| 20 | + readonly property real hoverBackgroundRadius: taskbarRadius - hoverInset |
| 21 | + readonly property int adaptiveCardLeftWidth: 150 + Math.max(0, dockSize / 4) |
| 22 | + readonly property int rightContentPadding: Math.max(10, Math.round(adaptiveCardLeftWidth * 0.07)) |
| 23 | + readonly property int verticalInset: Math.max(5, Math.round(dockSize * 0.16)) |
| 24 | + readonly property int pageContentHeight: Math.max(24, dockSize - verticalInset * 2) |
| 25 | + |
| 26 | + readonly property color primaryTextColor: Panel.colorTheme === Dock.Dark |
| 27 | + ? Qt.rgba(1, 1, 1, 0.96) |
| 28 | + : Qt.rgba(0, 0, 0, 0.92) |
| 29 | + |
| 30 | + dockOrder: 5 |
| 31 | + shouldVisible: fashionMode && pageCount > 0 |
| 32 | + property int currentIndex: 0 |
| 33 | + property int previousIndex: -1 |
| 34 | + property int switchDirection: 1 |
| 35 | + property real pageTransitionProgress: 1 |
| 36 | + readonly property int pageCount: DockCompositor.cardPluginSurfaces.count |
| 37 | + property bool contentHovered: false |
| 38 | + readonly property bool pageTransitioning: pageTransitionAnimation.running || pageTransitionProgress < 1 |
| 39 | + readonly property bool effectiveHovered: rootHoverHandler.hovered || contentHovered |
| 40 | + |
| 41 | + visible: shouldVisible |
| 42 | + implicitWidth: adaptiveCardLeftWidth |
| 43 | + implicitHeight: dockSize |
| 44 | + clip: true |
| 45 | + |
| 46 | + function step(delta) { |
| 47 | + if (pageCount <= 1) { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + const nextIndex = (currentIndex + delta + pageCount) % pageCount |
| 52 | + if (nextIndex === currentIndex) { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + pageTransitionAnimation.stop() |
| 57 | + previousIndex = currentIndex |
| 58 | + switchDirection = delta > 0 ? 1 : -1 |
| 59 | + pageTransitionProgress = 0 |
| 60 | + currentIndex = nextIndex |
| 61 | + pageTransitionAnimation.restart() |
| 62 | + } |
| 63 | + |
| 64 | + function updateContentHovered() { |
| 65 | + const item = pluginRepeater.itemAt(currentIndex) |
| 66 | + contentHovered = !!item && item.visible && item.surfaceHovered |
| 67 | + } |
| 68 | + |
| 69 | + onCurrentIndexChanged: updateContentHovered() |
| 70 | + onPageCountChanged: { |
| 71 | + if (pageCount <= 0) { |
| 72 | + previousIndex = -1 |
| 73 | + pageTransitionProgress = 1 |
| 74 | + currentIndex = 0 |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if (currentIndex >= pageCount) { |
| 79 | + previousIndex = -1 |
| 80 | + pageTransitionProgress = 1 |
| 81 | + currentIndex = pageCount - 1 |
| 82 | + } else if (currentIndex < 0) { |
| 83 | + previousIndex = -1 |
| 84 | + pageTransitionProgress = 1 |
| 85 | + currentIndex = 0 |
| 86 | + } |
| 87 | + |
| 88 | + updateContentHovered() |
| 89 | + } |
| 90 | + |
| 91 | + NumberAnimation { |
| 92 | + id: pageTransitionAnimation |
| 93 | + |
| 94 | + target: root |
| 95 | + property: "pageTransitionProgress" |
| 96 | + from: 0 |
| 97 | + to: 1 |
| 98 | + duration: 300 |
| 99 | + easing.type: Easing.OutCubic |
| 100 | + onStopped: { |
| 101 | + root.pageTransitionProgress = 1 |
| 102 | + root.previousIndex = -1 |
| 103 | + root.updateContentHovered() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + HoverHandler { |
| 108 | + id: rootHoverHandler |
| 109 | + acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad | PointerDevice.Stylus |
| 110 | + } |
| 111 | + |
| 112 | + AppletItemBackground { |
| 113 | + x: root.hoverInset |
| 114 | + y: root.hoverInset |
| 115 | + width: parent.width - root.hoverInset |
| 116 | + height: parent.height - root.hoverInset * 2 |
| 117 | + radius: root.hoverBackgroundRadius |
| 118 | + enabled: false |
| 119 | + opacity: root.effectiveHovered ? 1 : 0 |
| 120 | + D.ColorSelector.hovered: root.effectiveHovered |
| 121 | + |
| 122 | + Behavior on opacity { |
| 123 | + NumberAnimation { |
| 124 | + duration: 150 |
| 125 | + easing.type: Easing.OutCubic |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + CardLeftDockSwitcher { |
| 131 | + anchors.right: parent.right |
| 132 | + anchors.rightMargin: 8 |
| 133 | + anchors.verticalCenter: parent.verticalCenter |
| 134 | + visible: root.effectiveHovered && root.pageCount > 1 |
| 135 | + itemCount: root.pageCount |
| 136 | + currentIndex: root.currentIndex |
| 137 | + indicatorSize: 2 |
| 138 | + indicatorSpacing: 4 |
| 139 | + indicatorColor: root.primaryTextColor |
| 140 | + } |
| 141 | + |
| 142 | + Item { |
| 143 | + anchors.left: parent.left |
| 144 | + anchors.right: parent.right |
| 145 | + anchors.verticalCenter: parent.verticalCenter |
| 146 | + anchors.leftMargin: 8 |
| 147 | + anchors.rightMargin: root.rightContentPadding |
| 148 | + height: root.pageContentHeight |
| 149 | + clip: true |
| 150 | + |
| 151 | + Repeater { |
| 152 | + id: pluginRepeater |
| 153 | + model: DockCompositor.cardPluginSurfaces |
| 154 | + |
| 155 | + delegate: Item { |
| 156 | + id: surfaceHost |
| 157 | + |
| 158 | + property var plugin: model.shellSurface |
| 159 | + readonly property bool surfaceHovered: visible && surfaceHoverHandler.hovered |
| 160 | + function itemGlobalPoint() { |
| 161 | + var item = surfaceHost |
| 162 | + var x = 0 |
| 163 | + var y = 0 |
| 164 | + while (item) { |
| 165 | + x += item.x |
| 166 | + y += item.y |
| 167 | + item = item.parent |
| 168 | + } |
| 169 | + return Qt.point(x, y) |
| 170 | + } |
| 171 | + function itemGlobalPos() { |
| 172 | + var point = itemGlobalPoint() |
| 173 | + if (surfaceHost.Window.window) { |
| 174 | + point.x += surfaceHost.Window.window.x |
| 175 | + point.y += surfaceHost.Window.window.y |
| 176 | + } |
| 177 | + return point |
| 178 | + } |
| 179 | + |
| 180 | + y: { |
| 181 | + if (index === root.currentIndex) { |
| 182 | + return (1 - root.pageTransitionProgress) * root.switchDirection * height |
| 183 | + } |
| 184 | + |
| 185 | + if (index === root.previousIndex && root.pageTransitioning) { |
| 186 | + return -root.pageTransitionProgress * root.switchDirection * height |
| 187 | + } |
| 188 | + |
| 189 | + return 0 |
| 190 | + } |
| 191 | + width: parent.width |
| 192 | + height: parent.height |
| 193 | + visible: !!plugin && (index === root.currentIndex || (root.pageTransitioning && index === root.previousIndex)) |
| 194 | + |
| 195 | + ShellSurfaceItem { |
| 196 | + id: surfaceItem |
| 197 | + width: parent.width |
| 198 | + height: parent.height |
| 199 | + shellSurface: surfaceHost.plugin |
| 200 | + smooth: false |
| 201 | + HoverHandler { |
| 202 | + id: surfaceHoverHandler |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + function updateSurfaceGeometry() { |
| 207 | + if (!plugin || !visible) { |
| 208 | + return |
| 209 | + } |
| 210 | + |
| 211 | + const globalPoint = itemGlobalPoint() |
| 212 | + const globalPos = itemGlobalPos() |
| 213 | + plugin.updatePluginGeometry(Qt.rect(Math.round(globalPoint.x), |
| 214 | + Math.round(globalPoint.y), |
| 215 | + Math.round(width), |
| 216 | + Math.round(height))) |
| 217 | + plugin.setGlobalPos(Qt.point(Math.round(globalPos.x), |
| 218 | + Math.round(globalPos.y))) |
| 219 | + surfaceItem.fixPosition() |
| 220 | + } |
| 221 | + |
| 222 | + Component.onCompleted: updateSurfaceGeometry() |
| 223 | + onYChanged: geometryUpdateTimer.restart() |
| 224 | + onWidthChanged: geometryUpdateTimer.restart() |
| 225 | + onHeightChanged: geometryUpdateTimer.restart() |
| 226 | + onVisibleChanged: { |
| 227 | + geometryUpdateTimer.restart() |
| 228 | + root.updateContentHovered() |
| 229 | + } |
| 230 | + onSurfaceHoveredChanged: root.updateContentHovered() |
| 231 | + |
| 232 | + Timer { |
| 233 | + id: geometryUpdateTimer |
| 234 | + interval: 50 |
| 235 | + repeat: false |
| 236 | + onTriggered: surfaceHost.updateSurfaceGeometry() |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + Item { |
| 243 | + anchors.fill: parent |
| 244 | + z: 1 |
| 245 | + |
| 246 | + WheelHandler { |
| 247 | + target: null |
| 248 | + enabled: root.pageCount > 1 |
| 249 | + acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad |
| 250 | + onWheel: function(wheel) { |
| 251 | + const deltaY = wheel.angleDelta.y !== 0 ? wheel.angleDelta.y : wheel.pixelDelta.y |
| 252 | + if (deltaY !== 0) { |
| 253 | + root.step(deltaY < 0 ? 1 : -1) |
| 254 | + wheel.accepted = true |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments