mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-01-30 00:12:50 -05:00
meta: many resource usage improvements and consolidations
This commit is contained in:
62
Widgets/CachingImage.qml
Normal file
62
Widgets/CachingImage.qml
Normal file
@@ -0,0 +1,62 @@
|
||||
pragma ComponentBehavior: Bound
|
||||
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Common
|
||||
|
||||
Image {
|
||||
id: root
|
||||
|
||||
property string imagePath: ""
|
||||
property string imageHash: ""
|
||||
property int maxCacheSize: 512
|
||||
|
||||
readonly property string cachePath: imageHash ? `${Paths.stringify(Paths.imagecache)}/${imageHash}@${maxCacheSize}x${maxCacheSize}.png` : ""
|
||||
|
||||
asynchronous: true
|
||||
fillMode: Image.PreserveAspectCrop
|
||||
sourceSize.width: maxCacheSize
|
||||
sourceSize.height: maxCacheSize
|
||||
smooth: true
|
||||
|
||||
onImagePathChanged: {
|
||||
if (imagePath) {
|
||||
hashProcess.command = ["sha256sum", Paths.strip(imagePath)]
|
||||
hashProcess.running = true
|
||||
} else {
|
||||
source = ""
|
||||
imageHash = ""
|
||||
}
|
||||
}
|
||||
|
||||
onCachePathChanged: {
|
||||
if (imageHash && cachePath) {
|
||||
// Ensure cache directory exists before trying to load from cache
|
||||
Paths.mkdir(Paths.imagecache)
|
||||
source = cachePath
|
||||
}
|
||||
}
|
||||
|
||||
onStatusChanged: {
|
||||
if (source == cachePath && status === Image.Error) {
|
||||
source = imagePath
|
||||
} else if (source == imagePath && status === Image.Ready && imageHash && cachePath) {
|
||||
Paths.mkdir(Paths.imagecache)
|
||||
const grabPath = cachePath
|
||||
if (visible && width > 0 && height > 0 && Window.window && Window.window.visible) {
|
||||
grabToImage(res => res.saveToFile(grabPath))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: hashProcess
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
root.imageHash = text.split(" ")[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
pragma ComponentBehavior: Bound
|
||||
|
||||
import QtQuick
|
||||
import QtQuick.Controls
|
||||
import QtCore
|
||||
@@ -30,6 +32,12 @@ DankModal {
|
||||
folder: currentPath ? "file://" + currentPath : "file://" + homeDir
|
||||
}
|
||||
|
||||
function isImageFile(fileName) {
|
||||
if (!fileName) return false
|
||||
var ext = fileName.toLowerCase().split('.').pop()
|
||||
return ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'].includes(ext)
|
||||
}
|
||||
|
||||
function getLastPath() {
|
||||
var lastPath = "";
|
||||
if (browserType === "wallpaper") {
|
||||
@@ -69,20 +77,13 @@ DankModal {
|
||||
|
||||
onVisibleChanged: {
|
||||
if (visible) {
|
||||
// Use last path or home directory when opening
|
||||
var startPath = getLastPath();
|
||||
console.log("Opening file browser, setting path to:", startPath);
|
||||
currentPath = startPath;
|
||||
}
|
||||
}
|
||||
|
||||
onCurrentPathChanged: {
|
||||
console.log("Current path changed to:", currentPath);
|
||||
console.log("Model count:", folderModel.count);
|
||||
// Log first few files to debug
|
||||
for (var i = 0; i < Math.min(3, folderModel.count); i++) {
|
||||
console.log("File", i, ":", folderModel.get(i, "fileName"));
|
||||
}
|
||||
// Path changed, model will update automatically
|
||||
}
|
||||
|
||||
function navigateUp() {
|
||||
@@ -90,20 +91,16 @@ DankModal {
|
||||
|
||||
// Don't go above home directory
|
||||
if (path === homeDir) {
|
||||
console.log("Already at home directory, can't go up");
|
||||
return;
|
||||
}
|
||||
|
||||
var lastSlash = path.lastIndexOf('/');
|
||||
if (lastSlash > 0) {
|
||||
var newPath = path.substring(0, lastSlash);
|
||||
// Make sure we don't go above home (check if newPath starts with homeDir)
|
||||
if (newPath.startsWith(homeDir)) {
|
||||
console.log("Navigating up from", path, "to", newPath);
|
||||
currentPath = newPath;
|
||||
saveLastPath(newPath);
|
||||
} else {
|
||||
console.log("Would go above home directory, stopping at", homeDir);
|
||||
currentPath = homeDir;
|
||||
saveLastPath(homeDir);
|
||||
}
|
||||
@@ -207,10 +204,18 @@ DankModal {
|
||||
|
||||
cellWidth: 150
|
||||
cellHeight: 130
|
||||
cacheBuffer: 260 // Only cache ~2 rows worth of items
|
||||
|
||||
model: folderModel
|
||||
|
||||
delegate: StyledRect {
|
||||
id: delegateRoot
|
||||
|
||||
required property bool fileIsDir
|
||||
required property string filePath
|
||||
required property string fileName
|
||||
required property url fileURL
|
||||
|
||||
width: 140
|
||||
height: 120
|
||||
radius: Theme.cornerRadius
|
||||
@@ -228,26 +233,34 @@ DankModal {
|
||||
height: 60
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
|
||||
Image {
|
||||
CachingImage {
|
||||
anchors.fill: parent
|
||||
source: !model.fileIsDir ? model.fileURL : ""
|
||||
imagePath: !delegateRoot.fileIsDir ? delegateRoot.filePath : ""
|
||||
fillMode: Image.PreserveAspectCrop
|
||||
visible: !model.fileIsDir
|
||||
asynchronous: true
|
||||
visible: !delegateRoot.fileIsDir && isImageFile(delegateRoot.fileName)
|
||||
maxCacheSize: 80
|
||||
}
|
||||
|
||||
DankIcon {
|
||||
anchors.centerIn: parent
|
||||
name: "description"
|
||||
size: Theme.iconSizeLarge
|
||||
color: Theme.primary
|
||||
visible: !delegateRoot.fileIsDir && !isImageFile(delegateRoot.fileName)
|
||||
}
|
||||
|
||||
DankIcon {
|
||||
anchors.centerIn: parent
|
||||
name: model.fileIsDir ? "folder" : "description"
|
||||
name: "folder"
|
||||
size: Theme.iconSizeLarge
|
||||
color: Theme.primary
|
||||
visible: model.fileIsDir
|
||||
visible: delegateRoot.fileIsDir
|
||||
}
|
||||
}
|
||||
|
||||
// File name
|
||||
StyledText {
|
||||
text: model.fileName || ""
|
||||
text: delegateRoot.fileName || ""
|
||||
font.pixelSize: Theme.fontSizeSmall
|
||||
color: Theme.surfaceText
|
||||
width: 120
|
||||
@@ -266,10 +279,10 @@ DankModal {
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
|
||||
onClicked: {
|
||||
if (model.fileIsDir) {
|
||||
navigateTo(model.filePath);
|
||||
if (delegateRoot.fileIsDir) {
|
||||
navigateTo(delegateRoot.filePath);
|
||||
} else {
|
||||
fileSelected(model.filePath);
|
||||
fileSelected(delegateRoot.filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,12 +27,15 @@ Item {
|
||||
|
||||
StateLayer {
|
||||
visible: toggle.text
|
||||
disabled: !toggle.enabled
|
||||
stateColor: Theme.primary
|
||||
cornerRadius: parent.radius
|
||||
onClicked: {
|
||||
toggle.checked = !toggle.checked;
|
||||
toggle.clicked();
|
||||
toggle.toggled(toggle.checked);
|
||||
if (toggle.enabled) {
|
||||
toggle.checked = !toggle.checked;
|
||||
toggle.clicked();
|
||||
toggle.toggled(toggle.checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +60,7 @@ Item {
|
||||
text: toggle.text
|
||||
font.pixelSize: Appearance.fontSize.normal
|
||||
font.weight: Font.Medium
|
||||
opacity: toggle.enabled ? 1 : 0.4
|
||||
}
|
||||
|
||||
StyledText {
|
||||
@@ -81,8 +85,8 @@ Item {
|
||||
anchors.rightMargin: toggle.text ? Theme.spacingM : 0
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
radius: height / 2
|
||||
color: toggle.checked ? Theme.primary : Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.3)
|
||||
opacity: toggle.toggling ? 0.6 : 1
|
||||
color: (toggle.checked && toggle.enabled) ? Theme.primary : Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.3)
|
||||
opacity: toggle.toggling ? 0.6 : (toggle.enabled ? 1 : 0.4)
|
||||
|
||||
StyledRect {
|
||||
id: toggleHandle
|
||||
@@ -92,7 +96,7 @@ Item {
|
||||
radius: 10
|
||||
color: Theme.surface
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
x: toggle.checked ? parent.width - width - 2 : 2
|
||||
x: (toggle.checked && toggle.enabled) ? parent.width - width - 2 : 2
|
||||
|
||||
StyledRect {
|
||||
anchors.centerIn: parent
|
||||
@@ -121,9 +125,11 @@ Item {
|
||||
stateColor: Theme.primary
|
||||
cornerRadius: parent.radius
|
||||
onClicked: {
|
||||
toggle.checked = !toggle.checked;
|
||||
toggle.clicked();
|
||||
toggle.toggled(toggle.checked);
|
||||
if (toggle.enabled) {
|
||||
toggle.checked = !toggle.checked;
|
||||
toggle.clicked();
|
||||
toggle.toggled(toggle.checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user