diff options
| -rw-r--r-- | dev/data/manifest-variants.json | 1 | ||||
| -rw-r--r-- | ext/css/display.css | 14 | ||||
| -rw-r--r-- | ext/css/popup-outer.css | 3 | ||||
| -rw-r--r-- | ext/data/schemas/options-schema.json | 4 | ||||
| -rw-r--r-- | ext/js/app/popup-window.js | 2 | ||||
| -rw-r--r-- | ext/js/app/popup.js | 55 | ||||
| -rw-r--r-- | ext/js/app/theme-controller.js | 213 | ||||
| -rw-r--r-- | ext/js/data/options-util.js | 21 | ||||
| -rw-r--r-- | ext/js/display/display.js | 18 | ||||
| -rw-r--r-- | ext/manifest.json | 1 | ||||
| -rw-r--r-- | ext/popup-preview.html | 1 | ||||
| -rw-r--r-- | ext/popup.html | 1 | ||||
| -rw-r--r-- | ext/search.html | 1 | ||||
| -rw-r--r-- | ext/settings.html | 8 | ||||
| -rw-r--r-- | test/test-options-util.js | 6 | 
15 files changed, 280 insertions, 69 deletions
| diff --git a/dev/data/manifest-variants.json b/dev/data/manifest-variants.json index e8edd977..c797705a 100644 --- a/dev/data/manifest-variants.json +++ b/dev/data/manifest-variants.json @@ -49,6 +49,7 @@                      "js/app/popup-factory.js",                      "js/app/popup-proxy.js",                      "js/app/popup-window.js", +                    "js/app/theme-controller.js",                      "js/comm/api.js",                      "js/comm/cross-frame-api.js",                      "js/comm/frame-ancestry-handler.js", diff --git a/ext/css/display.css b/ext/css/display.css index f0908021..f703be85 100644 --- a/ext/css/display.css +++ b/ext/css/display.css @@ -309,25 +309,25 @@ a {  /* Scrollbars */ -:root:not([data-theme=default]) .scrollbar { +:root:not([data-theme=light]) .scrollbar {      scrollbar-color: var(--scrollbar-thumb-color) var(--scrollbar-track-color);  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar {      width: auto;  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar-button { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar-button {      height: 0;  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar-thumb { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar-thumb {      background-color: var(--scrollbar-thumb-color);  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar-track { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar-track {      background-color: var(--scrollbar-thumb-color);  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar-track-piece { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar-track-piece {      background-color: var(--scrollbar-track-color);  } -:root:not([data-theme=default]) .scrollbar::-webkit-scrollbar-corner { +:root:not([data-theme=light]) .scrollbar::-webkit-scrollbar-corner {      background-color: var(--scrollbar-track-color);  }  :root .scrollbar-inverse { diff --git a/ext/css/popup-outer.css b/ext/css/popup-outer.css index 119a6b90..e020a956 100644 --- a/ext/css/popup-outer.css +++ b/ext/css/popup-outer.css @@ -31,8 +31,7 @@ iframe.yomichan-popup[data-theme=dark] {      background-color: #1e1e1e;      border-color: #666666;  } -iframe.yomichan-popup[data-outer-theme=dark], -iframe.yomichan-popup[data-outer-theme=auto][data-site-color=dark] { +iframe.yomichan-popup[data-outer-theme=dark] {      box-shadow: 0 0 10em rgba(255, 255, 255, 0.5);  }  iframe.yomichan-popup[data-popup-display-mode=full-width] { diff --git a/ext/data/schemas/options-schema.json b/ext/data/schemas/options-schema.json index 5c01f9f8..74ae3300 100644 --- a/ext/data/schemas/options-schema.json +++ b/ext/data/schemas/options-schema.json @@ -214,12 +214,12 @@                                      },                                      "popupTheme": {                                          "type": "string", -                                        "enum": ["default", "dark"], +                                        "enum": ["light", "dark", "browser"],                                          "default": "default"                                      },                                      "popupOuterTheme": {                                          "type": "string", -                                        "enum": ["default", "dark", "auto"], +                                        "enum": ["light", "dark", "browser", "site"],                                          "default": "default"                                      },                                      "customPopupCss": { diff --git a/ext/js/app/popup-window.js b/ext/js/app/popup-window.js index f25de274..edb3a181 100644 --- a/ext/js/app/popup-window.js +++ b/ext/js/app/popup-window.js @@ -212,7 +212,7 @@ class PopupWindow extends EventDispatcher {      /**       * Updates the outer theme of the popup.       */ -    updateTheme() { +    async updateTheme() {          // NOP      } diff --git a/ext/js/app/popup.js b/ext/js/app/popup.js index 80b8d895..6c19a0b4 100644 --- a/ext/js/app/popup.js +++ b/ext/js/app/popup.js @@ -18,6 +18,7 @@  /* global   * DocumentUtil   * FrameClient + * ThemeController   * dynamicLoader   */ @@ -65,6 +66,8 @@ class Popup extends EventDispatcher {          this._container = this._frame;          this._shadow = null; +        this._themeController = new ThemeController(this._frame); +          this._fullscreenEventListeners = new EventListenerCollection();      } @@ -153,6 +156,7 @@ class Popup extends EventDispatcher {          this._visible.on('change', this._onVisibleChange.bind(this));          yomichan.on('extensionUnloaded', this._onExtensionUnloaded.bind(this));          this._onVisibleChange({value: this.isVisibleSync()}); +        this._themeController.prepare();      }      /** @@ -286,11 +290,8 @@ class Popup extends EventDispatcher {       * Updates the outer theme of the popup.       * @returns {Promise<void>}       */ -    updateTheme() { -        const {popupTheme, popupOuterTheme} = this._options.general; -        this._frame.dataset.theme = popupTheme; -        this._frame.dataset.outerTheme = popupOuterTheme; -        this._frame.dataset.siteColor = this._getSiteColor(); +    async updateTheme() { +        this._themeController.updateTheme();      }      /** @@ -588,19 +589,6 @@ class Popup extends EventDispatcher {          }      } -    _getSiteColor() { -        const color = [255, 255, 255]; -        const {documentElement, body} = document; -        if (documentElement !== null) { -            this._addColor(color, window.getComputedStyle(documentElement).backgroundColor); -        } -        if (body !== null) { -            this._addColor(color, window.getComputedStyle(body).backgroundColor); -        } -        const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); -        return dark ? 'dark' : 'light'; -    } -      async _invoke(action, params={}) {          const contentWindow = this._frame.contentWindow;          if (this._frameClient === null || !this._frameClient.isConnected() || contentWindow === null) { return; } @@ -761,34 +749,6 @@ class Popup extends EventDispatcher {          return [position, size, after];      } -    _addColor(target, cssColor) { -        if (typeof cssColor !== 'string') { return; } - -        const color = this._getColorInfo(cssColor); -        if (color === null) { return; } - -        const a = color[3]; -        if (a <= 0.0) { return; } - -        const aInv = 1.0 - a; -        for (let i = 0; i < 3; ++i) { -            target[i] = target[i] * aInv + color[i] * a; -        } -    } - -    _getColorInfo(cssColor) { -        const m = /^\s*rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)\s*$/.exec(cssColor); -        if (m === null) { return null; } - -        const m4 = m[4]; -        return [ -            Number.parseInt(m[1], 10), -            Number.parseInt(m[2], 10), -            Number.parseInt(m[3], 10), -            m4 ? Math.max(0.0, Math.min(1.0, Number.parseFloat(m4))) : 1.0 -        ]; -    } -      _getViewport(useVisualViewport) {          const visualViewport = window.visualViewport;          if (visualViewport !== null && typeof visualViewport === 'object') { @@ -825,6 +785,9 @@ class Popup extends EventDispatcher {      async _setOptionsContext(optionsContext) {          this._optionsContext = optionsContext;          this._options = await yomichan.api.optionsGet(optionsContext); +        const {general} = this._options; +        this._themeController.theme = general.popupTheme; +        this._themeController.outerTheme = general.popupOuterTheme;          this.updateTheme();      } diff --git a/ext/js/app/theme-controller.js b/ext/js/app/theme-controller.js new file mode 100644 index 00000000..4ee82c52 --- /dev/null +++ b/ext/js/app/theme-controller.js @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2022  Yomichan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program.  If not, see <https://www.gnu.org/licenses/>. + */ + +/** + * This class is used to control theme attributes on DOM elements. + */ +class ThemeController { +    /** +     * Creates a new instance of the class. +     * @param {?Element} element A DOM element which theme properties are applied to. +     */ +    constructor(element) { +        this._element = element; +        this._theme = 'default'; +        this._outerTheme = 'default'; +        this._siteTheme = null; +        this._browserTheme = 'light'; +    } + +    /** +     * Gets the DOM element which theme properties are applied to. +     * @type {?Element} +     */ +    get element() { +        return this._element; +    } + +    /** +     * Sets the DOM element which theme properties are applied to. +     * @param {?Element} value The DOM element to assign. +     */ +    set element(value) { +        this._element = value; +    } + +    /** +     * Gets the main theme for the content. +     * @type {string} +     */ +    get theme() { +        return this._theme; +    } + +    /** +     * Sets the main theme for the content. +     * @param {string} value The theme value to assign. +     */ +    set theme(value) { +        this._theme = value; +    } + +    /** +     * Gets the outer theme for the content. +     * @type {string} +     */ +    get outerTheme() { +        return this._outerTheme; +    } + +    /** +     * Sets the outer theme for the content. +     * @param {string} value The outer theme value to assign. +     */ +    set outerTheme(value) { +        this._outerTheme = value; +    } + +    /** +     * Gets the override value for the site theme. +     * If this value is `null`, the computed value will be used. +     * @type {?string} +     */ +    get siteTheme() { +        return this._siteTheme; +    } + +    /** +     * Sets the override value for the site theme. +     * If this value is `null`, the computed value will be used. +     * @param {?string} value The site theme value to assign. +     */ +    set siteTheme(value) { +        this._siteTheme = value; +    } + +    /** +     * Gets the browser's preferred color theme. +     * The value can be either 'light' or 'dark'. +     * @type {?string} +     */ +    get browserTheme() { +        return this._browserTheme; +    } + +    /** +     * Prepares the instance for use and applies the theme settings. +     */ +    prepare() { +        const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)'); +        mediaQueryList.addEventListener('change', this._onPrefersColorSchemeDarkChange.bind(this)); +        this._onPrefersColorSchemeDarkChange(mediaQueryList); +    } + +    /** +     * Updates the theme attributes on the target element. +     * If the site theme value isn't overridden, the current site theme is recomputed. +     */ +    updateTheme() { +        if (this._element === null) { return; } +        const computedSiteTheme = this._siteTheme !== null ? this._siteTheme : this.computeSiteTheme(); +        const data = this._element.dataset; +        data.theme = this._resolveThemeValue(this._theme, computedSiteTheme); +        data.outerTheme = this._resolveThemeValue(this._outerTheme, computedSiteTheme); +        data.siteTheme = computedSiteTheme; +        data.browserTheme = this._browserTheme; +        data.themeRaw = this._theme; +        data.outerThemeRaw = this._outerTheme; +    } + +    /** +     * Computes the current site theme based on the background color. +     * @returns {'light'|'dark'} The theme of the site. +     */ +    computeSiteTheme() { +        const color = [255, 255, 255]; +        const {documentElement, body} = document; +        if (documentElement !== null) { +            this._addColor(color, window.getComputedStyle(documentElement).backgroundColor); +        } +        if (body !== null) { +            this._addColor(color, window.getComputedStyle(body).backgroundColor); +        } +        const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); +        return dark ? 'dark' : 'light'; +    } + +    /** +     * Event handler for when the preferred browser theme changes. +     * @param {MediaQueryList|MediaQueryListEvent} detail The object containing event details. +     * @param {boolean} detail.matches The object containing event details. +     */ +    _onPrefersColorSchemeDarkChange({matches}) { +        this._browserTheme = (matches ? 'dark' : 'light'); +        this.updateTheme(); +    } + +    /** +     * Resolves a settings theme value to the actual value which should be used. +     * @param {string} theme The theme value to resolve. +     * @param {string} computedSiteTheme The computed site theme value to use for when the theme value is `'auto'`. +     * @returns {string} The resolved theme value. +     */ +    _resolveThemeValue(theme, computedSiteTheme) { +        switch (theme) { +            case 'auto': return computedSiteTheme; +            case 'browser': return this._browserTheme; +            default: return theme; +        } +    } + +    /** +     * Adds the value of a CSS color to an accumulation target. +     * @param {[number, number, number]} target The target color buffer to accumulate into. +     * @param {string|*} cssColor The CSS color value to add to the target. If this value is not a string, +     *   the target will not be modified. +     */ +    _addColor(target, cssColor) { +        if (typeof cssColor !== 'string') { return; } + +        const color = this._getColorInfo(cssColor); +        if (color === null) { return; } + +        const a = color[3]; +        if (a <= 0.0) { return; } + +        const aInv = 1.0 - a; +        for (let i = 0; i < 3; ++i) { +            target[i] = target[i] * aInv + color[i] * a; +        } +    } + +    /** +     * Decomposes a CSS color string into its RGBA values. +     * @param {string} cssColor The color value to decompose. This value is expected to be in the form RGB(r, g, b) or RGBA(r, g, b, a). +     * @returns {?[number, number, number, number]} The color and alpha values. The color component values range from [0, 255], and the alpha ranges from [0, 1]. +     */ +    _getColorInfo(cssColor) { +        const m = /^\s*rgba?\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+)\s*)?\)\s*$/.exec(cssColor); +        if (m === null) { return null; } + +        const m4 = m[4]; +        return [ +            Number.parseInt(m[1], 10), +            Number.parseInt(m[2], 10), +            Number.parseInt(m[3], 10), +            m4 ? Math.max(0.0, Math.min(1.0, Number.parseFloat(m4))) : 1.0 +        ]; +    } +} diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 1a7f661f..3422b773 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -466,7 +466,8 @@ class OptionsUtil {              {async: false, update: this._updateVersion14.bind(this)},              {async: false, update: this._updateVersion15.bind(this)},              {async: false, update: this._updateVersion16.bind(this)}, -            {async: false, update: this._updateVersion17.bind(this)} +            {async: false, update: this._updateVersion17.bind(this)}, +            {async: false, update: this._updateVersion18.bind(this)}          ];          if (typeof targetVersion === 'number' && targetVersion < result.length) {              result.splice(targetVersion); @@ -924,4 +925,22 @@ class OptionsUtil {          }          return options;      } + +    _updateVersion18(options) { +        // Version 18 changes: +        //  general.popupTheme's 'default' value changed to 'light' +        //  general.popupOuterTheme's 'default' value changed to 'light' +        //  general.popupOuterTheme's 'auto' value changed to 'site' +        for (const profile of options.profiles) { +            const {general} = profile.options; +            if (general.popupTheme === 'default') { +                general.popupTheme = 'light'; +            } +            switch (general.popupOuterTheme) { +                case 'default': general.popupOuterTheme = 'light'; break; +                case 'auto': general.popupOuterTheme = 'site'; break; +            } +        } +        return options; +    }  } diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 02d8513f..c16f7cae 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -31,6 +31,7 @@   * QueryParser   * ScrollElement   * TextScanner + * ThemeController   * dynamicLoader   */ @@ -114,6 +115,7 @@ class Display extends EventDispatcher {          this._onTagClickBind = this._onTagClick.bind(this);          this._onMenuButtonClickBind = this._onMenuButtonClick.bind(this);          this._onMenuButtonMenuCloseBind = this._onMenuButtonMenuClose.bind(this); +        this._themeController = new ThemeController(document.documentElement);          this._hotkeyHandler.registerActions([              ['close',             () => { this._onHotkeyClose(); }], @@ -210,6 +212,10 @@ class Display extends EventDispatcher {      }      async prepare() { +        // Theme +        this._themeController.siteTheme = 'light'; +        this._themeController.prepare(); +          // State setup          const {documentElement} = document;          const {browser} = await yomichan.api.getEnvironmentInfo(); @@ -302,8 +308,7 @@ class Display extends EventDispatcher {          this._updateHotkeys(options);          this._updateDocumentOptions(options); -        this._updateTheme(options.general.popupTheme); -        this.setCustomCss(options.general.customPopupCss); +        this._setTheme(options);          this._hotkeyHelpController.setOptions(options);          this._displayGenerator.updateHotkeys();          this._hotkeyHelpController.setupNode(document.documentElement); @@ -837,8 +842,13 @@ class Display extends EventDispatcher {          data.popupActionBarLocation = `${options.general.popupActionBarLocation}`;      } -    _updateTheme(themeName) { -        document.documentElement.dataset.theme = themeName; +    _setTheme(options) { +        const {general} = options; +        const {popupTheme} = general; +        this._themeController.theme = popupTheme; +        this._themeController.outerTheme = general.popupOuterTheme; +        this._themeController.updateTheme(); +        this.setCustomCss(general.customPopupCss);      }      async _findDictionaryEntries(isKanji, source, wildcardsEnabled, optionsContext) { diff --git a/ext/manifest.json b/ext/manifest.json index 99d4fab5..07377694 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -48,6 +48,7 @@                  "js/app/popup-factory.js",                  "js/app/popup-proxy.js",                  "js/app/popup-window.js", +                "js/app/theme-controller.js",                  "js/comm/api.js",                  "js/comm/cross-frame-api.js",                  "js/comm/frame-ancestry-handler.js", diff --git a/ext/popup-preview.html b/ext/popup-preview.html index 29497709..cffd71f0 100644 --- a/ext/popup-preview.html +++ b/ext/popup-preview.html @@ -44,6 +44,7 @@  <script src="/js/app/frontend.js"></script>  <script src="/js/app/popup.js"></script>  <script src="/js/app/popup-factory.js"></script> +<script src="/js/app/theme-controller.js"></script>  <script src="/js/comm/api.js"></script>  <script src="/js/comm/cross-frame-api.js"></script>  <script src="/js/comm/frame-ancestry-handler.js"></script> diff --git a/ext/popup.html b/ext/popup.html index c8719da6..cb6ca7da 100644 --- a/ext/popup.html +++ b/ext/popup.html @@ -97,6 +97,7 @@  <script src="/js/yomichan.js"></script> +<script src="/js/app/theme-controller.js"></script>  <script src="/js/comm/api.js"></script>  <script src="/js/comm/cross-frame-api.js"></script>  <script src="/js/comm/frame-endpoint.js"></script> diff --git a/ext/search.html b/ext/search.html index 9451471f..e9f624b3 100644 --- a/ext/search.html +++ b/ext/search.html @@ -84,6 +84,7 @@  <script src="/js/yomichan.js"></script> +<script src="/js/app/theme-controller.js"></script>  <script src="/js/comm/api.js"></script>  <script src="/js/comm/clipboard-monitor.js"></script>  <script src="/js/comm/cross-frame-api.js"></script> diff --git a/ext/settings.html b/ext/settings.html index 6c218bb5..d6e8f3d4 100644 --- a/ext/settings.html +++ b/ext/settings.html @@ -664,16 +664,18 @@                      <div class="settings-item-group-item">                          <div class="settings-item-group-item-label">Body</div>                          <select data-setting="general.popupTheme" class="short-width short-height"> -                            <option value="default">Light</option> +                            <option value="light">Light</option>                              <option value="dark">Dark</option> +                            <option value="browser">Browser</option>                          </select>                      </div>                      <div class="settings-item-group-item">                          <div class="settings-item-group-item-label">Shadow</div>                          <select data-setting="general.popupOuterTheme" class="short-width short-height"> -                            <option value="auto">Auto</option> -                            <option value="default">Light</option> +                            <option value="site">Auto</option> +                            <option value="light">Light</option>                              <option value="dark">Dark</option> +                            <option value="browser">Browser</option>                          </select>                      </div>                  </div> diff --git a/test/test-options-util.js b/test/test-options-util.js index a46e4248..aa8a5bdb 100644 --- a/test/test-options-util.js +++ b/test/test-options-util.js @@ -286,8 +286,8 @@ function createProfileOptionsUpdatedTestData1() {              compactTags: false,              glossaryLayoutMode: 'default',              mainDictionary: '', -            popupTheme: 'default', -            popupOuterTheme: 'default', +            popupTheme: 'light', +            popupOuterTheme: 'light',              customPopupCss: '',              customPopupOuterCss: '',              enableWanakana: true, @@ -600,7 +600,7 @@ function createOptionsUpdatedTestData1() {              }          ],          profileCurrent: 0, -        version: 17, +        version: 18,          global: {              database: {                  prefixWildcardsSupported: false |