summaryrefslogtreecommitdiffstats
path: root/QtLauncher/DemoSurface.qml
blob: 938b887f68dd2afb7a6b57bf7814aa8194342a27 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
pragma ComponentBehavior: Bound

import QtQuick
import QtWayland.Compositor

Repeater {
    id: root
    property string filename: ""
    anchors.fill: parent

    signal surfaceVisible(visible: bool)
    signal surfaceDestroyed(index: int)

    function scheduleScreenshot(filename: string) {
        root.filename = filename
    }

    delegate: ShellSurfaceItem {
        id: chrome
        required property var modelData
        required property int index
        anchors.centerIn: parent
        shellSurface: modelData

        onWidthChanged: downscaleIfNeeded()
        onHeightChanged: downscaleIfNeeded()

        function downscaleIfNeeded() {
            if (width <= root.width && height <= root.height) {
                scale = 1.0
                return;
            }
            scale = Math.min(root.width / width, root.height / height)
        }

        onSurfaceChanged: {
            if (root.filename) ssTimer.start()
            root.surfaceVisible(false)
        }

        onSurfaceDestroyed: root.surfaceDestroyed(index)

        Timer {
            id: ssTimer
            interval: 5000
            running: false

            onTriggered: {
                chrome.grabToImage(function(result) {
                    result.saveToFile(root.filename);
                    root.filename = ""
                    demoHeader.title = qsTr("Screenshot taken. Thumbnail for this demo will be updated on next startup.")
                })
            }
        }
    }
}