From 8e312466aab7441c8ca7d182fb44c9b107067fac Mon Sep 17 00:00:00 2001 From: Francesco Spissu Date: Fri, 8 Jul 2022 17:29:35 +0200 Subject: [PATCH 1/3] add patch for setting IDE startup theme based on OS theme --- .../build/scripts/patch-frontend-index.js | 26 +++++++++++++++++++ electron/build/template-package.json | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 electron/build/scripts/patch-frontend-index.js diff --git a/electron/build/scripts/patch-frontend-index.js b/electron/build/scripts/patch-frontend-index.js new file mode 100644 index 000000000..eeb579c1a --- /dev/null +++ b/electron/build/scripts/patch-frontend-index.js @@ -0,0 +1,26 @@ +// Patch for the startup theme. Replaces the `ThemeService.get().loadUserTheme();` function at startup by directly setting the theme based on the OS theme. +// For all subsequent starts of the IDE the theme applied will be the last one set by the user. + +// 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. +// Once the version of Theia is updated, this patch will be removed and this functionality will be implemented via dependency injection. + +let arg = process.argv.splice(2)[0] +if (!arg) { + console.error("The path to the index.js to patch is missing. Use 'node ./scripts/patch-frontend-index.js ./src-gen/frontend/index.js'") + process.exit(1) +} +(async () => { + const snippet = ` + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + ThemeService.get().setCurrentTheme('arduino-theme-dark'); + } else { + ThemeService.get().setCurrentTheme('arduino-theme'); + }` + const { promises: fs } = require('fs') + const path = require('path') + const index = path.isAbsolute(arg) ? arg : path.join(process.cwd(), arg) + console.log(`>>> Patching 'ThemeService.get().loadUserTheme()' with the snippet to set the theme in ${index}.`) + const content = await fs.readFile(index, { encoding: 'utf-8' }) + await fs.writeFile(index, content.replace('ThemeService.get().loadUserTheme();', snippet), { encoding: 'utf-8' }) + console.log(`<<< Successfully patched index.js.`) +})() diff --git a/electron/build/template-package.json b/electron/build/template-package.json index caa8087cd..11311c6e8 100644 --- a/electron/build/template-package.json +++ b/electron/build/template-package.json @@ -23,7 +23,7 @@ "package": "cross-env DEBUG=* && electron-builder --publish=never", "package:publish": "cross-env DEBUG=* && electron-builder --publish=always", "download:plugins": "theia download:plugins", - "patch": "ncp ./patch/main.js ./src-gen/backend/main.js && node ./scripts/patch-theia-preload.js ./lib/index.html" + "patch": "ncp ./patch/main.js ./src-gen/backend/main.js && node ./scripts/patch-theia-preload.js ./lib/index.html && node ./scripts/patch-frontend-index.js ./src-gen/frontend/index.js" }, "engines": { "node": ">=14.0.0 <15" @@ -152,4 +152,4 @@ "vscode-language-pack-ja":"https://open-vsx.org/api/MS-CEINTL/vscode-language-pack-ja/1.53.2/file/MS-CEINTL.vscode-language-pack-ja-1.53.2.vsix", "vscode-language-pack-tr": "https://open-vsx.org/api/MS-CEINTL/vscode-language-pack-tr/1.53.2/file/MS-CEINTL.vscode-language-pack-tr-1.53.2.vsix" } -} \ No newline at end of file +} From 83d6f9b0919af962ce959f4e14f88a91241a3aaa Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 12 Jul 2022 16:54:51 +0200 Subject: [PATCH 2/3] Patched the default theme behavior. Signed-off-by: Akos Kitta --- electron/.gitignore | 1 - electron/build/patch/{ => backend}/main.js | 0 electron/build/patch/frontend/index.js | 41 +++++++++++++++++++ .../build/scripts/patch-frontend-index.js | 26 ------------ electron/build/template-package.json | 2 +- electron/build/webpack.config.js | 10 +++++ electron/packager/index.js | 1 + 7 files changed, 53 insertions(+), 28 deletions(-) rename electron/build/patch/{ => backend}/main.js (100%) create mode 100644 electron/build/patch/frontend/index.js delete mode 100644 electron/build/scripts/patch-frontend-index.js create mode 100644 electron/build/webpack.config.js diff --git a/electron/.gitignore b/electron/.gitignore index b6d346320..9f6844f02 100644 --- a/electron/.gitignore +++ b/electron/.gitignore @@ -6,7 +6,6 @@ working-copy/ src-gen/ node_modules/ build/yarn.lock -webpack.config.js lib/ # The electron-builder output. diff --git a/electron/build/patch/main.js b/electron/build/patch/backend/main.js similarity index 100% rename from electron/build/patch/main.js rename to electron/build/patch/backend/main.js diff --git a/electron/build/patch/frontend/index.js b/electron/build/patch/frontend/index.js new file mode 100644 index 000000000..2f8abefd2 --- /dev/null +++ b/electron/build/patch/frontend/index.js @@ -0,0 +1,41 @@ +// Patch for the startup theme. Customizes the `ThemeService.get().defaultTheme();` to dispatch the default IDE2 theme based on the OS' theme. +// For all subsequent starts of the IDE the theme applied will be the last one set by the user. + +// 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. +// Once the version of Theia is updated, this patch will be removed and this functionality will be implemented via dependency injection. +// Ideally, we should open a PR in Theia and add support for `light` and `dark` default themes in the app config. + +const { + ThemeService, + ThemeServiceSymbol, + BuiltinThemeProvider, +} = require('@theia/core/lib/browser/theming'); +const { + ApplicationProps, +} = require('@theia/application-package/lib/application-props'); + +const lightTheme = 'arduino-theme'; +const darkTheme = 'arduino-theme-dark'; +const defaultTheme = + window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches + ? darkTheme + : lightTheme; + +if (!window[ThemeServiceSymbol]) { + const themeService = new ThemeService(); + Object.defineProperty(themeService, 'defaultTheme', { + get: function () { + return ( + this.themes[defaultTheme] || + this.themes[ApplicationProps.DEFAULT.frontend.config.defaultTheme] + ); + }, + }); + themeService.register(...BuiltinThemeProvider.themes); + themeService.startupTheme(); + themeService.setCurrentTheme(defaultTheme); + window[ThemeServiceSymbol] = themeService; +} + +// Require the original, generated `index.js` for `webpack` as the next entry for the `bundle.js`. +require('../../src-gen/frontend/index'); diff --git a/electron/build/scripts/patch-frontend-index.js b/electron/build/scripts/patch-frontend-index.js deleted file mode 100644 index eeb579c1a..000000000 --- a/electron/build/scripts/patch-frontend-index.js +++ /dev/null @@ -1,26 +0,0 @@ -// Patch for the startup theme. Replaces the `ThemeService.get().loadUserTheme();` function at startup by directly setting the theme based on the OS theme. -// For all subsequent starts of the IDE the theme applied will be the last one set by the user. - -// 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. -// Once the version of Theia is updated, this patch will be removed and this functionality will be implemented via dependency injection. - -let arg = process.argv.splice(2)[0] -if (!arg) { - console.error("The path to the index.js to patch is missing. Use 'node ./scripts/patch-frontend-index.js ./src-gen/frontend/index.js'") - process.exit(1) -} -(async () => { - const snippet = ` - if (window.matchMedia('(prefers-color-scheme: dark)').matches) { - ThemeService.get().setCurrentTheme('arduino-theme-dark'); - } else { - ThemeService.get().setCurrentTheme('arduino-theme'); - }` - const { promises: fs } = require('fs') - const path = require('path') - const index = path.isAbsolute(arg) ? arg : path.join(process.cwd(), arg) - console.log(`>>> Patching 'ThemeService.get().loadUserTheme()' with the snippet to set the theme in ${index}.`) - const content = await fs.readFile(index, { encoding: 'utf-8' }) - await fs.writeFile(index, content.replace('ThemeService.get().loadUserTheme();', snippet), { encoding: 'utf-8' }) - console.log(`<<< Successfully patched index.js.`) -})() diff --git a/electron/build/template-package.json b/electron/build/template-package.json index 11311c6e8..9ce815e33 100644 --- a/electron/build/template-package.json +++ b/electron/build/template-package.json @@ -23,7 +23,7 @@ "package": "cross-env DEBUG=* && electron-builder --publish=never", "package:publish": "cross-env DEBUG=* && electron-builder --publish=always", "download:plugins": "theia download:plugins", - "patch": "ncp ./patch/main.js ./src-gen/backend/main.js && node ./scripts/patch-theia-preload.js ./lib/index.html && node ./scripts/patch-frontend-index.js ./src-gen/frontend/index.js" + "patch": "ncp ./patch/backend/main.js ./src-gen/backend/main.js && node ./scripts/patch-theia-preload.js ./lib/index.html" }, "engines": { "node": ">=14.0.0 <15" diff --git a/electron/build/webpack.config.js b/electron/build/webpack.config.js new file mode 100644 index 000000000..bd042642a --- /dev/null +++ b/electron/build/webpack.config.js @@ -0,0 +1,10 @@ +// @ts-check +const config = require('./gen-webpack.config.js'); +const path = require('path'); + +// Load the patched `index.js` that sets the desired theme in IDE2 based on the OS' theme. +// The `patch/frontend/index.js` will require the original, generated `index.js`. +// See: https://github.com/arduino/arduino-ide/pull/1160. +config.entry.bundle = path.resolve(__dirname, 'patch/frontend/index.js'); + +module.exports = config; diff --git a/electron/packager/index.js b/electron/packager/index.js index 2f216f45c..a5e785706 100644 --- a/electron/packager/index.js +++ b/electron/packager/index.js @@ -52,6 +52,7 @@ 'resources', 'scripts', 'template-package.json', + 'webpack.config.js' ]; fs.readdirSync(path('..', 'build')) .filter((filename) => resourcesToKeep.indexOf(filename) === -1) From 35e18b6a1462fac4b5725f022b0da8d6364a9904 Mon Sep 17 00:00:00 2001 From: Francesco Spissu Date: Thu, 14 Jul 2022 14:40:36 +0200 Subject: [PATCH 3/3] add custom themes in register --- electron/build/patch/frontend/index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/electron/build/patch/frontend/index.js b/electron/build/patch/frontend/index.js index 2f8abefd2..1d6a9ff32 100644 --- a/electron/build/patch/frontend/index.js +++ b/electron/build/patch/frontend/index.js @@ -21,6 +21,24 @@ const defaultTheme = ? darkTheme : lightTheme; +const arduinoDarkTheme = { + id: 'arduino-theme-dark', + type: 'dark', + label: 'Dark (Arduino)', + editorTheme: 'arduino-theme-dark', + activate() { }, + deactivate() { } +}; + +const arduinoLightTheme = { + id: 'arduino-theme', + type: 'light', + label: 'Light (Arduino)', + editorTheme: 'arduino-theme', + activate() { }, + deactivate() { } +}; + if (!window[ThemeServiceSymbol]) { const themeService = new ThemeService(); Object.defineProperty(themeService, 'defaultTheme', { @@ -31,7 +49,7 @@ if (!window[ThemeServiceSymbol]) { ); }, }); - themeService.register(...BuiltinThemeProvider.themes); + themeService.register(...BuiltinThemeProvider.themes, arduinoDarkTheme, arduinoLightTheme); themeService.startupTheme(); themeService.setCurrentTheme(defaultTheme); window[ThemeServiceSymbol] = themeService;