on 656b044 help sign in
unity8/Components/ApplicationManagerWrapper.qml
169 lines 6.1 kB history raw
1/*
2 * Copyright (C) 2013 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 */
17import QtQuick 2.0
18import Unity.Application 0.1
20Item {
21 id: root
23 property alias mainStageApplications: mainStageModel
24 property alias sideStageApplications: sideStageModel
25 property variant mainStageFocusedApplication: null
26 property variant sideStageFocusedApplication: null
27 property bool sideStageEnabled: true
28 property bool keyboardVisible: ApplicationManager.keyboardVisible
29 property int keyboardHeight: ApplicationManager.keyboardHeight
31 property bool fake: ApplicationManager.fake ? ApplicationManager.fake : false
33 signal focusRequested(string appId)
35 ApplicationsModelStageFiltered {
36 id: mainStageModel
37 stage: ApplicationInfo.MainStage
38 }
40 ApplicationsModelStageFiltered {
41 id: sideStageModel
42 stage: ApplicationInfo.SideStage
43 }
45 Connections {
46 target: ApplicationManager
47 onFocusedApplicationIdChanged: {
48 var app = ApplicationManager.findApplication(ApplicationManager.focusedApplicationId)
49 if (!app) { //nothing at all focused, so clear all
50 mainStageFocusedApplication = null;
51 sideStageFocusedApplication = null;
52 } else {
53 if (app.stage == ApplicationInfo.MainStage) {
54 mainStageFocusedApplication = app;
55 // possible the side stage app being unfocused fired this signal, so check for it
56 if (sideStageFocusedApplication && !sideStageFocusedApplication.focused)
57 sideStageFocusedApplication = null;
58 } else {
59 sideStageFocusedApplication = app;
60 // possible the main stage app being unfocused fired this signal, so check for it
61 if (mainStageFocusedApplication && !mainStageFocusedApplication.focused)
62 mainStageFocusedApplication = null;
63 }
64 }
65 }
67 onFocusRequested: {
68 // if no side stage enabled, override application's stage parameter
69 var app = ApplicationManager.findApplication(appId);
70 if (app && app.stage === ApplicationInfo.SideStage && !sideStageEnabled) {
71 app.stage = ApplicationInfo.MainStage;
72 }
74 root.focusRequested(appId);
75 }
76 }
78 function activateApplication(desktopFile, argument) {
79 var appId;
81 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
82 // TODO: convert entire shell to use appId only.
83 if (desktopFile.indexOf(".desktop") >= 0) {
84 appId = desktopFileToAppId(desktopFile);
85 } else {
86 appId = desktopFile;
87 }
89 var application = ApplicationManager.findApplication(appId);
90 if (application !== null) {
91 return application;
92 }
94 var execFlags = sideStageEnabled ? ApplicationManager.NoFlag : ApplicationManager.ForceMainStage;
96 if (argument) {
97 return ApplicationManager.startApplication(appId, execFlags, [argument]);
98 } else {
99 return ApplicationManager.startApplication(appId, execFlags);
100 }
101 }
103 function stopApplication(application) {
104 var appId;
106 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
107 // TODO: convert entire shell to use appId only.
108 if (typeof application == "string") {
109 appId = application;
110 } else {
111 appId = desktopFileToAppId(application.desktopFile);
112 }
114 ApplicationManager.stopApplication(appId);
115 }
117 function focusApplication(application) {
118 if (application == null || application == undefined) {
119 return;
120 }
122 ApplicationManager.focusApplication(application.appId);
123 }
125 function unfocusCurrentApplication() {
126 ApplicationManager.unfocusCurrentApplication();
127 }
129 function moveRunningApplicationStackPosition(from, to, stage) {
130 if (from == to || from < 0 || to < 0) return;
132 if (stage == ApplicationInfo.SideStage) {
133 sideStageModel.move(from, to);
134 } else {
135 mainStageModel.move(from, to);
136 }
137 }
139 function getApplicationFromDesktopFile(desktopFile, stage) {
140 var appId;
142 // HACK: Applications identified sometimes with with appId, but mostly with desktopFile.
143 // TODO: convert entire shell to use appId only.
144 if (desktopFile.indexOf(".desktop") >= 0) {
145 appId = desktopFileToAppId(desktopFile);
146 } else {
147 appId = desktopFile;
148 }
150 for (var i = 0, len = ApplicationManager.count; i < len; i++ ) {
151 var app = ApplicationManager.get(i);
153 // if stage not specified, return whichever app running on either stage
154 if (app.appId == appId && (stage == undefined || app.stage == stage)) {
155 return app;
156 }
157 }
158 }
160 function desktopFileToAppId(desktopFile) {
161 var right = desktopFile.lastIndexOf(".desktop");
162 var left = desktopFile.lastIndexOf("/");
163 if (left == -1 || right == -1 || left == right) {
164 console.log("ApplicationManagerWrapper: unable to extract appId from '" + desktopFile + "'");
165 return "";
166 }
167 return desktopFile.substring(left+1, right);
168 }