Skip to content

Commit

Permalink
feat: support theme mode switching from native app (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frewacom committed Nov 27, 2020
1 parent ff0cf97 commit 3c71f35
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Pywalfox",
"version": "2.0.5",
"version": "2.0.6",
"description": "Dyanmic theming of the browser using your pywal colors",
"browser_specific_settings": {
"gecko": {
Expand Down
5 changes: 5 additions & 0 deletions src/background/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default class Extension {
cssToggleFailed: this.cssToggleFailed.bind(this),
cssFontSizeSetSuccess: this.cssFontSizeSetSuccess.bind(this),
cssFontSizeSetFailed: this.cssFontSizeSetFailed.bind(this),
themeModeSet: this.themeModeSet.bind(this),
});

browser.commands.onCommand.addListener(this.onCommand.bind(this));
Expand Down Expand Up @@ -607,6 +608,10 @@ export default class Extension {
Messenger.UI.sendNotification('Font size', error, false);
}

private themeModeSet(mode: ThemeModes) {
this.setThemeMode(mode);
}

public async start() {
this.settingsPage = new ExtensionPage(EXTENSION_PAGES.SETTINGS);
this.updatePage = new ExtensionPage(EXTENSION_PAGES.UPDATE);
Expand Down
23 changes: 23 additions & 0 deletions src/communication/native-messenger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ThemeModes,
INativeAppMessage,
INativeAppRequest,
INativeAppMessageCallbacks,
Expand Down Expand Up @@ -59,6 +60,9 @@ export default class NativeApp {
case NATIVE_MESSAGES.CSS_FONT_SIZE:
this.onCssFontSizeResponse(message);
break;
case NATIVE_MESSAGES.THEME_MODE:
this.onThemeModeResponse(message);
break;
case NATIVE_MESSAGES.INVALID_ACTION:
this.logError(`Native app recieved unhandled message action: ${message.action}`);
break;
Expand Down Expand Up @@ -134,6 +138,25 @@ export default class NativeApp {
}
}

private onThemeModeResponse(message: INativeAppMessage) {
const mode: string = this.getData(message);

if (!mode) {
this.logError('Received theme mode command, but the new mode was not specified');
return;
}

if (mode === ThemeModes.Dark) {
this.callbacks.themeModeSet(ThemeModes.Dark);
} else if (mode === ThemeModes.Light) {
this.callbacks.themeModeSet(ThemeModes.Light);
} else if (mode === ThemeModes.Auto) {
this.callbacks.themeModeSet(ThemeModes.Auto);
} else {
this.logError(`Received theme mode command, but the mode "${mode}" was invalid`);
}
}

private onDebuggingOutput(message: INativeAppMessage) {
const output: string = this.getData(message);
output && this.callbacks.output(output);
Expand Down
1 change: 1 addition & 0 deletions src/config/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const NATIVE_MESSAGES = {
CSS_ENABLE: 'css:enable',
CSS_DISABLE: 'css:disable',
CSS_FONT_SIZE: 'css:font:size',
THEME_MODE: 'theme:mode',
};

export const DARKREADER_MESSAGES = {
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export interface INativeAppMessageCallbacks {
cssToggleFailed: (target: string, error: string) => void,
cssFontSizeSetSuccess: (size: number) => void,
cssFontSizeSetFailed: (error: string) => void,
themeModeSet: (mode: ThemeModes) => void,
}

export interface INodeLookup {
Expand Down

0 comments on commit 3c71f35

Please sign in to comment.