aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/StudioControls/PopupDialog.qml
blob: 4da26cf49403f18fc4909b0dd3fc8364d17f024c (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import QtQuick.Controls.Basic as Basic
import QtQuick.Window
import QtQuick.Shapes
import StudioTheme as StudioTheme
import StudioWindowManager

QtObject {
    id: root

    property StudioTheme.ControlStyle style: StudioTheme.Values.controlStyle

    property alias titleBar: titleBarContent.children
    default property alias content: mainContent.children
    property alias contentWidth: mainContent.width

    property int width: 320
    property int height: column.implicitHeight
    property int maximumWidth: -1
    property int maximumHeight: -1

    property alias flags: window.flags
    property alias visible: window.visible

    property int anchorGap: 10
    property int edge: Qt.LeftEdge
    property int actualEdge: root.edge
    //property alias chevronVisible: chevron.visible

    property rect __itemGlobal: Qt.rect(0, 0, 100, 100)

    property bool keepOpen: false

    signal closing(close: var)

    function showGlobal() {
        var pos = WindowManager.globalCursorPosition()
        root.__itemGlobal = Qt.rect(pos.x, pos.y, 300, 20)
        //root.chevronVisible = false
        root.layout()
        window.show()
        window.raise()
    }

    function show(target: Item) {
        var originGlobal = target.mapToGlobal(0, 0)
        root.__itemGlobal = Qt.rect(originGlobal.x, originGlobal.y, target.width, target.height)
        //root.chevronVisible = true
        root.layout()
        window.show()
        window.raise()
    }

    function close() {
        window.close()
    }

    function layout() {
        let position = Qt.point(root.__itemGlobal.x, root.__itemGlobal.y)
        var screen = WindowManager.getScreenGeometry(position)

        // Collect region information
        let edges = window.getRegions(screen, root.__itemGlobal)

        if (Object.keys(edges).length === 0) {
            console.log("Warning: Couldn't find regions.")
            return
        }

        // Determine edge
        let edge = window.findPlacement(edges)
        root.actualEdge = edge

        let anchor = edges[edge].anchor
        let popoverRect = window.popoverGeometry(edge, anchor, edges[edge].region)

        //if (chevron.visible)
        //    chevron.layout(edge, popoverRect, anchor)

        window.x = popoverRect.x
        window.y = popoverRect.y
    }

    property Window window: Window {
        id: window

        property int margin: 0 //20

        width: root.width + (2 * window.margin)
        height: root.height + (2 * window.margin)
        maximumWidth: {
            if (root.maximumWidth < 0)
                return root.width + (2 * window.margin)
            else
                return root.maximumWidth + (2 * window.margin)
        }
        maximumHeight:{
            if (root.maximumHeight < 0)
                return root.height + (2 * window.margin)
            else
                return root.maximumHeight + (2 * window.margin)
        }
        visible: false
        flags: Qt.FramelessWindowHint | Qt.Dialog | Qt.WindowStaysOnTopHint
        color: "transparent"

        onClosing: function (close) {
            root.closing(close)
        }

        function findPlacement(edges: var): int {
            if (edges[root.edge].fit) // Original edge does fit
                return root.edge

            return window.findAlternativePlacement(edges)
        }

        function findAlternativePlacement(edges: var): int {
            let horizontal = (Qt.LeftEdge | Qt.RightEdge)
            if (root.edge & horizontal)
                if (edges[root.edge ^ horizontal].fit)
                    return root.edge ^ horizontal

            let vertical = (Qt.TopEdge | Qt.BottomEdge)
            if (root.edge & vertical)
                if (edges[root.edge ^ vertical].fit)
                    return root.edge ^ vertical

            // Take the first that fits
            for (var key in edges) {
                if (edges[key].fit)
                    return Number(key)
            }

            return Qt.LeftEdge // Default
        }

        function contains(a: rect, b: rect): bool {
            let halfSizeA = Qt.size(a.width * 0.5, a.height * 0.5)
            let halfSizeB = Qt.size(b.width * 0.5, b.height * 0.5)

            let centerA = Qt.point(a.x + halfSizeA.width, a.y + halfSizeA.height)
            let centerB = Qt.point(b.x + halfSizeB.width, b.y + halfSizeB.height)

            if (Math.abs(centerA.x - centerB.x) > (halfSizeA.width + halfSizeB.width))
                return false

            if (Math.abs(centerA.y - centerB.y) > (halfSizeA.height + halfSizeB.height))
                return false

            return true
        }

        function getRegions(source: rect, target: rect) {
            var edges = {}

            // Overlaps or Inside
            if (!window.contains(source, target))
                return edges

            var targetCenter = Qt.point(target.x + (target.width * 0.5),
                                        target.y + (target.height * 0.5))

            // Just as a reminder why calculating custom right and bottom:
            // > Note that for historical reasons this function returns top() + height() - 1;
            // > use y() + height() to retrieve the true y-coordinate.
            let sourceRight = source.x + source.width
            let sourceBottom = source.y + source.height

            // TOP
            let topAnchor = Qt.point(targetCenter.x, target.y - root.anchorGap)
            let topRegion = Qt.rect(source.x,
                                    source.y,
                                    source.width,
                                    (topAnchor.y < source.top) ? 0 : Math.abs(topAnchor.y - source.top))

            edges[Qt.TopEdge] = {
                anchor: topAnchor,
                region: topRegion,
                fit: topRegion.width >= window.maximumWidth
                     && topRegion.height >= window.maximumHeight
            }

            // RIGHT
            let rightAnchor = Qt.point(target.x + target.width + root.anchorGap, targetCenter.y)
            let rightRegion = Qt.rect(rightAnchor.x,
                                      source.y,
                                      (rightAnchor.x > sourceRight) ? 0 : Math.abs(sourceRight - rightAnchor.x),
                                      source.height)

            edges[Qt.RightEdge] = {
                anchor: rightAnchor,
                region: rightRegion,
                fit: rightRegion.width >= window.maximumWidth
                     && rightRegion.height >= window.maximumHeight
            }

            // BOTTOM
            let bottomAnchor = Qt.point(targetCenter.x, target.y + target.height + root.anchorGap)
            let bottomRegion = Qt.rect(source.x,
                                       bottomAnchor.y,
                                       source.width,
                                       (bottomAnchor.y > sourceBottom) ? 0 : Math.abs(sourceBottom - bottomAnchor.y))

            edges[Qt.BottomEdge] = {
                anchor: bottomAnchor,
                region: bottomRegion,
                fit: bottomRegion.width >= window.maximumWidth
                     && bottomRegion.height >= window.maximumHeight
            }

            // LEFT
            let leftAnchor = Qt.point(target.x - root.anchorGap, targetCenter.y)
            let leftRegion = Qt.rect(source.x,
                                     source.y,
                                     (leftAnchor.x < source.left) ? 0 : Math.abs(leftAnchor.x - source.left),
                                     source.height)

            edges[Qt.LeftEdge] = {
                anchor: leftAnchor,
                region: leftRegion,
                fit: leftRegion.width >= window.maximumWidth
                     && leftRegion.height >= window.maximumHeight
            }

            return edges
        }

        function popoverGeometry(edge: int, anchor: point, region: rect) {
            if (edge === Qt.TopEdge) {
                let height = Math.min(window.height, region.height)
                return Qt.rect(Math.max(region.x,
                                        Math.min(anchor.x - (window.width * 0.5),
                                                 region.x + region.width - window.width)),
                               anchor.y - height,
                               Math.min(window.width, region.width),
                               height)
            }

            if (edge === Qt.RightEdge) {
                let width = Math.min(window.width, region.width)
                return Qt.rect(anchor.x,
                               Math.max(region.y,
                                        Math.min(anchor.y - (window.height * 0.5),
                                                 region.y + region.height - window.height)),
                               width,
                               Math.min(window.height, region.height))
            }

            if (edge === Qt.BottomEdge) {
                let height = Math.min(window.height, region.height)
                return Qt.rect(Math.max(region.x,
                                        Math.min(anchor.x - (window.width * 0.5),
                                                 region.x + region.width - window.width)),
                               anchor.y,
                               Math.min(window.width, region.width),
                               height)
            }

            if (edge === Qt.LeftEdge) {
                let width = Math.min(window.width, region.width)
                return Qt.rect(anchor.x - width,
                               Math.max(region.y,
                                        Math.min(anchor.y - (window.height * 0.5),
                                                 region.y + region.height - window.height)),
                               width,
                               Math.min(window.height, region.height))
            }
        }

        onHeightChanged: {
            if (window.visible)
                root.layout()
        }

        Component.onCompleted: {
            if (window.visible)
                root.layout()
        }

        Connections {
            target: WindowManager
            enabled: root.visible

            function onFocusWindowChanged(focusWindow) {
                if (!focusWindow)
                    return

                if (root.keepOpen)
                    return

                if (focusWindow !== window && focusWindow.transientParent !== window)
                    root.close()
            }

            function onAboutToQuit() {
                root.close()
            }

            function onMainWindowVisibleChanged(value) {
                if (!value)
                    root.close()
            }
        }

        Rectangle {
            id: background
            anchors.fill: parent
            anchors.margins: window.margin
            color: StudioTheme.Values.themePopoutBackground
            border.color: "#636363"
            border.width: StudioTheme.Values.border

            TapHandler {
                id: tapHandler
                onTapped: root.close()
            }

            containmentMask: QtObject {
                function contains(point: point): bool {
                    return point.x < 0 || point.x > background.width
                        || point.y < 0 || point.y > background.height
                }
            }
        }
/*
        // The chevron will be reactivated when we fixed all the issues that where found during testing.
        // * Potential Qt bug: black background instead of transparent border due to GPU selection on Windows
        // * Ghost chevron on macOS after dragging the window
        Shape {
            id: chevron

            property int chevronWidth: window.margin

            anchors.fill: parent

            function layout(edge: int, rect: rect, anchor: point) {
                let center = Qt.point(rect.x + (rect.width * 0.5),
                                      rect.y + (rect.height * 0.5))

                // Horizontal
                if (edge === Qt.LeftEdge || edge === Qt.RightEdge) {
                    let topLimit = window.margin
                    let bottomLimit = window.height - window.margin - background.border.width
                    let point = Math.round((window.height * 0.5) + (anchor.y - center.y))

                    peak.y = Math.max(topLimit, Math.min(bottomLimit, point))

                    let topLimitChevron = topLimit + chevron.chevronWidth
                    let bottomLimitChevron = bottomLimit - chevron.chevronWidth

                    path.startY = Math.max(topLimit, Math.min(bottomLimitChevron, point - chevron.chevronWidth))
                    end.y = Math.max(topLimitChevron, Math.min(bottomLimit, point + chevron.chevronWidth))
                }

                if (edge === Qt.LeftEdge) {
                    peak.x = window.width - background.border.width
                    path.startX = end.x = window.width - window.margin - background.border.width
                }

                if (edge === Qt.RightEdge) {
                    peak.x = background.border.width
                    path.startX = end.x = window.margin + background.border.width
                }

                // Vertical
                if (edge === Qt.TopEdge || edge === Qt.BottomEdge) {
                    let leftLimit = window.margin + background.border.width
                    let rightLimit = window.width - window.margin
                    let point = Math.round((window.width * 0.5) + (anchor.x - center.x))

                    peak.x = Math.max(leftLimit, Math.min(rightLimit, point))

                    let leftLimitChevron = leftLimit + chevron.chevronWidth
                    let rightLimitChevron = rightLimit - chevron.chevronWidth

                    path.startX = Math.max(leftLimit, Math.min(rightLimitChevron, point - chevron.chevronWidth))
                    end.x = Math.max(leftLimitChevron, Math.min(rightLimit, point + chevron.chevronWidth))
                }

                if (edge === Qt.TopEdge) {
                    peak.y = window.height - background.border.width
                    path.startY = end.y = window.height - window.margin - background.border.width
                }

                if (edge === Qt.BottomEdge) {
                    peak.y = background.border.width
                    path.startY = end.y = window.margin + background.border.width
                }
            }

            ShapePath {
                id: path
                strokeStyle: ShapePath.SolidLine
                strokeWidth: background.border.width
                strokeColor: background.border.color
                fillColor: background.color
                startX: 0
                startY: 0

                PathLine { id: peak; x: 0; y: 0 }

                PathLine { id: end; x: 0; y: 0 }
            }
        }
*/
        Column {
            id: column
            anchors.fill: parent
            anchors.margins: window.margin + StudioTheme.Values.border

            Item {
                id: titleBarItem
                width: parent.width
                height: StudioTheme.Values.titleBarHeight

                DragHandler {
                    id: dragHandler

                    target: null
                    grabPermissions: PointerHandler.CanTakeOverFromAnything
                    onActiveChanged: {
                        if (dragHandler.active)
                            window.startSystemMove() // QTBUG-102488
                    }
                }

                Row {
                    id: row
                    anchors.fill: parent
                    anchors.leftMargin: StudioTheme.Values.popupMargin
                    anchors.rightMargin: StudioTheme.Values.popupMargin
                    spacing: 0

                    Item {
                        id: titleBarContent
                        width: row.width - closeIndicator.width
                        height: row.height
                    }

                    IconIndicator {
                        id: closeIndicator
                        anchors.verticalCenter: parent.verticalCenter
                        icon: StudioTheme.Constants.colorPopupClose
                        pixelSize: StudioTheme.Values.myIconFontSize
                        onClicked: root.close()
                    }
                }
            }

            Rectangle {
                width: parent.width - 8
                height: StudioTheme.Values.border
                anchors.horizontalCenter: parent.horizontalCenter
                color: "#636363"
            }

            Basic.ScrollView {
                id: scrollView
                width: parent.width
                height: {
                    let actualHeight = mainContent.childrenRect.height + 2 * StudioTheme.Values.popupMargin

                    if (root.maximumHeight < 0)
                        return actualHeight

                    return Math.min(actualHeight,
                                    root.maximumHeight - titleBarItem.height - 3 * StudioTheme.Values.border)
                }
                padding: StudioTheme.Values.popupMargin
                clip: true

                ScrollBar.vertical: TransientScrollBar {
                    id: verticalBar
                    style: StudioTheme.Values.controlStyle
                    parent: scrollView
                    x: scrollView.width - verticalBar.width
                    y: scrollView.topPadding
                    height: scrollView.availableHeight
                    orientation: Qt.Vertical

                    show: (scrollView.hovered || scrollView.focus) && verticalBar.isNeeded
                    //otherInUse: horizontalBar.inUse
                }

                Flickable {
                    id: frame
                    boundsMovement: Flickable.StopAtBounds
                    boundsBehavior: Flickable.StopAtBounds
                    contentWidth: mainContent.width
                    contentHeight: mainContent.height

                    Item {
                        id: mainContent
                        width: scrollView.width - 2 * scrollView.padding
                        height: mainContent.childrenRect.height
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                }
            }
        }
    }
}