Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to change the luminosity depending on the time of day #625

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/db/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/db/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface BackgroundState {
export interface BackgroundDisplay {
blur?: number;
luminosity?: number;
nightDim?: boolean;
}

export interface WidgetState {
Expand Down Expand Up @@ -64,6 +65,7 @@ const initData: State = {
display: {
luminosity: -0.2,
blur: 0,
nightDim: false,
},
},
"widget/default-time": {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./useObjectUrl";
export * from "./useSavedReducer";
export * from "./useTime";
export * from "./useToggle";
export * from "./useIsNight";
9 changes: 9 additions & 0 deletions src/hooks/useIsNight.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 7 additions & 1 deletion src/views/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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/<target>/index.html`)
React.useEffect(() => {
Expand Down
13 changes: 13 additions & 0 deletions src/views/settings/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ const Background: React.FC = () => {
<option value="1" label="Lighten" />
</datalist>
</label>

<label>
<input
type="checkbox"
checked={data.display.nightDim}
onChange={(e) => {
setBackgroundDisplay({
nightDim: e.target.checked,
});
}}
/>{" "}
Automatically dim at night
</label>
</>
</ToggleSection>
)}
Expand Down
12 changes: 9 additions & 3 deletions src/views/shared/Backdrop.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { db } from "../../db/state";
import { useValue } from "../../lib/db/react";
import { useIsNight } from "../../hooks";

type Props = React.HTMLAttributes<HTMLDivElement> & {
ready?: boolean;
Expand All @@ -21,7 +22,8 @@ const Backdrop: React.FC<Props> = ({
const focus = useValue(db, "focus");
// TODO: Consider passing display in via prop
const background = useValue(db, "background");
const { blur, luminosity = 0 } = background.display;
const { blur, luminosity = 0, nightDim } = background.display;
const isNight = useIsNight();

style = { ...style };

Expand All @@ -30,8 +32,12 @@ const Backdrop: React.FC<Props> = ({
style["transform"] = `scale(${blur / 500 + 1})`;
}

if (luminosity && !focus) {
style["opacity"] = 1 - Math.abs(luminosity);
if (luminosity !== null && !focus) {
if (nightDim && isNight) {
style["opacity"] = (luminosity + 1) / 2;
} else {
style["opacity"] = 1 - Math.abs(luminosity);
}
}

return (
Expand Down