Improve preview of wide images

Avoid clipping. Also, handling file updates on C++ side should be good
for performance.
This commit is contained in:
Slava Monich 2020-12-03 04:42:51 +02:00
parent 0241817678
commit 2638c3837d

View file

@ -18,70 +18,63 @@
*/ */
import QtQuick 2.6 import QtQuick 2.6
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import WerkWolf.Fernschreiber 1.0
Item { Item {
id: imagePreviewItem id: imagePreviewItem
property ListItem messageListItem property ListItem messageListItem
property MessageOverlayFlickable overlayFlickable property MessageOverlayFlickable overlayFlickable
property var rawMessage: messageListItem ? messageListItem.myMessage : overlayFlickable.overlayMessage property var rawMessage: messageListItem ? messageListItem.myMessage : overlayFlickable.overlayMessage
property var photoData: rawMessage.content.photo; property var photoData: rawMessage.content.photo
property var pictureFileInformation; readonly property int defaultHeight: Math.round(width * 2 / 3)
width: parent.width width: parent.width
height: width * 2 / 3 height: singleImage.visible ? Math.min(defaultHeight, singleImage.bestHeight + Theme.paddingSmall) : defaultHeight
Component.onCompleted: { Component.onCompleted: {
updatePicture();
}
function updatePicture() {
if (photoData) { if (photoData) {
// Check first which size fits best... // Check first which size fits best...
var photo
for (var i = 0; i < photoData.sizes.length; i++) { for (var i = 0; i < photoData.sizes.length; i++) {
imagePreviewItem.pictureFileInformation = photoData.sizes[i].photo; photo = photoData.sizes[i].photo
if (photoData.sizes[i].width >= imagePreviewItem.width) { if (photoData.sizes[i].width >= imagePreviewItem.width) {
break; break
} }
} }
if (photo) {
if (imagePreviewItem.pictureFileInformation.local.is_downloading_completed) { imageFile.fileInformation = photo
singleImage.source = imagePreviewItem.pictureFileInformation.local.path;
} else {
tdLibWrapper.downloadFile(imagePreviewItem.pictureFileInformation.id);
} }
} }
} }
Connections { TDLibFile {
target: tdLibWrapper id: imageFile
onFileUpdated: { tdlib: tdLibWrapper
if (imagePreviewItem.pictureFileInformation) { autoLoad: true
if (fileId === imagePreviewItem.pictureFileInformation.id && fileInformation.local.is_downloading_completed) {
imagePreviewItem.pictureFileInformation = fileInformation;
singleImage.source = fileInformation.local.path;
}
}
}
} }
Image { Image {
id: singleImage id: singleImage
width: parent.width - Theme.paddingSmall width: parent.width - Theme.paddingSmall
height: parent.height - Theme.paddingSmall height: parent.height - Theme.paddingSmall
readonly property int bestHeight: (status === Image.Ready) ? Math.round(implicitHeight * width / implicitWidth) : 0
anchors.centerIn: parent anchors.centerIn: parent
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
autoTransform: true autoTransform: true
asynchronous: true asynchronous: true
source: imageFile.isDownloadingCompleted ? imageFile.path : ""
visible: status === Image.Ready visible: status === Image.Ready
opacity: status === Image.Ready ? 1 : 0 opacity: visible ? 1 : 0
Behavior on opacity { NumberAnimation {} } Behavior on opacity { FadeAnimation {} }
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {
pageStack.push(Qt.resolvedUrl("../pages/ImagePage.qml"), { "photoData" : imagePreviewItem.photoData, "pictureFileInformation" : imagePreviewItem.pictureFileInformation }); pageStack.push(Qt.resolvedUrl("../pages/ImagePage.qml"), {
"photoData" : imagePreviewItem.photoData,
"pictureFileInformation" : imageFile.fileInformation
})
} }
} }
} }