Debian Codegraph @

src/app/qml/components/PathHistoryRow.qml

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2016, 2017 Stefano Verzegnassi
3 * Copyright (C) 2025 UBports Foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 import QtQuick 2.4
19 import Lomiri.Components 1.3
20 import filemanager.ubports 1.0
21
22 import "../backend"
23
24 // FIXME: Update model when a folder is removed from the path we have stored
25
26 ListView {
27 id: rootItem
28
29 implicitHeight: units.gu(4)
30
31 rightMargin: rootItem.width * 0.382
32
33 property FolderListModel folderModel
34
35 orientation: ListView.Horizontal
36 boundsBehavior: Flickable.StopAtBounds
37 clip: true
38
39 delegate: AbstractButton {
40 property bool isCurrentFolder: ListView.isCurrentItem
41 property bool isLastItem: model.index == (rootItem.count - 1)
42 property string name: model.name
43
44 height: rootItem.height
45 style: delegateStyle // Style defined below
46
47 onClicked: folderModel.path = model.path.replace("~", folderModel.model.homePath())
48 }
49
50 model: ListModel {
51 id: internal
52 property string storedPath
53
54 function updateModel() {
55 var current_path = folderModel.path.toString()
56 var path_slices = []
57
58 var local_prefix = "/"
59 var home_prefix = "~"
60 var smb_prefix = "smb://"
61 var trash_prefix = "trash://"
62
63 current_path = current_path.replace(folderModel.model.homePath(), home_prefix)
64
65 // Check if current path is included in the path we stored previously. This way
66 // we still keep a trace of older nodes
67 var stored_path = internal.storedPath
68 var cur = stored_path.indexOf(current_path)
69 if (stored_path && cur == 0) {
70 cur += current_path.length
71 current_path = current_path + stored_path.slice(cur)
72 }
73
74 if (current_path.indexOf(smb_prefix) > -1) {
75 current_path = current_path.replace(smb_prefix, "")
76 path_slices.push({ name: smb_prefix, path: smb_prefix })
77 }
78
79 else if (current_path.indexOf(trash_prefix) > -1) {
80 current_path = current_path.replace(trash_prefix, "")
81 path_slices.push({ name: trash_prefix, path: trash_prefix })
82 }
83
84 else if (current_path.indexOf(home_prefix) > -1) {
85 current_path = current_path.replace(home_prefix, "")
86 current_path = current_path.slice(1)
87 path_slices.push({ name: home_prefix, path: home_prefix })
88
89 }
90
91 else {
92 current_path = current_path.slice(1)
93 path_slices.push({ name: local_prefix, path: local_prefix })
94 }
95
96
97 var splitted_path = current_path.split("/")
98
99 // Sanitize splitted_path. Remove empty strings from array
100 splitted_path = splitted_path.filter(Boolean)
101
102 // Clear the model.
103 internal.clear()
104
105 var highlight_index = 0
106 for (var i = 0; i < splitted_path.length; ++i) {
107 var f = splitted_path[i]
108
109 var objName = f
110 var objPath = ""
111
112 if (path_slices[path_slices.length - 1].path != smb_prefix
113 && path_slices[path_slices.length - 1].path != local_prefix) {
114 objPath = path_slices[path_slices.length - 1].path + "/" + f
115 } else {
116 objPath = path_slices[path_slices.length - 1].path + f
117 }
118
119 path_slices.push({ name: objName, path: objPath })
120
121 // Set the current item, if necessary
122 if (objPath == folderModel.path.toString().replace(folderModel.model.homePath(), home_prefix)) {
123 highlight_index = i + 1
124 }
125 }
126
127 internal.append(path_slices)
128 rootItem.currentIndex = highlight_index
129 internal.storedPath = path_slices[path_slices.length - 1].path
130 }
131 }
132
133 // Ensure the currentItem is always visible
134 onCurrentIndexChanged: {
135 positionViewTimerWorkaround.restart()
136 }
137
138 Timer {
139 id: positionViewTimerWorkaround
140
141 function __gotoIndex(i) {
142 if (currentItem.x > rootItem.contentX &&
143 currentItem.x + currentItem.height < rootItem.contentX + rootItem.width)
144 return;
145
146 var pX = rootItem.contentX
147 var dpX
148 rootItem.positionViewAtIndex(i, ListView.Center)
149 dpX = rootItem.contentX
150 positionAnimation.from = pX
151 positionAnimation.to = dpX
152 positionAnimation.running = true
153 }
154
155 interval: 1
156 onTriggered: __gotoIndex(currentIndex)
157
158 property QtObject positionAnimation: LomiriNumberAnimation {
159 target: rootItem
160 property: "contentX"
161 duration: LomiriAnimation.SnapDuration
162 }
163 }
164
165 // Subscribe to folderModel 'folder' changes
166 Component.onCompleted: internal.updateModel()
167 Connections {
•168 target: folderModel
169 function onPathChanged(newPath) {
170 internal.updateModel()
171 }
172 }
173
174 /*
175 * Scroll helpers
176 * On desktop, or when a mouse is connected to the device, we may want to make easier
177 * for the user to scroll the path bar.
178 * For that reason, we add a MouseArea which automatically scrolls the content when
179 * the mouse cursor hovers rootItem's boundaries.
180 */
181
182 MouseArea {
183 id: scrollLeftArea
184 width: units.gu(4)
185 anchors {
186 left: parent.left
187 top: parent.top
188 bottom: parent.bottom
189 }
190
191 // TODO: Uncomment when UITK will support this property
192 enabled: rootItem.contentX != 0 // && QuickUtils.mouseAttached
193 visible: enabled // Don't steal mouse event when not enabled
194
195 hoverEnabled: true
196 SmoothedAnimation {
197 target: rootItem
198 property: "contentX"
199 to: 0
200 velocity: units.gu(30)
201 running: scrollLeftArea.containsMouse
202 }
203 }
204
205 MouseArea {
206 id: scrollRightArea
207 width: units.gu(4)
208 anchors {
209 right: parent.right
210 top: parent.top
211 bottom: parent.bottom
212 }
213
214 // TODO: Uncomment when UITK will support this property
215 enabled: rootItem.contentX != parseInt(rootItem.contentWidth - rootItem.width) && (rootItem.contentWidth > rootItem.width) // && QuickUtils.mouseAttached
216 visible: enabled // Don't steal mouse event when not enabled
217
218 hoverEnabled: true
219 SmoothedAnimation {
220 target: rootItem
221 property: "contentX"
222 to: contentWidth - width
223 velocity: units.gu(30)
224 running: scrollRightArea.containsMouse
225 }
226 }
227
228 /*
229 * Delegate style
230 * AbstractButton inherits StyledItem. This allows us to keep logic and style
231 * separated, improving the readability of the code.
232 */
233
234 Rectangle {
235 anchors.fill: parent
236 color: theme.palette.normal.base
237 z: -1000
238 }
239
240 Component {
241 id: delegateStyle
242 Item {
243 property color inactiveColor: theme.palette.normal.backgroundText
244 property color activeColor: theme.palette.selected.positionText
245
246 property int labelMaximumWidth: units.gu(24)
247 property int labelTextSize: Label.Small /*Medium*/
248
249 property string dividerIconName: "go-next"
250 property int dividerIconSize: units.gu(1.5)
251
252 implicitWidth: delegateRow.width + delegateRow.spacing
253
254 PathArrowBackground {
255 anchors.fill: parent
256 anchors.margins: units.dp(1)
257 anchors.leftMargin: units.dp(1) - arrowWidth
258 arrowWidth: units.gu(1)
259 color: styledItem.pressed ? theme.palette.highlighted.background
260 : styledItem.hovered ? theme.palette.selected.background : theme.palette.normal.background
261 }
262
263 Row {
264 id: delegateRow
265 anchors.verticalCenter: parent ? parent.verticalCenter : undefined
266 //spacing: units.gu(0.5)
267
268 property bool isHomePath: styledItem.name == "~"
269 property bool isRootPath: styledItem.name == "/"
270 property bool isTrashPath: styledItem.name == "trash://"
271 property bool isSmbPath: styledItem.name == "smb://"
272
273 Item {
274 height: parent.height
275 width: units.gu(2.25)
276 /* SPACER */
277 }
278
279 Label {
280 anchors.verticalCenter: parent ? parent.verticalCenter : undefined
281 width: Math.min(implicitWidth, labelMaximumWidth)
282 textSize: labelTextSize
283 elide: Text.ElideMiddle
284 text: delIcon.visible ? "" : styledItem.name
285 color: styledItem.isCurrentFolder ? activeColor : inactiveColor
286 font.weight: styledItem.hovered ? Font.Normal : Font.Light
287 }
288
289 Icon {
290 id: delIcon
291 anchors.verticalCenter: parent ? parent.verticalCenter : undefined
292 width: visible ? units.gu(2) : 0
293 height: width
294 name: delegateRow.isRootPath
295 ? "computer-symbolic"
296 : delegateRow.isHomePath ? "go-home"
297 : delegateRow.isSmbPath ? "network-vpn" : "delete"
298 color: styledItem.isCurrentFolder ? activeColor : inactiveColor
299 visible: delegateRow.isRootPath || delegateRow.isHomePath || delegateRow.isTrashPath || delegateRow.isSmbPath
300 }
301
302 Item {
303 height: parent.height
304 width: units.gu(3.25)
305 /* SPACER */
306 }
307 }
308 }
309 }
310
311 /*
312 * Fade out ListView at boundaries.
313 * This is enabled only when if content is wider than the container, in
314 * order to suggest that it can be flipped.
315 * Also this works as hint for scrolling by mouse hovering.
316 *
317 * ref. http://stackoverflow.com/questions/13138868/qt-qml-fade-image-horizontally-i-e-from-left-to-right-not-whole-image
318 */
319
320 layer.enabled: true
321 layer.effect: ShaderEffect {
322 property int fadeOutEffectWidth: units.gu(4)
323 property real fadeOutEffectRelativeWidth: fadeOutEffectWidth / rootItem.width
324
325 property bool leftFadeOutEffectActive: scrollLeftArea.enabled
326 property bool rightFadeOutEffectActive: scrollRightArea.enabled
327
328 fragmentShader: "
329 varying highp vec2 qt_TexCoord0;
330 uniform lowp sampler2D source;
331
332 // Properties defined above
333 uniform lowp float fadeOutEffectRelativeWidth;
334 uniform bool leftFadeOutEffectActive;
335 uniform bool rightFadeOutEffectActive;
336
337 void main(void)
338 {
339 highp vec4 texture = texture2D(source, qt_TexCoord0);
340 lowp float alpha = 1.0;
341
342 if (leftFadeOutEffectActive && (qt_TexCoord0.x < fadeOutEffectRelativeWidth))
343 alpha = 0.25 + (qt_TexCoord0.x / fadeOutEffectRelativeWidth) * 0.75;
344
345 if (rightFadeOutEffectActive && (qt_TexCoord0.x > (1.0 - fadeOutEffectRelativeWidth)))
346 alpha = 0.25 + (1.0 - qt_TexCoord0.x) / fadeOutEffectRelativeWidth * 0.75;
347
348 gl_FragColor = texture * alpha;
349 }
350 "
351 }
352 }