|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
| 3 | +import QtQuick |
| 4 | +import QtQuick.Controls |
| 5 | +import QtQuick.Controls.Material |
| 6 | +import QtQuick.Layouts |
| 7 | +import ScratchCPP.Ui |
| 8 | +import "internal" |
| 9 | + |
| 10 | +Item { |
| 11 | + id: root |
| 12 | + |
| 13 | + property Component contentItem: Item {} |
| 14 | + readonly property alias contents: dialog.contents |
| 15 | + property int standardButtons |
| 16 | + property string title: Qt.application.displayName |
| 17 | + property bool fixedSize: true |
| 18 | + property bool maximized: false |
| 19 | + property bool autoClose: true |
| 20 | + property int nativeDialogMinimumWidth: dialog.contentItem.contentLayout.implicitWidth |
| 21 | + property int nativeDialogMinimumHeight: dialog.contentItem.contentLayout.implicitHeight |
| 22 | + readonly property bool isNative: true |
| 23 | + property bool closable: true |
| 24 | + signal accepted() |
| 25 | + signal applied() |
| 26 | + signal discarded() |
| 27 | + signal helpRequested() |
| 28 | + signal rejected() |
| 29 | + signal reset() |
| 30 | + signal opened() |
| 31 | + signal closed() |
| 32 | + signal aboutToShow() |
| 33 | + signal aboutToHide() |
| 34 | + signal focusReset() |
| 35 | + |
| 36 | + visible: dialog.visible |
| 37 | + |
| 38 | + onVisibleChanged: { |
| 39 | + if (!priv.initialized) { |
| 40 | + priv.initialized = true; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + priv.contentsActive = visible; |
| 45 | + dialog.visible = visible; |
| 46 | + |
| 47 | + if (visible) { |
| 48 | + UiEngine.activeFocusItem = dialog.activeFocusItem; |
| 49 | + aboutToShow(); |
| 50 | + opened(); |
| 51 | + priv.sizeUpdate = !priv.sizeUpdate; |
| 52 | + } else { |
| 53 | + aboutToHide(); |
| 54 | + closed(); |
| 55 | + |
| 56 | + if (!priv.closedFromQml) |
| 57 | + rejected(); |
| 58 | + } |
| 59 | + |
| 60 | + priv.closedFromQml = false; |
| 61 | + visible = Qt.binding(function() { return dialog.visible }); |
| 62 | + } |
| 63 | + |
| 64 | + onMaximizedChanged: { |
| 65 | + if (maximized) |
| 66 | + dialog.setMaximizedState(); |
| 67 | + else |
| 68 | + dialog.setNormalState(); |
| 69 | + } |
| 70 | + |
| 71 | + function open() { |
| 72 | + visible = true; |
| 73 | + } |
| 74 | + |
| 75 | + function close() { |
| 76 | + priv.closedFromQml = true; |
| 77 | + visible = false; |
| 78 | + } |
| 79 | + |
| 80 | + function accept() { |
| 81 | + close(); |
| 82 | + accepted(); |
| 83 | + } |
| 84 | + |
| 85 | + function reject() { |
| 86 | + close(); |
| 87 | + rejected(); |
| 88 | + } |
| 89 | + |
| 90 | + function standardButton(button) { |
| 91 | + return dialog.contentItem.buttonBoxLoader.item.standardButton(button); |
| 92 | + } |
| 93 | + |
| 94 | + QtObject { |
| 95 | + id: priv |
| 96 | + property bool closedFromQml: true |
| 97 | + property bool initialized: false |
| 98 | + property bool sizeUpdate: false |
| 99 | + property bool contentsActive: true |
| 100 | + } |
| 101 | + |
| 102 | + DialogView { |
| 103 | + readonly property Item contents: contentItem.contentsLoader.item |
| 104 | + id: dialog |
| 105 | + title: root.title |
| 106 | + visible: false |
| 107 | + autoClose: root.autoClose |
| 108 | + closable: root.closable |
| 109 | + minimumWidth: nativeDialogMinimumWidth |
| 110 | + minimumHeight: nativeDialogMinimumHeight |
| 111 | + maximumWidth: { |
| 112 | + priv.sizeUpdate; |
| 113 | + return fixedSize ? Math.max(contentItem.contentLayout.implicitWidth, minimumWidth) : { maximumWidth = maximumWidth }; |
| 114 | + } |
| 115 | + maximumHeight: { |
| 116 | + priv.sizeUpdate; |
| 117 | + return fixedSize? Math.max(contentItem.contentLayout.implicitHeight, minimumHeight) : { maximumHeight = maximumHeight }; |
| 118 | + } |
| 119 | + onActiveFocusItemChanged: UiEngine.activeFocusItem = activeFocusItem |
| 120 | + |
| 121 | + contentItem: Rectangle { |
| 122 | + property alias contentLayout: contentLayout |
| 123 | + property alias contentsLoader: contentsLoader |
| 124 | + property alias buttonBoxLoader: buttonBoxLoader |
| 125 | + anchors.fill: parent |
| 126 | + // TODO: Read colors from ThemeEngine |
| 127 | + color: /*ThemeEngine.bgColor*/ Material.background |
| 128 | + //Material.background: ThemeEngine.bgColor |
| 129 | + //Material.accent: ThemeEngine.currentAccentColor |
| 130 | + //Material.theme: ThemeEngine.theme === ThemeEngine.DarkTheme ? Material.Dark : Material.Light |
| 131 | + Material.theme: Material.Dark |
| 132 | + |
| 133 | + ColumnLayout { |
| 134 | + id: contentLayout |
| 135 | + anchors.fill: parent |
| 136 | + |
| 137 | + Loader { |
| 138 | + id: contentsLoader |
| 139 | + sourceComponent: root.contentItem |
| 140 | + active: priv.contentsActive |
| 141 | + Layout.fillWidth: true |
| 142 | + Layout.fillHeight: true |
| 143 | + } |
| 144 | + |
| 145 | + Loader { |
| 146 | + id: buttonBoxLoader |
| 147 | + |
| 148 | + sourceComponent: CustomDialogButtonBox { |
| 149 | + standardButtons: root.standardButtons |
| 150 | + onAccepted: root.accept() |
| 151 | + onApplied: root.applied() |
| 152 | + onDiscarded: root.discarded() |
| 153 | + onHelpRequested: root.helpRequested() |
| 154 | + onRejected: root.reject() |
| 155 | + onReset: root.reset() |
| 156 | + onFocusOut: { |
| 157 | + root.focusReset(); |
| 158 | + root.forceActiveFocus(Qt.TabFocusReason); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + Layout.fillWidth: true |
| 163 | + |
| 164 | + /*Connections { |
| 165 | + target: LanguageManager |
| 166 | +
|
| 167 | + function onLanguageChanged() { |
| 168 | + translationTimer.start(); |
| 169 | + } |
| 170 | + } |
| 171 | +
|
| 172 | + Timer { |
| 173 | + id: translationTimer |
| 174 | + interval: 16 |
| 175 | + running: false |
| 176 | + repeat: false |
| 177 | + onTriggered: { |
| 178 | + buttonBoxLoader.active = 0; |
| 179 | + buttonBoxLoader.active = 1; |
| 180 | + } |
| 181 | + }*/ |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + Component.onCompleted: { |
| 188 | + if (!visible) |
| 189 | + priv.contentsActive = false; |
| 190 | + |
| 191 | + maximizedChanged(); |
| 192 | + } |
| 193 | +} |
0 commit comments