aerothemeplasma/plasma/plasmoids/io.gitgud.wackyideas.seventasks/contents/ui/MouseHandler.qml
2025-06-20 20:27:02 +02:00

140 lines
3.9 KiB
QML

/*
SPDX-FileCopyrightText: 2012-2016 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
import QtQuick
import org.kde.taskmanager as TaskManager
import org.kde.plasma.plasmoid 2.0
import "code/tools.js" as TaskTools
DropArea {
id: dropArea
signal urlsDropped(var urls)
property Item target
property Item ignoredItem
property Item hoveredItem
property bool moved: false
property alias handleWheelEvents: wheelHandler.handleWheelEvents
//ignore anything that is neither internal to TaskManager or a URL list
onEntered: event => {
if (event.formats.indexOf("text/x-plasmoidservicename") >= 0) {
event.accepted = false;
}
}
onPositionChanged: event => {
let i = target.indexAt(event.x, event.y);
let above;
above = target.itemAtIndex(i);
if (!above) {
hoveredItem = null;
return;
}
// If we're mixing launcher tasks with other tasks and are moving
// a (small) launcher task across a non-launcher task, don't allow
// the latter to be the move target twice in a row for a while, as
// it will naturally be moved underneath the cursor as result of the
// initial move, due to being far larger than the launcher delegate.
// TODO: This restriction (minus the timer, which improves things)
// has been proven out in the EITM fork, but could be improved later
// by tracking the cursor movement vector and allowing the drag if
// the movement direction has reversed, establishing user intent to
// move back.
if (!Plasmoid.configuration.separateLaunchers && tasks.dragSource != null
&& tasks.dragSource.model.IsLauncher && !above.model.IsLauncher
&& above === ignoredItem) {
return;
} else {
ignoredItem = null;
}
if (!tasks.dragItem && hoveredItem !== above) {
console.log(hoveredItem + " " + above);
hoveredItem = above;
}
}
onExited: {
hoveredItem = null;
}
onDropped: event => {
// Reject internal drops.
if (event.formats.indexOf("application/x-orgkdeplasmataskmanager_taskbuttonitem") >= 0) {
event.accepted = false;
return;
}
// Reject plasmoid drops.
if (event.formats.indexOf("text/x-plasmoidservicename") >= 0) {
event.accepted = false;
return;
}
if (event.hasUrls) {
urlsDropped(event.urls);
return;
}
}
Connections {
target: tasks
function onDragSourceChanged() {
if (!dragSource) {
ignoredItem = null;
ignoreItemTimer.stop();
}
}
}
Timer {
id: ignoreItemTimer
repeat: false
interval: 750
onTriggered: {
ignoredItem = null;
}
}
WheelHandler {
id: wheelHandler
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
property bool handleWheelEvents: true
enabled: handleWheelEvents && Plasmoid.configuration.wheelEnabled
onWheel: event => {
// magic number 15 for common "one scroll"
// See https://doc.qt.io/qt-6/qml-qtquick-wheelhandler.html#rotation-prop
let increment = 0;
while (rotation >= 15) {
rotation -= 15;
increment++;
}
while (rotation <= -15) {
rotation += 15;
increment--;
}
const anchor = dropArea.target.childAt(event.x, event.y);
while (increment !== 0) {
TaskTools.activateNextPrevTask(anchor, increment < 0, Plasmoid.configuration.wheelSkipMinimized, tasks);
increment += (increment < 0) ? 1 : -1;
}
}
}
}