Skip to content

Commit

Permalink
Set Dark Mode based on the system theme.
Browse files Browse the repository at this point in the history
- Added a state to track if in dark mode in App.js
- Updated theme mode based on inDarkMode in App.js
- Modified getDarkMode function in GlobalContext.js to check theme and native dark mode preference.
  • Loading branch information
obra committed Mar 14, 2024
1 parent f543727 commit c48ddf3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/renderer/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const App = (props) => {
const darkMode = state.darkMode;
const [activeDevice, setActiveDevice] = state.activeDevice;
const [bgColor, setBgColor] = useState(null);

const [inDarkMode, setInDarkMode] = useState(darkMode());
const handleDeviceDisconnect = async (sender, vid, pid) => {
if (!focus.focusDeviceDescriptor) return;
if (flashing) return;
Expand Down Expand Up @@ -93,12 +93,16 @@ const App = (props) => {

const handleNativeThemeUpdate = (event, v) => {
if (theme != "system") return;

// This enforces a re-render, without having to use another state.
setTheme(null);
setTheme("system");
if (darkMode() === inDarkMode) return;
// This forces a re-render, without having to use another state.
setInDarkMode(darkMode());
};

// Listening for changes to the native theme
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (event) => {
handleNativeThemeUpdate(event);
});

useEffect(() => {
async function setupLayout() {
const layoutSetting = await settings.get("keyboard.layout", "English (US)");
Expand All @@ -121,15 +125,15 @@ const App = (props) => {

const uiTheme = createTheme({
palette: {
mode: darkMode() ? "dark" : "light",
mode: inDarkMode ? "dark" : "light",
primary: {
main: "#EF5022",
},
secondary: {
main: darkMode() ? "#ed91f3" : "#ab0edd",
main: inDarkMode ? "#ed91f3" : "#ab0edd",
},
background: {
default: darkMode() ? "#353535" : "#f5f5f5",
default: inDarkMode ? "#353535" : "#f5f5f5",
},
},
components: {
Expand All @@ -151,7 +155,7 @@ const App = (props) => {
useEffect(() => {
setTheme(theme);
setBgColor(uiTheme.palette.body);
}, [theme, uiTheme, setTheme]);
}, [theme, uiTheme, setTheme, inDarkMode]);

const toggleFlashing = async () => {
flashing = !flashing;
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/components/GlobalContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ export const GlobalContextProvider = (props) => {
const [firmwareUpdateWarning, setFirmwareUpdateWarning] = useState(false);
const [hideHeaderInPrint, setHideHeaderInPrint] = useState(false);
const getDarkMode = () => {
return theme == "dark";
if (theme === "dark") {
return true;
}
if (theme === "system") {
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return true;
} else {
return false;
}
}
return false; // light
};

const state = {
Expand Down

0 comments on commit c48ddf3

Please sign in to comment.