Skip to content

Commit

Permalink
fix: 修复快捷键异常触发 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Dec 2, 2023
1 parent 72650a5 commit 046b8f3
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ RENDERER_VITE_SITE_APPLE_LOGO = "/images/logo/favicon-apple.png"
# Cookie
## 咪咕音乐 Cookie
MAIN_VITE_MIGU_COOKIE = ""

# 公告配置
## 若无需公告,请将标题或内容任意一项设为空即可
## 公告类型
RENDERER_VITE_ANN_TYPE = "info"
## 公告标题
RENDERER_VITE_ANN_TITLE = ""
## 公告内容
RENDERER_VITE_ANN_CONTENT = ""
## 公告时长(毫秒)不可超过 999999
RENDERER_VITE_ANN_DURATION = 8000
34 changes: 27 additions & 7 deletions electron/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,38 @@ class MainProcess {
},
});
// 初始化
this.init();
this.checkApp().then((lockObtained) => {
if (lockObtained) {
this.init();
}
});
}

// 初始化程序
async init() {
log.info("主进程初始化");

// 单例锁
// 检查上次程序
async checkApp() {
if (!app.requestSingleInstanceLock()) {
app.quit();
log.error("已有一个程序正在运行,本次启动阻止");
app.quit();
// 未获得锁
return false;
}
// 聚焦到当前程序
else {
app.on("second-instance", () => {
if (this.mainWindow) {
this.mainWindow.show();
if (this.mainWindow.isMinimized()) this.mainWindow.restore();
this.mainWindow.focus();
}
});
// 获得锁
return true;
}
}

// 初始化程序
async init() {
log.info("主进程初始化");

// 启动网易云 API
this.ncmServer = await startNcmServer({
Expand Down
2 changes: 1 addition & 1 deletion electron/main/utils/createGlobalShortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { globalShortcut } from "electron";
const createGlobalShortcut = (win) => {
// 刷新程序
globalShortcut.register("CommandOrControl+R", () => {
win?.reload();
if (win && win.isFocused()) win?.reload();
});
};

Expand Down
9 changes: 9 additions & 0 deletions electron/main/utils/createSystemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const createSystemInfo = (win) => {
app.setUserTasks([]);
// 系统托盘
const mainTray = new Tray(join(__dirname, "../../public/images/logo/favicon.png"));
// 默认托盘菜单
Menu.setApplicationMenu(Menu.buildFromTemplate(createTrayMenu(win)));
// 给托盘图标设置气球提示
mainTray.setToolTip(app.getName());
// 歌曲数据改变时
Expand All @@ -36,6 +38,10 @@ const createSystemInfo = (win) => {
mainTray.on("right-click", () => {
mainTray.popUpContextMenu(Menu.buildFromTemplate(createTrayMenu(win)));
});
// linux 右键菜单
if (process.platform === "linux") {
mainTray.setContextMenu(Menu.buildFromTemplate(createTrayMenu(win)));
}
};

// 生成右键菜单
Expand Down Expand Up @@ -69,20 +75,23 @@ const createTrayMenu = (win) => {
{
label: "上一曲",
icon: createIcon("prev"),
accelerator: "CommandOrControl+Left",
click: () => {
win.webContents.send("playNextOrPrev", "prev");
},
},
{
label: playSongState ? "暂停" : "播放",
icon: createIcon(playSongState ? "pause" : "play"),
accelerator: "Space",
click: () => {
win.webContents.send("playOrPause");
},
},
{
label: "下一曲",
icon: createIcon("next"),
accelerator: "CommandOrControl+Right",
click: () => {
win.webContents.send("playNextOrPrev", "next");
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@rushstack/eslint-patch": "^1.3.3",
"@vitejs/plugin-vue": "^4.3.1",
"@vue/eslint-config-prettier": "^8.0.0",
"ajv": "^8.12.0",
"electron": "^25.6.0",
"electron-builder": "^24.6.4",
"electron-log": "^5.0.1",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ const music = musicData();
const status = siteStatus();
const settings = siteSettings();
// 公告数据
const annShow =
import.meta.env.RENDERER_VITE_ANN_TITLE && import.meta.env.RENDERER_VITE_ANN_CONTENT
? true
: false;
const annType = import.meta.env.RENDERER_VITE_ANN_TYPE;
const annTitle = import.meta.env.RENDERER_VITE_ANN_TITLE;
const annContene = import.meta.env.RENDERER_VITE_ANN_CONTENT;
const annDuration = Number(import.meta.env.RENDERER_VITE_ANN_DURATION);
// 显示公告
const showAnnouncements = () => {
if (annShow) {
$notification[annType]({
content: annTitle,
meta: annContene,
duration: annDuration,
});
}
};
// 网络无法连接
const canNotConnect = (error) => {
console.error("网络连接错误:", error.message);
Expand All @@ -120,11 +141,13 @@ onMounted(() => {
// 全局事件
globalEvents(router);
// 键盘监听
window.addEventListener("keyup", globalShortcut);
if (!checkPlatform.electron()) window.addEventListener("keyup", globalShortcut);
// 显示公告
showAnnouncements();
});
onUnmounted(() => {
window.removeEventListener("keyup", globalShortcut);
if (!checkPlatform.electron()) window.removeEventListener("keyup", globalShortcut);
});
</script>
Expand Down
3 changes: 2 additions & 1 deletion src/assets/icon.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@
"download": "M16.59 9H15V4c0-.55-.45-1-1-1h-4c-.55 0-1 .45-1 1v5H7.41c-.89 0-1.34 1.08-.71 1.71l4.59 4.59c.39.39 1.02.39 1.41 0l4.59-4.59c.63-.63.19-1.71-.7-1.71zM5 19c0 .55.45 1 1 1h12c.55 0 1-.45 1-1s-.45-1-1-1H6c-.55 0-1 .45-1 1z",
"radio": "M3.24 6.15C2.51 6.43 2 7.17 2 8v12a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V8c0-1.11-.89-2-2-2H8.3l8.26-3.34L15.88 1L3.24 6.15zM7 20c-1.66 0-3-1.34-3-3s1.34-3 3-3s3 1.34 3 3s-1.34 3-3 3zm13-8h-2v-2h-2v2H4V8h16v4z",
"person-add": "M15.39 14.56C13.71 13.7 11.53 13 9 13s-4.71.7-6.39 1.56A2.97 2.97 0 0 0 1 17.22V20h16v-2.78c0-1.12-.61-2.15-1.61-2.66zM9 12c2.21 0 4-1.79 4-4s-1.79-4-4-4s-4 1.79-4 4s1.79 4 4 4zm11-3V7c0-.55-.45-1-1-1s-1 .45-1 1v2h-2c-.55 0-1 .45-1 1s.45 1 1 1h2v2c0 .55.45 1 1 1s1-.45 1-1v-2h2c.55 0 1-.45 1-1s-.45-1-1-1h-2z",
"person-remove": "M14 8c0-2.21-1.79-4-4-4S6 5.79 6 8s1.79 4 4 4s4-1.79 4-4zM2 18v1c0 .55.45 1 1 1h14c.55 0 1-.45 1-1v-1c0-2.66-5.33-4-8-4s-8 1.34-8 4zm16-8h4c.55 0 1 .45 1 1s-.45 1-1 1h-4c-.55 0-1-.45-1-1s.45-1 1-1z"
"person-remove": "M14 8c0-2.21-1.79-4-4-4S6 5.79 6 8s1.79 4 4 4s4-1.79 4-4zM2 18v1c0 .55.45 1 1 1h14c.55 0 1-.45 1-1v-1c0-2.66-5.33-4-8-4s-8 1.34-8 4zm16-8h4c.55 0 1 .45 1 1s-.45 1-1 1h-4c-.55 0-1-.45-1-1s.45-1 1-1z",
"github": "M12 2A10 10 0 0 0 2 12c0 4.42 2.87 8.17 6.84 9.5c.5.08.66-.23.66-.5v-1.69c-2.77.6-3.36-1.34-3.36-1.34c-.46-1.16-1.11-1.47-1.11-1.47c-.91-.62.07-.6.07-.6c1 .07 1.53 1.03 1.53 1.03c.87 1.52 2.34 1.07 2.91.83c.09-.65.35-1.09.63-1.34c-2.22-.25-4.55-1.11-4.55-4.92c0-1.11.38-2 1.03-2.71c-.1-.25-.45-1.29.1-2.64c0 0 .84-.27 2.75 1.02c.79-.22 1.65-.33 2.5-.33c.85 0 1.71.11 2.5.33c1.91-1.29 2.75-1.02 2.75-1.02c.55 1.35.2 2.39.1 2.64c.65.71 1.03 1.6 1.03 2.71c0 3.82-2.34 4.66-4.57 4.91c.36.31.69.92.69 1.85V21c0 .27.16.59.67.5C19.14 20.16 22 16.42 22 12A10 10 0 0 0 12 2Z"
}
19 changes: 19 additions & 0 deletions src/components/Nav/MainNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
</div>
<!-- 搜索框 -->
<SearchInp />
<!-- GitHub -->
<n-button class="github" circle quaternary @click="openGithub">
<template #icon>
<n-icon size="20">
<SvgIcon icon="github" />
</n-icon>
</template>
</n-button>
<!-- 用户信息 -->
<userData />
</nav>
Expand All @@ -36,12 +44,19 @@
<script setup>
import { siteStatus } from "@/stores";
import { useRouter } from "vue-router";
import packageJson from "@/../package.json";

const router = useRouter();
const status = siteStatus();

// 站点信息
const siteName = import.meta.env.RENDERER_VITE_SITE_TITLE;

// 打开 GitHub
const openGithub = () => {
console.log(packageJson.github);
window.open(packageJson.github);
};
</script>

<style lang="scss" scoped>
Expand Down Expand Up @@ -101,5 +116,9 @@ const siteName = import.meta.env.RENDERER_VITE_SITE_TITLE;
}
}
}
.github {
margin-left: 12px;
-webkit-app-region: no-drag;
}
}
</style>
1 change: 1 addition & 0 deletions src/components/Player/MainControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
<!-- 播放暂停 -->
<n-button
:loading="playLoading"
:keyboard="false"
type="primary"
class="play-control"
strong
Expand Down
1 change: 1 addition & 0 deletions src/components/Player/PlayerControl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<!-- 播放暂停 -->
<n-button
:loading="playLoading"
:keyboard="false"
class="play-control"
strong
secondary
Expand Down
1 change: 0 additions & 1 deletion src/style/animate.css

This file was deleted.

1 change: 0 additions & 1 deletion src/style/main.css

This file was deleted.

5 changes: 5 additions & 0 deletions src/style/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ body,
--n-border-radius: 8px !important;
}

// n-notification
.n-notification {
--n-border-radius: 8px !important;
}

// n-skeleton
.n-skeleton {
background: linear-gradient(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/globalShortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const globalShortcut = (e) => {
e.stopPropagation();

// 播放或暂停
if (e.ctrlKey && e.code === "Space") playOrPause();
if (e.code === "Space") playOrPause();

// 调整音量
if (e.ctrlKey && (e.code === "ArrowUp" || e.code === "ArrowDown")) {
if (e.code === "ArrowUp" || e.code === "ArrowDown") {
const music = musicData();
const volume = music.playVolume;
const delta = e.code === "ArrowUp" ? 0.1 : -0.1;
Expand Down

0 comments on commit 046b8f3

Please sign in to comment.