From 0cc9c124228ad83597f983b366b2180ba10aee6f Mon Sep 17 00:00:00 2001 From: Tim Nguyen Date: Sun, 3 Jun 2018 00:04:02 +0200 Subject: [PATCH] Fix #54 - Allow specifying custom night mode times --- background/addon-state.js | 47 ++++++++++++++++++++++++----------- background/background.js | 17 +++++++++++-- options/components/input.js | 26 +++++++++++++++++++ options/components/options.js | 29 +++++++++++++++++++-- options/options.css | 15 +++++++++-- options/options.html | 3 ++- options/options.js | 10 ++++++++ shared/settings.js | 12 +++++++++ 8 files changed, 137 insertions(+), 22 deletions(-) create mode 100644 options/components/input.js diff --git a/background/addon-state.js b/background/addon-state.js index 1da7f51..d431021 100644 --- a/background/addon-state.js +++ b/background/addon-state.js @@ -64,7 +64,6 @@ class AddonState { // await browser.tabs.sendMessage(tabId, {hi: "hi"}); await browser.tabs.executeScript(tabId, {code: "'hi';"}); } catch (e) { - console.log(e); let color = false; let baseColor = await findBaseColor(tab); if (baseColor) { @@ -83,6 +82,10 @@ class AddonState { this.refreshAddon = async () => { onInit(); + browser.alarms.create( + "nightToggle", { when: (await firstAlarm()) } + ); + await new Promise(r => setTimeout(r, 400)); let tabs = await browser.tabs.query({ active: true }); @@ -101,31 +104,45 @@ class AddonState { Settings.onChanged(this.refreshAddon); browser.windows.onFocusChanged.addListener(onWindowFocusChange); - browser.alarms.create( - "nightToggle", - { - when: firstAlarm(), - periodInMinutes: (NIGHTMODE_EVENING - NIGHTMODE_MORNING) * 60, - } - ); - browser.alarms.onAlarm.addListener(({name}) => { + (async () => { + browser.alarms.create( + "nightToggle", { when: (await firstAlarm()) } + ); + })(); + browser.alarms.onAlarm.addListener(async ({name}) => { if (name === "nightToggle") { onNightMode(); + browser.alarms.create( + "nightToggle", { when: (await firstAlarm()) } + ); } }); onInit(); } } -function firstAlarm() { +async function firstAlarm() { let now = new Date(Date.now()); let then = new Date(Date.now()); - if (now.getHours() >= NIGHTMODE_EVENING) { - then.setHours(24 + NIGHTMODE_MORNING); - } else if (now.getHours() < NIGHTMODE_MORNING) { - then.setHours(NIGHTMODE_MORNING); + let nightModeStart = await Settings.getNightModeStart(); + let nightModeEnd = await Settings.getNightModeEnd(); + + if (nightModeStart > nightModeEnd) { + if (now.getHours() >= nightModeStart) { + then.setHours(24 + nightModeEnd); + } else if (now.getHours() < nightModeEnd) { + then.setHours(nightModeEnd); + } else { + then.setHours(nightModeStart); + } } else { - then.setHours(NIGHTMODE_EVENING); + if (now.getHours() >= nightModeEnd) { + then.setHours(24 + nightModeStart); + } else if (now.getHours() < nightModeStart) { + then.setHours(nightModeStart); + } else { + then.setHours(nightModeEnd); + } } then.setMinutes(0); then.setSeconds(0); diff --git a/background/background.js b/background/background.js index a6122e2..c08c965 100644 --- a/background/background.js +++ b/background/background.js @@ -15,13 +15,26 @@ async function setColor({id, windowId}, tabColorMap) { } } +async function isDayMode() { + let date = new Date(Date.now()); + let nightModeStart = await Settings.getNightModeStart(); + let nightModeEnd = await Settings.getNightModeEnd(); + + if (nightModeStart > nightModeEnd) { + return date.getHours() >= nightModeEnd && + date.getHours() < nightModeStart; + } else { + return date.getHours() < nightModeStart || + date.getHours() >= nightModeEnd; + } +} + new AddonState({ async onInit() { let themes = await Settings.getThemes(); - let date = new Date(Date.now()); let selectedTheme; - if (date.getHours() >= NIGHTMODE_MORNING && date.getHours() < NIGHTMODE_EVENING) { + if (await isDayMode()) { selectedTheme = await Settings.getDefaultTheme(); } else { selectedTheme = await Settings.getNightTheme(); diff --git a/options/components/input.js b/options/components/input.js new file mode 100644 index 0000000..adf7171 --- /dev/null +++ b/options/components/input.js @@ -0,0 +1,26 @@ +"use strict"; + +/* exported Checkbox */ + +const Input = createFactory(class extends Component { + componentDidUpdate() { + if (this.refs.input.value !== this.props.defaultValue) { + this.refs.input.value = this.props.defaultValue; + } + } + render() { + let {defaultValue, onChange, type, min, max} = this.props; + return createElement("input", { + type, + defaultValue, + onChange: (e) => { + if (e.target.reportValidity()) { + onChange(e); + } + }, + min, + max, + ref: "input" + }); + } +}); diff --git a/options/components/options.js b/options/components/options.js index 6958ee5..f813104 100644 --- a/options/components/options.js +++ b/options/components/options.js @@ -7,6 +7,8 @@ function Options({ settings: { colorSource, defaultTheme, nightTheme, + nightModeStart, + nightModeEnd, whiteBackgroundFavicons, pageColorsOnInactive, usePageDefinedColors, @@ -18,7 +20,7 @@ function Options({ settings: { component: ThemeEditor(themes[theme], Object.keys(themes).length > 1), }; }); - let nightThemeDesc = "The night theme is enabled from 8pm to 8am. " + + let nightThemeDesc = "To disable this feature, choose the same theme as the default theme."; let pageColorDesc = "Select where the page color is extracted from:"; return createElement("div", {}, @@ -32,9 +34,32 @@ function Options({ settings: { } }), createElement("h2", {}, "Night theme"), + createElement("p", { className: "disabled" }, + createElement("span", {}, "Enable from "), + Input({ + defaultValue: nightModeStart, + type: "number", + min: 0, + max: 23, + onChange: ({ target }) => { + app.actions.setNightModeStart(target.value); + } + }), + createElement("span", {}, "h to "), + Input({ + defaultValue: nightModeEnd, + type: "number", + min: 0, + max: 23, + onChange: ({ target }) => { + app.actions.setNightModeEnd(target.value); + } + }), + createElement("span", {}, "h. " + nightThemeDesc), + ), createElement("p", { className: "disabled" - }, nightThemeDesc), + }, ), ThemeSelect({ themes, defaultValue: nightTheme, diff --git a/options/options.css b/options/options.css index faec739..d2b82fe 100644 --- a/options/options.css +++ b/options/options.css @@ -26,6 +26,14 @@ p { text-align: start; } +p > input { + font: inherit; + border: 0; + width: 2ch; + -moz-appearance: textfield; + border-bottom: 1px solid currentColor; +} + .header { position: sticky; top: 0; @@ -56,6 +64,9 @@ p { .disabled { opacity: 0.5; +} + +.disabled:not(p) { pointer-events: none; } @@ -98,7 +109,7 @@ section { width: 100%; position: absolute; bottom: 0; - left: 0; + left: 0; } .tabs li.selected a { @@ -108,7 +119,7 @@ section { transform: none; } -/* +/* .browser { border: 1px solid rgba(12, 12, 13, 0.3); font: message-box; diff --git a/options/options.html b/options/options.html index 8ebc053..52dec66 100644 --- a/options/options.html +++ b/options/options.html @@ -14,7 +14,7 @@

VivaldiFox Options

-
+

About

This add-on was created by Tim Nguyen.

You can support the add-on by:

@@ -30,6 +30,7 @@

About

+ diff --git a/options/options.js b/options/options.js index 8617362..af8a591 100644 --- a/options/options.js +++ b/options/options.js @@ -17,6 +17,8 @@ async function init() { themes: processThemes(await Settings.getThemes()), defaultTheme: await Settings.getDefaultTheme(), nightTheme: await Settings.getNightTheme(), + nightModeStart: await Settings.getNightModeStart(), + nightModeEnd: await Settings.getNightModeEnd(), pageColorsOnInactive: await Settings.getPageColorsOnInactive(), whiteBackgroundFavicons: await Settings.getWhiteBackgroundFavicons(), colorSource: await Settings.getColorSource(), @@ -65,6 +67,14 @@ async function init() { this.state.settings.nightTheme = name; Settings.setNightTheme(name); }, + setNightModeStart(time) { + this.state.settings.nightModeStart = time; + Settings.setNightModeStart(time); + }, + setNightModeEnd(time) { + this.state.settings.nightModeEnd = time; + Settings.setNightModeEnd(time); + }, setThemeProperty(theme, type, property, value) { let {themes} = this.state.settings; themes[theme].properties[type][property] = value; diff --git a/shared/settings.js b/shared/settings.js index 1b6bdb1..58baacc 100644 --- a/shared/settings.js +++ b/shared/settings.js @@ -48,6 +48,12 @@ const Settings = { getNightTheme() { return getSetting("nightTheme", "light"); }, + getNightModeStart() { + return getSetting("nightModeStart", 20); + }, + getNightModeEnd() { + return getSetting("nightModeEnd", 8); + }, getPageColorsOnInactive() { return getSetting("pageColorsOnInactive", true); }, @@ -72,6 +78,12 @@ const Settings = { setNightTheme(theme) { setSetting("nightTheme", theme); }, + setNightModeStart(time) { + setSetting("nightModeStart", time); + }, + setNightModeEnd(time) { + setSetting("nightModeEnd", time); + }, setPageColorsOnInactive(value) { setSetting("pageColorsOnInactive", value); },