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

137 lines
4.1 KiB
QML
Raw Normal View History

2023-08-24 22:32:11 +00:00
import QtQuick 2.4
2024-08-09 01:20:25 +00:00
import QtQuick.Controls
2023-08-24 22:32:11 +00:00
import QtQuick.Layouts 1.1
2024-08-09 01:20:25 +00:00
import QtQuick.Dialogs
2023-08-24 22:32:11 +00:00
import QtQuick.Window 2.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
2024-08-09 01:20:25 +00:00
import org.kde.plasma.components as PlasmaComponents
2023-08-24 22:32:11 +00:00
import org.kde.plasma.extras 2.0 as PlasmaExtras
2024-08-09 01:20:25 +00:00
import org.kde.ksvg as KSvg
import org.kde.kirigami as Kirigami
Item {
2023-08-24 22:32:11 +00:00
id: sidePanelDelegate
objectName: "SidePanelItemDelegate"
2024-08-09 01:20:25 +00:00
property int iconSizeSide: Kirigami.Units.iconSizes.smallMedium
2023-08-24 22:32:11 +00:00
property string itemText: ""
property string itemIcon: ""
property string executableString: ""
property bool executeProgram: false
2024-08-09 01:20:25 +00:00
property alias textLabel: label
//text: itemText
2023-08-24 22:32:11 +00:00
//icon: itemIcon
2024-08-09 01:20:25 +00:00
width: label.implicitWidth + Kirigami.Units.largeSpacing*2
//Layout.preferredWidth: label.implicitWidth
height: 33
2023-08-24 22:32:11 +00:00
KeyNavigation.backtab: findPrevious();
KeyNavigation.tab: findNext();
2024-08-09 01:20:25 +00:00
function findItem() {
for(var i = 1; i < parent.visibleChildren.length-1; i++) {
if(sidePanelDelegate == parent.visibleChildren[i])
return i;
}
return -1;
}
2023-08-24 22:32:11 +00:00
function findPrevious() {
2024-08-09 01:20:25 +00:00
var i = findItem()-1;
if(i < 1) {
2023-08-24 22:32:11 +00:00
return root.m_searchField;
}
2024-01-20 02:08:06 +00:00
if(parent.visibleChildren[i].objectName == "SidePanelItemSeparator") {
i--;
}
2023-08-24 22:32:11 +00:00
return parent.visibleChildren[i];
}
function findNext() {
2024-08-09 01:20:25 +00:00
var i = findItem()+1;
if(i >= parent.visibleChildren.length-1) {
2023-08-24 22:32:11 +00:00
return root.m_shutDownButton;
}
2024-01-20 02:08:06 +00:00
if(parent.visibleChildren[i].objectName == "SidePanelItemSeparator") {
i++;
}
2023-08-24 22:32:11 +00:00
return parent.visibleChildren[i];
}
2024-08-09 01:20:25 +00:00
Keys.onPressed: event => {
2023-08-24 22:32:11 +00:00
if(event.key == Qt.Key_Return) {
sidePanelMouseArea.clicked(null);
} else if(event.key == Qt.Key_Up) {
2024-08-09 01:20:25 +00:00
//console.log(findPrevious());
2023-08-24 22:32:11 +00:00
findPrevious().focus = true;
} else if(event.key == Qt.Key_Down) {
2024-08-09 01:20:25 +00:00
//console.log(findNext());
2023-08-24 22:32:11 +00:00
findNext().focus = true;
} else if(event.key == Qt.Key_Left) {
var pos = parent.mapToItem(mainFocusScope, sidePanelDelegate.x, sidePanelDelegate.y);
2024-08-09 01:20:25 +00:00
var obj = mainFocusScope.childAt(Kirigami.Units.smallSpacing*10, pos.y);
2023-08-24 22:32:11 +00:00
if(obj.objectName == "") {
obj = root.m_recents;
}
obj.focus = true;
}
}
//For some reason this is the only thing that prevents a width reduction bug, despite it being UB in QML
2024-08-09 01:20:25 +00:00
/*anchors.left: parent.left;
anchors.right: parent.right;*/
2023-08-24 22:32:11 +00:00
2024-08-09 01:20:25 +00:00
KSvg.FrameSvgItem {
2023-08-24 22:32:11 +00:00
id: itemFrame
z: -1
opacity: sidePanelMouseArea.containsMouse || parent.focus
anchors.fill: parent
imagePath: Qt.resolvedUrl("svgs/sidebaritem.svg")
prefix: "menuitem"
}
2024-08-09 01:20:25 +00:00
PlasmaComponents.Label {
id: label
wrapMode: Text.NoWrap
//elide: Text.ElideRight
anchors.left: parent.left
anchors.leftMargin: Kirigami.Units.smallSpacing * 2
anchors.verticalCenter: sidePanelDelegate.verticalCenter
anchors.verticalCenterOffset: -1
style: Text.Sunken
styleColor: "transparent"
text: itemText
}
2023-08-24 22:32:11 +00:00
onFocusChanged: {
/*if(focus) {
root.m_sidebarIcon.source = itemIcon;
} else {
root.m_sidebarIcon.source = "";
}*/
if(root.m_delayTimer.running) root.m_delayTimer.restart();
else root.m_delayTimer.start();
}
MouseArea {
id: sidePanelMouseArea
enabled: !root.hoverDisabled
acceptedButtons: Qt.LeftButton
onEntered: {
sidePanelDelegate.focus = true;
}
onExited: {
sidePanelDelegate.focus = false;
}
onClicked: {
root.visible = false;
if(executeProgram)
executable.exec(executableString);
else {
Qt.openUrlExternally(executableString);
}
}
hoverEnabled: true
anchors.fill: parent
}
}