|  | 
| 1 |  | -import type { Theme } from '@theia/core/lib/common/theme'; | 
| 2 |  | -import { injectable } from '@theia/core/shared/inversify'; | 
| 3 |  | -import { ThemeServiceWithDB as TheiaThemeServiceWithDB } from '@theia/monaco/lib/browser/monaco-indexed-db'; | 
|  | 1 | +import { | 
|  | 2 | + BuiltinThemeProvider, | 
|  | 3 | + ThemeService, | 
|  | 4 | +} from '@theia/core/lib/browser/theming'; | 
|  | 5 | +import { nls } from '@theia/core/lib/common/nls'; | 
|  | 6 | +import type { Theme, ThemeType } from '@theia/core/lib/common/theme'; | 
|  | 7 | +import { assertUnreachable } from '../../../common/utils'; | 
| 4 | 8 | 
 | 
| 5 | 9 | export namespace ArduinoThemes { | 
| 6 |  | - export const Light: Theme = { | 
|  | 10 | + export const light: Theme = { | 
| 7 | 11 |  id: 'arduino-theme', | 
| 8 | 12 |  type: 'light', | 
| 9 | 13 |  label: 'Light (Arduino)', | 
| 10 | 14 |  editorTheme: 'arduino-theme', | 
| 11 | 15 |  }; | 
| 12 |  | - export const Dark: Theme = { | 
|  | 16 | + export const dark: Theme = { | 
| 13 | 17 |  id: 'arduino-theme-dark', | 
| 14 | 18 |  type: 'dark', | 
| 15 | 19 |  label: 'Dark (Arduino)', | 
| 16 | 20 |  editorTheme: 'arduino-theme-dark', | 
| 17 | 21 |  }; | 
| 18 | 22 | } | 
| 19 | 23 | 
 | 
| 20 |  | -@injectable() | 
| 21 |  | -export class ThemeServiceWithDB extends TheiaThemeServiceWithDB { | 
| 22 |  | - protected override init(): void { | 
| 23 |  | - this.register(ArduinoThemes.Light, ArduinoThemes.Dark); | 
| 24 |  | - super.init(); | 
|  | 24 | +const builtInThemeIds = new Set( | 
|  | 25 | + [ | 
|  | 26 | + ArduinoThemes.light, | 
|  | 27 | + ArduinoThemes.dark, | 
|  | 28 | + BuiltinThemeProvider.hcTheme, | 
|  | 29 | + // TODO: add the HC light theme after Theia 1.36 | 
|  | 30 | + ].map(({ id }) => id) | 
|  | 31 | +); | 
|  | 32 | +const deprecatedThemeIds = new Set( | 
|  | 33 | + [BuiltinThemeProvider.lightTheme, BuiltinThemeProvider.darkTheme].map( | 
|  | 34 | + ({ id }) => id | 
|  | 35 | + ) | 
|  | 36 | +); | 
|  | 37 | + | 
|  | 38 | +export const lightThemeLabel = nls.localize('arduino/theme/light', 'Light'); | 
|  | 39 | +export const darkThemeLabel = nls.localize('arduino/theme/dark', 'Dark'); | 
|  | 40 | +export const hcThemeLabel = nls.localize('arduino/theme/hc', 'High Contrast'); | 
|  | 41 | +export function userThemeLabel(theme: Theme): string { | 
|  | 42 | + return nls.localize('arduino/theme/user', '{0} (user)', theme.label); | 
|  | 43 | +} | 
|  | 44 | +export function deprecatedThemeLabel(theme: Theme): string { | 
|  | 45 | + return nls.localize( | 
|  | 46 | + 'arduino/theme/deprecated', | 
|  | 47 | + '{0} (deprecated)', | 
|  | 48 | + theme.label | 
|  | 49 | + ); | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +export function themeLabelForSettings(theme: Theme): string { | 
|  | 53 | + switch (theme.id) { | 
|  | 54 | + case ArduinoThemes.light.id: | 
|  | 55 | + return lightThemeLabel; | 
|  | 56 | + case ArduinoThemes.dark.id: | 
|  | 57 | + return darkThemeLabel; | 
|  | 58 | + case BuiltinThemeProvider.hcTheme.id: | 
|  | 59 | + return hcThemeLabel; | 
|  | 60 | + case BuiltinThemeProvider.lightTheme.id: // fall-through | 
|  | 61 | + case BuiltinThemeProvider.darkTheme.id: | 
|  | 62 | + return deprecatedThemeLabel(theme); | 
|  | 63 | + default: | 
|  | 64 | + return userThemeLabel(theme); | 
|  | 65 | + } | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +export function compatibleBuiltInTheme(theme: Theme): Theme { | 
|  | 69 | + switch (theme.type) { | 
|  | 70 | + case 'light': | 
|  | 71 | + return ArduinoThemes.light; | 
|  | 72 | + case 'dark': | 
|  | 73 | + return ArduinoThemes.dark; | 
|  | 74 | + case 'hc': | 
|  | 75 | + return BuiltinThemeProvider.hcTheme; | 
|  | 76 | + default: { | 
|  | 77 | + console.warn( | 
|  | 78 | + `Unhandled theme type: ${theme.type}. Theme ID: ${theme.id}, label: ${theme.label}` | 
|  | 79 | + ); | 
|  | 80 | + return ArduinoThemes.light; | 
|  | 81 | + } | 
|  | 82 | + } | 
|  | 83 | +} | 
|  | 84 | + | 
|  | 85 | +// For tests without DI | 
|  | 86 | +interface ThemeProvider { | 
|  | 87 | + themes(): Theme[]; | 
|  | 88 | + currentTheme(): Theme; | 
|  | 89 | +} | 
|  | 90 | + | 
|  | 91 | +/** | 
|  | 92 | + * Returns with a list of built-in themes officially supported by IDE2 (https://github.com/arduino/arduino-ide/issues/1283). | 
|  | 93 | + * The themes in the array follow the following order: | 
|  | 94 | + * - built-in themes first (in `Light`, `Dark`, `High Contrast`), // TODO -> High Contrast will be split up to HC Dark and HC Light after the Theia version uplift | 
|  | 95 | + * - followed by user installed (VSIX) themes grouped by theme type, then alphabetical order, | 
|  | 96 | + * - if the `currentTheme` is either Light (Theia) or Dark (Theia), the last item of the array will be the selected theme with `(deprecated)` suffix. | 
|  | 97 | + */ | 
|  | 98 | +export function userConfigurableThemes(service: ThemeService): Theme[][]; | 
|  | 99 | +export function userConfigurableThemes(provider: ThemeProvider): Theme[][]; | 
|  | 100 | +export function userConfigurableThemes( | 
|  | 101 | + serviceOrProvider: ThemeService | ThemeProvider | 
|  | 102 | +): Theme[][] { | 
|  | 103 | + const provider = | 
|  | 104 | + serviceOrProvider instanceof ThemeService | 
|  | 105 | + ? { | 
|  | 106 | + currentTheme: () => serviceOrProvider.getCurrentTheme(), | 
|  | 107 | + themes: () => serviceOrProvider.getThemes(), | 
|  | 108 | + } | 
|  | 109 | + : serviceOrProvider; | 
|  | 110 | + const currentTheme = provider.currentTheme(); | 
|  | 111 | + const allThemes = provider | 
|  | 112 | + .themes() | 
|  | 113 | + .map((theme) => ({ ...theme, arduinoThemeType: arduinoThemeTypeOf(theme) })) | 
|  | 114 | + .filter( | 
|  | 115 | + (theme) => | 
|  | 116 | + theme.arduinoThemeType !== 'deprecated' || currentTheme.id === theme.id | 
|  | 117 | + ) | 
|  | 118 | + .sort((left, right) => { | 
|  | 119 | + const leftArduinoThemeType = left.arduinoThemeType; | 
|  | 120 | + const rightArduinoThemeType = right.arduinoThemeType; | 
|  | 121 | + if (leftArduinoThemeType === rightArduinoThemeType) { | 
|  | 122 | + const result = themeTypeOrder[left.type] - themeTypeOrder[right.type]; | 
|  | 123 | + if (result) { | 
|  | 124 | + return result; | 
|  | 125 | + } | 
|  | 126 | + return left.label.localeCompare(right.label); // alphabetical order | 
|  | 127 | + } | 
|  | 128 | + return ( | 
|  | 129 | + arduinoThemeTypeOrder[leftArduinoThemeType] - | 
|  | 130 | + arduinoThemeTypeOrder[rightArduinoThemeType] | 
|  | 131 | + ); | 
|  | 132 | + }); | 
|  | 133 | + const builtInThemes: Theme[] = []; | 
|  | 134 | + const userThemes: Theme[] = []; | 
|  | 135 | + const deprecatedThemes: Theme[] = []; | 
|  | 136 | + allThemes.forEach((theme) => { | 
|  | 137 | + const { arduinoThemeType } = theme; | 
|  | 138 | + switch (arduinoThemeType) { | 
|  | 139 | + case 'built-in': | 
|  | 140 | + builtInThemes.push(theme); | 
|  | 141 | + break; | 
|  | 142 | + case 'user': | 
|  | 143 | + userThemes.push(theme); | 
|  | 144 | + break; | 
|  | 145 | + case 'deprecated': | 
|  | 146 | + deprecatedThemes.push(theme); | 
|  | 147 | + break; | 
|  | 148 | + default: | 
|  | 149 | + assertUnreachable(arduinoThemeType); | 
|  | 150 | + } | 
|  | 151 | + }); | 
|  | 152 | + const groupedThemes: Theme[][] = []; | 
|  | 153 | + if (builtInThemes.length) { | 
|  | 154 | + groupedThemes.push(builtInThemes); | 
|  | 155 | + } | 
|  | 156 | + if (userThemes.length) { | 
|  | 157 | + groupedThemes.push(userThemes); | 
|  | 158 | + } | 
|  | 159 | + if (deprecatedThemes.length) { | 
|  | 160 | + groupedThemes.push(deprecatedThemes); | 
|  | 161 | + } | 
|  | 162 | + return groupedThemes; | 
|  | 163 | +} | 
|  | 164 | + | 
|  | 165 | +export type ArduinoThemeType = 'built-in' | 'user' | 'deprecated'; | 
|  | 166 | +const arduinoThemeTypeOrder: Record<ArduinoThemeType, number> = { | 
|  | 167 | + 'built-in': 0, | 
|  | 168 | + user: 1, | 
|  | 169 | + deprecated: 2, | 
|  | 170 | +}; | 
|  | 171 | +const themeTypeOrder: Record<ThemeType, number> = { | 
|  | 172 | + light: 0, | 
|  | 173 | + dark: 1, | 
|  | 174 | + hc: 2, | 
|  | 175 | +}; | 
|  | 176 | + | 
|  | 177 | +export function arduinoThemeTypeOf(theme: Theme | string): ArduinoThemeType { | 
|  | 178 | + const themeId = typeof theme === 'string' ? theme : theme.id; | 
|  | 179 | + if (builtInThemeIds.has(themeId)) { | 
|  | 180 | + return 'built-in'; | 
|  | 181 | + } | 
|  | 182 | + if (deprecatedThemeIds.has(themeId)) { | 
|  | 183 | + return 'deprecated'; | 
| 25 | 184 |  } | 
|  | 185 | + return 'user'; | 
| 26 | 186 | } | 
0 commit comments