dock/qml/DockItem.qml

201 lines
6.2 KiB
QML
Raw Permalink Normal View History

2021-05-25 04:44:02 -07:00
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: rekols <revenmartin@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-03-15 20:17:11 -07:00
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0
2021-03-16 03:20:04 -07:00
import Cutefish.Dock 1.0
2021-04-09 07:38:15 -07:00
import FishUI 1.0 as FishUI
2021-03-15 20:17:11 -07:00
Item {
id: control
property bool isLeft: Settings.direction === DockSettings.Left
2021-03-24 09:14:48 -07:00
property bool isRight: Settings.direction === DockSettings.Right
property bool isBottom: Settings.direction === DockSettings.Bottom
2021-03-15 20:17:11 -07:00
2021-03-24 09:14:48 -07:00
property var iconSize: root.isHorizontal ? control.height * iconSizeRatio
: control.width * iconSizeRatio
2021-03-15 20:17:11 -07:00
property bool draggable: false
property int dragItemIndex
property alias icon: icon
property alias mouseArea: iconArea
property alias dropArea: iconDropArea
property bool enableActivateDot: true
property bool isActive: false
property var popupText
property double iconSizeRatio: 0.8
property var iconName
property bool dragStarted: false
signal positionChanged()
signal released()
signal pressed(var mouse)
signal pressAndHold(var mouse)
signal clicked(var mouse)
signal rightClicked(var mouse)
signal doubleClicked(var mouse)
2021-12-26 01:07:57 -08:00
signal dropped(var drop)
2021-03-15 20:17:11 -07:00
Drag.active: mouseArea.drag.active && control.draggable
Drag.dragType: Drag.Automatic
Drag.supportedActions: Qt.MoveAction
Drag.hotSpot.x: icon.width / 2
Drag.hotSpot.y: icon.height / 2
Drag.onDragStarted: {
dragStarted = true
}
Drag.onDragFinished: {
dragStarted = false
}
2021-04-09 07:38:15 -07:00
FishUI.IconItem {
2021-03-15 20:17:11 -07:00
id: icon
anchors.centerIn: parent
2021-03-17 06:33:06 -07:00
width: control.iconSize
height: control.iconSize
source: iconName
2021-03-15 20:17:11 -07:00
visible: !dragStarted
ColorOverlay {
id: iconColorize
anchors.fill: icon
source: icon
color: "#000000"
2021-05-30 07:21:04 -07:00
opacity: iconArea.pressed && !mouseArea.drag.active ? 0.4 : 0
2021-03-15 20:17:11 -07:00
}
}
DropArea {
id: iconDropArea
anchors.fill: icon
enabled: draggable
2021-12-26 01:07:57 -08:00
onDropped: control.dropped(drop)
2021-03-15 20:17:11 -07:00
}
MouseArea {
id: iconArea
anchors.fill: icon
hoverEnabled: true
acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton
2021-03-15 20:17:11 -07:00
drag.axis: Drag.XAndYAxis
onClicked: {
if (mouse.button === Qt.RightButton)
control.rightClicked(mouse)
else
control.clicked(mouse)
}
onPressed: {
control.pressed(mouse)
popupTips.hide()
}
onPositionChanged: {
if (pressed) {
2021-08-28 02:35:05 -07:00
if (control.draggable && mouse.source !== Qt.MouseEventSynthesizedByQt) {
2021-03-15 20:17:11 -07:00
drag.target = icon
icon.grabToImage(function(result) {
control.Drag.imageSource = result.url
})
} else {
drag.target = null
}
}
control.positionChanged()
}
onPressAndHold : control.pressAndHold(mouse)
onReleased: {
drag.target = null
control.released()
}
onContainsMouseChanged: {
if (containsMouse && control.popupText !== "") {
popupTips.popupText = control.popupText
if (Settings.direction === DockSettings.Left)
2021-04-09 07:38:15 -07:00
popupTips.position = Qt.point(root.width + FishUI.Units.largeSpacing,
2021-03-15 20:17:11 -07:00
control.mapToGlobal(0, 0).y + (control.height / 2 - popupTips.height / 2))
2021-03-24 09:14:48 -07:00
else if (Settings.direction === DockSettings.Right)
2021-04-09 07:38:15 -07:00
popupTips.position = Qt.point(control.mapToGlobal(0, 0).x - popupTips.width - FishUI.Units.smallSpacing / 2,
2021-03-24 09:14:48 -07:00
control.mapToGlobal(0, 0).y + (control.height / 2 - popupTips.height / 2))
2021-03-15 20:17:11 -07:00
else
popupTips.position = Qt.point(control.mapToGlobal(0, 0).x + (control.width / 2 - popupTips.width / 2),
2021-04-09 07:38:15 -07:00
control.mapToGlobal(0, 0).y - popupTips.height - FishUI.Units.smallSpacing)
2021-03-15 20:17:11 -07:00
popupTips.show()
} else {
popupTips.hide()
}
}
}
Rectangle {
2021-05-25 04:44:02 -07:00
id: activeRect
2021-03-15 20:17:11 -07:00
2021-11-02 10:29:31 -07:00
property var leftX: 2
2021-03-24 09:14:48 -07:00
property var leftY: (parent.height - height) / 2
property var bottomX: (parent.width - width) / 2
2021-11-02 10:29:31 -07:00
property var bottomY: icon.y + icon.height + activeRect.height / 2 - 1
2021-03-24 09:14:48 -07:00
2021-11-02 10:29:31 -07:00
property var rightX: icon.x + icon.width + activeRect.width / 2 - 1
2021-03-24 09:14:48 -07:00
property var rightY: (parent.height - height) / 2
2021-05-25 04:44:02 -07:00
property var circleSize: isBottom ? control.height * 0.06 : control.width * 0.06
property var activeLength: isBottom ? control.height * 0.5 : control.height * 0.5
width: !isBottom ? circleSize : (isActive ? activeLength : circleSize)
height: !isBottom ? (isActive ? activeLength : circleSize) : circleSize
radius: !isBottom ? width / 2 : height / 2
visible: enableActivateDot && !dragStarted
color: FishUI.Theme.textColor
2021-03-24 09:14:48 -07:00
x: isLeft ? leftX : isBottom ? bottomX : rightX
y: isLeft ? leftY : isBottom ? bottomY : rightY
2021-03-15 20:17:11 -07:00
Behavior on width {
NumberAnimation {
duration: isBottom ? 250 : 0
2021-05-25 04:44:02 -07:00
easing.type: Easing.InOutSine
2021-03-15 20:17:11 -07:00
}
}
Behavior on height {
NumberAnimation {
duration: !isBottom ? 250 : 0
2021-05-25 04:44:02 -07:00
easing.type: Easing.InOutSine
2021-03-15 20:17:11 -07:00
}
}
}
}