moment/src/qml/Base/HMxcImage.qml
2019-11-05 09:19:48 -04:00

63 lines
1.6 KiB
QML

import QtQuick 2.12
HImage {
id: image
source: sourceOverride || (show ? cachedPath : "")
onWidthChanged: Qt.callLater(update)
onHeightChanged: Qt.callLater(update)
onVisibleChanged: Qt.callLater(update)
onMxcChanged: {
Qt.callLater(update)
if (mxc.startsWith("mxc://")) {
py.callCoro("mxc_to_http", [mxc], http => {
image.httpUrl = http || ""
})
} else {
httpUrl = mxc
}
}
property string clientUserId
property string mxc
property string sourceOverride: ""
property bool thumbnail: true
property var cryptDict: ({})
property bool show: false
property string cachedPath: ""
property string httpUrl: ""
function update() {
let w = sourceSize.width || width
let h = sourceSize.height || height
if (! image.mxc || w < 1 || h < 1 ) {
show = false
return
}
if (! image) return // if it was destroyed
if (! image.mxc.startsWith("mxc://")) {
if (source != mxc) source = mxc
show = image.visible
return
}
let method = image.thumbnail ? "get_thumbnail" : "get_media"
let args = image.thumbnail ?
[image.mxc, w, h, cryptDict] : [image.mxc, cryptDict]
py.callClientCoro(
clientUserId, "media_cache." + method, args, path => {
if (! image) return
if (image.cachedPath != path) image.cachedPath = path
show = image.visible
}
)
}
}