1 |
|
2 |
|
3 |
|
4 | import { IChangedArgs, URLExt } from '@jupyterlab/coreutils';
|
5 | import { ISettingRegistry } from '@jupyterlab/settingregistry';
|
6 | import {
|
7 | ITranslator,
|
8 | nullTranslator,
|
9 | TranslationBundle
|
10 | } from '@jupyterlab/translation';
|
11 | import { DisposableDelegate, IDisposable } from '@lumino/disposable';
|
12 | import { ISignal, Signal } from '@lumino/signaling';
|
13 | import { Widget } from '@lumino/widgets';
|
14 | import { Dialog, showDialog } from './dialog';
|
15 | import { ISplashScreen, IThemeManager } from './tokens';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const REQUEST_INTERVAL = 75;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const REQUEST_THRESHOLD = 20;
|
26 |
|
27 | type Dict<T> = { [key: string]: T };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export class ThemeManager implements IThemeManager {
|
33 | |
34 |
|
35 |
|
36 | constructor(options: ThemeManager.IOptions) {
|
37 | const { host, key, splash, url } = options;
|
38 | this.translator = options.translator || nullTranslator;
|
39 | this._trans = this.translator.load('jupyterlab');
|
40 | const registry = options.settings;
|
41 |
|
42 | this._base = url;
|
43 | this._host = host;
|
44 | this._splash = splash || null;
|
45 |
|
46 | void registry.load(key).then(settings => {
|
47 | this._settings = settings;
|
48 |
|
49 | this._initOverrideProps();
|
50 |
|
51 | this._settings.changed.connect(this._loadSettings, this);
|
52 | this._loadSettings();
|
53 | });
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 | get theme(): string | null {
|
60 | return this._current;
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 | get preferredLightTheme(): string {
|
67 | return this._settings.composite['preferred-light-theme'] as string;
|
68 | }
|
69 |
|
70 | |
71 |
|
72 |
|
73 | get preferredDarkTheme(): string {
|
74 | return this._settings.composite['preferred-dark-theme'] as string;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | get preferredTheme(): string | null {
|
83 | if (!this.isToggledAdaptiveTheme()) {
|
84 | return this.theme;
|
85 | }
|
86 | if (this.isSystemColorSchemeDark()) {
|
87 | return this.preferredDarkTheme;
|
88 | }
|
89 | return this.preferredLightTheme;
|
90 | }
|
91 |
|
92 | |
93 |
|
94 |
|
95 | get themes(): ReadonlyArray<string> {
|
96 | return Object.keys(this._themes);
|
97 | }
|
98 |
|
99 | |
100 |
|
101 |
|
102 | get lightThemes(): ReadonlyArray<string> {
|
103 | return Object.entries(this._themes)
|
104 | .filter(([_, theme]) => theme.isLight)
|
105 | .map(([name, _]) => name);
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 | get darkThemes(): ReadonlyArray<string> {
|
112 | return Object.entries(this._themes)
|
113 | .filter(([_, theme]) => !theme.isLight)
|
114 | .map(([name, _]) => name);
|
115 | }
|
116 |
|
117 | |
118 |
|
119 |
|
120 | get themeChanged(): ISignal<this, IChangedArgs<string, string | null>> {
|
121 | return this._themeChanged;
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 | isSystemColorSchemeDark(): boolean {
|
128 | return (
|
129 | window.matchMedia &&
|
130 | window.matchMedia('(prefers-color-scheme: dark)').matches
|
131 | );
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | getCSS(key: string): string {
|
142 | return (
|
143 | this._overrides[key] ??
|
144 | getComputedStyle(document.documentElement).getPropertyValue(`--jp-${key}`)
|
145 | );
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 | loadCSS(path: string): Promise<void> {
|
154 | const base = this._base;
|
155 | const href = URLExt.isLocal(path) ? URLExt.join(base, path) : path;
|
156 | const links = this._links;
|
157 |
|
158 | return new Promise((resolve, reject) => {
|
159 | const link = document.createElement('link');
|
160 |
|
161 | link.setAttribute('rel', 'stylesheet');
|
162 | link.setAttribute('type', 'text/css');
|
163 | link.setAttribute('href', href);
|
164 | link.addEventListener('load', () => {
|
165 | resolve(undefined);
|
166 | });
|
167 | link.addEventListener('error', () => {
|
168 | reject(`Stylesheet failed to load: ${href}`);
|
169 | });
|
170 |
|
171 | document.body.appendChild(link);
|
172 | links.push(link);
|
173 |
|
174 |
|
175 | this.loadCSSOverrides();
|
176 | });
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 | loadCSSOverrides(): void {
|
184 | const newOverrides =
|
185 | (this._settings.user['overrides'] as Dict<string>) ?? {};
|
186 |
|
187 |
|
188 | Object.keys({ ...this._overrides, ...newOverrides }).forEach(key => {
|
189 | const val = newOverrides[key];
|
190 |
|
191 | if (val && this.validateCSS(key, val)) {
|
192 |
|
193 | document.documentElement.style.setProperty(`--jp-${key}`, val);
|
194 | } else {
|
195 |
|
196 | delete newOverrides[key];
|
197 | document.documentElement.style.removeProperty(`--jp-${key}`);
|
198 | }
|
199 | });
|
200 |
|
201 |
|
202 | this._overrides = newOverrides;
|
203 | }
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | validateCSS(key: string, val: string): boolean {
|
213 |
|
214 | const prop = this._overrideProps[key];
|
215 |
|
216 | if (!prop) {
|
217 | console.warn(
|
218 | 'CSS validation failed: could not find property corresponding to key.\n' +
|
219 | `key: '${key}', val: '${val}'`
|
220 | );
|
221 | return false;
|
222 | }
|
223 |
|
224 |
|
225 | if (CSS.supports(prop, val)) {
|
226 | return true;
|
227 | } else {
|
228 | console.warn(
|
229 | 'CSS validation failed: invalid value.\n' +
|
230 | `key: '${key}', val: '${val}', prop: '${prop}'`
|
231 | );
|
232 | return false;
|
233 | }
|
234 | }
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | register(theme: IThemeManager.ITheme): IDisposable {
|
244 | const { name } = theme;
|
245 | const themes = this._themes;
|
246 |
|
247 | if (themes[name]) {
|
248 | throw new Error(`Theme already registered for ${name}`);
|
249 | }
|
250 |
|
251 | themes[name] = theme;
|
252 |
|
253 | return new DisposableDelegate(() => {
|
254 | delete themes[name];
|
255 | });
|
256 | }
|
257 |
|
258 | |
259 |
|
260 |
|
261 | setCSSOverride(key: string, value: string): Promise<void> {
|
262 | return this._settings.set('overrides', {
|
263 | ...this._overrides,
|
264 | [key]: value
|
265 | });
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 | setTheme(name: string): Promise<void> {
|
272 | return this._settings.set('theme', name);
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 | setPreferredLightTheme(name: string): Promise<void> {
|
279 | return this._settings.set('preferred-light-theme', name);
|
280 | }
|
281 |
|
282 | |
283 |
|
284 |
|
285 | setPreferredDarkTheme(name: string): Promise<void> {
|
286 | return this._settings.set('preferred-dark-theme', name);
|
287 | }
|
288 |
|
289 | |
290 |
|
291 |
|
292 | isLight(name: string): boolean {
|
293 | return this._themes[name].isLight;
|
294 | }
|
295 |
|
296 | |
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | incrFontSize(key: string): Promise<void> {
|
303 | return this._incrFontSize(key, true);
|
304 | }
|
305 |
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | decrFontSize(key: string): Promise<void> {
|
313 | return this._incrFontSize(key, false);
|
314 | }
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 | themeScrollbars(name: string): boolean {
|
321 | return (
|
322 | !!this._settings.composite['theme-scrollbars'] &&
|
323 | !!this._themes[name].themeScrollbars
|
324 | );
|
325 | }
|
326 |
|
327 | |
328 |
|
329 |
|
330 | isToggledThemeScrollbars(): boolean {
|
331 | return !!this._settings.composite['theme-scrollbars'];
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 | toggleThemeScrollbars(): Promise<void> {
|
338 | return this._settings.set(
|
339 | 'theme-scrollbars',
|
340 | !this._settings.composite['theme-scrollbars']
|
341 | );
|
342 | }
|
343 |
|
344 | |
345 |
|
346 |
|
347 | isToggledAdaptiveTheme(): boolean {
|
348 | return !!this._settings.composite['adaptive-theme'];
|
349 | }
|
350 |
|
351 | |
352 |
|
353 |
|
354 | toggleAdaptiveTheme(): Promise<void> {
|
355 | return this._settings.set(
|
356 | 'adaptive-theme',
|
357 | !this._settings.composite['adaptive-theme']
|
358 | );
|
359 | }
|
360 |
|
361 | |
362 |
|
363 |
|
364 | getDisplayName(name: string): string {
|
365 | return this._themes[name]?.displayName ?? name;
|
366 | }
|
367 |
|
368 | |
369 |
|
370 |
|
371 | private _incrFontSize(key: string, add: boolean = true): Promise<void> {
|
372 |
|
373 | const parts = (this.getCSS(key) ?? '13px').split(/([a-zA-Z]+)/);
|
374 |
|
375 |
|
376 | const incr = (add ? 1 : -1) * (parts[1] === 'em' ? 0.1 : 1);
|
377 |
|
378 |
|
379 | return this.setCSSOverride(key, `${Number(parts[0]) + incr}${parts[1]}`);
|
380 | }
|
381 |
|
382 | |
383 |
|
384 |
|
385 | private _initOverrideProps(): void {
|
386 | const definitions = this._settings.schema.definitions as any;
|
387 | const overidesSchema = definitions.cssOverrides.properties;
|
388 |
|
389 | Object.keys(overidesSchema).forEach(key => {
|
390 |
|
391 |
|
392 | let description;
|
393 | switch (key) {
|
394 | case 'code-font-size':
|
395 | case 'content-font-size1':
|
396 | case 'ui-font-size1':
|
397 | description = 'font-size';
|
398 | break;
|
399 | default:
|
400 | description = overidesSchema[key].description;
|
401 | break;
|
402 | }
|
403 | this._overrideProps[key] = description;
|
404 | });
|
405 | }
|
406 |
|
407 | |
408 |
|
409 |
|
410 | private _loadSettings(): void {
|
411 | const outstanding = this._outstanding;
|
412 | const pending = this._pending;
|
413 | const requests = this._requests;
|
414 |
|
415 |
|
416 | if (pending) {
|
417 | window.clearTimeout(pending);
|
418 | this._pending = 0;
|
419 | }
|
420 |
|
421 | const settings = this._settings;
|
422 | const themes = this._themes;
|
423 |
|
424 | let theme = settings.composite['theme'] as string;
|
425 | if (this.isToggledAdaptiveTheme()) {
|
426 | if (this.isSystemColorSchemeDark()) {
|
427 | theme = this.preferredDarkTheme;
|
428 | } else {
|
429 | theme = this.preferredLightTheme;
|
430 | }
|
431 | }
|
432 |
|
433 |
|
434 |
|
435 |
|
436 | if (outstanding) {
|
437 | outstanding
|
438 | .then(() => {
|
439 | this._loadSettings();
|
440 | })
|
441 | .catch(() => {
|
442 | this._loadSettings();
|
443 | });
|
444 | this._outstanding = null;
|
445 | return;
|
446 | }
|
447 |
|
448 |
|
449 | requests[theme] = requests[theme] ? requests[theme] + 1 : 1;
|
450 |
|
451 |
|
452 | if (themes[theme]) {
|
453 | this._outstanding = this._loadTheme(theme);
|
454 | delete requests[theme];
|
455 | return;
|
456 | }
|
457 |
|
458 |
|
459 | if (requests[theme] > REQUEST_THRESHOLD) {
|
460 | const fallback = settings.default('theme') as string;
|
461 |
|
462 |
|
463 | delete requests[theme];
|
464 |
|
465 | if (!themes[fallback]) {
|
466 | this._onError(
|
467 | this._trans.__(
|
468 | 'Neither theme %1 nor default %2 loaded.',
|
469 | theme,
|
470 | fallback
|
471 | )
|
472 | );
|
473 | return;
|
474 | }
|
475 |
|
476 | console.warn(`Could not load theme ${theme}, using default ${fallback}.`);
|
477 | this._outstanding = this._loadTheme(fallback);
|
478 | return;
|
479 | }
|
480 |
|
481 |
|
482 | this._pending = window.setTimeout(() => {
|
483 | this._loadSettings();
|
484 | }, REQUEST_INTERVAL);
|
485 | }
|
486 |
|
487 | |
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | private _loadTheme(theme: string): Promise<void> {
|
494 | const current = this._current;
|
495 | const links = this._links;
|
496 | const themes = this._themes;
|
497 | const splash = this._splash
|
498 | ? this._splash.show(themes[theme].isLight)
|
499 | : new DisposableDelegate(() => undefined);
|
500 |
|
501 |
|
502 | links.forEach(link => {
|
503 | if (link.parentElement) {
|
504 | link.parentElement.removeChild(link);
|
505 | }
|
506 | });
|
507 | links.length = 0;
|
508 |
|
509 | const themeProps = this._settings.schema.properties?.theme;
|
510 | if (themeProps) {
|
511 | themeProps.enum = Object.keys(themes).map(
|
512 | value => themes[value].displayName ?? value
|
513 | );
|
514 | }
|
515 |
|
516 |
|
517 | const old = current ? themes[current].unload() : Promise.resolve();
|
518 |
|
519 | return Promise.all([old, themes[theme].load()])
|
520 | .then(() => {
|
521 | this._current = theme;
|
522 | this._themeChanged.emit({
|
523 | name: 'theme',
|
524 | oldValue: current,
|
525 | newValue: theme
|
526 | });
|
527 |
|
528 |
|
529 |
|
530 | this._host.hide();
|
531 |
|
532 |
|
533 |
|
534 | requestAnimationFrame(() => {
|
535 | this._host.show();
|
536 | Private.fitAll(this._host);
|
537 | splash.dispose();
|
538 | });
|
539 | })
|
540 | .catch(reason => {
|
541 | this._onError(reason);
|
542 | splash.dispose();
|
543 | });
|
544 | }
|
545 |
|
546 | |
547 |
|
548 |
|
549 | private _onError(reason: any): void {
|
550 | void showDialog({
|
551 | title: this._trans.__('Error Loading Theme'),
|
552 | body: String(reason),
|
553 | buttons: [Dialog.okButton({ label: this._trans.__('OK') })]
|
554 | });
|
555 | }
|
556 |
|
557 | protected translator: ITranslator;
|
558 | private _trans: TranslationBundle;
|
559 | private _base: string;
|
560 | private _current: string | null = null;
|
561 | private _host: Widget;
|
562 | private _links: HTMLLinkElement[] = [];
|
563 | private _overrides: Dict<string> = {};
|
564 | private _overrideProps: Dict<string> = {};
|
565 | private _outstanding: Promise<void> | null = null;
|
566 | private _pending = 0;
|
567 | private _requests: { [theme: string]: number } = {};
|
568 | private _settings: ISettingRegistry.ISettings;
|
569 | private _splash: ISplashScreen | null;
|
570 | private _themes: { [key: string]: IThemeManager.ITheme } = {};
|
571 | private _themeChanged = new Signal<this, IChangedArgs<string, string | null>>(
|
572 | this
|
573 | );
|
574 | }
|
575 |
|
576 | export namespace ThemeManager {
|
577 | |
578 |
|
579 |
|
580 | export interface IOptions {
|
581 | |
582 |
|
583 |
|
584 | host: Widget;
|
585 |
|
586 | |
587 |
|
588 |
|
589 | key: string;
|
590 |
|
591 | |
592 |
|
593 |
|
594 | settings: ISettingRegistry;
|
595 |
|
596 | |
597 |
|
598 |
|
599 | splash?: ISplashScreen;
|
600 |
|
601 | |
602 |
|
603 |
|
604 | url: string;
|
605 |
|
606 | |
607 |
|
608 |
|
609 | translator?: ITranslator;
|
610 | }
|
611 | }
|
612 |
|
613 |
|
614 |
|
615 |
|
616 | namespace Private {
|
617 | |
618 |
|
619 |
|
620 | export function fitAll(widget: Widget): void {
|
621 | for (const child of widget.children()) {
|
622 | fitAll(child);
|
623 | }
|
624 | widget.fit();
|
625 | }
|
626 | }
|