| 1 |
/* |
| 2 |
KWin - the KDE window manager |
| 3 |
This file is part of the KDE project. |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2020 Chris Holland <[email protected]> |
| 6 |
SPDX-FileCopyrightText: 2023 Nate Graham <[email protected]> |
| 7 |
|
| 8 |
SPDX-License-Identifier: GPL-2.0-or-later |
| 9 |
*/ |
| 10 |
|
| 11 |
import QtQuick |
| 12 |
import QtQuick.Layouts 1.1 |
| 13 |
import org.kde.plasma.core as PlasmaCore |
| 14 |
import org.kde.ksvg 1.0 as KSvg |
| 15 |
import org.kde.plasma.components 3.0 as PlasmaComponents3 |
| 16 |
import org.kde.kwin 3.0 as KWin |
| 17 |
import org.kde.kirigami 2.20 as Kirigami |
| 18 |
|
| 19 |
KWin.TabBoxSwitcher { |
| 20 |
id: tabBox |
| 21 |
|
| 22 |
Instantiator { |
| 23 |
active: tabBox.visible |
| 24 |
delegate: PlasmaCore.Dialog { |
| 25 |
location: PlasmaCore.Types.Floating |
| 26 |
visible: true |
| 27 |
flags: Qt.Popup | Qt.X11BypassWindowManagerHint |
| 28 |
x: tabBox.screenGeometry.x + tabBox.screenGeometry.width * 0.5 - dialogMainItem.width * 0.5 |
| 29 |
y: tabBox.screenGeometry.y + tabBox.screenGeometry.height * 0.5 - dialogMainItem.height * 0.5 |
| 30 |
|
| 31 |
mainItem: FocusScope { |
| 32 |
id: dialogMainItem |
| 33 |
|
| 34 |
focus: true |
| 35 |
|
| 36 |
property int maxWidth: tabBox.screenGeometry.width * 0.9 |
| 37 |
property int maxHeight: tabBox.screenGeometry.height * 0.7 |
| 38 |
property real screenFactor: tabBox.screenGeometry.width / tabBox.screenGeometry.height |
| 39 |
property int maxGridColumnsByWidth: Math.floor(maxWidth / thumbnailGridView.cellWidth) |
| 40 |
|
| 41 |
property int gridColumns: { // Simple greedy algorithm |
| 42 |
// respect screenGeometry |
| 43 |
const c = Math.min(thumbnailGridView.count, maxGridColumnsByWidth); |
| 44 |
const residue = thumbnailGridView.count % c; |
| 45 |
if (residue == 0) { |
| 46 |
return c; |
| 47 |
} |
| 48 |
// start greedy recursion |
| 49 |
return columnCountRecursion(c, c, c - residue); |
| 50 |
} |
| 51 |
|
| 52 |
property int gridRows: Math.ceil(thumbnailGridView.count / gridColumns) |
| 53 |
property int optimalWidth: thumbnailGridView.cellWidth * gridColumns |
| 54 |
property int optimalHeight: thumbnailGridView.cellHeight * gridRows |
| 55 |
width: Math.min(Math.max(thumbnailGridView.cellWidth, optimalWidth), maxWidth) |
| 56 |
height: Math.min(Math.max(thumbnailGridView.cellHeight, optimalHeight), maxHeight) |
| 57 |
|
| 58 |
clip: true |
| 59 |
|
| 60 |
// Step for greedy algorithm |
| 61 |
function columnCountRecursion(prevC, prevBestC, prevDiff) { |
| 62 |
const c = prevC - 1; |
| 63 |
|
| 64 |
// don't increase vertical extent more than horizontal |
| 65 |
// and don't exceed maxHeight |
| 66 |
if (prevC * prevC <= thumbnailGridView.count + prevDiff || |
| 67 |
maxHeight < Math.ceil(thumbnailGridView.count / c) * thumbnailGridView.cellHeight) { |
| 68 |
return prevBestC; |
| 69 |
} |
| 70 |
const residue = thumbnailGridView.count % c; |
| 71 |
// halts algorithm at some point |
| 72 |
if (residue == 0) { |
| 73 |
return c; |
| 74 |
} |
| 75 |
// empty slots |
| 76 |
const diff = c - residue; |
| 77 |
|
| 78 |
// compare it to previous count of empty slots |
| 79 |
if (diff < prevDiff) { |
| 80 |
return columnCountRecursion(c, c, diff); |
| 81 |
} else if (diff == prevDiff) { |
| 82 |
// when it's the same try again, we'll stop early enough thanks to the landscape mode condition |
| 83 |
return columnCountRecursion(c, prevBestC, diff); |
| 84 |
} |
| 85 |
// when we've found a local minimum choose this one (greedy) |
| 86 |
return columnCountRecursion(c, prevBestC, diff); |
| 87 |
} |
| 88 |
|
| 89 |
// Just to get the margin sizes |
| 90 |
KSvg.FrameSvgItem { |
| 91 |
id: hoverItem |
| 92 |
imagePath: "widgets/viewitem" |
| 93 |
prefix: "hover" |
| 94 |
visible: false |
| 95 |
} |
| 96 |
|
| 97 |
GridView { |
| 98 |
id: thumbnailGridView |
| 99 |
anchors.fill: parent |
| 100 |
focus: true |
| 101 |
model: tabBox.model |
| 102 |
currentIndex: tabBox.currentIndex |
| 103 |
|
| 104 |
readonly property int iconSize: Kirigami.Units.iconSizes.huge |
| 105 |
readonly property int captionRowHeight: Kirigami.Units.gridUnit * 2 |
| 106 |
readonly property int columnSpacing: Kirigami.Units.gridUnit |
| 107 |
readonly property int thumbnailWidth: Kirigami.Units.gridUnit * 16 |
| 108 |
readonly property int thumbnailHeight: thumbnailWidth * (1.0/dialogMainItem.screenFactor) |
| 109 |
cellWidth: hoverItem.margins.left + thumbnailWidth + hoverItem.margins.right |
| 110 |
cellHeight: hoverItem.margins.top + captionRowHeight + thumbnailHeight + hoverItem.margins.bottom |
| 111 |
|
| 112 |
keyNavigationWraps: true |
| 113 |
highlightMoveDuration: 0 |
| 114 |
|
| 115 |
delegate: MouseArea { |
| 116 |
id: thumbnailGridItem |
| 117 |
width: thumbnailGridView.cellWidth |
| 118 |
height: thumbnailGridView.cellHeight |
| 119 |
focus: GridView.isCurrentItem |
| 120 |
hoverEnabled: true |
| 121 |
|
| 122 |
Accessible.name: model.caption |
| 123 |
Accessible.role: Accessible.ListItem |
| 124 |
|
| 125 |
onClicked: { |
| 126 |
tabBox.model.activate(index); |
| 127 |
} |
| 128 |
|
| 129 |
ColumnLayout { |
| 130 |
id: columnLayout |
| 131 |
z: 0 |
| 132 |
spacing: thumbnailGridView.columnSpacing |
| 133 |
anchors.fill: parent |
| 134 |
anchors.leftMargin: hoverItem.margins.left * 2 |
| 135 |
anchors.topMargin: hoverItem.margins.top * 2 |
| 136 |
anchors.rightMargin: hoverItem.margins.right * 2 |
| 137 |
anchors.bottomMargin: hoverItem.margins.bottom * 2 |
| 138 |
|
| 139 |
|
| 140 |
// KWin.WindowThumbnail needs a container |
| 141 |
// otherwise it will be drawn the same size as the parent ColumnLayout |
| 142 |
Item { |
| 143 |
Layout.fillWidth: true |
| 144 |
Layout.fillHeight: true |
| 145 |
|
| 146 |
KWin.WindowThumbnail { |
| 147 |
anchors.fill: parent |
| 148 |
wId: windowId |
| 149 |
} |
| 150 |
|
| 151 |
Kirigami.Icon { |
| 152 |
anchors.horizontalCenter: parent.horizontalCenter |
| 153 |
anchors.verticalCenter: parent.bottom |
| 154 |
anchors.verticalCenterOffset: Math.round(-thumbnailGridView.iconSize / 4) |
| 155 |
width: thumbnailGridView.iconSize |
| 156 |
height: thumbnailGridView.iconSize |
| 157 |
|
| 158 |
source: model.icon |
| 159 |
} |
| 160 |
|
| 161 |
PlasmaComponents3.ToolButton { |
| 162 |
id: closeButton |
| 163 |
anchors { |
| 164 |
right: parent.right |
| 165 |
top: parent.top |
| 166 |
// Deliberately touch the inner edges of the frame |
| 167 |
rightMargin: -columnLayout.anchors.rightMargin |
| 168 |
topMargin: -columnLayout.anchors.topMargin |
| 169 |
} |
| 170 |
visible: model.closeable && typeof tabBox.model.close !== 'undefined' && |
| 171 |
(thumbnailGridItem.containsMouse |
| 172 |
|| closeButton.hovered |
| 173 |
|| thumbnailGridItem.focus |
| 174 |
|| Kirigami.Settings.tabletMode |
| 175 |
|| Kirigami.Settings.hasTransientTouchInput |
| 176 |
) |
| 177 |
icon.name: 'window-close-symbolic' |
| 178 |
onClicked: { |
| 179 |
tabBox.model.close(index); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
PlasmaComponents3.Label { |
| 185 |
Layout.fillWidth: true |
| 186 |
text: model.caption |
| 187 |
font.weight: thumbnailGridItem.focus ? Font.Bold : Font.Normal |
| 188 |
horizontalAlignment: Text.AlignHCenter |
| 189 |
verticalAlignment: Text.AlignVCenter |
| 190 |
textFormat: Text.PlainText |
| 191 |
elide: Text.ElideRight |
| 192 |
} |
| 193 |
} |
| 194 |
} // GridView.delegate |
| 195 |
|
| 196 |
highlight: KSvg.FrameSvgItem { |
| 197 |
imagePath: "widgets/viewitem" |
| 198 |
prefix: "hover" |
| 199 |
} |
| 200 |
|
| 201 |
onCurrentIndexChanged: tabBox.currentIndex = thumbnailGridView.currentIndex; |
| 202 |
} // GridView |
| 203 |
|
| 204 |
Kirigami.PlaceholderMessage { |
| 205 |
anchors.centerIn: parent |
| 206 |
width: parent.width - Kirigami.Units.largeSpacing * 2 |
| 207 |
icon.source: "edit-none" |
| 208 |
text: i18ndc("kwin_x11", "@info:placeholder no entries in the task switcher", "No open windows") |
| 209 |
visible: thumbnailGridView.count === 0 |
| 210 |
} |
| 211 |
|
| 212 |
Keys.onPressed: { |
| 213 |
if (event.key == Qt.Key_Left) { |
| 214 |
thumbnailGridView.moveCurrentIndexLeft(); |
| 215 |
} else if (event.key == Qt.Key_Right) { |
| 216 |
thumbnailGridView.moveCurrentIndexRight(); |
| 217 |
} else if (event.key == Qt.Key_Up) { |
| 218 |
thumbnailGridView.moveCurrentIndexUp(); |
| 219 |
} else if (event.key == Qt.Key_Down) { |
| 220 |
thumbnailGridView.moveCurrentIndexDown(); |
| 221 |
} else { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
thumbnailGridView.currentIndexChanged(thumbnailGridView.currentIndex); |
| 226 |
} |
| 227 |
} // Dialog.mainItem |
| 228 |
} // Dialog |
| 229 |
} // Instantiator |
| 230 |
} |