Lomiri
Wallpaper.qml
1 /*
2  * Copyright (C) 2013-2016 Canonical Ltd.
3  * Copyright (C) 2021 UBports Foundation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 import QtQuick 2.12
19 import QtQuick.Window 2.2
20 import Lomiri.Components 1.3
21 
22 Item {
23  id: root
24  objectName: "Wallpaper"
25  property url source: ""
26 
27  // sourceSize should be set to the largest width or height that this
28  // Wallpaper will likely be set to.
29  // In most cases inside Lomiri, this should be set to the width or height
30  // of the shell, whichever is larger.
31  // However, care should be taken to avoid changing sourceSize once
32  // it's set.
33  property real sourceSize: -1.0
34 
35  Image {
36  id: image
37  objectName: "wallpaperImage"
38  anchors.fill: parent
39  fillMode: Image.PreserveAspectCrop
40  autoTransform: true
41  asynchronous: true
42 
43  property real oldSourceSize: 0.0
44  property real intermediarySourceSize: {
45  // If minSourceSize is defined by root's parent, it will be 0 until
46  // the binding resolves. Otherwise, it'll be -1.0. Abuse this
47  // behavior to avoid changing sourceSize more than once during
48  // startup
49  if (root.sourceSize === 0.0) {
50  developerSanityTimer.start();
51  // -1.0 signals that the wallpaper shouldn't render yet.
52  return -1.0
53  }
54  developerSanityTimer.stop();
55  if (root.sourceSize === -1.0 && root.source !== "") {
56  console.warn(`${root.objectName}'s sourceSize is unset. It will use a lot of memory.`);
57  return 0.0
58  }
59 
60  return root.sourceSize
61  }
62 
63  onIntermediarySourceSizeChanged: {
64  if (oldSourceSize !== 0.0 && oldSourceSize !== -1.0 && root.source !== "") {
65  console.warn(`${root.objectName} source size is changing from ${oldSourceSize} to ${intermediarySourceSize}. This will cause an expensive image reload.`);
66  }
67  oldSourceSize = intermediarySourceSize;
68  }
69 
70  sourceSize: Qt.size(intermediarySourceSize, intermediarySourceSize)
71  source: intermediarySourceSize !== -1.0 ? root.source : ""
72  }
73 
74  Rectangle {
75  id: wallpaperFadeRectangle
76  objectName: "wallpaperFadeRectangle"
77  color: theme.palette.normal.background
78  anchors.fill: parent
79  opacity: image.status === Image.Ready ? 0: 1
80  visible: opacity !== 0
81  Behavior on opacity {
82  LomiriNumberAnimation { duration: LomiriAnimation.FastDuration }
83  }
84  }
85 
86  Timer {
87  id: developerSanityTimer
88  interval: 2000
89  running: false
90  onTriggered: {
91  if (root.sourceSize === 0.0 && root.source !== "") {
92  console.warn(`${root.objectName} has not received its sourceSize yet. It will not render.`);
93  }
94  }
95  }
96 }