2020-09-27 18:38:33 +00:00
|
|
|
/*
|
|
|
|
Sailfin: a Jellyfin client written using Qt
|
|
|
|
Copyright (C) 2020 Chris Josten
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
import QtQuick 2.6
|
|
|
|
import Sailfish.Silica 1.0
|
|
|
|
|
2021-01-14 19:35:24 +00:00
|
|
|
import nl.netsoj.chris.blurhash 1.0
|
|
|
|
|
2020-09-25 13:21:08 +00:00
|
|
|
/**
|
2021-02-12 02:32:28 +00:00
|
|
|
* An image for "remote" images (loaded over e.g. http), with a spinner and a fallback image.
|
|
|
|
*
|
|
|
|
* If placed in a page, it will delay the loading of the image until the transition is over,
|
|
|
|
* displaying a simpler blurhash instead, to reduce the stutter during the transition
|
2020-09-25 13:21:08 +00:00
|
|
|
*/
|
2020-10-26 21:29:07 +00:00
|
|
|
SilicaItem {
|
2021-01-14 19:35:24 +00:00
|
|
|
id: root
|
2020-10-26 21:29:07 +00:00
|
|
|
property string fallbackImage
|
|
|
|
property bool usingFallbackImage
|
2020-10-01 10:55:11 +00:00
|
|
|
property color fallbackColor: Theme.highlightColor
|
2021-08-21 22:29:44 +00:00
|
|
|
property bool forceBlurhash: false // Force display the blurhash state even though the image is already loaded
|
2020-10-26 21:29:07 +00:00
|
|
|
|
2021-01-14 19:35:24 +00:00
|
|
|
property var __parentPage : null
|
2021-01-17 20:11:55 +00:00
|
|
|
property bool _alreadyLoaded: false
|
2021-02-13 20:42:57 +00:00
|
|
|
readonly property bool _shouldLoad: _alreadyLoaded
|
|
|
|
|| __parentPage == null // Not an indirect child of a page. Immediatly start loading.
|
|
|
|
|| __parentPage && [PageStatus.Active, PageStatus.Deactivating].indexOf(__parentPage.status) >= 0
|
2021-01-14 19:35:24 +00:00
|
|
|
|
2021-01-17 20:11:55 +00:00
|
|
|
onSourceChanged: _alreadyLoaded = false
|
2021-01-14 19:35:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BlurHash that is used as placeholder
|
|
|
|
*/
|
|
|
|
property string blurhash: ""
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
property real aspectRatio: 1.0
|
|
|
|
property string source: ""
|
2020-10-26 21:29:07 +00:00
|
|
|
property alias sourceSize: realImage.sourceSize
|
2021-01-14 19:35:24 +00:00
|
|
|
property var fillMode: Image.Stretch
|
2021-01-17 20:11:55 +00:00
|
|
|
property int status: {
|
|
|
|
if (!source) return Image.Null
|
|
|
|
if (realImage.status == Image.Null && !_shouldLoad) return Image.Loading
|
|
|
|
return realImage.status
|
|
|
|
}
|
2020-10-26 21:29:07 +00:00
|
|
|
implicitHeight: realImage.implicitHeight
|
|
|
|
implicitWidth: realImage.implicitWidth
|
|
|
|
|
|
|
|
Image {
|
|
|
|
id: realImage
|
|
|
|
anchors.fill: parent
|
|
|
|
asynchronous: true
|
2021-01-14 19:35:24 +00:00
|
|
|
fillMode: root.fillMode
|
|
|
|
opacity: 1
|
2021-01-17 20:11:55 +00:00
|
|
|
source: _shouldLoad ? root.source : ""
|
2021-01-14 19:35:24 +00:00
|
|
|
onStatusChanged: {
|
|
|
|
if (status == Image.Ready) {
|
2021-01-17 20:11:55 +00:00
|
|
|
_alreadyLoaded = true
|
2021-01-14 19:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 21:29:07 +00:00
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: fallbackBackground
|
|
|
|
anchors.fill: parent
|
|
|
|
gradient: Gradient {
|
2020-10-01 10:55:11 +00:00
|
|
|
GradientStop { position: 0.0; color: fallbackColor; }
|
|
|
|
GradientStop { position: 1.0; color: Theme.highlightDimmerFromColor(fallbackColor, Theme.colorScheme); }
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
2021-01-14 19:35:24 +00:00
|
|
|
opacity: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
Image {
|
|
|
|
id: blurhashImage
|
|
|
|
anchors.fill: parent
|
|
|
|
fillMode: root.fillMode
|
2022-07-28 17:55:33 +00:00
|
|
|
sourceSize.height: 16
|
|
|
|
sourceSize.width: 16 * aspectRatio
|
2021-01-17 16:34:17 +00:00
|
|
|
source: blurhash.length > 0 ? "image://blurhash/" + encodeURIComponent(blurhash) : ""
|
2021-01-14 19:35:24 +00:00
|
|
|
opacity: 0
|
2020-10-26 21:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rectangle {
|
|
|
|
id: highlightOverlay
|
|
|
|
anchors.fill: parent
|
|
|
|
color: Theme.rgba(Theme.highlightColor, Theme.opacityOverlay)
|
|
|
|
visible: parent.highlighted
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
2020-10-01 10:55:11 +00:00
|
|
|
|
|
|
|
BusyIndicator {
|
|
|
|
anchors.centerIn: parent
|
2020-10-26 21:29:07 +00:00
|
|
|
running: realImage.status === Image.Loading
|
2020-10-01 10:55:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 00:51:37 +00:00
|
|
|
HighlightImage {
|
2020-09-15 14:53:13 +00:00
|
|
|
id: fallbackImageItem
|
|
|
|
anchors.centerIn: parent
|
2021-01-14 19:35:24 +00:00
|
|
|
visible: realImage.status === Image.Error || (realImage.status === Image.Null && blurhash.length === 0)
|
|
|
|
source: fallbackImage ? fallbackImage : "image://theme/icon-m-question"
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
2021-01-14 19:35:24 +00:00
|
|
|
|
|
|
|
states: [
|
|
|
|
State {
|
|
|
|
name: "fallback"
|
2021-08-21 22:29:44 +00:00
|
|
|
when: !forceBlurhash && (blurhash.length === 0) && (realImage.status === Image.Error || realImage.status === Image.Null || realImage.status === Image.Loading)
|
2021-01-14 19:35:24 +00:00
|
|
|
PropertyChanges {
|
|
|
|
target: fallbackBackground
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
State {
|
|
|
|
name: "blurhash"
|
2021-08-21 22:29:44 +00:00
|
|
|
when: !forceBlurhash && blurhash.length > 0 && (realImage.status === Image.Error || realImage.status === Image.Null || realImage.status === Image.Loading)
|
2021-01-14 19:35:24 +00:00
|
|
|
PropertyChanges {
|
|
|
|
target: blurhashImage
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
State {
|
|
|
|
name: "loaded"
|
2021-08-21 22:29:44 +00:00
|
|
|
when: !root.forceBlurhash && realImage.status === Image.Ready
|
2021-01-14 19:35:24 +00:00
|
|
|
PropertyChanges {
|
|
|
|
target: realImage
|
|
|
|
//opacity: 1
|
|
|
|
}
|
2021-08-21 22:29:44 +00:00
|
|
|
},
|
|
|
|
State {
|
|
|
|
name: "forcedBlurhash"
|
|
|
|
when: forceBlurhash
|
|
|
|
PropertyChanges {
|
|
|
|
target: blurhashImage
|
|
|
|
opacity: 1
|
|
|
|
}
|
2021-01-14 19:35:24 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
transitions: [
|
|
|
|
Transition {
|
2021-02-14 23:27:36 +00:00
|
|
|
from: "blurhash,fallback"
|
|
|
|
to: "loaded"
|
2021-01-14 19:35:24 +00:00
|
|
|
FadeAnimation {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
Component.onCompleted: {
|
|
|
|
var item = parent;
|
2021-01-17 20:11:55 +00:00
|
|
|
while (item !== null) {
|
2021-01-14 19:35:24 +00:00
|
|
|
if ("__silica_page" in item) {
|
|
|
|
__parentPage = item
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
item = item.parent
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|