forked from karip/harbour-file-browser
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
DirectoryPage.qml
623 lines (560 loc) · 23.5 KB
/
DirectoryPage.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* This file is part of File Browser.
*
* SPDX-FileCopyrightText: 2013-2019 Kari Pihkala
* SPDX-FileCopyrightText: 2016 Joona Petrell
* SPDX-FileCopyrightText: 2019-2022 Mirian Margiani
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* File Browser is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* File Browser is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import harbour.file.browser.FileModel 1.0
import harbour.file.browser.Settings 1.0
import harbour.file.browser.FileClipboard 1.0
import "../components"
import "../js/paths.js" as Paths
import "../js/files.js" as Files
Page {
id: page
objectName: "DirectoryPage"
allowedOrientations: Orientation.All
property string dir: "/"
property bool remorsePopupActive: false // set to true when remorsePopup is active
property bool remorseItemActive: false // set to true when remorseItem is active (item level)
property alias progressPanel: progressPanel
property alias notificationPanel: notificationPanel
property string currentFilter: ""
// set to true when full dir path should be shown in page header
property bool fullPathShown: prefs.generalShowFullDirectoryPaths
// set to true to enable starting deep search when pressing 'Enter' in filter input
property bool quickSearchEnabled: prefs.generalDefaultFilterAction === "search"
property bool navMenuIconShown: prefs.generalShowNavigationMenuIcon
property bool sectionsEnabled: prefs.viewSortRole === "type"
property string viewState: { // state for list delegates
if (prefs.viewViewMode === "gallery") return "gallery"
else if (prefs.viewPreviewsShown) return "preview/" + prefs.viewPreviewsSize
else return ""
}
property int _baseIconSize: (viewState === '' || viewState === 'gallery') ? Theme.iconSizeSmall : _baseEntryHeight
property bool _thumbnailsEnabled: viewState !== '' && viewState !== 'gallery'
property int _baseEntryHeight: {
if (viewState === 'gallery') {
return Theme.itemSizeMedium
} else if (viewState === "preview/small") {
return Theme.itemSizeMedium
} else if (viewState === "preview/medium") {
return Theme.itemSizeExtraLarge
} else if (viewState === "preview/large") {
return width/3
} else if (viewState === "preview/huge") {
return width/3*2
} else {
return Theme.itemSizeSmall
}
}
property string _fnElide: prefs.generalFilenameElideMode
property int _nameTruncMode: _fnElide === 'fade' ? TruncationMode.Fade : TruncationMode.Elide
property int _nameElideMode: _nameTruncMode === TruncationMode.Fade ?
Text.ElideNone : (_fnElide === 'middle' ?
Text.ElideMiddle : Text.ElideRight)
signal clearViewFilter()
signal multiSelectionStarted(var index)
signal multiSelectionFinished(var index)
signal selectionChanged(var index)
signal markAsDoomed(var files) // to be used from other pages
onMarkAsDoomed: {
clearSelectedFiles()
fileModel.markAsDoomed(files)
}
DirectorySettings {
id: prefs
path: dir
}
FileModel {
id: fileModel
dir: page.dir
filterString: currentFilter
active: page.status === PageStatus.Active ||
page.status === PageStatus.Activating
}
RemorsePopup {
id: remorsePopup
onCanceled: remorsePopupActive = false
onTriggered: remorsePopupActive = false
}
SilicaListView {
id: fileList
anchors.fill: parent
anchors.bottomMargin: selectionPanel.visible ? selectionPanel.visibleSize : 0
clip: true
visible: true
opacity: (visible ? (busyIndicator.enabled ? Theme.opacityLow : 1.0) : 0.0)
Behavior on opacity { NumberAnimation { duration: 300 } }
model: fileModel
property bool useSmartScrollbar: fileList.count > 40
onUseSmartScrollbarChanged: refreshSmartScrollbar()
property Item _scrollbar: null
quickScroll: !useSmartScrollbar || !_scrollbar || fileList.count > 1000
VerticalScrollDecorator {
flickable: fileList
visible: !fileList.useSmartScrollbar || !fileList._scrollbar
}
function refreshSmartScrollbar() {
if (!useSmartScrollbar) {
if (!!_scrollbar) _scrollbar.deleteLater()
return
}
try {
_scrollbar = Qt.createQmlObject("
import QtQuick 2.0
import %1 1.0 as Private
Private.Scrollbar {
property int currentIndex: fileList.indexAt(fileList.contentX, fileList.contentY) + 2
text: fileList.currentSection
description: '%2'.arg(currentIndex).arg(fileList.count)
headerHeight: root.headerItem ? root.headerItem.height : 0
}".arg("Sailfish.Silica.private").arg("%1 / %2"), fileList, 'Scrollbar')
} catch (e) {
if (!_scrollbar) {
console.warn(e)
console.warn('[BUG] failed to load customized scrollbar')
console.warn('[BUG] this probably means the private API has changed')
}
}
}
Component.onCompleted: {
refreshSmartScrollbar()
}
PullDownMenu {
id: pullDownMenu
on_AtFinalPositionChanged: {
if (!_filterBlocked) {
filterField.forceActiveFocus()
}
}
onActiveChanged: filterField.enabled = active
enabled: !dirPopup.active
busy: fileModel.busy || fileModel.partlyBusy
// We explicitly block the filter field when a
// menu item is selected so the keyboard won't flicker
// while a new page is pushed on the stack.
// After returning the user has to enable the filter
// field once manually by clicking on it.
// This is the only solution that actually worked
// because on_AtFinalPositionChanged seems to be
// quite unreliable/buggy. (We shouldn't be using it anyways.)
property bool _filterBlocked: false
MenuItem {
//: This describes a page with settings for how things are displayed,
//: i.e. "preferences regarding the view" (and not "let's view the preferences").
text: qsTr("View Preferences")
onClicked: {
pullDownMenu._filterBlocked = true;
pageStack.push(Qt.resolvedUrl("ViewSettingsPage.qml"), { "dir": dir })
}
}
MenuItem {
text: qsTr("Create New...")
onClicked: {
pullDownMenu._filterBlocked = true;
var dialog = pageStack.push(Qt.resolvedUrl("CreateNewDialog.qml"),
{ path: page.dir })
dialog.accepted.connect(function() {
if (dialog.errorMessage !== "")
notificationPanel.showText(dialog.errorMessage, "")
})
}
}
MenuItem {
visible: FileClipboard.count > 0
text: qsTr("Paste") +
(FileClipboard.count > 0 ? (" (" + FileClipboard.count + ")") : "")
onClicked: {
if (remorsePopupActive) return;
Files.pasteFiles(page.dir, progressPanel, clearSelectedFiles);
}
}
Item {
height: Theme.itemSizeMedium
width: parent.width
TextField {
id: filterField
width: parent.width-2*clearFilterButton.width
height: Theme.itemSizeMedium
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
placeholderText: qsTr("Filter directory contents")
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
font.pixelSize: Theme.fontSizeMedium
horizontalAlignment: Text.AlignHCenter
background: null
onActiveFocusChanged: {
if (pullDownMenu._filterBlocked) {
pullDownMenu._filterBlocked = !activeFocus
}
if (!activeFocus) page.currentFilter = text
}
EnterKey.enabled: true
EnterKey.iconSource: "image://theme/icon-m-enter-accept"
EnterKey.onClicked: {
if (page.quickSearchEnabled) {
pageStack.push(Qt.resolvedUrl("SearchPage.qml"),
{ dir: page.dir, searchText: filterField.text,
startImmediately: true });
} else {
pullDownMenu.close();
}
}
Component.onCompleted: {
page.clearViewFilter.connect(function() {
text = "";
page.currentFilter = "";
})
}
}
IconButton {
id: clearFilterButton
anchors {
right: parent.right
rightMargin: Theme.horizontalPageMargin
leftMargin: Theme.horizontalPageMargin
top: filterField.top
}
width: Theme.iconSizeMedium
height: Theme.iconSizeMedium
icon.source: "image://theme/icon-m-clear"
enabled: filterField.enabled
opacity: filterField.text.length > 0 ? 1 : 0
Behavior on opacity { FadeAnimation {} }
onClicked: {
filterField.text = "";
pullDownMenu.close();
}
}
IconButton {
id: searchFilterButton
anchors {
left: parent.left
rightMargin: Theme.horizontalPageMargin
leftMargin: Theme.horizontalPageMargin
top: filterField.top
}
width: Theme.iconSizeMedium
height: Theme.iconSizeMedium
icon.source: "../images/icon-btn-search.png"
enabled: filterField.enabled
highlighted: down || page.quickSearchEnabled
opacity: filterField.text.length > 0 ? 1 : 0
Behavior on opacity { FadeAnimation {} }
onClicked: {
pageStack.push(Qt.resolvedUrl("SearchPage.qml"),
{ dir: page.dir, searchText: filterField.text,
startImmediately: true });
}
}
}
}
PushUpMenu {
busy: pullDownMenu.busy
MenuItem {
text: qsTr("Search")
onClicked: pageStack.push(Qt.resolvedUrl("SearchPage.qml"),
{ dir: page.dir })
}
MenuItem {
text: bookmark.marked ? qsTr("Remove bookmark") : qsTr("Add to bookmarks")
onDelayedClick: bookmark.toggle()
Bookmark {
id: bookmark
path: dir
}
}
MenuItem {
text: qsTr("Copy path to clipboard")
onClicked: Clipboard.text = page.dir
}
}
header: PageHeader {
id: header
title: Paths.formatPathForTitle(page.dir)
description: page.fullPathShown ? Paths.dirName(page.dir) : ""
Component.onCompleted: dirPopup.menuTop = y+height
leftMargin: (navMenuIconShown ? menuIcon.width + Theme.paddingMedium : 0)
+ Theme.horizontalPageMargin
_titleItem.elide: Text.ElideMiddle
IconButton {
id: menuIcon
visible: navMenuIconShown
anchors {
left: parent.left; leftMargin: Theme.horizontalPageMargin
rightMargin: Theme.paddingMedium
verticalCenter: parent.verticalCenter
}
opacity: Theme.opacityHigh
icon.source: "image://theme/icon-m-menu"
highlighted: true
}
BackgroundItem {
anchors.fill: parent
onClicked: dirPopup.show()
onPressAndHold: dirPopup.show()
}
}
footer: Column {
x: 0; width: page.width
height: footerSpacer.height +
(footerLabel.visible ? topSpacer.height + footerLabel.height : 0)
Spacer {
id: topSpacer
visible: footerLabel.visible
height: Theme.paddingLarge
}
Row {
id: footerLabel
visible: currentFilter !== ""
spacing: Theme.paddingLarge
anchors.right: parent.right
anchors.rightMargin: Theme.horizontalPageMargin
layoutDirection: Qt.RightToLeft
IconButton {
id: clearButton
anchors.verticalCenter: parent.verticalCenter
width: Theme.iconSizeMedium
onClicked: page.clearViewFilter()
icon.source: "image://theme/icon-m-clear"
}
Label {
anchors.verticalCenter: parent.verticalCenter
width: page.width-(2*Theme.horizontalPageMargin+parent.spacing+clearButton.width)
color: Theme.highlightColor
text: qsTr("filtered by: %1").arg(currentFilter)
// use elide instead of fade because it must be truncated on the right
truncationMode: TruncationMode.Elide
elide: "ElideRight"
horizontalAlignment: Text.AlignRight
verticalAlignment: Text.AlignVCenter
}
}
Spacer { id: footerSpacer }
}
section.property: {
console.error(prefs.viewSortRole, dir)
if (prefs.viewSortRole === "type") return "fileType"
// else if (prefs.viewSortRole === "modificationtime") return "lastModified"
// else if (prefs.viewSortRole === "name") return "filename"
else return ""
}
section.criteria: prefs.viewSortRole === "name" ?
ViewSection.FirstCharacter :
ViewSection.FullString
section.labelPositioning: ViewSection.InlineLabels
section.delegate: sectionsEnabled ? sectionDelegate : sectionDelegateDummy
Component {
id: sectionDelegate
Column {
spacing: 0
x: Theme.horizontalPageMargin
width: fileList.width - 2*Theme.horizontalPageMargin
Spacer { height: Theme.paddingLarge }
Label {
width: parent.width
text: section
font.pixelSize: Theme.fontSizeMedium
horizontalAlignment: Text.AlignRight
verticalAlignment: Text.AlignBottom
color: Theme.secondaryHighlightColor
}
Spacer { height: Theme.paddingMedium }
}
}
Component {
id: sectionDelegateDummy
Item {}
}
delegate: Component {
DirectoryPageEntry {}
}
// text if no files or error message
ViewPlaceholder {
id: statusMessage
enabled: !busyIndicator.enabled &&
(fileModel.fileCount === 0 || fileModel.errorMessage !== "")
text: (fileModel.errorMessage !== "" ?
fileModel.errorMessage :
(currentFilter !== "" ?
qsTr("No match", "as in “no files matched " +
"the current filter”") :
qsTr("Empty", "as in “this folder is empty”")))
hintText: (fileModel.errorMessage !== "") ?
"" : (currentFilter !== "" ?
qsTr("No files matched the filter.") :
qsTr("This directory contains no files."))
onEnabledChanged: {
// We explicitly disable the model to show an error
// message or the current status. The view behaves
// erratic when doing this in a binding on the model
// property. (It jumps to random positions...)
if (enabled) {
fileList.model = null
} else {
fileList.model = fileModel
}
}
}
}
PageBusyIndicator {
id: busyIndicator
enabled: fileModel.busy
running: enabled
}
DirectoryPopup {
id: dirPopup
directory: dir
flickable: fileList
}
Connections {
id: quickSelectionConnections
property int startIndex: -1
target: null
onSelectionChanged: {
quickSelectionConnections.target = null;
var startIndex = quickSelectionConnections.startIndex
multiSelectionFinished(startIndex);
if (index !== startIndex) {
fileModel.selectRange(startIndex, index);
}
quickSelectionConnections.startIndex = -1;
}
}
onMultiSelectionStarted: {
quickSelectionConnections.startIndex = index;
quickSelectionConnections.target = page;
}
function toggleSelection(index, notify) {
fileModel.toggleSelectedFile(index);
selectionPanel.open = (fileModel.selectedFileCount > 0);
selectionPanel.overrideText = "";
if (notify === false) return;
selectionChanged(index);
}
function clearSelectedFiles() {
fileModel.clearSelectedFiles();
selectionPanel.overrideText = "";
}
function selectAllFiles() {
fileModel.selectAllFiles();
selectionPanel.overrideText = "";
}
SelectionPanel {
id: selectionPanel
selectedCount: fileModel.selectedFileCount
enabled: !page.remorsePopupActive && !page.remorseItemActive
displayClose: fileModel.selectedFileCount == fileModel.fileCount
selectedFiles: function() { return fileModel.selectedFiles(); }
Connections {
target: selectionPanel.actions
onCloseTriggered: clearSelectedFiles()
onSelectAllTriggered: selectAllFiles()
onCutTriggered: clearSelectedFiles()
onCopyTriggered: clearSelectedFiles()
onDeleteTriggered: {
var files = fileModel.selectedFiles();
remorsePopupActive = true;
remorsePopup.execute(qsTr("Deleting"), function() {
fileModel.markSelectedAsDoomed();
clearSelectedFiles();
progressPanel.showText(qsTr("Deleting"));
engine.deleteFiles(files);
});
}
onTransferTriggered: {
if (remorsePopupActive) return;
if (transferPanel.status === Loader.Ready) {
if (selectedAction === "move") {
fileModel.markAsDoomed(toTransfer);
}
transferPanel.item.startTransfer(toTransfer, targets, selectedAction, goToTarget);
}
else notificationPanel.showText(qsTr("Internally not ready"), qsTr("Please simply try again"));
}
}
}
NotificationPanel {
id: notificationPanel
page: page
}
ProgressPanel {
id: progressPanel
page: page
onCancelled: engine.cancel()
}
Loader {
id: transferPanel
asynchronous: true
anchors.fill: parent
Component.onCompleted: {
setSource(Qt.resolvedUrl("../components/TransferPanel.qml"),
{ "page": page, "progressPanel": progressPanel,
"notificationPanel": notificationPanel });
}
onStatusChanged: {
if (status === Loader.Ready) {
item.overlayShown.connect(function() { fileList.visible = false; });
item.overlayHidden.connect(function() { fileList.visible = true; });
}
}
}
// connect signals from engine to panels
Connections {
target: engine
onProgressChanged: progressPanel.text = engine.progressFilename
onWorkerDone: progressPanel.hide()
onWorkerErrorOccurred: {
// the error signal goes to all pages in pagestack, show it only in the active one
if (progressPanel.open) {
progressPanel.hide();
if (message === "Unknown error")
filename = qsTr("Trying to move between phone and SD Card? It does not work, try copying.");
else if (message === "Failure to write block")
filename = qsTr("Perhaps the storage is full?");
notificationPanel.showText(message, filename);
}
}
}
Connections {
target: main
onShortcutsPageChanged: {
if (main.shortcutsPage !== null && status === PageStatus.Active && !canNavigateForward) {
pageStack.completeAnimation();
pageStack.pushAttached(main.shortcutsPage, { currentPath: dir });
}
}
}
onStatusChanged: {
if (status === PageStatus.Active) {
if (!canNavigateForward && main.shortcutsPage !== null) {
pageStack.completeAnimation();
pageStack.pushAttached(main.shortcutsPage, { currentPath: dir });
}
coverText = Paths.lastPartOfPath(page.dir)+"/"; // update cover
} else if (status === PageStatus.Activating) {
console.log("page: activating --", dir);
main.activePage = {type: "dir", path: dir};
navigate_syncNavStack();
console.log("page: activating done --", dir);
}
}
}