forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy paththeme-manager.js
464 lines (412 loc) · 13.9 KB
/
theme-manager.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* global snapshotAuxiliaryData */
const path = require('path');
const _ = require('underscore-plus');
const { Emitter, CompositeDisposable } = require('event-kit');
const { File } = require('pathwatcher');
const fs = require('fs-plus');
const LessCompileCache = require('./less-compile-cache');
// Extended: Handles loading and activating available themes.
//
// An instance of this class is always available as the `atom.themes` global.
module.exports = class ThemeManager {
constructor({
packageManager,
config,
styleManager,
notificationManager,
viewRegistry
}) {
this.packageManager = packageManager;
this.config = config;
this.styleManager = styleManager;
this.notificationManager = notificationManager;
this.viewRegistry = viewRegistry;
this.emitter = new Emitter();
this.styleSheetDisposablesBySourcePath = {};
this.lessCache = null;
this.initialLoadComplete = false;
this.packageManager.registerPackageActivator(this, ['theme']);
this.packageManager.onDidActivateInitialPackages(() => {
this.onDidChangeActiveThemes(() =>
this.packageManager.reloadActivePackageStyleSheets()
);
});
}
initialize({ resourcePath, configDirPath, safeMode, devMode }) {
this.resourcePath = resourcePath;
this.configDirPath = configDirPath;
this.safeMode = safeMode;
this.lessSourcesByRelativeFilePath = null;
if (devMode || typeof snapshotAuxiliaryData === 'undefined') {
this.lessSourcesByRelativeFilePath = {};
this.importedFilePathsByRelativeImportPath = {};
} else {
this.lessSourcesByRelativeFilePath =
snapshotAuxiliaryData.lessSourcesByRelativeFilePath;
this.importedFilePathsByRelativeImportPath =
snapshotAuxiliaryData.importedFilePathsByRelativeImportPath;
}
}
/*
Section: Event Subscription
*/
// Essential: Invoke `callback` when style sheet changes associated with
// updating the list of active themes have completed.
//
// * `callback` {Function}
//
// Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidChangeActiveThemes(callback) {
return this.emitter.on('did-change-active-themes', callback);
}
/*
Section: Accessing Available Themes
*/
getAvailableNames() {
// TODO: Maybe should change to list all the available themes out there?
return this.getLoadedNames();
}
/*
Section: Accessing Loaded Themes
*/
// Public: Returns an {Array} of {String}s of all the loaded theme names.
getLoadedThemeNames() {
return this.getLoadedThemes().map(theme => theme.name);
}
// Public: Returns an {Array} of all the loaded themes.
getLoadedThemes() {
return this.packageManager
.getLoadedPackages()
.filter(pack => pack.isTheme());
}
/*
Section: Accessing Active Themes
*/
// Public: Returns an {Array} of {String}s of all the active theme names.
getActiveThemeNames() {
return this.getActiveThemes().map(theme => theme.name);
}
// Public: Returns an {Array} of all the active themes.
getActiveThemes() {
return this.packageManager
.getActivePackages()
.filter(pack => pack.isTheme());
}
activatePackages() {
return this.activateThemes();
}
/*
Section: Managing Enabled Themes
*/
warnForNonExistentThemes() {
let themeNames = this.config.get('core.themes') || [];
if (!Array.isArray(themeNames)) {
themeNames = [themeNames];
}
for (let themeName of themeNames) {
if (
!themeName ||
typeof themeName !== 'string' ||
!this.packageManager.resolvePackagePath(themeName)
) {
console.warn(`Enabled theme '${themeName}' is not installed.`);
}
}
}
// Public: Get the enabled theme names from the config.
//
// Returns an array of theme names in the order that they should be activated.
getEnabledThemeNames() {
let themeNames = this.config.get('core.themes') || [];
if (!Array.isArray(themeNames)) {
themeNames = [themeNames];
}
themeNames = themeNames.filter(
themeName =>
typeof themeName === 'string' &&
this.packageManager.resolvePackagePath(themeName)
);
// Use a built-in syntax and UI theme any time the configured themes are not
// available.
if (themeNames.length < 2) {
const builtInThemeNames = [
'atom-dark-syntax',
'atom-dark-ui',
'atom-light-syntax',
'atom-light-ui',
'base16-tomorrow-dark-theme',
'base16-tomorrow-light-theme',
'solarized-dark-syntax',
'solarized-light-syntax'
];
themeNames = _.intersection(themeNames, builtInThemeNames);
if (themeNames.length === 0) {
themeNames = ['one-dark-syntax', 'one-dark-ui'];
} else if (themeNames.length === 1) {
if (themeNames[0].endsWith('-ui')) {
themeNames.unshift('one-dark-syntax');
} else {
themeNames.push('one-dark-ui');
}
}
}
// Reverse so the first (top) theme is loaded after the others. We want
// the first/top theme to override later themes in the stack.
return themeNames.reverse();
}
/*
Section: Private
*/
// Resolve and apply the stylesheet specified by the path.
//
// This supports both CSS and Less stylesheets.
//
// * `stylesheetPath` A {String} path to the stylesheet that can be an absolute
// path or a relative path that will be resolved against the load path.
//
// Returns a {Disposable} on which `.dispose()` can be called to remove the
// required stylesheet.
requireStylesheet(
stylesheetPath,
priority,
skipDeprecatedSelectorsTransformation
) {
let fullPath = this.resolveStylesheet(stylesheetPath);
if (fullPath) {
const content = this.loadStylesheet(fullPath);
return this.applyStylesheet(
fullPath,
content,
priority,
skipDeprecatedSelectorsTransformation
);
} else {
throw new Error(`Could not find a file at path '${stylesheetPath}'`);
}
}
unwatchUserStylesheet() {
if (this.userStylesheetSubscriptions != null)
this.userStylesheetSubscriptions.dispose();
this.userStylesheetSubscriptions = null;
this.userStylesheetFile = null;
if (this.userStyleSheetDisposable != null)
this.userStyleSheetDisposable.dispose();
this.userStyleSheetDisposable = null;
}
loadUserStylesheet() {
this.unwatchUserStylesheet();
const userStylesheetPath = this.styleManager.getUserStyleSheetPath();
if (!fs.isFileSync(userStylesheetPath)) {
return;
}
try {
this.userStylesheetFile = new File(userStylesheetPath);
this.userStylesheetSubscriptions = new CompositeDisposable();
const reloadStylesheet = () => this.loadUserStylesheet();
this.userStylesheetSubscriptions.add(
this.userStylesheetFile.onDidChange(reloadStylesheet)
);
this.userStylesheetSubscriptions.add(
this.userStylesheetFile.onDidRename(reloadStylesheet)
);
this.userStylesheetSubscriptions.add(
this.userStylesheetFile.onDidDelete(reloadStylesheet)
);
} catch (error) {
const message = `\
Unable to watch path: \`${path.basename(userStylesheetPath)}\`. Make sure
you have permissions to \`${userStylesheetPath}\`.
On linux there are currently problems with watch sizes. See
[this document][watches] for more info.
[watches]:https://github.com/atom/atom/blob/master/docs/build-instructions/linux.md#typeerror-unable-to-watch-path\
`;
this.notificationManager.addError(message, { dismissable: true });
}
let userStylesheetContents;
try {
userStylesheetContents = this.loadStylesheet(userStylesheetPath, true);
} catch (error) {
return;
}
this.userStyleSheetDisposable = this.styleManager.addStyleSheet(
userStylesheetContents,
{ sourcePath: userStylesheetPath, priority: 2 }
);
}
loadBaseStylesheets() {
this.reloadBaseStylesheets();
}
reloadBaseStylesheets() {
this.requireStylesheet('../static/atom', -2, true);
}
stylesheetElementForId(id) {
const escapedId = id.replace(/\\/g, '\\\\');
return document.head.querySelector(
`atom-styles style[source-path="${escapedId}"]`
);
}
resolveStylesheet(stylesheetPath) {
if (path.extname(stylesheetPath).length > 0) {
return fs.resolveOnLoadPath(stylesheetPath);
} else {
return fs.resolveOnLoadPath(stylesheetPath, ['css', 'less']);
}
}
loadStylesheet(stylesheetPath, importFallbackVariables) {
if (path.extname(stylesheetPath) === '.less') {
return this.loadLessStylesheet(stylesheetPath, importFallbackVariables);
} else {
return fs.readFileSync(stylesheetPath, 'utf8');
}
}
loadLessStylesheet(lessStylesheetPath, importFallbackVariables = false) {
if (this.lessCache == null) {
this.lessCache = new LessCompileCache({
resourcePath: this.resourcePath,
lessSourcesByRelativeFilePath: this.lessSourcesByRelativeFilePath,
importedFilePathsByRelativeImportPath: this
.importedFilePathsByRelativeImportPath,
importPaths: this.getImportPaths()
});
}
try {
if (importFallbackVariables) {
const baseVarImports = `\
@import "variables/ui-variables";
@import "variables/syntax-variables";\
`;
const relativeFilePath = path.relative(
this.resourcePath,
lessStylesheetPath
);
const lessSource = this.lessSourcesByRelativeFilePath[relativeFilePath];
let content, digest;
if (lessSource != null) {
({ content } = lessSource);
({ digest } = lessSource);
} else {
content =
baseVarImports + '\n' + fs.readFileSync(lessStylesheetPath, 'utf8');
digest = null;
}
return this.lessCache.cssForFile(lessStylesheetPath, content, digest);
} else {
return this.lessCache.read(lessStylesheetPath);
}
} catch (error) {
let detail, message;
error.less = true;
if (error.line != null) {
// Adjust line numbers for import fallbacks
if (importFallbackVariables) {
error.line -= 2;
}
message = `Error compiling Less stylesheet: \`${lessStylesheetPath}\``;
detail = `Line number: ${error.line}\n${error.message}`;
} else {
message = `Error loading Less stylesheet: \`${lessStylesheetPath}\``;
detail = error.message;
}
this.notificationManager.addError(message, { detail, dismissable: true });
throw error;
}
}
removeStylesheet(stylesheetPath) {
if (this.styleSheetDisposablesBySourcePath[stylesheetPath] != null) {
this.styleSheetDisposablesBySourcePath[stylesheetPath].dispose();
}
}
applyStylesheet(path, text, priority, skipDeprecatedSelectorsTransformation) {
this.styleSheetDisposablesBySourcePath[
path
] = this.styleManager.addStyleSheet(text, {
priority,
skipDeprecatedSelectorsTransformation,
sourcePath: path
});
return this.styleSheetDisposablesBySourcePath[path];
}
activateThemes() {
return new Promise(resolve => {
// @config.observe runs the callback once, then on subsequent changes.
this.config.observe('core.themes', () => {
this.deactivateThemes().then(() => {
this.warnForNonExistentThemes();
this.refreshLessCache(); // Update cache for packages in core.themes config
const promises = [];
for (const themeName of this.getEnabledThemeNames()) {
if (this.packageManager.resolvePackagePath(themeName)) {
promises.push(this.packageManager.activatePackage(themeName));
} else {
console.warn(
`Failed to activate theme '${themeName}' because it isn't installed.`
);
}
}
return Promise.all(promises).then(() => {
this.addActiveThemeClasses();
this.refreshLessCache(); // Update cache again now that @getActiveThemes() is populated
this.loadUserStylesheet();
this.reloadBaseStylesheets();
this.initialLoadComplete = true;
this.emitter.emit('did-change-active-themes');
resolve();
});
});
});
});
}
deactivateThemes() {
this.removeActiveThemeClasses();
this.unwatchUserStylesheet();
const results = this.getActiveThemes().map(pack =>
this.packageManager.deactivatePackage(pack.name)
);
return Promise.all(
results.filter(r => r != null && typeof r.then === 'function')
);
}
isInitialLoadComplete() {
return this.initialLoadComplete;
}
addActiveThemeClasses() {
const workspaceElement = this.viewRegistry.getView(this.workspace);
if (workspaceElement) {
for (const pack of this.getActiveThemes()) {
workspaceElement.classList.add(`theme-${pack.name}`);
}
}
}
removeActiveThemeClasses() {
const workspaceElement = this.viewRegistry.getView(this.workspace);
for (const pack of this.getActiveThemes()) {
workspaceElement.classList.remove(`theme-${pack.name}`);
}
}
refreshLessCache() {
if (this.lessCache) this.lessCache.setImportPaths(this.getImportPaths());
}
getImportPaths() {
let themePaths;
const activeThemes = this.getActiveThemes();
if (activeThemes.length > 0) {
themePaths = activeThemes
.filter(theme => theme)
.map(theme => theme.getStylesheetsPath());
} else {
themePaths = [];
for (const themeName of this.getEnabledThemeNames()) {
const themePath = this.packageManager.resolvePackagePath(themeName);
if (themePath) {
const deprecatedPath = path.join(themePath, 'stylesheets');
if (fs.isDirectorySync(deprecatedPath)) {
themePaths.push(deprecatedPath);
} else {
themePaths.push(path.join(themePath, 'styles'));
}
}
}
}
return themePaths.filter(themePath => fs.isDirectorySync(themePath));
}
};