Skip to content

Commit

Permalink
main,renderer: add settings to show track name on title bar
Browse files Browse the repository at this point in the history
  • Loading branch information
rocka committed Jul 10, 2023
1 parent 6bfa66a commit e98bd40
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 16 deletions.
14 changes: 13 additions & 1 deletion src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function createMainWindow(settings, url = MainURL) {
}
});

win.on('page-title-updated', (ev) => {
// prevent window title update from document.title
ev.preventDefault();
});

if (settings.showTrayIcon && settings.minimizeOnStartup) {
win.hide();
}
Expand Down Expand Up @@ -228,8 +233,15 @@ ipcMain.on('showLoginWindow', () => {
});
});

ipcMain.handle('controlMainWindow', (event, method) => {
ipcMain.handle('controlMainWindow', (event, method, ...args) => {
switch (method) {
case 'setTitle': {
const title = args[0];
if (typeof title === 'string') {
mainWindow.setTitle(title);
}
break;
}
case 'maximize':
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
Expand Down
3 changes: 2 additions & 1 deletion src/main/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const defaultSettings = {
themeSecondaryColor: '#ff4081',
themeVariety: 'auto',
autoReplacePlaylist: false,
lyricTranslation: 'translation'
lyricTranslation: 'translation',
titleBarShowsTrackName: true
};

/**
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/components/AppNav/AppNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</template>

<script>
import { mapActions, mapState } from 'vuex';
import { mapActions } from 'vuex';
import Routes from '@/routes';
import SearchBox from './SearchBox.vue';
Expand All @@ -111,7 +111,11 @@ export default {
};
},
computed: {
...mapState(['settings', 'user']),
/** @returns {import('@/store/modules/user').State}*/
user() { return this.$store.state.user; },
/** @returns {import('@/store/modules/settings').State}*/
settings() { return this.$store.state.settings; },
/** @returns {import('@/routes').Route[]} */
validRoutes() {
return Routes.filter(r => r.title);
},
Expand Down
3 changes: 0 additions & 3 deletions src/renderer/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ window.onbeforeunload = () => {
};

const router = new Router({ routes });
router.afterEach(() => {
store.dispatch('updateDocumentTitle');
});
if (store.state.settings.startupPage !== 'index') {
router.replace({ name: store.state.settings.startupPage });
}
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/page/Settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export default {
methods: {
...mapActions([
'updateSettings',
'resetSettings'
'resetSettings',
'updateMainWindowTitle'
]),
shouldShowOption(item) {
if (!item.depends && !item.exclude) return true;
Expand Down Expand Up @@ -184,6 +185,9 @@ export default {
case 'windowBorder':
this.$nextTick(() => this.recreateWindow());
break;
case 'titleBarShowsTrackName':
this.updateMainWindowTitle(val);
break;
case 'windowZoom':
webContents.setZoomFactor(val || 1);
break;
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/page/Settings/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export const Entries = [
title: '使用系统标题栏',
prop: 'windowBorder'
},
{
type: 'toggle',
title: '标题栏显示歌曲名称',
prop: 'titleBarShowsTrackName'
},
{
type: 'toggle',
title: '显示托盘图标',
Expand Down
22 changes: 15 additions & 7 deletions src/renderer/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import Api from '@/api/ipc';
import * as ApiTyped from '@/api/typed';
import * as DbPlaylist from '@/api/database/playlist';
import * as DbRadio from '@/api/database/radio';
import * as types from './mutation-types';

import { Track, Video } from '@/util/models';
import { browserWindow } from '@/util/globals';

import * as types from './mutation-types';
import { LOOP_MODE } from './modules/playlist';

/**
Expand Down Expand Up @@ -375,21 +378,26 @@ export async function updateUiLyric({ commit, getters }, { ignoreCache = false }

/**
* @param {ActionContext} param0
* @param {boolean} payload show track name in window title
*/
export function updateDocumentTitle({ getters }) {
export function updateMainWindowTitle({ getters }, payload = true) {
let title;
const track = getters.playing;
if (track && track.id) {
document.title = `${track.name} | Electron NCM`;
if (payload && track && track.id) {
title = `${track.name} | Electron NCM`;
} else {
document.title = 'Electron NCM';
title = 'Electron NCM';
}
browserWindow.setTitle(title);
}

/**
* @param {ActionContext} param0
*/
export function updateUiTrack({ dispatch }) {
dispatch('updateDocumentTitle');
export function updateUiTrack({ state, dispatch }) {
if (state.settings.titleBarShowsTrackName) {
dispatch('updateMainWindowTitle');
}
dispatch('updateUiLyric');
dispatch('updateUiCoverImgSrc');
return dispatch('updateUiAudioSrc');
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const DefaultSettings = {
themeSecondaryColor: '#ff4081',
themeVariety: 'auto',
autoReplacePlaylist: false,
lyricTranslation: 'translation'
lyricTranslation: 'translation',
titleBarShowsTrackName: true
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/renderer/util/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const isLinux = process.platform === 'linux';
const controlMainWindow = encm.invoke.bind(null, 'controlMainWindow');

export const browserWindow = {
setTitle: controlMainWindow.bind(null, 'setTitle'),
maximize: controlMainWindow.bind(null, 'maximize'),
minimize: controlMainWindow.bind(null, 'minimize'),
close: controlMainWindow.bind(null, 'close'),
Expand Down

0 comments on commit e98bd40

Please sign in to comment.