a638d876e38b747b176c828846077a589b80ec85
[apps/homescreen.git] / homescreen / qml / ShortcutArea.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4  * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 import QtQuick 2.2
20 import QtQuick.Layouts 1.1
21 import QtQuick.Window 2.2
22
23 Item {
24     id: root
25
26     ListModel {
27         id: applicationModel
28         ListElement {
29             appid: "launcher"
30             name: 'launcher'
31             application: 'launcher'
32         }
33         ListElement {
34             appid: "mediaplayer"
35             name: 'MediaPlayer'
36             application: 'mediaplayer'
37         }
38         ListElement {
39             appid: "hvac"
40             name: 'HVAC'
41             application: 'hvac'
42         }
43         ListElement {
44             appid: "navigation"
45             name: 'Navigation'
46             application: 'navigation'
47         }
48     }
49
50         property int pid: -1
51
52         property string current_appid: ''
53         property variant current_window: {}
54
55         property variant applications: {'' : -1}
56
57         function find_app(app, apps) {
58                 for (var x in apps) {
59
60                         if (apps[x] == -1)
61                                 continue
62
63                         if (x === app)
64                                 return true
65                 }
66                 return false
67         }
68
69         function set_current_window_app(appid, window) {
70                 current_appid = appid
71                 current_window = window
72         }
73
74
75     Timer {
76         id: timer
77         interval: 500
78         running: false
79         repeat: false
80         onTriggered: {
81                 if (current_appid != '' && current_window != undefined) {
82                         console.log("Timer expired, switching to " + current_appid)
83                         shell.activate_app(current_window, current_appid)
84                 }
85         }
86     }
87
88
89     RowLayout {
90         anchors.fill: parent
91         spacing: 0
92         Repeater {
93             model: applicationModel
94             delegate: ShortcutIcon {
95                 Layout.fillWidth: true
96                 Layout.fillHeight: true
97                 name: model.name
98                 active: model.name === launcher.current
99
100                 onClicked: {
101                         // if timer still running ignore
102                         if (timer.running) {
103                                 console.log("Timer still running")
104                                 return
105                         }
106
107                         // find the app before trying to start
108                         if (find_app(model.application, applications)) {
109                                 console.log("application " + model.appid + "already started. Just switching")
110                                 shell.activate_app(Window.window, model.appid)
111                                 return
112                         }
113
114                         pid = launcher.launch(model.application)
115                         if (pid > 0) {
116                                 set_current_window_app(model.appid, Window.window)
117                                 applications[model.application] = pid
118                                 timer.running = true
119                         }
120                 }
121             }
122         }
123     }
124 }