summaryrefslogtreecommitdiff
path: root/services/Thumbnailer.qml
blob: 3d7a6c11e3cecde232f6222876579b6055716d40 (plain)
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
pragma Singleton
pragma ComponentBehavior: Bound

import Quickshell
import Quickshell.Io
import QtQuick
import Qt.labs.platform

Singleton {
    id: root

    readonly property string thumbDir: `${StandardPaths.standardLocations(StandardPaths.GenericCacheLocation)[0]}/caelestia/thumbnails`.slice(7)

    function go(obj: var): var {
        return thumbComp.createObject(obj, {
            originalPath: obj.path,
            width: obj.width,
            height: obj.height,
            loadOriginal: obj.loadOriginal
        });
    }

    component Thumbnail: QtObject {
        id: obj

        required property string originalPath
        required property int width
        required property int height
        required property bool loadOriginal

        property string path

        readonly property Process proc: Process {
            running: true
            command: ["fish", "-c", `
set -l path "${root.thumbDir}/$(sha1sum ${obj.originalPath} | cut -d ' ' -f 1)@${obj.width}x${obj.height}-exact.png"
if test -f $path
    echo $path
else
    echo 'start'
    set -l size (identify -ping -format '%w\n%h' ${obj.originalPath})
    if test $size[1] -gt ${obj.width} -o $size[2] -gt ${obj.height}
        magick ${obj.originalPath} -${obj.width > 1024 || obj.height > 1024 ? "resize" : "thumbnail"} ${obj.width}x${obj.height}^ -background none -gravity center -extent ${obj.width}x${obj.height} -unsharp 0x.5 $path
    else
        cp ${obj.originalPath} $path
    end
    echo $path
end`]
            stdout: SplitParser {
                onRead: data => {
                    if (data === "start") {
                        if (obj.loadOriginal)
                            obj.path = obj.originalPath;
                    } else {
                        obj.path = data;
                    }
                }
            }
        }

        function reload(): void {
            proc.signal(9);
            proc.running = true;
        }
    }

    Component {
        id: thumbComp

        Thumbnail {}
    }
}