summaryrefslogtreecommitdiff
path: root/examples/wayland/multi-screen/qml/main.qml
blob: 1180d30b40e332dab456034ae8d9f5228b37005d (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
// Copyright (C) 2017 The Qt Company Ltd.
// Copyright (C) 2017 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQml
import QtQuick
import QtWayland.Compositor
import QtWayland.Compositor.XdgShell
import QtWayland.Compositor.WlShell
import QtQml.Models

WaylandCompositor {
    id: comp

    ListModel {
        id: emulatedScreens
        ListElement { name: "left";   virtualX: 0;    virtualY: 0; width: 800; height: 600 }
        ListElement { name: "middle"; virtualX: 800;  virtualY: 0; width: 800; height: 600 }
        ListElement { name: "right";  virtualX: 1600; virtualY: 0; width: 800; height: 600 }
    }

    property bool emulated: Qt.application.screens.length < 2

    //! [screens]
    Instantiator {
        id: screens
        model: emulated ? emulatedScreens : Qt.application.screens

        delegate: CompositorScreen {
            surfaceArea.color: "lightsteelblue"
            text: name
            compositor: comp
            screen: emulated ? Qt.application.screens[0] : modelData
            Component.onCompleted: if (!comp.defaultOutput) comp.defaultOutput = this
            position: Qt.point(virtualX, virtualY)
            windowed: emulated
        }
    }
    //! [screens]

    Component {
        id: chromeComponent
        Chrome {}
    }

    Component {
        id: moveItemComponent
        Item {}
    }

    Item {
        id: rootItem
    }

    WlShell {
        onWlShellSurfaceCreated: (shellSurface) => handleShellSurfaceCreated(shellSurface)
    }

    XdgShell {
        onToplevelCreated: (toplevel, xdgSurface) => handleShellSurfaceCreated(xdgSurface)
    }

    function createShellSurfaceItem(shellSurface, moveItem, output) {
        // ![parenting]
        var parentSurfaceItem = output.viewsBySurface[shellSurface.parentSurface];
        var parent = parentSurfaceItem || output.surfaceArea;
        // ![parenting]
        var item = chromeComponent.createObject(parent, {
            "shellSurface": shellSurface,
            "moveItem": moveItem,
            "output": output
        });
        if (parentSurfaceItem) {
            item.x += output.position.x;
            item.y += output.position.y;
        }
        output.viewsBySurface[shellSurface.surface] = item;
    }

    function handleShellSurfaceCreated(shellSurface) {
        var moveItem = moveItemComponent.createObject(rootItem, {
            "x": screens.objectAt(0).position.x,
            "y": screens.objectAt(0).position.y,
            "width": Qt.binding(function() { return shellSurface.surface.width; }),
            "height": Qt.binding(function() { return shellSurface.surface.height; })
        });
        //! [createShellSurfaceItems]
        for (var i = 0; i < screens.count; ++i)
            createShellSurfaceItem(shellSurface, moveItem, screens.objectAt(i));
        //! [createShellSurfaceItems]
    }
}