Warning, /plasma/kdeplasma-addons/kwin/windowswitchers/compact/contents/ui/main.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  KWin - the KDE window manager
0003  This file is part of the KDE project.
0004 
0005  SPDX-FileCopyrightText: 2011 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007  SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 import QtQuick 2.15
0010 import QtQuick.Layouts 1.15
0011 import org.kde.plasma.core as PlasmaCore
0012 import org.kde.kirigami 2.20 as Kirigami
0013 import org.kde.ksvg 1.0 as KSvg
0014 import org.kde.plasma.components 3.0 as PlasmaComponents3
0015 import org.kde.kwin 3.0 as KWin
0016 
0017 KWin.TabBoxSwitcher {
0018     id: tabBox
0019     currentIndex: compactListView.currentIndex
0020 
0021     /**
0022     * Returns the caption with adjustments for minimized items.
0023     * @param caption the original caption
0024     * @param mimized whether the item is minimized
0025     * @return Caption adjusted for minimized state
0026     **/
0027     function itemCaption(caption, minimized) {
0028         if (minimized) {
0029             return "(" + caption + ")";
0030         }
0031         return caption;
0032     }
0033 
0034     TextMetrics {
0035         id: textMetrics
0036         property string longestCaption: tabBox.model.longestCaption()
0037         text: itemCaption(longestCaption, true)
0038         font.bold: true
0039     }
0040 
0041     onVisibleChanged: {
0042         if (visible) {
0043             // Window captions may have change completely
0044             textMetrics.longestCaption = tabBox.model.longestCaption();
0045         }
0046     }
0047     onModelChanged: {
0048         textMetrics.longestCaption = tabBox.model.longestCaption();
0049     }
0050 
0051     PlasmaCore.Dialog {
0052         id: dialog
0053         location: PlasmaCore.Types.Floating
0054         visible: tabBox.visible
0055         flags: Qt.X11BypassWindowManagerHint
0056         x: tabBox.screenGeometry.x + tabBox.screenGeometry.width * 0.5 - dialogMainItem.width * 0.5
0057         y: tabBox.screenGeometry.y + tabBox.screenGeometry.height * 0.5 - dialogMainItem.height * 0.5
0058 
0059         mainItem: Item {
0060             id: dialogMainItem
0061 
0062             property int optimalWidth: textMetrics.width + Kirigami.Units.iconSizes.small + 2 * Kirigami.Units.smallSpacing + hoverItem.margins.right + hoverItem.margins.left
0063             property int optimalHeight: compactListView.rowHeight * compactListView.count
0064             width: Math.min(Math.max(tabBox.screenGeometry.width * 0.2, optimalWidth), tabBox.screenGeometry.width * 0.8)
0065             height: Math.min(optimalHeight, tabBox.screenGeometry.height * 0.8)
0066             focus: true
0067 
0068             // just to get the margin sizes
0069             KSvg.FrameSvgItem {
0070                 id: hoverItem
0071                 imagePath: "widgets/viewitem"
0072                 prefix: "hover"
0073                 visible: false
0074             }
0075 
0076             ListView {
0077                 id: compactListView
0078 
0079                 // the maximum text width + icon item width (32 + 4 margin) + margins for hover item
0080                 property int rowHeight: Math.max(Kirigami.Units.iconSizes.small, textMetrics.height + hoverItem.margins.top + hoverItem.margins.bottom)
0081 
0082                 anchors.fill: parent
0083                 clip: true
0084 
0085                 model: tabBox.model
0086                 delegate: RowLayout {
0087 
0088                     width: compactListView.width
0089                     height: compactListView.rowHeight
0090                     opacity: minimized ? 0.6 : 1.0
0091 
0092                     spacing: 2 * Kirigami.Units.smallSpacing
0093 
0094                     Kirigami.Icon {
0095                         id: iconItem
0096                         source: model.icon
0097                         Layout.preferredWidth: Kirigami.Units.iconSizes.small
0098                         Layout.preferredHeight: Kirigami.Units.iconSizes.small
0099                         Layout.leftMargin: hoverItem.margins.left
0100                     }
0101                     PlasmaComponents3.Label {
0102                         id: captionItem
0103                         horizontalAlignment: Text.AlignLeft
0104                         verticalAlignment: Text.AlignBottom
0105                         text: itemCaption(caption, minimized)
0106                         textFormat: Text.PlainText
0107                         font.weight: index === compactListView.currentIndex ? Font.Bold : Font.Normal
0108                         elide: Text.ElideMiddle
0109                         Layout.fillWidth: true
0110                         Layout.rightMargin: hoverItem.margins.right
0111                         Layout.topMargin: hoverItem.margins.top
0112                         Layout.bottomMargin: hoverItem.margins.bottom
0113                     }
0114                     TapHandler {
0115                         onSingleTapped: {
0116                             if (index === compactListView.currentIndex) {
0117                                 compactListView.model.activate(index);
0118                                 return;
0119                             }
0120                             compactListView.currentIndex = index;
0121                         }
0122                         onDoubleTapped: compactListView.model.activate(index)
0123                     }
0124                 }
0125                 highlight: KSvg.FrameSvgItem {
0126                     imagePath: "widgets/viewitem"
0127                     prefix: "hover"
0128                     width: compactListView.width
0129                 }
0130                 highlightMoveDuration: 0
0131                 highlightResizeDuration: 0
0132                 boundsBehavior: Flickable.StopAtBounds
0133                 Connections {
0134                     target: tabBox
0135                     function onCurrentIndexChanged() {compactListView.currentIndex = tabBox.currentIndex;}
0136                 }
0137             }
0138             /*
0139             * Key navigation on outer item for two reasons:
0140             * @li we have to emit the change signal
0141             * @li on multiple invocation it does not work on the list view. Focus seems to be lost.
0142             **/
0143             Keys.onPressed: {
0144                 if (event.key == Qt.Key_Up) {
0145                     compactListView.decrementCurrentIndex();
0146                 } else if (event.key == Qt.Key_Down) {
0147                     compactListView.incrementCurrentIndex();
0148                 }
0149             }
0150         }
0151     }
0152 }