Skip to content

Commit

Permalink
Add auto option to body theme
Browse files Browse the repository at this point in the history
* Add auto option to body theme

* Fix firefox bug where themes do not set correctly in settings due to getOptions failing

<rikaitan.link>NDUxYmNhYjAxZmQ1OTFlYzU0YjYzYWYwZmM3MDg0ZGM5ZjM3OTI4Ygo=</rikaitan.link>
  • Loading branch information
Kuuuube committed Jun 19, 2024
1 parent 1f660d6 commit 12823e2
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 23 deletions.
6 changes: 4 additions & 2 deletions ext/css/popup-preview.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@
--animation-duration: 0s;

--example-text-color: #333333;
--background-color: #f8f9fa;
}
:root[data-loaded=true] {
--animation-duration: 0.25s;
}

:root[data-theme=dark] {
--example-text-color: #d4d4d4;
--background-color: #1e1e1e;
}


html {
transition: background-color var(--animation-duration) linear 0s, color var(--animation-duration) linear 0s;
background-color: rgba(255, 255, 255, 0);
background-color: var(--background-color);
}
html.dark {
background-color: #1e1e1e;
background-color: var(--background-color);
}
html,
body {
Expand Down
4 changes: 2 additions & 2 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@
},
"popupTheme": {
"type": "string",
"enum": ["light", "dark", "browser"],
"default": "browser"
"enum": ["light", "dark", "browser", "site"],
"default": "site"
},
"popupOuterTheme": {
"type": "string",
Expand Down
8 changes: 5 additions & 3 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,14 @@ export class Frontend {
/**
* @param {import('text-scanner').EventArgument<'searchSuccess'>} details
*/
_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo: {eventType, detail: inputInfoDetail}, textSource, optionsContext, detail}) {
_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo: {eventType, detail: inputInfoDetail}, textSource, optionsContext, detail, pageTheme}) {
this._stopClearSelectionDelayed();
let focus = (eventType === 'mouseMove');
if (typeof inputInfoDetail === 'object' && inputInfoDetail !== null) {
const focus2 = inputInfoDetail.focus;
if (typeof focus2 === 'boolean') { focus = focus2; }
}
this._showContent(textSource, focus, dictionaryEntries, type, sentence, detail !== null ? detail.documentTitle : null, optionsContext);
this._showContent(textSource, focus, dictionaryEntries, type, sentence, detail !== null ? detail.documentTitle : null, optionsContext, pageTheme);
}

/** */
Expand Down Expand Up @@ -716,15 +716,17 @@ export class Frontend {
* @param {?import('display').HistoryStateSentence} sentence
* @param {?string} documentTitle
* @param {import('settings').OptionsContext} optionsContext
* @param {'dark' | 'light'} pageTheme
*/
_showContent(textSource, focus, dictionaryEntries, type, sentence, documentTitle, optionsContext) {
_showContent(textSource, focus, dictionaryEntries, type, sentence, documentTitle, optionsContext, pageTheme) {
const query = textSource.text();
const {url} = optionsContext;
/** @type {import('display').HistoryState} */
const detailsState = {
focusEntry: 0,
optionsContext,
url,
pageTheme,
};
if (sentence !== null) { detailsState.sentence = sentence; }
if (documentTitle !== null) { detailsState.documentTitle = documentTitle; }
Expand Down
14 changes: 8 additions & 6 deletions ext/js/app/theme-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export class ThemeController {
constructor(element) {
/** @type {?HTMLElement} */
this._element = element;
/** @type {'light'|'dark'|'browser'} */
this._theme = 'light';
/** @type {'light'|'dark'|'browser'|'site'} */
this._outerTheme = 'light';
this._theme = 'site';
/** @type {'light'|'dark'|'browser'|'site'} */
this._outerTheme = 'site';
/** @type {?('dark'|'light')} */
this._siteTheme = null;
/** @type {'dark'|'light'} */
this._browserTheme = 'light';
/** @type {boolean} */
this.siteOverride = false;
}

/**
Expand All @@ -55,15 +57,15 @@ export class ThemeController {

/**
* Gets the main theme for the content.
* @type {'light'|'dark'|'browser'}
* @type {'light'|'dark'|'browser'|'site'}
*/
get theme() {
return this._theme;
}

/**
* Sets the main theme for the content.
* @param {'light'|'dark'|'browser'} value The theme value to assign.
* @param {'light'|'dark'|'browser'|'site'} value The theme value to assign.
*/
set theme(value) {
this._theme = value;
Expand Down Expand Up @@ -171,7 +173,7 @@ export class ThemeController {
*/
_resolveThemeValue(theme, computedSiteTheme) {
switch (theme) {
case 'site': return computedSiteTheme;
case 'site': return this.siteOverride ? this._browserTheme : computedSiteTheme;
case 'browser': return this._browserTheme;
default: return theme;
}
Expand Down
14 changes: 13 additions & 1 deletion ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ export class Display extends EventDispatcher {
/** */
async prepare() {
// Theme
this._themeController.siteTheme = 'light';
this._themeController.prepare();

// State setup
Expand Down Expand Up @@ -488,6 +487,10 @@ export class Display extends EventDispatcher {
this._history.pushState(state, content, url);
break;
}

if (this._options) {
this._setTheme(this._options);
}
}

/**
Expand Down Expand Up @@ -1149,8 +1152,16 @@ export class Display extends EventDispatcher {
_setTheme(options) {
const {general} = options;
const {popupTheme} = general;
try {
// eslint-disable-next-line no-underscore-dangle
const pageTheme = this._history._current.state?.pageTheme;
this._themeController.siteTheme = pageTheme ?? null;
} catch (e) {
log.error(e);
}
this._themeController.theme = popupTheme;
this._themeController.outerTheme = general.popupOuterTheme;
this._themeController.siteOverride = this._pageType === 'search';
this._themeController.updateTheme();
this.setCustomCss(general.customPopupCss);
}
Expand Down Expand Up @@ -1933,6 +1944,7 @@ export class Display extends EventDispatcher {
url,
sentence: sentence !== null ? sentence : void 0,
documentTitle,
pageTheme: 'light',
},
content: {
dictionaryEntries: dictionaryEntries !== null ? dictionaryEntries : void 0,
Expand Down
3 changes: 2 additions & 1 deletion ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class QueryParser extends EventDispatcher {
/**
* @param {import('text-scanner').EventArgument<'searchSuccess'>} details
*/
_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo, textSource, optionsContext}) {
_onSearchSuccess({type, dictionaryEntries, sentence, inputInfo, textSource, optionsContext, pageTheme}) {
this.trigger('searched', {
textScanner: this._textScanner,
type,
Expand All @@ -165,6 +165,7 @@ export class QueryParser extends EventDispatcher {
textSource,
optionsContext,
sentenceOffset: this._getSentenceOffset(textSource),
pageTheme: pageTheme,
});
}

Expand Down
7 changes: 7 additions & 0 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {ThemeController} from '../app/theme-controller.js';
import {EventDispatcher} from '../core/event-dispatcher.js';
import {EventListenerCollection} from '../core/event-listener-collection.js';
import {log} from '../core/log.js';
Expand Down Expand Up @@ -478,6 +479,11 @@ export class TextScanner extends EventDispatcher {
this.setCurrentTextSource(textSource);
this._selectionRestoreInfo = selectionRestoreInfo;

/** @type {ThemeController} */
this._themeController = new ThemeController(document.documentElement);
this._themeController.prepare();
const pageTheme = this._themeController.computeSiteTheme();

this.trigger('searchSuccess', {
type,
dictionaryEntries,
Expand All @@ -486,6 +492,7 @@ export class TextScanner extends EventDispatcher {
textSource,
optionsContext,
detail,
pageTheme,
});
} else {
this._triggerSearchEmpty(inputInfo);
Expand Down
2 changes: 1 addition & 1 deletion ext/js/pages/action-popup-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class DisplayController {

/** */
async prepare() {
this._themeController.siteTheme = 'light';
this._themeController.prepare();

const manifest = chrome.runtime.getManifest();
Expand Down Expand Up @@ -213,6 +212,7 @@ class DisplayController {
void this._updatePermissionsWarnings(options);

this._themeController.theme = options.general.popupTheme;
this._themeController.siteOverride = true;
this._themeController.updateTheme();
}

Expand Down
1 change: 1 addition & 0 deletions ext/js/pages/info-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ await Application.main(true, async (application) => {
const primaryProfile = (profileCurrent >= 0 && profileCurrent < profiles.length) ? profiles[profileCurrent] : null;
if (primaryProfile !== null) {
themeController.theme = primaryProfile.options.general.popupTheme;
themeController.siteOverride = true;
themeController.updateTheme();
}

Expand Down
1 change: 1 addition & 0 deletions ext/js/pages/quick-start-guide-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ await Application.main(true, async (application) => {
const primaryProfile = (profileCurrent >= 0 && profileCurrent < profiles.length) ? profiles[profileCurrent] : null;
if (primaryProfile !== null) {
themeController.theme = primaryProfile.options.general.popupTheme;
themeController.siteOverride = true;
themeController.updateTheme();
}
});
2 changes: 1 addition & 1 deletion ext/js/pages/settings/popup-preview-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export class PopupPreviewFrame {
async prepare() {
window.addEventListener('message', this._onMessage.bind(this), false);

this._themeController.siteTheme = 'light';
this._themeController.prepare();

// Setup events
Expand Down Expand Up @@ -141,6 +140,7 @@ export class PopupPreviewFrame {
options.general.popupVerticalTextPosition = 'before';
options.scanning.selectText = false;
this._themeController.theme = options.general.popupTheme;
this._themeController.siteOverride = true;
this._themeController.updateTheme();
return options;
}
Expand Down
15 changes: 9 additions & 6 deletions ext/js/pages/settings/settings-display-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export class SettingsDisplayController {
this._onMenuButtonClickBind = this._onMenuButtonClick.bind(this);
/** @type {ThemeController} */
this._themeController = new ThemeController(document.documentElement);
/** @type {HTMLSelectElement | null}*/
this._themeDropdown = document.querySelector('[data-setting="general.popupTheme"]');
}

/** */
prepare() {
this._themeController.siteTheme = 'light';
this._themeController.prepare();
void this._updateTheme();

Expand Down Expand Up @@ -92,16 +93,18 @@ export class SettingsDisplayController {
window.addEventListener('popstate', this._onPopState.bind(this), false);
this._updateScrollTarget();

const themeDropdown = document.querySelector('[data-setting="general.popupTheme"]');
if (themeDropdown) {
themeDropdown.addEventListener('change', this._updateTheme.bind(this), false);
if (this._themeDropdown) {
this._themeDropdown.addEventListener('change', this._updateTheme.bind(this), false);
}
}

/** */
async _updateTheme() {
const options = await this._settingsController.getOptions();
this._themeController.theme = options.general.popupTheme;
const theme = this._themeDropdown?.value;
if (theme === 'site' || theme === 'light' || theme === 'dark' || theme === 'browser') {
this._themeController.theme = theme;
}
this._themeController.siteOverride = true;
this._themeController.updateTheme();
}

Expand Down
1 change: 1 addition & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ <h1>Rikaitan Settings</h1>
<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="site">Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
<option value="browser">Browser</option>
Expand Down
1 change: 1 addition & 0 deletions ext/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ <h2>Basic customization</h2>
</div>
<div class="settings-item-right">
<select data-setting="general.popupTheme" class="short-width">
<option value="site">Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
<option value="browser">Browser</option>
Expand Down
2 changes: 2 additions & 0 deletions types/ext/display.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export type HistoryState = {
url?: string;
/** The originating document title of the content. */
documentTitle?: string;
/** Computed theme of the page */
pageTheme?: 'dark' | 'light';
};

/**
Expand Down
1 change: 1 addition & 0 deletions types/ext/query-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type Events = {
textSource: TextSource;
optionsContext: OptionsContext;
sentenceOffset: number | null;
pageTheme: 'dark' | 'light';
};
};

Expand Down
1 change: 1 addition & 0 deletions types/ext/text-scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export type Events = {
textSource: TextSource.TextSource;
optionsContext: Settings.OptionsContext;
detail: SearchResultDetail;
pageTheme: 'dark' | 'light';
};
searchEmpty: {
inputInfo: InputInfo;
Expand Down

0 comments on commit 12823e2

Please sign in to comment.