Skip to content

Commit

Permalink
preserve dark mode setting
Browse files Browse the repository at this point in the history
  • Loading branch information
lideming committed Apr 12, 2022
1 parent 0979e21 commit 0d50879
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { activities, ActivityName, defaultActivitiy } from './activities';
import { appTheme } from './appTheme';
import { navContext } from './contexts';

appTheme.init();

function App() {
// The Ref of (router) navigation state is created here.
Expand All @@ -33,7 +34,6 @@ function App() {

// Initializing app theme
useEffect(() => {
appTheme.init();
var keydown = (e: KeyboardEvent) => {
if (e.ctrlKey && e.key == 'd') {
e.preventDefault();
Expand Down
8 changes: 4 additions & 4 deletions src/activities/PairActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Container from "@material-ui/core/Container"
export const PairActivity = createActivity(function (props) {
return (
<Activity hidden={props.hidden} className={"pair"}>
<Container>
<QRCodeSVG value="https://reactjs.org/" size={300} />
<h2>手机端扫码绑定</h2>
</Container>
<div className="qrcode">
<QRCodeSVG value="https://reactjs.org/" size={300} fgColor="black" />
</div>
<h2>手机端扫码绑定</h2>
</Activity>
)
});
3 changes: 2 additions & 1 deletion src/activities/SettingsActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { navContext } from "../contexts"

export const SettingsActivity = createActivity(function (props) {
const navState = useContext(navContext);
const dark = useWebfxRef(appTheme.enabledRef);
return (
<Activity hidden={props.hidden} className={"settings"}>
<ThemeProvider theme={createMuiTheme({
Expand All @@ -26,7 +27,7 @@ export const SettingsActivity = createActivity(function (props) {
<Container>
<FormGroup>
<FormControlLabel control={
<Switch size="medium" value={useWebfxRef(appTheme.enabledRef)} onChange={(e, val) => {
<Switch size="medium" checked={dark} onChange={(e, val) => {
appTheme.toggle(val);
}} />
} label="夜间模式" />
Expand Down
4 changes: 3 additions & 1 deletion src/appTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export const appTheme = {
enabledRef: new Ref<boolean>(),
init() {
this.enabledRef.value = false;
if (!(darkreader as any).isEnabled()) {
if (localStorage.getItem('drivingui-dark') == '1') {
this.toggle(true);
}
},
toggle(enable?: boolean) {
if (enable == this.enabledRef.value) return;
this.enabledRef.value = !this.enabledRef.value;
localStorage.setItem('drivingui-dark', this.enabledRef.value ? '1' : '0');
if (this.enabledRef.value) {
darkreader.enable({
brightness: 100,
Expand Down

0 comments on commit 0d50879

Please sign in to comment.