-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
addons.js
171 lines (138 loc) · 4.21 KB
/
addons.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { GLib } = imports.gi;
const Main = imports.ui.main;
const AggregateMenu = Main.panel.statusArea.aggregateMenu;
let castMenu;
let addonMenuItems = {};
let addonMenuSignals = {};
let timeouts = {};
function findCastToTv()
{
let menuItems = AggregateMenu.menu._getMenuItems();
let index = 0;
while(index < menuItems.length)
{
if(
menuItems[index].hasOwnProperty('extensionId')
&& menuItems[index].extensionId === 'cast-to-tv'
) {
return menuItems[index];
}
index++;
}
return null;
}
function setLastMenuItem(extMenu, item, endOffset)
{
if(!endOffset) endOffset = 0;
let subMenuItems = extMenu.castSubMenu.menu._getMenuItems();
let lastItemIndex = subMenuItems.length - 1;
extMenu.castSubMenu.menu.moveMenuItem(item, lastItemIndex - endOffset);
}
function enableAddon(uuid)
{
let addonName = uuid.split('@')[0];
if(timeouts[addonName])
return;
/* Give main extension time to finish startup */
timeouts[addonName] = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2500, () =>
{
timeouts[addonName] = null;
castMenu = findCastToTv();
let mainExtension = Main.extensionManager.lookup('cast-to-tv@rafostar.github.com');
let addonExtension = Main.extensionManager.lookup(uuid);
if(!castMenu || !mainExtension || !addonExtension)
return GLib.SOURCE_REMOVE;
let { helper, soup, shared } = mainExtension.imports;
let { AddonMenuItem } = addonExtension.imports.widget;
addonMenuItems[addonName] = new AddonMenuItem({
helper: helper,
soupClient: soup.client,
shared: shared.module.exports,
path: mainExtension.path
});
addonMenuSignals[addonName] = [
addonMenuItems[addonName].connect('destroy', () =>
{
addonMenuSignals[addonName].forEach(signal =>
addonMenuItems[addonName].disconnect(signal))
addonMenuSignals[addonName] = null;
addonMenuItems[addonName].destroyed = true;
})
];
if(
addonMenuItems[addonName].hasOwnProperty('hasExtApp')
&& addonMenuItems[addonName].hasExtApp
) {
addonMenuSignals[addonName].push(
addonMenuItems[addonName].connect('activate', () =>
helper.startApp(addonExtension.path)
)
)
}
else
{
addonMenuSignals[addonName].push(
addonMenuItems[addonName].connect('activate', () =>
helper.closeOtherApps()
)
)
}
let castMenuItems = castMenu.castSubMenu.menu._getMenuItems();
let insertIndex = castMenuItems.length - 1;
let prevMenuItem = castMenuItems.find(item =>
{
if(
item.hasOwnProperty('isDesktopStream')
|| (castMenuItems.indexOf(item) > 2
&& addonMenuItems[addonName].label.text < item.label.text)
) {
return true;
}
return false;
});
/* Desktop streaming should be last on the list (experimental feature) */
if(prevMenuItem && !addonMenuItems[addonName].hasOwnProperty('isDesktopStream'))
insertIndex = castMenuItems.indexOf(prevMenuItem);
castMenu.castSubMenu.menu.addMenuItem(addonMenuItems[addonName], insertIndex);
if(
castMenu.hasOwnProperty('isServiceEnabled')
&& castMenu.isServiceEnabled === false
) {
if(addonMenuItems[addonName].hasOwnProperty('actor'))
addonMenuItems[addonName].actor.hide();
else
addonMenuItems[addonName].hide();
}
if(castMenu.hasOwnProperty('serviceMenuItem'))
setLastMenuItem(castMenu, castMenu.serviceMenuItem);
if(castMenu.hasOwnProperty('settingsMenuItem'))
setLastMenuItem(castMenu, castMenu.settingsMenuItem);
return GLib.SOURCE_REMOVE;
});
}
function disableAddon(uuid)
{
let addonName = uuid.split('@')[0];
if(timeouts[addonName])
{
GLib.source_remove(timeouts[addonName]);
timeouts[addonName] = null;
}
if(!addonMenuItems[addonName] || addonMenuItems[addonName].destroyed)
return;
/* No need to reorder menu items when locking screen,
as whole cast menu will be destroyed then anyway */
let lockingScreen = (
Main.sessionMode.currentMode == 'unlock-dialog'
|| Main.sessionMode.currentMode == 'lock-screen'
);
if(!lockingScreen && castMenu)
setLastMenuItem(castMenu, addonMenuItems[addonName]);
/* Force GUI refresh by hiding item before removal */
if(addonMenuItems[addonName].hasOwnProperty('actor'))
addonMenuItems[addonName].actor.hide();
else
addonMenuItems[addonName].hide();
addonMenuItems[addonName].destroy();
addonMenuItems[addonName] = null;
}