-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy paththeming.ts
198 lines (189 loc) · 5.82 KB
/
theming.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
BuiltinThemeProvider,
ThemeService,
} from '@theia/core/lib/browser/theming';
import { nls } from '@theia/core/lib/common/nls';
import type { Theme, ThemeType } from '@theia/core/lib/common/theme';
import { assertUnreachable } from '../../../common/utils';
export namespace ArduinoThemes {
export const light: Theme = {
id: 'arduino-theme',
type: 'light',
label: 'Light (Arduino)',
editorTheme: 'arduino-theme',
};
export const dark: Theme = {
id: 'arduino-theme-dark',
type: 'dark',
label: 'Dark (Arduino)',
editorTheme: 'arduino-theme-dark',
};
}
const builtInThemeIds = new Set(
[
ArduinoThemes.light,
ArduinoThemes.dark,
BuiltinThemeProvider.hcLightTheme,
BuiltinThemeProvider.hcTheme,
].map(({ id }) => id)
);
const deprecatedThemeIds = new Set(
[BuiltinThemeProvider.lightTheme, BuiltinThemeProvider.darkTheme].map(
({ id }) => id
)
);
export const lightThemeLabel = nls.localize('arduino/theme/light', 'Light');
export const darkThemeLabel = nls.localize('arduino/theme/dark', 'Dark');
export const hcLightThemeLabel = nls.localize(
'arduino/theme/hcLight',
'Light High Contrast'
);
export const hcThemeLabel = nls.localize(
'arduino/theme/hc',
'Dark High Contrast'
);
export function userThemeLabel(theme: Theme): string {
return nls.localize('arduino/theme/user', '{0} (user)', theme.label);
}
export function deprecatedThemeLabel(theme: Theme): string {
return nls.localize(
'arduino/theme/deprecated',
'{0} (deprecated)',
theme.label
);
}
export function themeLabelForSettings(theme: Theme): string {
switch (theme.id) {
case ArduinoThemes.light.id:
return lightThemeLabel;
case ArduinoThemes.dark.id:
return darkThemeLabel;
case BuiltinThemeProvider.hcTheme.id:
return hcThemeLabel;
case BuiltinThemeProvider.hcLightTheme.id:
return hcLightThemeLabel;
case BuiltinThemeProvider.lightTheme.id: // fall-through
case BuiltinThemeProvider.darkTheme.id:
return deprecatedThemeLabel(theme);
default:
return userThemeLabel(theme);
}
}
export function compatibleBuiltInTheme(theme: Theme): Theme {
switch (theme.type) {
case 'light':
return ArduinoThemes.light;
case 'dark':
return ArduinoThemes.dark;
case 'hc':
return BuiltinThemeProvider.hcTheme;
case 'hcLight':
return BuiltinThemeProvider.hcLightTheme;
default: {
console.warn(
`Unhandled theme type: ${theme.type}. Theme ID: ${theme.id}, label: ${theme.label}`
);
return ArduinoThemes.light;
}
}
}
// For tests without DI
interface ThemeProvider {
themes(): Theme[];
currentTheme(): Theme;
}
/**
* Returns with a list of built-in themes officially supported by IDE2 (https://github.com/arduino/arduino-ide/issues/1283).
* The themes in the array follow the following order:
* - built-in themes first (in `Light`, `Dark`, `Light High Contrast`, and `Dark High Contrast`),
* - followed by user installed (VSIX) themes grouped by theme type, then alphabetical order,
* - if the `currentTheme` is either Light (Theia) or Dark (Theia), the last item of the array will be the selected theme with `(deprecated)` suffix.
*/
export function userConfigurableThemes(service: ThemeService): Theme[][];
export function userConfigurableThemes(provider: ThemeProvider): Theme[][];
export function userConfigurableThemes(
serviceOrProvider: ThemeService | ThemeProvider
): Theme[][] {
const provider =
serviceOrProvider instanceof ThemeService
? {
currentTheme: () => serviceOrProvider.getCurrentTheme(),
themes: () => serviceOrProvider.getThemes(),
}
: serviceOrProvider;
const currentTheme = provider.currentTheme();
const allThemes = provider
.themes()
.map((theme) => ({ ...theme, arduinoThemeType: arduinoThemeTypeOf(theme) }))
.filter(
(theme) =>
theme.arduinoThemeType !== 'deprecated' || currentTheme.id === theme.id
)
.sort((left, right) => {
const leftArduinoThemeType = left.arduinoThemeType;
const rightArduinoThemeType = right.arduinoThemeType;
if (leftArduinoThemeType === rightArduinoThemeType) {
const result = themeTypeOrder[left.type] - themeTypeOrder[right.type];
if (result) {
return result;
}
return left.label.localeCompare(right.label); // alphabetical order
}
return (
arduinoThemeTypeOrder[leftArduinoThemeType] -
arduinoThemeTypeOrder[rightArduinoThemeType]
);
});
const builtInThemes: Theme[] = [];
const userThemes: Theme[] = [];
const deprecatedThemes: Theme[] = [];
allThemes.forEach((theme) => {
const { arduinoThemeType } = theme;
switch (arduinoThemeType) {
case 'built-in':
builtInThemes.push(theme);
break;
case 'user':
userThemes.push(theme);
break;
case 'deprecated':
deprecatedThemes.push(theme);
break;
default:
assertUnreachable(arduinoThemeType);
}
});
const groupedThemes: Theme[][] = [];
if (builtInThemes.length) {
groupedThemes.push(builtInThemes);
}
if (userThemes.length) {
groupedThemes.push(userThemes);
}
if (deprecatedThemes.length) {
groupedThemes.push(deprecatedThemes);
}
return groupedThemes;
}
export type ArduinoThemeType = 'built-in' | 'user' | 'deprecated';
const arduinoThemeTypeOrder: Record<ArduinoThemeType, number> = {
'built-in': 0,
user: 1,
deprecated: 2,
};
const themeTypeOrder: Record<ThemeType, number> = {
light: 0,
dark: 1,
hcLight: 2,
hc: 3,
};
export function arduinoThemeTypeOf(theme: Theme | string): ArduinoThemeType {
const themeId = typeof theme === 'string' ? theme : theme.id;
if (builtInThemeIds.has(themeId)) {
return 'built-in';
}
if (deprecatedThemeIds.has(themeId)) {
return 'deprecated';
}
return 'user';
}