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

Extra change for Moonfire NVR Web UI #315

Merged
merged 13 commits into from
Apr 14, 2024
6 changes: 3 additions & 3 deletions ui/src/AppMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import MenuIcon from "@mui/icons-material/Menu";
import React from "react";
import { LoginState } from "./App";
import Box from "@mui/material/Box";
import { useThemeMode } from "./components/ThemeMode";
import { CurrentMode, useThemeMode } from "./components/ThemeMode";
import { Brightness2, Brightness7, BrightnessAuto } from "@mui/icons-material";
import { Tooltip } from "@mui/material";

Expand All @@ -29,7 +29,7 @@ interface Props {

// https://material-ui.com/components/app-bar/
function MoonfireMenu(props: Props) {
const { getTheme, changeTheme } = useThemeMode();
const { choosenTheme, changeTheme } = useThemeMode();
const theme = useTheme();
const [accountMenuAnchor, setAccountMenuAnchor] =
React.useState<null | HTMLElement>(null);
Expand Down Expand Up @@ -79,7 +79,7 @@ function MoonfireMenu(props: Props) {
color="inherit"
size="small"
>
{getTheme === 1 ? <Brightness7 /> : getTheme === 2 ? <Brightness2 /> : <BrightnessAuto />}
{choosenTheme === CurrentMode.Light ? <Brightness7 /> : choosenTheme === CurrentMode.Dark ? <Brightness2 /> : <BrightnessAuto />}
</IconButton>
</Tooltip>
{props.loginState !== "unknown" && props.loginState !== "logged-in" && (
Expand Down
21 changes: 2 additions & 19 deletions ui/src/Live/Multiview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,13 @@ const Multiview = (props: MultiviewProps) => {
const handleFullScreen = useCallback(() => {
if (outerRef.current) {
const elem = outerRef.current;
//@ts-expect-error another browser (if not bruh)
if (document.fullscreenElement || document.webkitFullscreenElement || document.msFullscreenElement) {
if (document.fullscreenElement) {
if (document.exitFullscreen) {
document.exitFullscreen();
//@ts-expect-error another browser (if not bruh)
} else if (document.webkitExitFullscreen) {
//@ts-expect-error another browser (if not bruh)
document.webkitExitFullscreen();
//@ts-expect-error another browser (if not bruh)
} else if (document.msExitFullscreen) {
//@ts-expect-error another browser (if not bruh)
document.msExitFullscreen();
}
} else {
if (elem.requestFullscreen) {
elem.requestFullscreen();
//@ts-expect-error another browser (if not bruh)
} else if (elem.webkitRequestFullscreen) {
//@ts-expect-error another browser (if not bruh)
elem.webkitRequestFullscreen();
//@ts-expect-error another browser (if not bruh)
} else if (elem.msRequestFullscreen) {
//@ts-expect-error another browser (if not bruh)
elem.msRequestFullscreen();
}
}
}
Expand Down Expand Up @@ -213,7 +196,7 @@ const Multiview = (props: MultiviewProps) => {
>
<Tooltip title="Toggle full screen">
<IconButton size="small" sx={{
position: 'fixed', background: 'rgba(255,255,255,0.3) !important', transition: '0.2s', opacity: '0.1', bottom: 10, right: 10, zIndex: 9, color: "#fff", ":hover": {
position: 'fixed', background: 'rgba(255,255,255,0.4) !important', transition: '0.2s', opacity: '0.4', bottom: 10, right: 10, zIndex: 9, color: "#fff", ":hover": {
opacity: '1'
}
}} onClick={handleFullScreen}>
Expand Down
45 changes: 27 additions & 18 deletions ui/src/components/ThemeMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,53 @@
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception

import { useColorScheme } from "@mui/material";
import React, { createContext } from "react";
import React, { createContext, useCallback } from "react";

export enum CurrentMode {
Auto = 0,
Light = 1,
Dark = 2
}

interface ThemeProps {
michioxd marked this conversation as resolved.
Show resolved Hide resolved
changeTheme: () => void,
currentTheme?: 'dark' | 'light',
getTheme: 0 | 1 | 2,
systemColor: 'dark' | 'light'
currentTheme: 'dark' | 'light',
choosenTheme: CurrentMode
}

export const ThemeContext = createContext<ThemeProps>({
currentTheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light',

Check failure on line 21 in ui/src/components/ThemeMode.tsx

View workflow job for this annotation

GitHub Actions / Node 18

src/App.test.tsx

TypeError: window.matchMedia is not a function ❯ src/components/ThemeMode.tsx:21:24 ❯ src/AppMenu.tsx:13:32

Check failure on line 21 in ui/src/components/ThemeMode.tsx

View workflow job for this annotation

GitHub Actions / Node 20

src/App.test.tsx

TypeError: window.matchMedia is not a function ❯ src/components/ThemeMode.tsx:21:24 ❯ src/AppMenu.tsx:13:32

Check failure on line 21 in ui/src/components/ThemeMode.tsx

View workflow job for this annotation

GitHub Actions / Node 21

src/App.test.tsx

TypeError: window.matchMedia is not a function ❯ src/components/ThemeMode.tsx:21:24 ❯ src/AppMenu.tsx:13:32
michioxd marked this conversation as resolved.
Show resolved Hide resolved
changeTheme: () => { },
getTheme: 0,
systemColor: window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light'
choosenTheme: CurrentMode.Auto
});

const ThemeMode = ({ children }: { children: JSX.Element }): JSX.Element => {
const { mode, setMode } = useColorScheme();
const [detectedSystemColorScheme, setDetectedSystemColorScheme] = React.useState<'dark' | 'light'>(
window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light'
);

React.useEffect(() => {
window.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (e) => {
setDetectedSystemColorScheme(e.matches ? 'dark' : 'light');
});

const useMediaQuery = useCallback((query: string) => {
const [matches, setMatches] = React.useState(
() => window.matchMedia(query).matches
);
React.useEffect(() => {
const m = window.matchMedia(query);
const l = () => setMatches(m.matches);
m.addEventListener("change", l);
return () => m.removeEventListener("change", l);
}, [query]);
return matches;
}, []);
michioxd marked this conversation as resolved.
Show resolved Hide resolved

const detectedSystemColorScheme = useMediaQuery("(prefers-color-scheme: dark)") ? "dark" : "light";

const changeTheme = React.useCallback(() => {
setMode(mode === 'dark' ? 'light' : mode === 'light' ? 'system' : 'dark')
}, [mode]);
michioxd marked this conversation as resolved.
Show resolved Hide resolved

const currentTheme = mode === 'system' ? detectedSystemColorScheme : mode;
const getTheme = mode === 'dark' ? 2 : mode === 'light' ? 1 : 0;
const currentTheme = mode === 'system' ? detectedSystemColorScheme : (mode ?? detectedSystemColorScheme);
const choosenTheme = mode === 'dark' ? CurrentMode.Dark : mode === 'light' ? CurrentMode.Light : CurrentMode.Auto;

return (
<ThemeContext.Provider value={{ changeTheme, currentTheme, getTheme, systemColor: detectedSystemColorScheme }}>
<ThemeContext.Provider value={{ changeTheme, currentTheme, choosenTheme }}>
{children}
</ThemeContext.Provider>
)
Expand Down
Loading