moment/src/gui/PythonBridge/PythonBridge.qml

80 lines
2.1 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) {
2019-12-04 21:22:04 +11:00
let uuid = name + "." + CppUtils.uuid()
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
let future = privates.makeFuture()
call("BRIDGE.call_backend_coro", [name, uuid, args], pyFuture => {
future.privates.pythonFuture = pyFuture
})
return future
}
function callClientCoro(
accountId, name, args=[], onSuccess=null, onError=null
) {
let future = privates.makeFuture()
callCoro("get_client", [accountId], () => {
2019-12-04 21:22:04 +11:00
let uuid = accountId + "." + name + "." + CppUtils.uuid()
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
let 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) {
2019-12-10 04:21:12 +11:00
let func = "load_settings"
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) }
})
}
}