-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
icon-theme-service.ts
179 lines (149 loc) · 6.31 KB
/
icon-theme-service.ts
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
// *****************************************************************************
// Copyright (C) 2019 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import { injectable, inject, postConstruct } from 'inversify';
import { Emitter } from '../common/event';
import { Disposable, DisposableCollection } from '../common/disposable';
import { LabelProviderContribution, DidChangeLabelEvent } from './label-provider';
import { FrontendApplicationConfigProvider } from './frontend-application-config-provider';
export interface IconThemeDefinition {
readonly id: string
readonly label: string
readonly description?: string
readonly hasFileIcons?: boolean;
readonly hasFolderIcons?: boolean;
readonly hidesExplorerArrows?: boolean;
}
export interface IconTheme extends IconThemeDefinition {
activate(): Disposable;
}
@injectable()
export class NoneIconTheme implements IconTheme, LabelProviderContribution {
readonly id = 'none';
readonly label = 'None';
readonly description = 'Disable file icons';
readonly hasFileIcons = true;
readonly hasFolderIcons = true;
protected readonly onDidChangeEmitter = new Emitter<DidChangeLabelEvent>();
readonly onDidChange = this.onDidChangeEmitter.event;
protected readonly toDeactivate = new DisposableCollection();
activate(): Disposable {
if (this.toDeactivate.disposed) {
this.toDeactivate.push(Disposable.create(() => this.fireDidChange()));
this.fireDidChange();
}
return this.toDeactivate;
}
protected fireDidChange(): void {
this.onDidChangeEmitter.fire({ affects: () => true });
}
canHandle(): number {
if (this.toDeactivate.disposed) {
return 0;
}
return Number.MAX_SAFE_INTEGER;
}
getIcon(): string {
return '';
}
}
@injectable()
export class IconThemeService {
protected static STORAGE_KEY = 'iconTheme';
protected readonly onDidChangeEmitter = new Emitter<void>();
readonly onDidChange = this.onDidChangeEmitter.event;
protected readonly _iconThemes = new Map<string, IconTheme>();
get ids(): IterableIterator<string> {
return this._iconThemes.keys();
}
get definitions(): IterableIterator<IconThemeDefinition> {
return this._iconThemes.values();
}
getDefinition(id: string): IconThemeDefinition | undefined {
return this._iconThemes.get(id);
}
@inject(NoneIconTheme)
protected readonly noneIconTheme: NoneIconTheme;
protected readonly onDidChangeCurrentEmitter = new Emitter<string>();
readonly onDidChangeCurrent = this.onDidChangeCurrentEmitter.event;
protected readonly toDeactivate = new DisposableCollection();
@postConstruct()
protected init(): void {
this.initDefaultTheme();
this.register(this.noneIconTheme);
}
protected initDefaultTheme(): void {
// If no theme is set yet, we store the desired default theme so it will be applied as soon as it is registered.
// The registration may happen after preference initialization when contributed by a plugin.
// If another theme is set by the user in the meantime, the local storage will be overridden anyway.
if (!window.localStorage.getItem(IconThemeService.STORAGE_KEY) && FrontendApplicationConfigProvider.get().defaultIconTheme) {
window.localStorage.setItem(IconThemeService.STORAGE_KEY, FrontendApplicationConfigProvider.get().defaultIconTheme);
}
}
register(iconTheme: IconTheme): Disposable {
if (this._iconThemes.has(iconTheme.id)) {
console.warn(new Error(`Icon theme '${iconTheme.id}' has already been registered, skipping.`));
return Disposable.NULL;
}
this._iconThemes.set(iconTheme.id, iconTheme);
this.onDidChangeEmitter.fire(undefined);
if (this.toDeactivate.disposed
&& window.localStorage.getItem(IconThemeService.STORAGE_KEY) === iconTheme.id) {
this.setCurrent(iconTheme);
}
return Disposable.create(() => this.unregister(iconTheme.id));
}
unregister(id: string): IconTheme | undefined {
const iconTheme = this._iconThemes.get(id);
if (!iconTheme) {
return undefined;
}
this._iconThemes.delete(id);
if (window.localStorage.getItem(IconThemeService.STORAGE_KEY) === id) {
window.localStorage.removeItem(IconThemeService.STORAGE_KEY);
this.onDidChangeCurrentEmitter.fire(this.default.id);
}
this.onDidChangeEmitter.fire(undefined);
return iconTheme;
}
get current(): string {
return this.getCurrent().id;
}
set current(id: string) {
const newCurrent = this._iconThemes.get(id) || this.default;
if (this.getCurrent().id !== newCurrent.id) {
this.setCurrent(newCurrent);
}
}
protected getCurrent(): IconTheme {
const id = window.localStorage.getItem(IconThemeService.STORAGE_KEY);
return id && this._iconThemes.get(id) || this.default;
}
protected setCurrent(current: IconTheme): void {
window.localStorage.setItem(IconThemeService.STORAGE_KEY, current.id);
this.toDeactivate.dispose();
this.toDeactivate.push(current.activate());
this.onDidChangeCurrentEmitter.fire(current.id);
}
get default(): IconTheme {
return this._iconThemes.get(FrontendApplicationConfigProvider.get().defaultIconTheme) || this.fallback;
}
get fallback(): IconTheme {
return this.noneIconTheme;
}
protected load(): string | undefined {
return window.localStorage.getItem(IconThemeService.STORAGE_KEY) || undefined;
}
}