~dandrader/qtmir/fixSurfaceFocus-lp1491034

« back to all changes in this revision

Viewing changes to demos/qml-demo-shell/Window.qml

  • Committer: CI Train Bot
  • Author(s): Daniel d'Andrada
  • Date: 2015-08-27 13:58:46 UTC
  • mfrom: (363.2.6 mirSurface)
  • Revision ID: ci-train-bot@canonical.com-20150827135846-ozjrdoghb3k35q7d
Enable multiple MirSurfaceItems rendering the same MirSurface

So MirSurface is the model and MirSurfaceItem is the view+controller.

+ Make MirSurfaceItem instantiable from QML
+ Isolate all mir::scene::Surface code inside MirSurface
+ Enhanced qml-demo-shell: you can now move, resize and *clone* windows
Approved by: Gerry Boland

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import QtQuick 2.0
 
2
import Unity.Application 0.1
2
3
 
3
4
Rectangle {
4
5
    id: root
5
6
    color: "red"
6
 
    readonly property int margin: 5
7
 
 
8
 
    function setSurface(surface) {
9
 
        surface.parent = root //windowContainer;
10
 
        root.width = surface.width //+ 2*margin;
11
 
        root.height = surface.height //+ margin + title.height;
12
 
 
13
 
        surface.anchors.fill = root //windowContainer;
14
 
//        surface.opacity = 0.99;
15
 
 
16
 
//        title.text = surface.name;
17
 
    }
18
 
 
19
 
//    Text {
20
 
//        id: title
21
 
//        anchors {
22
 
//            left: parent.left
23
 
//            right: parent.right
24
 
//            top: parent.top
25
 
//            leftMargin: margin
26
 
//            rightMargin: margin
27
 
//        }
28
 
//        font.pixelSize: 40
29
 
//    }
30
 
 
31
 
    Rectangle {
32
 
        id: windowContainer
33
 
        anchors {
34
 
            left: parent.left
35
 
            right: parent.right
36
 
            top: parent.top
37
 
//            top: title.bottom
38
 
            bottom: parent.bottom
39
 
            leftMargin: margin
40
 
            rightMargin: margin
41
 
            bottomMargin: margin
42
 
        }
43
 
        color: "pink"
44
 
        opacity: 0.2
 
7
 
 
8
    property alias surface: surfaceItem.surface
 
9
    property bool touchMode: false
 
10
 
 
11
    width: surfaceItem.implicitWidth + 2*borderThickness
 
12
    height: surfaceItem.implicitHeight + 2*borderThickness + titleBar.height
 
13
 
 
14
    signal cloneRequested()
 
15
    property bool cloned: false
 
16
 
 
17
    onTouchModeChanged: {
 
18
        if (touchMode) {
 
19
            x -= borderThicknessTouch - borderThicknessMouse;
 
20
            width += 2*(borderThicknessTouch - borderThicknessMouse);
 
21
            y -= borderThicknessTouch - borderThicknessMouse;
 
22
            height += 2*(borderThicknessTouch - borderThicknessMouse);
 
23
        } else {
 
24
            x += borderThicknessTouch - borderThicknessMouse;
 
25
            width -= 2*(borderThicknessTouch - borderThicknessMouse);
 
26
            y += borderThicknessTouch - borderThicknessMouse;
 
27
            height -= 2*(borderThicknessTouch - borderThicknessMouse);
 
28
        }
 
29
    }
 
30
 
 
31
    readonly property real minWidth: 100
 
32
    readonly property real minHeight: 100
 
33
 
 
34
    property real borderThickness: touchMode ? borderThicknessTouch : borderThicknessMouse
 
35
    readonly property real borderThicknessMouse: 10
 
36
    readonly property real borderThicknessTouch: 40
 
37
 
 
38
    states: [
 
39
        State {
 
40
            name: "closed"
 
41
            when: (surface && !surface.live) || titleBar.closeRequested
 
42
        }
 
43
    ]
 
44
    transitions: [
 
45
        Transition {
 
46
            from: ""; to: "closed"
 
47
            SequentialAnimation {
 
48
                PropertyAnimation {
 
49
                    target: root
 
50
                    property: "scale"
 
51
                    easing.type: Easing.InBack
 
52
                    duration: 400
 
53
                    from: 1.0
 
54
                    to: 0.0
 
55
                }
 
56
                ScriptAction { script: { root.destroy(); } }
 
57
            }
 
58
        }
 
59
    ]
 
60
 
 
61
 
 
62
    MouseArea {
 
63
        anchors.fill: parent
 
64
 
 
65
        property real startX
 
66
        property real startY
 
67
        property real startWidth
 
68
        property real startHeight
 
69
        property bool leftBorder
 
70
        property bool rightBorder
 
71
        property bool topBorder
 
72
        property bool bottomBorder
 
73
        property bool dragging
 
74
        onPressedChanged: {
 
75
            if (pressed) {
 
76
                var pos = mapToItem(root.parent, mouseX, mouseY);
 
77
                startX = pos.x;
 
78
                startY = pos.y;
 
79
                startWidth = width;
 
80
                startHeight = height;
 
81
                leftBorder = mouseX > 0 && mouseX < root.borderThickness;
 
82
                rightBorder = mouseX > (root.width - root.borderThickness) && mouseX < root.width;
 
83
                topBorder = mouseY > 0 && mouseY < root.borderThickness;
 
84
                bottomBorder = mouseY > (root.height - root.borderThickness) && mouseY < root.height;
 
85
                dragging = true;
 
86
            } else {
 
87
                dragging = false;
 
88
            }
 
89
        }
 
90
 
 
91
        onMouseXChanged: {
 
92
            if (!pressed || !dragging) {
 
93
                return;
 
94
            }
 
95
 
 
96
            var pos = mapToItem(root.parent, mouseX, mouseY);
 
97
 
 
98
            if (leftBorder) {
 
99
 
 
100
                if (startX + startWidth - pos.x > root.minWidth) {
 
101
                    root.x = pos.x;
 
102
                    root.width = startX + startWidth - root.x;
 
103
                    startX = root.x;
 
104
                    startWidth = root.width;
 
105
                }
 
106
 
 
107
            } else if (rightBorder) {
 
108
                var deltaX = pos.x - startX;
 
109
                if (startWidth + deltaX >= root.minWidth) {
 
110
                    root.width = startWidth + deltaX;
 
111
                } else {
 
112
                    root.width = root.minWidth;
 
113
                }
 
114
            }
 
115
        }
 
116
 
 
117
        onMouseYChanged: {
 
118
            if (!pressed || !dragging) {
 
119
                return;
 
120
            }
 
121
 
 
122
            var pos = mapToItem(root.parent, mouseX, mouseY);
 
123
 
 
124
            if (topBorder) {
 
125
 
 
126
                if (startY + startHeight - pos.y > root.minHeight) {
 
127
                    root.y = pos.y;
 
128
                    root.height = startY + startHeight - root.y;
 
129
                    startY = root.y;
 
130
                    startHeight = root.height;
 
131
                }
 
132
 
 
133
            } else if (bottomBorder) {
 
134
                var deltaY = pos.y - startY;
 
135
                if (startHeight + deltaY >= root.minHeight) {
 
136
                    root.height = startHeight + deltaY;
 
137
                } else {
 
138
                    root.height = root.minHeight;
 
139
                }
 
140
            }
 
141
        }
 
142
    }
 
143
 
 
144
    TitleBar {
 
145
        id: titleBar
 
146
        anchors.left: parent.left
 
147
        anchors.leftMargin: root.borderThickness
 
148
        anchors.right: parent.right
 
149
        anchors.rightMargin: root.borderThickness
 
150
        anchors.top: parent.top
 
151
        anchors.topMargin: root.borderThickness
 
152
 
 
153
        target: root
 
154
        cloned: root.cloned
 
155
        onCloneRequested: { root.cloneRequested(); }
 
156
    }
 
157
 
 
158
    MirSurfaceItem {
 
159
        id: surfaceItem
 
160
 
 
161
        anchors.top: titleBar.bottom
 
162
        anchors.left: parent.left
 
163
        anchors.leftMargin: root.borderThickness
 
164
        anchors.right: parent.right
 
165
        anchors.rightMargin: root.borderThickness
 
166
        anchors.bottom: parent.bottom
 
167
        anchors.bottomMargin: root.borderThickness
 
168
 
 
169
        consumesInput: !root.cloned
 
170
        surfaceWidth: root.cloned ? -1 : width
 
171
        surfaceHeight: root.cloned ? -1 : height
45
172
    }
46
173
}