|
| 1 | +// Patch for the startup theme. Customizes the `ThemeService.get().defaultTheme();` to dispatch the default IDE2 theme based on the OS' theme. |
| 2 | +// For all subsequent starts of the IDE the theme applied will be the last one set by the user. |
| 3 | + |
| 4 | +// With the current version of Theia adopted (1.25) it is not possible to extend the `ThemeService`, it will be possible starting from Theia 1.27. |
| 5 | +// Once the version of Theia is updated, this patch will be removed and this functionality will be implemented via dependency injection. |
| 6 | +// Ideally, we should open a PR in Theia and add support for `light` and `dark` default themes in the app config. |
| 7 | + |
| 8 | +const { |
| 9 | + ThemeService, |
| 10 | + ThemeServiceSymbol, |
| 11 | + BuiltinThemeProvider, |
| 12 | +} = require('@theia/core/lib/browser/theming'); |
| 13 | +const { |
| 14 | + ApplicationProps, |
| 15 | +} = require('@theia/application-package/lib/application-props'); |
| 16 | + |
| 17 | +const lightTheme = 'arduino-theme'; |
| 18 | +const darkTheme = 'arduino-theme-dark'; |
| 19 | +const defaultTheme = |
| 20 | + window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches |
| 21 | + ? darkTheme |
| 22 | + : lightTheme; |
| 23 | + |
| 24 | +const arduinoDarkTheme = { |
| 25 | + id: 'arduino-theme-dark', |
| 26 | + type: 'dark', |
| 27 | + label: 'Dark (Arduino)', |
| 28 | + editorTheme: 'arduino-theme-dark', |
| 29 | + activate() { }, |
| 30 | + deactivate() { } |
| 31 | +}; |
| 32 | + |
| 33 | +const arduinoLightTheme = { |
| 34 | + id: 'arduino-theme', |
| 35 | + type: 'light', |
| 36 | + label: 'Light (Arduino)', |
| 37 | + editorTheme: 'arduino-theme', |
| 38 | + activate() { }, |
| 39 | + deactivate() { } |
| 40 | +}; |
| 41 | + |
| 42 | +if (!window[ThemeServiceSymbol]) { |
| 43 | + const themeService = new ThemeService(); |
| 44 | + Object.defineProperty(themeService, 'defaultTheme', { |
| 45 | + get: function () { |
| 46 | + return ( |
| 47 | + this.themes[defaultTheme] || |
| 48 | + this.themes[ApplicationProps.DEFAULT.frontend.config.defaultTheme] |
| 49 | + ); |
| 50 | + }, |
| 51 | + }); |
| 52 | + themeService.register(...BuiltinThemeProvider.themes, arduinoDarkTheme, arduinoLightTheme); |
| 53 | + themeService.startupTheme(); |
| 54 | + themeService.setCurrentTheme(defaultTheme); |
| 55 | + window[ThemeServiceSymbol] = themeService; |
| 56 | +} |
| 57 | + |
| 58 | +// Require the original, generated `index.js` for `webpack` as the next entry for the `bundle.js`. |
| 59 | +require('../../src-gen/frontend/index'); |
0 commit comments