Skip to content

Commit

Permalink
renderer: 'auto' theme variety leveraging css prefers-color-scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
rocka committed Nov 21, 2020
1 parent f1448a3 commit 30927ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import App from './App.vue';
import store from './store';
import routes from './routes';
import { isLinux } from './util/globals';
import { initTheme } from './util/theme';
import { initTheme, setTheme } from './util/theme';
import DblclickRipple from './util/dblclick-ripple';
import './style.css';
import './transition.css';
Expand All @@ -23,12 +23,17 @@ Vue.use(Message);
Vue.use(DblclickRipple);
Vue.component('RecycleScroller', RecycleScroller);

const darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');

try {
const settings = JSON.parse(sessionStorage.getItem('settings'));
const themeVariety = settings.themeVariety === 'auto'
? (darkMediaQuery.matches ? 'dark' : 'light')
: settings.themeVariety;
initTheme({
primary: settings.themePrimaryColor,
secondary: settings.themeSecondaryColor
}, settings.themeVariety);
}, themeVariety);
} catch (e) { sessionStorage.removeItem('settings'); }

require('@/util/tray').injectStore(store);
Expand Down Expand Up @@ -76,9 +81,21 @@ if (store.state.settings.startupPage !== 'index') {
const app = new Vue({
store,
router,
provide: {
darkMediaQuery
},
extends: App
});

darkMediaQuery.addEventListener('change', e => {
if (store.state.settings.themeVariety !== 'auto') return;
const variety = e.matches ? 'dark' : 'light';
setTheme({
primary: store.state.settings.themePrimaryColor,
secondary: store.state.settings.themeSecondaryColor
}, variety);
});

if (isLinux) {
app.$once('audio-ready', audio => {
const m = require('@/util/mpris');
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/page/Settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default {
versionName: ''
};
},
inject: [
'darkMediaQuery'
],
computed: {
...mapState(['settings'])
},
Expand Down Expand Up @@ -166,10 +169,13 @@ export default {
case 'themePrimaryColor':
case 'themeSecondaryColor':
case 'themeVariety':
const variety = state.settings.themeVariety === 'auto'
? (this.darkMediaQuery.matches ? 'dark' : 'light')
: state.settings.themeVariety;
setTheme({
primary: state.settings.themePrimaryColor,
secondary: state.settings.themeSecondaryColor
}, state.settings.themeVariety);
}, variety);
break;
case 'windowBorder':
this.$nextTick(() => this.recreateWindow());
Expand All @@ -186,12 +192,12 @@ export default {
ipcRenderer.send(IpcTag, key, val);
break;
case 'bitRate':
if(val === 'ex') {
if (val === 'ex') {
this.$toast.message('实际播放码率取决于歌曲最高码率和帐号最高可播放码率');
}
break;
case 'bitRateDownload':
if(val === 'ex') {
if (val === 'ex') {
this.$toast.message('实际下载码率取决于歌曲最高码率和帐号最高可播放码率');
}
break;
Expand Down
1 change: 1 addition & 0 deletions src/renderer/page/Settings/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const Entries = [
title: '背景色',
prop: 'themeVariety',
options: [
{ label: '跟随系统', value: 'auto' },
{ label: '亮色', value: 'light' },
{ label: '暗色', value: 'dark' }
]
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DefaultSettings = {
minimizeOnStartup: false,
themePrimaryColor: '#7e57c2',
themeSecondaryColor: '#ff4081',
themeVariety: 'light',
themeVariety: 'auto',
autoReplacePlaylist: false
};

Expand Down

0 comments on commit 30927ef

Please sign in to comment.