From 1eb148d121dbcbe1e54085a9fb897fe7ac1529b7 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Mon, 23 Sep 2019 20:15:23 +0200 Subject: Add darkreader for universal darkmode --- src/webview/recipe.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/webview') diff --git a/src/webview/recipe.js b/src/webview/recipe.js index e00566283..e97013b3c 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -1,8 +1,13 @@ import { ipcRenderer } from 'electron'; import path from 'path'; import { autorun, computed, observable } from 'mobx'; +import fs from 'fs-extra'; import { loadModule } from 'cld3-asm'; import { debounce } from 'lodash'; +import { + enable as enableDarkMode, + disable as disableDarkMode, +} from 'darkreader'; import RecipeWebview from './lib/RecipeWebview'; @@ -109,12 +114,27 @@ class RecipeController { } } - if (this.settings.service.isDarkModeEnabled) { + if (this.settings.service.isDarkModeEnabled || this.settings.app.darkMode) { debug('Enable dark mode'); - injectDarkModeStyle(this.settings.service.recipe.path); - } else if (isDarkModeStyleInjected()) { + + // Check if recipe has a darkmode.css + const darkModeStyle = path.join(this.settings.service.recipe.path, 'darkmode.css'); + const darkModeExists = fs.pathExistsSync(darkModeStyle); + + if (darkModeExists) { + injectDarkModeStyle(this.settings.service.recipe.path); + } else { + // Use darkreader instead + enableDarkMode(); + } + } else { debug('Remove dark mode'); - removeDarkModeStyle(); + + if (isDarkModeStyleInjected()) { + removeDarkModeStyle(); + } else { + disableDarkMode(); + } } } -- cgit v1.2.3-70-g09d2 From aa18d3b84ece4fbc864f6a4f4d3b84fbef564b48 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Mon, 23 Sep 2019 20:18:01 +0200 Subject: Fix lint --- package.json | 2 +- src/components/services/content/ServiceWebview.js | 20 +++++++++++++++++++- src/features/quickSwitch/Component.js | 3 +++ src/i18n/locales/defaultMessages.json | 8 ++++---- .../messages/src/features/quickSwitch/Component.json | 8 ++++---- src/webview/recipe.js | 2 +- 6 files changed, 32 insertions(+), 11 deletions(-) (limited to 'src/webview') diff --git a/package.json b/package.json index 39ad362b9..98f3e0ec4 100644 --- a/package.json +++ b/package.json @@ -173,7 +173,7 @@ ], "husky": { "hooks": { - "pre-push": "npm run lint && npm run reformat-files && npm run apply-branding && npm run manage-translations" + "pre-push": "npm run lint && npm run reformat-files && npm run manage-translations && npm run apply-branding" } } } diff --git a/src/components/services/content/ServiceWebview.js b/src/components/services/content/ServiceWebview.js index 7252c695f..75b3d2cf0 100644 --- a/src/components/services/content/ServiceWebview.js +++ b/src/components/services/content/ServiceWebview.js @@ -1,10 +1,13 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { observer } from 'mobx-react'; +import { observable, reaction } from 'mobx'; import ElectronWebView from 'react-electron-web-view'; import ServiceModel from '../../../models/Service'; +const debug = require('debug')('Ferdi:Services'); + @observer class ServiceWebview extends Component { static propTypes = { @@ -13,7 +16,22 @@ class ServiceWebview extends Component { detachService: PropTypes.func.isRequired, }; - webview = null; + @observable webview = null; + + constructor(props) { + super(props); + + reaction( + () => this.webview, + () => { + if (this.webview.view) { + this.webview.view.addEventListener('console-message', (e) => { + debug('Service logged a message:', e.message); + }); + } + }, + ); + } componentWillUnmount() { const { service, detachService } = this.props; diff --git a/src/features/quickSwitch/Component.js b/src/features/quickSwitch/Component.js index 3217a3d93..ddbdbe304 100644 --- a/src/features/quickSwitch/Component.js +++ b/src/features/quickSwitch/Component.js @@ -1,4 +1,5 @@ import React, { Component, createRef } from 'react'; +import { remote } from 'electron'; import PropTypes from 'prop-types'; import { observer, inject } from 'mobx-react'; import { reaction } from 'mobx'; @@ -190,6 +191,8 @@ export default @injectSheet(styles) @inject('stores', 'actions') @observer class if (isModalVisible && !this.state.wasPrevVisible) { // Set focus back on current window if its in a service // TODO: Find a way to gain back focus + remote.getCurrentWindow().blurWebView(); + remote.getCurrentWindow().webContents.focus(); // The input "focus" attribute will only work on first modal open // Manually add focus to the input element diff --git a/src/i18n/locales/defaultMessages.json b/src/i18n/locales/defaultMessages.json index de1712048..ccb81a9f3 100644 --- a/src/i18n/locales/defaultMessages.json +++ b/src/i18n/locales/defaultMessages.json @@ -4167,26 +4167,26 @@ "defaultMessage": "!!!Search...", "end": { "column": 3, - "line": 17 + "line": 18 }, "file": "src/features/quickSwitch/Component.js", "id": "feature.quickSwitch.search", "start": { "column": 10, - "line": 14 + "line": 15 } }, { "defaultMessage": "!!!Select a service with TAB, ↑ and ↓. Open a service with ENTER.", "end": { "column": 3, - "line": 21 + "line": 22 }, "file": "src/features/quickSwitch/Component.js", "id": "feature.quickSwitch.info", "start": { "column": 8, - "line": 18 + "line": 19 } } ], diff --git a/src/i18n/messages/src/features/quickSwitch/Component.json b/src/i18n/messages/src/features/quickSwitch/Component.json index 248925a37..f42e1af49 100644 --- a/src/i18n/messages/src/features/quickSwitch/Component.json +++ b/src/i18n/messages/src/features/quickSwitch/Component.json @@ -4,11 +4,11 @@ "defaultMessage": "!!!Search...", "file": "src/features/quickSwitch/Component.js", "start": { - "line": 14, + "line": 15, "column": 10 }, "end": { - "line": 17, + "line": 18, "column": 3 } }, @@ -17,11 +17,11 @@ "defaultMessage": "!!!Select a service with TAB, ↑ and ↓. Open a service with ENTER.", "file": "src/features/quickSwitch/Component.js", "start": { - "line": 18, + "line": 19, "column": 8 }, "end": { - "line": 21, + "line": 22, "column": 3 } } diff --git a/src/webview/recipe.js b/src/webview/recipe.js index e97013b3c..fca00fde2 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js @@ -114,7 +114,7 @@ class RecipeController { } } - if (this.settings.service.isDarkModeEnabled || this.settings.app.darkMode) { + if (this.settings.service.isDarkModeEnabled || this.settings.app.darkMode) { debug('Enable dark mode'); // Check if recipe has a darkmode.css -- cgit v1.2.3-70-g09d2