Files
clide/qml/ClideTreeView.qml

168 lines
5.2 KiB
QML
Raw Normal View History

2026-01-31 08:02:16 -05:00
// SPDX-FileCopyrightText: 2026, Shaun Reed <shaunrd0@gmail.com>
//
// SPDX-License-Identifier: GNU General Public License v3.0 or later
2026-01-25 20:57:36 +00:00
import QtQuick
2026-02-01 15:35:40 -05:00
import QtQuick.Effects
2026-01-25 20:57:36 +00:00
import QtQuick.Controls
import clide.module 1.0
2026-02-01 20:20:39 -05:00
import Logger 1.0
2026-01-25 20:57:36 +00:00
2026-02-01 15:35:40 -05:00
TreeView {
id: fileSystemTreeView
property int lastIndex: -1
required property string originalRootDirectory
property string rootDirectory
2026-01-31 04:25:14 +00:00
2026-01-25 20:57:36 +00:00
signal fileClicked(string filePath)
2026-02-01 15:35:40 -05:00
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.StopAtBounds
clip: true
leftMargin: 25
2026-02-02 18:08:37 -05:00
model: FileSystem
rootIndex: FileSystem.setDirectory(fileSystemTreeView.rootDirectory)
// Provide our own custom ScrollIndicator for the TreeView.
ScrollIndicator.vertical: ScrollIndicator {
active: true
implicitWidth: 15
contentItem: Rectangle {
color: RustColors.scrollbar
implicitHeight: 6
implicitWidth: 6
opacity: fileSystemTreeView.movingVertically ? 0.5 : 0.0
Behavior on opacity {
OpacityAnimator {
duration: 500
}
}
}
}
2026-01-25 20:57:36 +00:00
2026-02-01 15:35:40 -05:00
// The delegate represents a single entry in the filesystem.
delegate: TreeViewDelegate {
id: treeDelegate
required property string fileName
2026-02-02 18:08:37 -05:00
required property url filePath
required property int index
implicitHeight: 25
implicitWidth: fileSystemTreeView.width > 0 ? fileSystemTreeView.width : 250
indentation: 12
2026-02-01 15:35:40 -05:00
2026-02-02 18:08:37 -05:00
background: Rectangle {
2026-02-07 12:42:56 -05:00
color: current ? RustColors.explorer_folder_open : "transparent"
2026-02-02 18:08:37 -05:00
radius: 2.5
}
contentItem: Text {
2026-02-07 12:42:56 -05:00
anchors.left: directoryIcon.right
anchors.leftMargin: 5
2026-02-02 18:08:37 -05:00
color: RustColors.explorer_text
text: treeDelegate.fileName
}
indicator: Label {
2026-02-01 15:35:40 -05:00
id: directoryIcon
anchors.verticalCenter: parent.verticalCenter
antialiasing: true
font.family: localFont.font.family
font.pixelSize: 18
2026-02-02 18:08:37 -05:00
smooth: true
text: fileSystemTreeView.model.icon(filePath)
2026-02-07 12:42:56 -05:00
x: treeDelegate.leftMargin + (treeDelegate.depth * treeDelegate.indentation) + (indicator.visible ? indicator.width : 0)
2026-02-01 15:35:40 -05:00
}
2026-01-25 20:57:36 +00:00
FontLoader {
id: localFont
source: "qrc:/fonts/saucecodepro-xlight.ttf"
}
2026-02-07 12:42:56 -05:00
Label {
id: indicator
anchors.verticalCenter: parent.verticalCenter
font.family: localFont.font.family
2026-02-07 12:42:56 -05:00
font.pixelSize: 10
font.weight: localFont.font.weight
2026-02-07 12:42:56 -05:00
text: expanded ? "⮟" : "⮞"
visible: isTreeNode && hasChildren
x: padding + (depth * indentation)
}
2026-02-01 15:35:40 -05:00
MultiEffect {
id: iconOverlay
anchors.fill: directoryIcon
brightness: 1.0
2026-02-02 18:08:37 -05:00
colorization: 1.0
2026-02-01 15:35:40 -05:00
colorizationColor: {
const isFile = !treeDelegate.hasChildren;
if (isFile)
2026-02-02 18:08:37 -05:00
return Qt.lighter(RustColors.explorer_folder, 2);
2026-02-01 15:35:40 -05:00
const isExpandedFolder = treeDelegate.expanded && treeDelegate.hasChildren;
if (isExpandedFolder)
2026-02-02 18:08:37 -05:00
return Qt.darker(RustColors.explorer_folder, 2);
2026-02-01 15:35:40 -05:00
else
2026-02-02 18:08:37 -05:00
return RustColors.explorer_folder;
2026-01-25 20:57:36 +00:00
}
2026-02-02 18:08:37 -05:00
source: directoryIcon
2026-02-01 15:35:40 -05:00
}
HoverHandler {
id: hoverHandler
2026-02-02 18:08:37 -05:00
2026-02-01 15:35:40 -05:00
acceptedDevices: PointerDevice.Mouse
}
TapHandler {
acceptedButtons: Qt.LeftButton | Qt.RightButton
2026-02-02 18:08:37 -05:00
2026-02-01 15:35:40 -05:00
onSingleTapped: (eventPoint, button) => {
switch (button) {
2026-02-02 18:08:37 -05:00
case Qt.LeftButton:
if (treeDelegate.hasChildren) {
fileSystemTreeView.toggleExpanded(treeDelegate.row);
} else {
2026-02-07 12:42:56 -05:00
// If this model item doesn't have children, it means it's representing a file.
2026-02-02 18:08:37 -05:00
fileSystemTreeView.fileClicked(treeDelegate.filePath);
}
2026-02-02 18:08:37 -05:00
break;
case Qt.RightButton:
contextMenu.popup();
break;
2026-01-25 20:57:36 +00:00
}
}
2026-02-01 15:35:40 -05:00
}
2026-02-07 12:59:44 -05:00
ClideMenu {
2026-02-01 15:35:40 -05:00
id: contextMenu
2026-02-02 18:08:37 -05:00
2026-02-07 12:59:44 -05:00
ClideMenuItem {
action: Action {
enabled: treeDelegate.hasChildren
text: qsTr("Set root")
2026-02-02 18:08:37 -05:00
2026-02-07 12:59:44 -05:00
onTriggered: {
Logger.debug("Setting new root directory: " + treeDelegate.filePath);
fileSystemTreeView.rootDirectory = treeDelegate.filePath;
}
2026-01-25 20:57:36 +00:00
}
2026-02-01 15:35:40 -05:00
}
2026-02-07 12:59:44 -05:00
ClideMenuItem {
action: Action {
text: qsTr("Reset root")
2026-01-25 20:57:36 +00:00
2026-02-07 12:59:44 -05:00
onTriggered: {
Logger.log("Resetting root directory: " + fileSystemTreeView.originalRootDirectory);
fileSystemTreeView.rootDirectory = fileSystemTreeView.originalRootDirectory;
}
2026-01-25 20:57:36 +00:00
}
}
}
}
selectionModel: ItemSelectionModel {
}
2026-01-25 20:57:36 +00:00
}