-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gui): Darkmode Switch & Language Selector (#295)
refactor(gui): rename zh to zh-Hans
- Loading branch information
Showing
32 changed files
with
88 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IconLanguage } from '@douyinfe/semi-icons'; | ||
import { Button } from '@douyinfe/semi-ui'; | ||
import React from 'react'; | ||
import { executeCommand } from '../CommandCenter'; | ||
|
||
export const LanguageSelector = React.memo(() => { | ||
return ( | ||
<Button | ||
icon={<IconLanguage />} | ||
type="tertiary" | ||
theme="borderless" | ||
onClick={() => { | ||
executeCommand('ChangeLanguage'); | ||
}} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { IconMoon, IconSun } from '@douyinfe/semi-icons'; | ||
import { Button } from '@douyinfe/semi-ui'; | ||
import { useObservableState } from 'observable-hooks'; | ||
import React from 'react'; | ||
import { isDarkMode$ } from './darkmode'; | ||
|
||
export const DarkmodeSwitch = React.memo(() => { | ||
const isDark = useObservableState(isDarkMode$); | ||
return ( | ||
<Button | ||
icon={isDark ? <IconMoon /> : <IconSun />} | ||
theme="borderless" | ||
type="tertiary" | ||
onClick={() => { | ||
isDarkMode$.next(!isDark); | ||
}} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { BehaviorSubject, Observable } from 'rxjs'; | ||
|
||
export const isDarkMode$ = new BehaviorSubject<boolean>(false); | ||
|
||
const mql = window.matchMedia('(prefers-color-scheme: dark)'); | ||
function matchMode(e: any) { | ||
const body = document.body; | ||
if (e.matches) { | ||
isDarkMode$.next(true); | ||
if (!body.hasAttribute('theme-mode')) { | ||
body.setAttribute('theme-mode', 'dark'); | ||
isDarkMode$.subscribe((isDark) => { | ||
if (isDark) { | ||
if (!document.body.hasAttribute('theme-mode')) { | ||
document.body.setAttribute('theme-mode', 'dark'); | ||
} | ||
} else { | ||
isDarkMode$.next(false); | ||
if (body.hasAttribute('theme-mode')) { | ||
body.removeAttribute('theme-mode'); | ||
if (document.body.hasAttribute('theme-mode')) { | ||
document.body.removeAttribute('theme-mode'); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
mql.addListener(matchMode); | ||
matchMode(mql); | ||
// Follow system dark mode | ||
new Observable<boolean>((subscriber) => { | ||
const mql = window.matchMedia('(prefers-color-scheme: dark)'); | ||
function matchMode(e: any) { | ||
if (e.matches) { | ||
subscriber.next(true); | ||
} else { | ||
subscriber.next(false); | ||
} | ||
} | ||
matchMode(mql); | ||
|
||
mql.addEventListener('change', matchMode); | ||
return () => { | ||
mql.removeEventListener('change', matchMode); | ||
}; | ||
}).subscribe((v) => { | ||
isDarkMode$.next(v); | ||
}); |