Debian Codegraph @

src/tabbox/switchers/thumbnail_grid/contents/ui/main.qml

raw
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
13 import org.kde.plasma.core as PlasmaCore
14 import org.kde.ksvg as KSvg
15 import org.kde.plasma.components as PlasmaComponents3
16 import org.kde.kwin as KWin
17 import org.kde.kirigami 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.Button {
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 horizontalAlignment: Text.AlignHCenter
188 verticalAlignment: Text.AlignVCenter
189 textFormat: Text.PlainText
190 elide: Text.ElideRight
191 }
192 }
193 } // GridView.delegate
194
195 highlight: KSvg.FrameSvgItem {
196 imagePath: "widgets/viewitem"
197 prefix: "hover"
198 }
199
200 onCurrentIndexChanged: tabBox.currentIndex = thumbnailGridView.currentIndex;
201 } // GridView
202
203 Kirigami.PlaceholderMessage {
204 anchors.centerIn: parent
205 width: parent.width - Kirigami.Units.largeSpacing * 2
206 icon.source: "edit-none"
207 text: i18ndc("kwin", "@info:placeholder no entries in the task switcher", "No open windows")
208 visible: thumbnailGridView.count === 0
209 }
210
211 Keys.onPressed: {
212 if (event.key == Qt.Key_Left) {
213 thumbnailGridView.moveCurrentIndexLeft();
214 } else if (event.key == Qt.Key_Right) {
215 thumbnailGridView.moveCurrentIndexRight();
216 } else if (event.key == Qt.Key_Up) {
217 thumbnailGridView.moveCurrentIndexUp();
218 } else if (event.key == Qt.Key_Down) {
219 thumbnailGridView.moveCurrentIndexDown();
220 } else {
221 return;
222 }
223
224 thumbnailGridView.currentIndexChanged(thumbnailGridView.currentIndex);
225 }
226 } // Dialog.mainItem
227
228 onSceneGraphError: () => {
229 // This slot is intentionally left blank, otherwise QtQuick may post a qFatal() message on a graphics reset.
230 }
231 } // Dialog
232 } // Instantiator
233 }