diff --git a/src/db/action.ts b/src/db/action.ts index 704a0f7e..791759a7 100644 --- a/src/db/action.ts +++ b/src/db/action.ts @@ -15,7 +15,7 @@ export const setBackground = (key: string): void => { DB.put(db, "background", { id, key, - display: { blur: 0, luminosity: -0.2 }, + display: { blur: 0, luminosity: -0.2, nightDim: false }, }); DB.del(db, `data/${current.id}`); DB.del(cache, current.id); diff --git a/src/db/state.ts b/src/db/state.ts index f19d5ca0..0207240c 100644 --- a/src/db/state.ts +++ b/src/db/state.ts @@ -28,6 +28,7 @@ export interface BackgroundState { export interface BackgroundDisplay { blur?: number; luminosity?: number; + nightDim?: boolean; } export interface WidgetState { @@ -64,6 +65,7 @@ const initData: State = { display: { luminosity: -0.2, blur: 0, + nightDim: false, }, }, "widget/default-time": { diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 0be26165..04728991 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -8,3 +8,4 @@ export * from "./useObjectUrl"; export * from "./useSavedReducer"; export * from "./useTime"; export * from "./useToggle"; +export * from "./useIsNight"; diff --git a/src/hooks/useIsNight.ts b/src/hooks/useIsNight.ts new file mode 100644 index 00000000..a845940a --- /dev/null +++ b/src/hooks/useIsNight.ts @@ -0,0 +1,9 @@ +import { useTime } from "./useTime"; + +export function useIsNight() { + const time = useTime(); + const currentHour = time.getHours(); + + // Return true if the current hour is between 9 PM (21:00) and 5 AM (05:00) + return currentHour >= 21 || currentHour < 5; +} diff --git a/src/views/dashboard/Dashboard.tsx b/src/views/dashboard/Dashboard.tsx index e93a33da..cf872813 100644 --- a/src/views/dashboard/Dashboard.tsx +++ b/src/views/dashboard/Dashboard.tsx @@ -1,5 +1,6 @@ import React from "react"; import { db } from "../../db/state"; +import { useIsNight } from "../../hooks"; import { useValue } from "../../lib/db/react"; import Background from "./Background"; import "./Dashboard.sass"; @@ -8,7 +9,12 @@ import Widgets from "./Widgets"; const Dashboard: React.FC = () => { const background = useValue(db, "background"); - const theme = (background.display.luminosity ?? 0) > 0 ? "light" : "dark"; + const isNight = useIsNight(); + const theme = + (background.display.luminosity ?? 0) > 0 && + !(background.display.nightDim && isNight) + ? "light" + : "dark"; // Set init theme for pre settings load (see `target//index.html`) React.useEffect(() => { diff --git a/src/views/settings/Background.tsx b/src/views/settings/Background.tsx index 922d910e..6132edfd 100644 --- a/src/views/settings/Background.tsx +++ b/src/views/settings/Background.tsx @@ -95,6 +95,19 @@ const Background: React.FC = () => {