This repository has been archived on 2026-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
powersync-app/mea/ui/MagicPage.qml
Michael Zanetti e2ceadc7a1 fix a crash when editing rules
there seems to be a race condition in StackView.onRemoved where it sometimes
makes the QML engine crash if accessing context properties from outside the
function context in the handler.

Sometimes it would just print that those properties are undefined and not complete
the handler code, other times however, it does actually find the properties
but crash upon accessing them.
2018-06-25 12:03:43 +02:00

133 lines
3.9 KiB
QML

import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.2
import "components"
import Mea 1.0
Page {
id: root
header: GuhHeader {
text: qsTr("Magic")
onBackPressed: pageStack.pop()
HeaderButton {
imageSource: Qt.resolvedUrl("images/add.svg")
onClicked: {
addRule()
}
}
}
function addRule() {
var newRule = Engine.ruleManager.createNewRule();
d.editRulePage = pageStack.push(Qt.resolvedUrl("magic/EditRulePage.qml"), {rule: newRule });
d.editRulePage.StackView.onRemoved.connect(function() {
newRule.destroy();
})
d.editRulePage.onAccept.connect(function() {
d.editRulePage.busy = true;
Engine.ruleManager.addRule(d.editRulePage.rule);
})
d.editRulePage.onCancel.connect(function() {
pageStack.pop();
})
}
QtObject {
id: d
property var editRulePage: null
}
Connections {
target: Engine.ruleManager
onAddRuleReply: {
d.editRulePage.busy = false;
if (ruleError == "RuleErrorNoError") {
pageStack.pop();
} else {
var popup = errorDialog.createObject(app, {errorCode: ruleError })
popup.open();
}
}
onEditRuleReply: {
d.editRulePage.busy = false;
if (ruleError == "RuleErrorNoError") {
pageStack.pop();
} else {
var popup = errorDialog.createObject(app, {errorCode: ruleError })
popup.open();
}
}
}
ListView {
anchors.fill: parent
model: Engine.ruleManager.rules
delegate: MeaListItemDelegate {
id: ruleDelegate
width: parent.width
iconName: "../images/magic.svg"
iconColor: !model.enabled ? "red" : (model.active ? app.guhAccent : "grey")
text: model.name
canDelete: true
onDeleteClicked: Engine.ruleManager.removeRule(model.id)
onClicked: {
var newRule = Engine.ruleManager.rules.get(index).clone();
d.editRulePage = pageStack.push(Qt.resolvedUrl("magic/EditRulePage.qml"), {rule: newRule})
d.editRulePage.StackView.onRemoved.connect(function() {
newRule.destroy();
})
d.editRulePage.onAccept.connect(function() {
d.editRulePage.busy = true;
Engine.ruleManager.editRule(d.editRulePage.rule);
})
d.editRulePage.onCancel.connect(function() {
pageStack.pop();
})
}
}
}
ColumnLayout {
anchors { left: parent.left; right: parent.right; margins: app.margins }
anchors.verticalCenter: parent.verticalCenter
spacing: app.margins * 2
visible: Engine.ruleManager.rules.count === 0
Label {
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: qsTr("There is no magic set up yet.")
wrapMode: Text.WordWrap
color: app.guhAccent
font.pixelSize: app.largeFont
}
Label {
text: qsTr("Add some using the wizard stick!")
Layout.fillWidth: true
wrapMode: Text.WordWrap
horizontalAlignment: Text.AlignHCenter
}
AbstractButton {
Layout.preferredHeight: app.iconSize * 4
Layout.preferredWidth: height
Layout.alignment: Qt.AlignHCenter
ColorIcon {
anchors.fill: parent
name: "../images/magic.svg"
}
onClicked: addRule()
}
}
Component {
id: errorDialog
ErrorDialog {}
}
}