forked from thurasw/emby-theater-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.js
104 lines (73 loc) · 2.54 KB
/
shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
define(['events', 'apphost'], function (events, apphost) {
function sendCommand(name) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'electronapphost://' + name, true);
xhr.onload = function () {
if (this.response) {
resolve(this.response);
} else {
reject();
}
};
xhr.onerror = reject;
xhr.send();
});
}
var shell = {};
var closingWindowState;
function getProcessClosePromise(pid) {
// returns a promise that resolves or rejects when a process closes
return new Promise(function (resolve, reject) {
events.on(shell, 'closed', function (e, processId, error) {
if (processId === pid) {
if (closingWindowState) {
apphost.setWindowState(closingWindowState);
closingWindowState = null;
}
if (error) {
reject();
} else {
resolve();
}
}
});
});
}
window.onChildProcessClosed = function (processId, error) {
events.trigger(shell, 'closed', [processId, error]);
};
function paramsToString(params) {
var values = [];
for (var key in params) {
var value = params[key];
if (value !== null && value !== undefined && value !== '') {
values.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
}
}
return values.join('&');
}
shell.openUrl = function (url) {
return sendCommand('openurl?url=' + url);
};
shell.canExec = true;
shell.close = function (processId) {
var url = 'shellclose?id=' + processId;
return sendCommand(url);
};
shell.exec = function (options) {
var url = 'shellstart?' + paramsToString(options);
return sendCommand(url).then(function (response) {
if (apphost.supports('windowstate')) {
closingWindowState = apphost.getWindowState();
apphost.setWindowState('Minimized');
}
events.trigger(shell, 'exec', [response]);
return {
id: response,
promise: getProcessClosePromise(response)
};
});
};
return shell;
});