summaryrefslogtreecommitdiffstats
path: root/tests/manual/qml-screencapture-advanced/WindowCapturePanel.qml
blob: aa35194b418dc450f832455557744e82f1d15dee (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtCore
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQml
import QtMultimedia

GridLayout {
    id: top

    required property var topWindow
    required property bool useLandscapeLayout

    // There is no signal for tracking when the list of CapturableWindows changed. We store them
    // here as a property so we can track them.
    property var capturableWindowsList: []
    property bool periodicallyRefreshWindows: false

    WindowCapture {
        id: windowCapture
        onActiveChanged: () => {
            console.log("QWindowCapture active changed: " + active)

            // CheckBox has a weird behavior where it will remove our bindings when activated.
            // Update the value manually here
            if (active === true)
                windowCaptureActiveCheckBox.checkState = Qt.Checked
            else
                windowCaptureActiveCheckBox.checkState = Qt.Unchecked
        }
        onErrorChanged: () => {
            console.log("QWindowCapture error changed: " + error)
        }
        onErrorStringChanged: (msg) => {
            console.log("QWindowCapture error string changed: " + errorString)
        }
    }

    CaptureSession {
        windowCapture: windowCapture
        videoOutput: windowCaptureVideoOutput
    }

    // There is no signal for refreshing capturable windows. This
    // timer lets us refresh it frequently.
    Timer {
        interval: 500
        running: top.periodicallyRefreshWindows
        repeat: true
        triggeredOnStart: true
        onTriggered: () => {
            top.capturableWindowsList = windowCapture.capturableWindows()
        }
    }

    Layout.fillWidth: true
    columns: useLandscapeLayout === true ? 2 : 1

    ColumnLayout {
        Layout.alignment: Qt.AlignTop

        Button {
            text: "Select app window"
            onClicked: () => {
                windowCapture.window = topWindow
            }
        }

        Button {
            text: "Refresh list of windows"
            onClicked: () => {
                top.capturableWindowsList = windowCapture.capturableWindows()
            }
        }

        CheckBox {
            id: windowCaptureActiveCheckBox
            text: "Active"
            onClicked: () => {
                if (checkState === Qt.Checked)
                    windowCapture.start()
                else
                    windowCapture.stop()

                checkState = windowCapture.active === true ? Qt.Checked : Qt.Unchecked
            }
        }

        CheckBox {
            text: "Periodically refresh windows"
            checkState: top.periodicallyRefreshWindows === true ? Qt.Checked : Qt.Unchecked
            onClicked: () => {
                top.periodicallyRefreshWindows = !top.periodicallyRefreshWindows
                checkState = top.periodicallyRefreshWindows === true ? Qt.Checked : Qt.Unchecked
            }
        }

        CheckBox {
            id: hideInvalidWindowsCheckbox
            text: "Hide invalid windows"
            checkState: Qt.Unchecked
        }

        Label {
            text: "Capturable windows:"
        }
        Frame {
            Layout.preferredWidth: 200
            Layout.preferredHeight: windowsListView.model.length > 0 ? 200 : 50
            ListView {
                id: windowsListView
                anchors.fill: parent
                clip: true
                model: top.capturableWindowsList
                    .filter((item) => {
                        if (hideInvalidWindowsCheckbox.checkState === Qt.Unchecked)
                            return true
                        if (item.isValid === false)
                            return false
                        return true
                    })
                currentIndex: model
                    .findIndex((item) => item === windowCapture.window)
                delegate: ItemDelegate {
                    required property var modelData
                    required property int index
                    highlighted: ListView.isCurrentItem
                    function buildText(item) {
                        let outString = ""
                        if (modelData.isValid === false)
                            outString += "(Invalid) "
                        outString += item.description
                        return outString
                    }
                    text: buildText(modelData)
                    onClicked: () => {
                        windowCapture.window = modelData
                    }
                }
            }
        }
    }

    Frame {
        Layout.alignment: Qt.AlignTop
        Layout.fillWidth: true
        Layout.preferredHeight: 400
        VideoOutput {
            anchors.fill: parent
            id: windowCaptureVideoOutput
        }
    }
}