Lomiri
InactivityTimer.qml
1 /*
2  * Copyright (C) 2015-2016 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 import QtQuick 2.12
18 import Lomiri.Components 1.3
19 
20 Item {
21  id: root
22 
23  readonly property bool running: delayTimer.running || inactivityTimer.running
24  property int interval: 3000
25  property var page
26  property int lastInputTimestamp
27 
28  function start() {
29  if (!delayTimer.running && !inactivityTimer.running) {
30  delayTimer.start();
31  }
32  }
33 
34  ////
35 
36  onLastInputTimestampChanged: {
37  if (inactivityTimer.running) {
38  inactivityTimer.restart();
39  }
40  }
41 
42  Connections {
43  target: page
44  onIsReadyChanged: {
45  if (page.isReady && inactivityTimer.running) {
46  inactivityTimer.restart();
47  }
48  }
49  onPausedChanged: {
50  if (root.paused) {
51  delayTimer.stop();
52  inactivityTimer.stop();
53  }
54  }
55  }
56 
57  Timer {
58  id: delayTimer
59  interval: Math.max(root.interval - 3000, 0)
60  onTriggered: inactivityTimer.start()
61  }
62 
63  Timer {
64  id: inactivityTimer
65 
66  interval: Math.min(root.interval, 3000)
67 
68  onTriggered: {
69  if (page.isReady) {
70  if (!page.shown) {
71  page.show();
72  }
73  } else if (!page.skipped) {
74  restart();
75  }
76  }
77  }
78 }