aerothemeplasma/plasma/plasmoids/io.gitgud.wackyideas.SevenStart/contents/ui/FavoritesView.qml

188 lines
5.8 KiB
QML
Raw Normal View History

2023-09-27 17:50:10 +00:00
/*
Copyright (C) 2011 Martin Gräßlin <mgraesslin@kde.org>
Copyright (C) 2012 Marco Martin <mart@kde.org>
Copyright 2014 Sebastian Kügler <sebas@kde.org>
Copyright (C) 2015-2018 Eike Hein <hein@kde.org>
Copyright (C) 2016 Jonathan Liu <net147@gmail.com>
Copyright (C) 2016 Kai Uwe Broulik <kde@privat.broulik.de>
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 2 of the License, or
(at your option) 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import QtQuick 2.0
import org.kde.kquickcontrolsaddons 2.0 as KQuickControlsAddons
2024-08-09 01:20:25 +00:00
import org.kde.plasma.plasmoid
2023-09-27 17:50:10 +00:00
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras
2024-08-09 01:20:25 +00:00
import org.kde.plasma.components as PlasmaComponents
2023-09-27 17:50:10 +00:00
import org.kde.draganddrop 2.0
import org.kde.plasma.private.kicker 0.1 as Kicker
Item {
property ListView listView: favoritesView.listView
2024-08-09 01:20:25 +00:00
function activateCurrentIndex() {
favoritesView.currentItem.activate();
}
2023-09-27 17:50:10 +00:00
function decrementCurrentIndex() {
2024-08-09 01:20:25 +00:00
if (favoritesView.currentIndex == 0)
return;
2023-09-27 17:50:10 +00:00
favoritesView.decrementCurrentIndex();
}
2024-08-09 01:20:25 +00:00
function getFavoritesCount() {
return favoritesView.count;
}
2023-09-27 17:50:10 +00:00
function incrementCurrentIndex() {
2024-08-09 01:20:25 +00:00
var tempIndex = favoritesView.currentIndex + 1;
if (tempIndex >= favoritesView.count) {
2023-09-27 17:50:10 +00:00
favoritesView.currentIndex = -1;
2024-08-09 01:20:25 +00:00
if (Plasmoid.configuration.showRecentsView) {
2023-09-27 17:50:10 +00:00
root.m_recents.focus = true;
root.m_recents.currentIndex = 0;
} else {
root.m_showAllButton.focus = true;
}
return;
}
favoritesView.incrementCurrentIndex();
}
2024-08-09 01:20:25 +00:00
// QQuickItem::isAncestorOf is not invokable...
function isChildOf(item, parent) {
if (!item || !parent) {
return false;
}
if (item.parent === parent) {
return true;
}
return isChildOf(item, item.parent);
2023-09-27 17:50:10 +00:00
}
function openContextMenu() {
favoritesView.currentItem.openActionMenu();
}
2024-08-09 01:20:25 +00:00
function resetCurrentIndex() {
favoritesView.currentIndex = -1;
2023-09-27 17:50:10 +00:00
}
function setCurrentIndex() {
favoritesView.currentIndex = 0;
}
2024-08-09 01:20:25 +00:00
KeyNavigation.tab: Plasmoid.configuration.showRecentsView ? root.m_recents : root.m_showAllButton
//anchors.fill: parent
anchors.topMargin: Kirigami.Units.smallSpacing
objectName: "FavoritesView"
2023-09-27 17:50:10 +00:00
2024-08-09 01:20:25 +00:00
Keys.onPressed: event => {
if (event.key == Qt.Key_Up) {
decrementCurrentIndex();
} else if (event.key == Qt.Key_Down) {
incrementCurrentIndex();
} else if (event.key == Qt.Key_Return) {
activateCurrentIndex();
} else if (event.key == Qt.Key_Menu) {
openContextMenu();
2023-09-27 17:50:10 +00:00
}
2024-08-09 01:20:25 +00:00
}
onFocusChanged: {
if (focus)
setCurrentIndex();
else
resetCurrentIndex();
2023-09-27 17:50:10 +00:00
}
DropArea {
property int startRow: -1
function syncTarget(event) {
if (favoritesView.animating) {
return;
}
var pos = mapToItem(listView.contentItem, event.x, event.y);
var above = listView.itemAt(pos.x, pos.y);
var source = kickoff.dragSource;
if (above && above !== source && isChildOf(source, favoritesView)) {
favoritesView.model.moveRow(source.itemIndex, above.itemIndex);
// itemIndex changes directly after moving,
// we can just set the currentIndex to it then.
favoritesView.currentIndex = source.itemIndex;
}
}
2024-08-09 01:20:25 +00:00
anchors.fill: parent
enabled: plasmoid.immutability !== PlasmaCore.Types.SystemImmutable
2023-09-27 17:50:10 +00:00
onDragEnter: {
syncTarget(event);
startRow = favoritesView.currentIndex;
}
onDragMove: syncTarget(event)
}
Transition {
id: moveTransition
2024-08-09 01:20:25 +00:00
SequentialAnimation {
PropertyAction {
property: "animating"
target: favoritesView
value: true
}
2023-09-27 17:50:10 +00:00
NumberAnimation {
duration: favoritesView.animationDuration
easing.type: Easing.OutQuad
2024-08-09 01:20:25 +00:00
properties: "x, y"
}
PropertyAction {
property: "animating"
target: favoritesView
value: false
2023-09-27 17:50:10 +00:00
}
}
}
Connections {
function onExpandedChanged() {
if (!plasmoid.expanded) {
favoritesView.currentIndex = -1;
}
}
2024-08-09 01:20:25 +00:00
target: kicker
}
2023-09-27 17:50:10 +00:00
KickoffListView {
id: favoritesView
property bool animating: false
property int animationDuration: resetAnimationDurationTimer.interval
2024-08-09 01:20:25 +00:00
anchors.fill: parent
2023-09-27 17:50:10 +00:00
interactive: contentHeight > height
2024-08-09 01:20:25 +00:00
model: globalFavorites
2023-09-27 17:50:10 +00:00
move: moveTransition
moveDisplaced: moveTransition
onCountChanged: {
animationDuration = 0;
resetAnimationDurationTimer.start();
}
}
Timer {
id: resetAnimationDurationTimer
interval: 150
onTriggered: favoritesView.animationDuration = interval - 20
}
}