summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/dms/Modules/Lock/VideoScreensaver.qml
blob: 129f507b62839c6a7ee87be99acc91b845f655ae (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
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
pragma ComponentBehavior: Bound

import QtQuick
import Quickshell.Io
import qs.Common
import qs.Services

Item {
    id: root

    required property string screenName
    property bool active: false
    property string videoSource: ""
    property bool inputEnabled: false
    property point lastMousePos: Qt.point(-1, -1)
    property bool mouseInitialized: false
    property var videoPlayer: null

    signal dismissed

    visible: active
    z: 1000

    Rectangle {
        id: background
        anchors.fill: parent
        color: "black"
        visible: root.active
    }

    Timer {
        id: inputEnableTimer
        interval: 500
        onTriggered: root.inputEnabled = true
    }

    Process {
        id: videoPicker
        property string result: ""
        property string folder: ""

        command: ["sh", "-c", "find '" + folder + "' -maxdepth 1 -type f \\( " + "-iname '*.mp4' -o -iname '*.mkv' -o -iname '*.webm' -o " + "-iname '*.mov' -o -iname '*.avi' -o -iname '*.m4v' " + "\\) 2>/dev/null | shuf -n1"]

        stdout: SplitParser {
            onRead: data => {
                const path = data.trim();
                if (path) {
                    videoPicker.result = path;
                    root.videoSource = "file://" + path;
                }
            }
        }

        onExited: exitCode => {
            if (exitCode !== 0 || !videoPicker.result) {
                console.warn("VideoScreensaver: no video found in folder");
                ToastService.showError(I18n.tr("Video Screensaver"), I18n.tr("No video found in folder"));
                root.dismiss();
            }
        }
    }

    Process {
        id: fileChecker
        command: ["test", "-d", SettingsData.lockScreenVideoPath]

        onExited: exitCode => {
            const isDir = exitCode === 0;
            const videoPath = SettingsData.lockScreenVideoPath;

            if (isDir) {
                videoPicker.folder = videoPath;
                videoPicker.running = true;
            } else if (SettingsData.lockScreenVideoCycling) {
                const parentFolder = videoPath.substring(0, videoPath.lastIndexOf('/'));
                videoPicker.folder = parentFolder;
                videoPicker.running = true;
            } else {
                root.videoSource = "file://" + videoPath;
            }
        }
    }

    function createVideoPlayer() {
        if (videoPlayer)
            return true;

        try {
            videoPlayer = Qt.createQmlObject(`
                import QtQuick
                import QtMultimedia
                Video {
                    anchors.fill: parent
                    fillMode: VideoOutput.PreserveAspectCrop
                    loops: MediaPlayer.Infinite
                    volume: 0
                }
            `, background, "VideoScreensaver.VideoPlayer");

            videoPlayer.errorOccurred.connect((error, errorString) => {
                console.warn("VideoScreensaver: playback error:", errorString);
                ToastService.showError(I18n.tr("Video Screensaver"), I18n.tr("Playback error: ") + errorString);
                root.dismiss();
            });

            return true;
        } catch (e) {
            console.warn("VideoScreensaver: Failed to create video player:", e);
            return false;
        }
    }

    function destroyVideoPlayer() {
        if (videoPlayer) {
            videoPlayer.stop();
            videoPlayer.destroy();
            videoPlayer = null;
        }
    }

    function start() {
        if (!SettingsData.lockScreenVideoEnabled || !SettingsData.lockScreenVideoPath)
            return;

        if (!MultimediaService.available) {
            ToastService.showError(I18n.tr("Video Screensaver"), I18n.tr("QtMultimedia is not available"));
            return;
        }

        if (!createVideoPlayer())
            return;

        videoPicker.result = "";
        videoPicker.folder = "";
        inputEnabled = false;
        mouseInitialized = false;
        lastMousePos = Qt.point(-1, -1);
        active = true;
        inputEnableTimer.start();
        fileChecker.running = true;
    }

    function dismiss() {
        if (!active)
            return;
        destroyVideoPlayer();
        inputEnabled = false;
        active = false;
        videoSource = "";
        dismissed();
    }

    onVideoSourceChanged: {
        if (videoSource && active && videoPlayer) {
            videoPlayer.source = videoSource;
            videoPlayer.play();
        }
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        enabled: root.active && root.inputEnabled
        hoverEnabled: true
        propagateComposedEvents: false

        onPositionChanged: mouse => {
            if (!root.mouseInitialized) {
                root.lastMousePos = Qt.point(mouse.x, mouse.y);
                root.mouseInitialized = true;
                return;
            }
            var dx = Math.abs(mouse.x - root.lastMousePos.x);
            var dy = Math.abs(mouse.y - root.lastMousePos.y);
            if (dx > 5 || dy > 5) {
                root.dismiss();
            }
        }
        onClicked: root.dismiss()
        onPressed: root.dismiss()
        onWheel: root.dismiss()
    }

    Connections {
        target: IdleService

        function onLockRequested() {
            if (SettingsData.lockScreenVideoEnabled && !root.active) {
                root.start();
            }
        }

        function onFadeToLockRequested() {
            if (SettingsData.lockScreenVideoEnabled && !root.active) {
                IdleService.cancelFadeToLock();
                root.start();
            }
        }
    }
}