moment/src/gui/PythonBridge/PythonBridge.qml

77 lines
2.2 KiB
QML
Raw Normal View History

2019-12-19 22:46:16 +11:00
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import io.thp.pyotherside 1.5
2019-12-27 23:58:24 +11:00
import CppUtils 0.1
import "Privates"
Python {
id: py
readonly property QtObject privates: QtObject {
function makeFuture(callback) {
return Qt.createComponent("Future.qml")
.createObject(py, { bridge: py })
}
}
2019-12-06 23:44:45 +11:00
function setattr(obj, attr, value, callback=null) {
py.call(py.getattr(obj, "__setattr__"), [attr, value], callback)
}
function callCoro(name, args=[], onSuccess=null, onError=null) {
2020-03-08 19:27:43 +11:00
const uuid = name + "." + CppUtils.uuid()
const future = privates.makeFuture()
2020-03-08 19:27:43 +11:00
Globals.pendingCoroutines[uuid] = {future, onSuccess, onError}
Globals.pendingCoroutinesChanged()
call("BRIDGE.call_backend_coro", [name, uuid, args], pyFuture => {
future.privates.pythonFuture = pyFuture
})
return future
}
function callClientCoro(
accountId, name, args=[], onSuccess=null, onError=null
) {
2020-03-08 19:27:43 +11:00
const future = privates.makeFuture()
callCoro("get_client", [accountId], () => {
2020-03-08 19:27:43 +11:00
const uuid = accountId + "." + name + "." + CppUtils.uuid()
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
Globals.pendingCoroutinesChanged()
2020-03-08 19:27:43 +11:00
const call_args = [accountId, name, uuid, args]
call("BRIDGE.call_client_coro", call_args, pyFuture => {
future.privates.pythonFuture = pyFuture
})
})
return future
}
function saveConfig(backend_attribute, data, callback=null) {
if (! py.ready) { return } // config not loaded yet
return callCoro(backend_attribute + ".write", [data], callback)
}
function loadSettings(callback=null) {
2020-03-08 19:27:43 +11:00
const func = "load_settings"
2019-12-10 04:21:12 +11:00
return callCoro(func, [], ([settings, uiState, history, theme]) => {
window.settings = settings
window.uiState = uiState
2019-12-10 04:21:12 +11:00
window.history = history
window.theme = Qt.createQmlObject(theme, window, "theme")
if (callback) { callback(settings, uiState, theme) }
})
}
}