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
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const {AddonManager} = ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
ChromeUtils.defineModuleGetter(this, "LightweightThemeManager",
"resource://gre/modules/LightweightThemeManager.jsm");
var gThemes = [];
var gApplyThemeBundle;
var gBackgroundIsActive;
function reloadThemes()
{
AddonManager.getAddonsByTypes(["theme"], function(themes) {
gThemes = themes.sort(function(a, b) {
return a.name.localeCompare(b.name);
});
});
}
var gAddonListener = {
onEnabling: function(val) {},
onEnabled: function(val) {},
onDisabling: function(val) {},
onDisabled: function(val) {},
onInstalling: function(val) {},
onInstalled: reloadThemes,
onUninstalling: function(val) {},
onUninstalled: reloadThemes,
onOperationCancelled: reloadThemes
};
function getNewThemes()
{
// get URL for more themes from prefs
try {
openURL(Services.urlFormatter.formatURLPref("extensions.getMoreThemesURL"));
}
catch (e) {
dump(e);
}
}
function getPersonas()
{
// get URL for more themes from prefs
try {
openURL(Services.urlFormatter.formatURLPref("extensions.getPersonasURL"));
}
catch (e) {
dump(e);
}
}
function checkTheme(popup)
{
const ID_SUFFIX = "@personas.mozilla.org";
gBackgroundIsActive = false;
var usedThemes = LightweightThemeManager.usedThemes;
usedThemes = new Map(usedThemes.map( x => [x.id + ID_SUFFIX, x] ));
while (popup.lastChild.localName != 'menuseparator')
popup.lastChild.remove();
gThemes.forEach(function(theme) {
var menuitem = document.createElement('menuitem');
menuitem.setAttribute("label", theme.name);
menuitem.setAttribute("type", "radio");
menuitem.setAttribute("name", "themeGroup");
if (theme.description)
menuitem.setAttribute("tooltiptext", theme.description);
if (!theme.userDisabled)
menuitem.setAttribute("checked", "true");
else if (!(theme.permissions & AddonManager.PERM_CAN_ENABLE))
menuitem.setAttribute("disabled", "true");
menuitem.theme = theme;
var persona = usedThemes.get(theme.id);
if (persona) {
menuitem.persona = persona;
if (theme.isActive)
gBackgroundIsActive = true;
}
popup.appendChild(menuitem);
});
}
function previewTheme(event)
{
if (!gBackgroundIsActive || !event.target.persona)
return;
switch (event.type) {
case "DOMMenuItemActive":
LightweightThemeManager.previewTheme(event.target.persona);
break;
case "DOMMenuItemInactive":
LightweightThemeManager.resetPreview();
break;
}
}
function restartApp()
{
// Notify all windows that an application quit has been requested.
var cancelQuit = Cc["@mozilla.org/supports-PRBool;1"]
.createInstance(Ci.nsISupportsPRBool);
Services.obs.notifyObservers(cancelQuit, "quit-application-requested", "restart");
// Something aborted the quit process.
if (cancelQuit.data)
return;
Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
const appStartup = Services.startup;
appStartup.quit(appStartup.eRestart | appStartup.eAttemptQuit);
}
function applyTheme(menuitem)
{
if (!menuitem.theme)
return;
menuitem.theme.userDisabled = false;
if (!menuitem.theme.isActive) {
var promptTitle = gApplyThemeBundle.getString("switchskinstitle");
// gBrandBundle: bundle_brand stringbundle from overlayed XUL file
var brandName = gBrandBundle.getString("brandShortName");
var promptMsg = gApplyThemeBundle.getFormattedString("switchskins", [brandName]);
var promptNow = gApplyThemeBundle.getString("switchskinsnow");
var promptLater = gApplyThemeBundle.getString("switchskinslater");
var check = {value: false};
var flags = Services.prompt.BUTTON_POS_0 * Services.prompt.BUTTON_TITLE_IS_STRING +
Services.prompt.BUTTON_POS_1 * Services.prompt.BUTTON_TITLE_IS_STRING;
var pressedVal = Services.prompt.confirmEx(window, promptTitle, promptMsg,
flags, promptNow, promptLater,
null, null, check);
if (pressedVal == 0)
restartApp();
}
}
function applyThemeOnLoad()
{
// init globals
gApplyThemeBundle = document.getElementById("bundle_viewApplyTheme");
AddonManager.addAddonListener(gAddonListener);
reloadThemes();
removeEventListener("load", applyThemeOnLoad, false);
addEventListener("unload", applyThemeOnUnload, false);
var popup = document.getElementById("menu_viewApplyTheme_Popup");
popup.addEventListener("DOMMenuItemActive", previewTheme);
popup.addEventListener("DOMMenuItemInactive", previewTheme);
}
function applyThemeOnUnload()
{
AddonManager.removeAddonListener(gAddonListener);
var popup = document.getElementById("menu_viewApplyTheme_Popup");
popup.removeEventListener("DOMMenuItemActive", previewTheme);
popup.removeEventListener("DOMMenuItemInactive", previewTheme);
}
addEventListener("load", applyThemeOnLoad, false);
|