From 317b8e9484d15296a7e21850d459b5f689ad597f Mon Sep 17 00:00:00 2001 From: michioxd Date: Sat, 13 Apr 2024 22:45:57 +0700 Subject: [PATCH] Resolved know problem. --- ui/src/AppMenu.tsx | 6 ++--- ui/src/Live/Multiview.tsx | 21 ++-------------- ui/src/components/ThemeMode.tsx | 43 ++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/ui/src/AppMenu.tsx b/ui/src/AppMenu.tsx index 8e7a26c..3b79ec5 100644 --- a/ui/src/AppMenu.tsx +++ b/ui/src/AppMenu.tsx @@ -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"; @@ -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); @@ -79,7 +79,7 @@ function MoonfireMenu(props: Props) { color="inherit" size="small" > - {getTheme === 1 ? : getTheme === 2 ? : } + {choosenTheme === CurrentMode.Light ? : choosenTheme === CurrentMode.Dark ? : } {props.loginState !== "unknown" && props.loginState !== "logged-in" && ( diff --git a/ui/src/Live/Multiview.tsx b/ui/src/Live/Multiview.tsx index 267e7c0..f747670 100644 --- a/ui/src/Live/Multiview.tsx +++ b/ui/src/Live/Multiview.tsx @@ -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(); } } } @@ -213,7 +196,7 @@ const Multiview = (props: MultiviewProps) => { > diff --git a/ui/src/components/ThemeMode.tsx b/ui/src/components/ThemeMode.tsx index f4bfeaa..d5277b2 100644 --- a/ui/src/components/ThemeMode.tsx +++ b/ui/src/components/ThemeMode.tsx @@ -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 { changeTheme: () => void, - currentTheme?: 'dark' | 'light', - getTheme: 0 | 1 | 2, - systemColor: 'dark' | 'light' + currentTheme: 'dark' | 'light', + choosenTheme: CurrentMode } export const ThemeContext = createContext({ currentTheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? 'dark' : 'light', 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; }, []); + const detectedSystemColorScheme = useMediaQuery("(prefers-color-scheme: dark)") ? "dark" : "light"; + const changeTheme = React.useCallback(() => { setMode(mode === 'dark' ? 'light' : mode === 'light' ? 'system' : 'dark') }, [mode]); - 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 ( - + {children} )