Multiple optimizations

This commit is contained in:
cutefishd 2021-04-05 04:06:24 +08:00
parent 72db8fceb4
commit 8635ca91fc
6 changed files with 190 additions and 89 deletions

View file

@ -64,6 +64,16 @@ Item {
id: folderModel id: folderModel
url: desktopPath() url: desktopPath()
isDesktop: true isDesktop: true
viewAdapter: viewAdapter
}
ItemViewAdapter {
id: viewAdapter
adapterView: _folderView
adapterModel: folderModel
adapterIconSize: 40
adapterVisibleArea: Qt.rect(_folderView.contentX, _folderView.contentY,
_folderView.contentWidth, _folderView.contentHeight)
} }
FolderGridView { FolderGridView {
@ -94,6 +104,10 @@ Item {
if (!activeFocus) if (!activeFocus)
folderModel.clearSelection() folderModel.clearSelection()
} }
Component.onCompleted: {
folderModel.requestRename.connect(rename)
}
} }
Connections { Connections {

View file

@ -7,12 +7,12 @@ import MeuiKit 1.0 as Meui
Window { Window {
id: control id: control
title: qsTr("New Folder") title: qsTr("New folder name")
flags: Qt.Dialog flags: Qt.Dialog
visible: true visible: true
width: 400 + Meui.Units.largeSpacing * 2 width: 400 + Meui.Units.largeSpacing * 2
height: _mainLayout.implicitHeight + Meui.Units.largeSpacing * 4 height: _mainLayout.implicitHeight + Meui.Units.largeSpacing * 2
minimumWidth: width minimumWidth: width
minimumHeight: height minimumHeight: height
@ -31,16 +31,15 @@ Window {
anchors.rightMargin: Meui.Units.largeSpacing anchors.rightMargin: Meui.Units.largeSpacing
spacing: 0 spacing: 0
RowLayout {
Label {
text: qsTr("Name")
}
TextField { TextField {
id: _textField id: _textField
Layout.fillWidth: true Layout.fillWidth: true
Keys.onEscapePressed: control.close() Keys.onEscapePressed: control.close()
text: qsTr("New folder")
focus: true focus: true
Component.onCompleted: {
_textField.selectAll()
} }
} }

View file

@ -63,6 +63,16 @@ GridView {
} }
} }
function reset() {
currentIndex = -1
anchorIndex = 0
previouslySelectedItemIndex = -1
cancelRename()
hoveredItem = null
pressedItem = null
cPress = null
}
highlightMoveDuration: 0 highlightMoveDuration: 0
keyNavigationEnabled : true keyNavigationEnabled : true
keyNavigationWraps : true keyNavigationWraps : true
@ -90,6 +100,13 @@ GridView {
anchorIndex = 0 anchorIndex = 0
} }
} }
Keys.onEscapePressed: {
if (!editor || !editor.targetItem) {
previouslySelectedItemIndex = -1
folderModel.clearSelection()
event.accepted = false
}
}
cellHeight: { cellHeight: {
var iconHeight = iconSize + (Meui.Units.fontMetrics.height * 2) + Meui.Units.largeSpacing * 2 var iconHeight = iconSize + (Meui.Units.fontMetrics.height * 2) + Meui.Units.largeSpacing * 2
@ -269,6 +286,9 @@ GridView {
control.dragY = -1 control.dragY = -1
clearPressState() clearPressState()
} else { } else {
if (control.editor && control.editor.targetItem)
return;
folderModel.pinSelection() folderModel.pinSelection()
control.rubberBand = rubberBandObject.createObject(control.contentItem, {x: cPress.x, y: cPress.y}) control.rubberBand = rubberBandObject.createObject(control.contentItem, {x: cPress.x, y: cPress.y})
control.interactive = false control.interactive = false
@ -429,6 +449,8 @@ GridView {
wrapMode: TextEdit.Wrap wrapMode: TextEdit.Wrap
horizontalAlignment: TextEdit.AlignHCenter horizontalAlignment: TextEdit.AlignHCenter
z: 999 z: 999
topPadding: Meui.Units.smallSpacing
bottomPadding: Meui.Units.smallSpacing
property Item targetItem: null property Item targetItem: null
@ -443,12 +465,14 @@ GridView {
targetItem.labelArea.visible = false targetItem.labelArea.visible = false
_editor.select(0, folderModel.fileExtensionBoundary(targetItem.index)) _editor.select(0, folderModel.fileExtensionBoundary(targetItem.index))
visible = true visible = true
control.interactive = false
} else { } else {
x = 0 x = 0
y = 0 y = 0
width = 0 width = 0
height = 0 height = 0
visible = false visible = false
control.interactive = true
} }
} }
@ -457,9 +481,11 @@ GridView {
case Qt.Key_Return: case Qt.Key_Return:
case Qt.Key_Enter: case Qt.Key_Enter:
commit() commit()
event.accepted = true
break break
case Qt.Key_Escape: case Qt.Key_Escape:
cancel() cancel()
event.accepted = true
break break
} }
} }
@ -477,13 +503,18 @@ GridView {
folderModel.rename(targetItem.index, text) folderModel.rename(targetItem.index, text)
control.currentIndex = targetItem.index control.currentIndex = targetItem.index
targetItem = null targetItem = null
control.editor.destroy()
} }
} }
function cancel() { function cancel() {
if (targetItem) { if (targetItem) {
targetItem.labelArea.visible = true targetItem.labelArea.visible = true
control.currentIndex = targetItem.index
targetItem = null targetItem = null
control.editor.destroy()
} }
} }
} }

View file

@ -38,9 +38,39 @@ ListView {
ScrollBar.vertical: ScrollBar { } ScrollBar.vertical: ScrollBar { }
function rename() {
if (currentIndex !== -1) {
var renameAction = control.model.action("rename")
if (renameAction && !renameAction.enabled)
return
if (!control.editor)
control.editor = editorComponent.createObject(control)
control.editor.targetItem = control.currentItem
}
}
function cancelRename() {
if (control.editor) {
control.editor.cancel()
control.editor.destroy()
control.editor = null
}
}
function reset() {
currentIndex = -1
anchorIndex = 0
previouslySelectedItemIndex = -1
cancelRename()
hoveredItem = null
pressedItem = null
cPress = null
}
highlightMoveDuration: 0 highlightMoveDuration: 0
keyNavigationEnabled : true Keys.enabled: true
keyNavigationWraps : true
Keys.onPressed: { Keys.onPressed: {
if (event.key === Qt.Key_Control) { if (event.key === Qt.Key_Control) {
ctrlPressed = true ctrlPressed = true
@ -53,6 +83,7 @@ ListView {
control.keyPress(event) control.keyPress(event)
} }
Keys.onReleased: { Keys.onReleased: {
if (event.key === Qt.Key_Control) { if (event.key === Qt.Key_Control) {
ctrlPressed = false ctrlPressed = false
@ -62,6 +93,40 @@ ListView {
} }
} }
Keys.onEscapePressed: {
if (!editor || !editor.targetItem) {
previouslySelectedItemIndex = -1
folderModel.clearSelection()
event.accepted = false
}
}
Keys.onUpPressed: {
if (!editor || !editor.targetItem) {
var newIndex = currentIndex
newIndex--;
if (newIndex < 0)
newIndex = 0
currentIndex = newIndex
updateSelection(event.modifiers)
}
}
Keys.onDownPressed: {
if (!editor || !editor.targetItem) {
var newIndex = currentIndex
newIndex++
if (newIndex >= control.count)
return
currentIndex = newIndex
updateSelection(event.modifiers)
}
}
onCachedRectangleSelectionChanged: { onCachedRectangleSelectionChanged: {
if (cachedRectangleSelection === null) if (cachedRectangleSelection === null)
return return
@ -88,26 +153,6 @@ ListView {
cPress = mapToItem(control.contentItem, pressX, pressY) cPress = mapToItem(control.contentItem, pressX, pressY)
} }
function rename() {
if (currentIndex !== -1) {
var renameAction = control.model.action("rename")
if (renameAction && !renameAction.enabled)
return
if (!control.editor)
control.editor = editorComponent.createObject(control)
control.editor.targetItem = control.currentItem
}
}
function cancelRename() {
if (control.editor) {
control.editor.cancel()
control.editor = null
}
}
MouseArea { MouseArea {
id: _mouseArea id: _mouseArea
anchors.fill: parent anchors.fill: parent
@ -253,6 +298,9 @@ ListView {
control.dragY = -1 control.dragY = -1
clearPressState() clearPressState()
} else { } else {
if (control.editor && control.editor.targetItem)
return;
folderModel.pinSelection() folderModel.pinSelection()
control.rubberBand = rubberBandObject.createObject(control.contentItem, {x: cPress.x, y: cPress.y}) control.rubberBand = rubberBandObject.createObject(control.contentItem, {x: cPress.x, y: cPress.y})
control.interactive = false control.interactive = false
@ -306,9 +354,9 @@ ListView {
function updateSelection(modifier) { function updateSelection(modifier) {
if (modifier & Qt.ShiftModifier) { if (modifier & Qt.ShiftModifier) {
folderModel.setRangeSelected(anchorIndex, hoveredItem) folderModel.setRangeSelected(anchorIndex, currentIndex)
} else { } else {
folderModel.clear() folderModel.clearSelection()
folderModel.setSelected(currentIndex) folderModel.setSelected(currentIndex)
if (currentIndex == -1) if (currentIndex == -1)
previouslySelectedItemIndex = -1 previouslySelectedItemIndex = -1
@ -319,46 +367,33 @@ ListView {
Component { Component {
id: editorComponent id: editorComponent
TextArea { TextField {
id: _editor id: _editor
visible: false visible: false
wrapMode: Text.NoWrap wrapMode: Text.NoWrap
textMargin: 0
verticalAlignment: TextEdit.AlignVCenter verticalAlignment: TextEdit.AlignVCenter
leftPadding: 0 z: 999
property Item targetItem: null property Item targetItem: null
background: Item {}
onTargetItemChanged: { onTargetItemChanged: {
if (targetItem != null) { if (targetItem != null) {
var pos = control.mapFromItem(targetItem, targetItem.labelArea.x, targetItem.labelArea.y) var pos = control.mapFromItem(targetItem, targetItem.labelArea.x, targetItem.labelArea.y)
width = targetItem.labelArea.width width = targetItem.labelArea.width
height = targetItem.height height = Meui.Units.fontMetrics.height + Meui.Units.largeSpacing * 2
x = control.mapFromItem(targetItem.labelArea, 0, 0).x x = control.mapFromItem(targetItem.labelArea, 0, 0).x
y = pos.y y = pos.y + (targetItem.height - height) / 2
text = targetItem.labelArea.text text = targetItem.labelArea.text
targetItem.labelArea.visible = false targetItem.labelArea.visible = false
targetItem.labelArea2.visible = false targetItem.labelArea2.visible = false
_editor.select(0, folderModel.fileExtensionBoundary(targetItem.index)) _editor.select(0, folderModel.fileExtensionBoundary(targetItem.index))
visible = true visible = true
control.interactive = false
} else { } else {
x: 0 x: 0
y: 0 y: 0
visible = false visible = false
} control.interactive = true
}
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Return:
case Qt.Key_Enter:
commit()
break
case Qt.Key_Escape:
cancel()
break
} }
} }
@ -369,6 +404,20 @@ ListView {
control.forceActiveFocus() control.forceActiveFocus()
} }
Keys.onPressed: {
switch (event.key) {
case Qt.Key_Return:
case Qt.Key_Enter:
commit()
event.accepted = true
break
case Qt.Key_Escape:
cancel()
event.accepted = true
break
}
}
function commit() { function commit() {
if (targetItem) { if (targetItem) {
targetItem.labelArea.visible = true targetItem.labelArea.visible = true
@ -376,6 +425,8 @@ ListView {
folderModel.rename(targetItem.index, text) folderModel.rename(targetItem.index, text)
control.currentIndex = targetItem.index control.currentIndex = targetItem.index
targetItem = null targetItem = null
control.editor.destroy()
} }
} }
@ -383,7 +434,10 @@ ListView {
if (targetItem) { if (targetItem) {
targetItem.labelArea.visible = true targetItem.labelArea.visible = true
targetItem.labelArea2.visible = true targetItem.labelArea2.visible = true
control.currentIndex = targetItem.index
targetItem = null targetItem = null
control.editor.destroy()
} }
} }
} }

View file

@ -17,6 +17,7 @@ Item {
signal requestPathEditor() signal requestPathEditor()
onCurrentUrlChanged: { onCurrentUrlChanged: {
_viewLoader.item.reset()
_viewLoader.item.forceActiveFocus() _viewLoader.item.forceActiveFocus()
} }
@ -31,7 +32,8 @@ Item {
Label { Label {
id: _fileTips id: _fileTips
text: qsTr("No files") text: qsTr("Empty folder")
font.pointSize: 20
anchors.centerIn: parent anchors.centerIn: parent
visible: false visible: false
} }
@ -70,6 +72,11 @@ Item {
case 0: return _listViewComponent case 0: return _listViewComponent
case 1: return _gridViewComponent case 1: return _gridViewComponent
} }
onSourceComponentChanged: {
//
_viewLoader.item.forceActiveFocus()
}
} }
Loader { Loader {

View file

@ -5,21 +5,21 @@
<name>CreateFolderDialog</name> <name>CreateFolderDialog</name>
<message> <message>
<location filename="../qml/Dialogs/CreateFolderDialog.qml" line="10"/> <location filename="../qml/Dialogs/CreateFolderDialog.qml" line="10"/>
<source>New Folder</source> <source>New folder name</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../qml/Dialogs/CreateFolderDialog.qml" line="36"/> <location filename="../qml/Dialogs/CreateFolderDialog.qml" line="38"/>
<source>Name</source> <source>New folder</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../qml/Dialogs/CreateFolderDialog.qml" line="49"/> <location filename="../qml/Dialogs/CreateFolderDialog.qml" line="48"/>
<source>Cancel</source> <source>Cancel</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../qml/Dialogs/CreateFolderDialog.qml" line="55"/> <location filename="../qml/Dialogs/CreateFolderDialog.qml" line="54"/>
<source>OK</source> <source>OK</source>
<translation></translation> <translation></translation>
</message> </message>
@ -27,7 +27,7 @@
<context> <context>
<name>DesktopView</name> <name>DesktopView</name>
<message> <message>
<location filename="../desktop/desktopview.cpp" line="38"/> <location filename="../desktop/desktopview.cpp" line="44"/>
<source>Desktop</source> <source>Desktop</source>
<translation></translation> <translation></translation>
</message> </message>
@ -35,81 +35,77 @@
<context> <context>
<name>FolderModel</name> <name>FolderModel</name>
<message> <message>
<source>%1 selected</source> <location filename="../model/foldermodel.cpp" line="139"/>
<translation type="vanished"> %1 </translation>
</message>
<message>
<location filename="../model/foldermodel.cpp" line="137"/>
<source>%1 item</source> <source>%1 item</source>
<translation>%1 </translation> <translation>%1 </translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="137"/> <location filename="../model/foldermodel.cpp" line="139"/>
<source>%1 items</source> <source>%1 items</source>
<translation>%1 </translation> <translation>%1 </translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="805"/> <location filename="../model/foldermodel.cpp" line="807"/>
<source>Select All</source> <source>Select All</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1007"/> <location filename="../model/foldermodel.cpp" line="1009"/>
<source>Open</source> <source>Open</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1010"/> <location filename="../model/foldermodel.cpp" line="1012"/>
<source>Cut</source> <source>Cut</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1013"/> <location filename="../model/foldermodel.cpp" line="1015"/>
<source>Copy</source> <source>Copy</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1016"/> <location filename="../model/foldermodel.cpp" line="1018"/>
<source>Paste</source> <source>Paste</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1019"/> <location filename="../model/foldermodel.cpp" line="1021"/>
<source>New Folder</source> <source>New Folder</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1022"/> <location filename="../model/foldermodel.cpp" line="1024"/>
<source>Move To Trash</source> <source>Move To Trash</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1025"/> <location filename="../model/foldermodel.cpp" line="1027"/>
<source>Empty Trash</source> <source>Empty Trash</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1028"/> <location filename="../model/foldermodel.cpp" line="1030"/>
<source>Delete</source> <source>Delete</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1031"/> <location filename="../model/foldermodel.cpp" line="1033"/>
<source>Rename</source> <source>Rename</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1034"/> <location filename="../model/foldermodel.cpp" line="1036"/>
<source>Open in Terminal</source> <source>Open in Terminal</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1037"/> <location filename="../model/foldermodel.cpp" line="1039"/>
<source>Set as Wallpaper</source> <source>Set as Wallpaper</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../model/foldermodel.cpp" line="1040"/> <location filename="../model/foldermodel.cpp" line="1042"/>
<source>Properties</source> <source>Properties</source>
<translation></translation> <translation></translation>
</message> </message>
@ -118,26 +114,26 @@
<name>FolderPage</name> <name>FolderPage</name>
<message> <message>
<location filename="../qml/FolderPage.qml" line="34"/> <location filename="../qml/FolderPage.qml" line="34"/>
<source>No files</source> <source>Empty folder</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../qml/FolderPage.qml" line="105"/> <location filename="../qml/FolderPage.qml" line="106"/>
<source>%1 item</source> <source>%1 item</source>
<translation>%1 </translation> <translation>%1 </translation>
</message> </message>
<message> <message>
<location filename="../qml/FolderPage.qml" line="106"/> <location filename="../qml/FolderPage.qml" line="107"/>
<source>%1 items</source> <source>%1 items</source>
<translation>%1 </translation> <translation>%1 </translation>
</message> </message>
<message> <message>
<location filename="../qml/FolderPage.qml" line="111"/> <location filename="../qml/FolderPage.qml" line="112"/>
<source>%1 selected</source> <source>%1 selected</source>
<translation> %1 </translation> <translation> %1 </translation>
</message> </message>
<message> <message>
<location filename="../qml/FolderPage.qml" line="122"/> <location filename="../qml/FolderPage.qml" line="123"/>
<source>Empty Trash</source> <source>Empty Trash</source>
<translation></translation> <translation></translation>
</message> </message>