Skip to content

Commit f417f36

Browse files
committed
feat: 优化任务栏歌词 IPC 通信
1 parent 5a9abe3 commit f417f36

6 files changed

Lines changed: 107 additions & 224 deletions

File tree

electron/main/ipc/ipc-taskbar.ts

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,12 @@ import taskbarLyricManager from "../utils/taskbar-lyric-manager";
88

99
let cachedIsPlaying = false;
1010

11+
/** 读取完整任务栏配置 */
1112
const getTaskbarConfig = (): TaskbarConfig => {
12-
const store = useStore();
13-
const legacyLineHeight = store.get("taskbar.lineHeight", 1.1);
14-
const legacyFontSize = store.get("taskbar.fontSize", 14);
15-
const legacyMainScale = store.get("taskbar.mainScale", 1.0);
16-
const legacySubScale = store.get("taskbar.subScale", 0.8);
17-
return {
18-
mode: store.get("taskbar.mode", "taskbar"),
19-
maxWidth: store.get("taskbar.maxWidth", 300),
20-
position: store.get("taskbar.position", "automatic"),
21-
autoShrink: store.get("taskbar.autoShrink", false),
22-
margin: store.get("taskbar.margin", 10),
23-
minWidth: store.get("taskbar.minWidth", 10),
24-
enabled: store.get("taskbar.enabled", false),
25-
floatingAlign: store.get("taskbar.floatingAlign", "right"),
26-
floatingAutoWidth: store.get("taskbar.floatingAutoWidth", true),
27-
floatingWidth: store.get("taskbar.floatingWidth", 300),
28-
floatingHeight: store.get("taskbar.floatingHeight", 48),
29-
floatingAlwaysOnTop: store.get("taskbar.floatingAlwaysOnTop", false),
30-
31-
showWhenPaused: store.get("taskbar.showWhenPaused", true),
32-
showCover: store.get("taskbar.showCover", true),
33-
themeMode: store.get("taskbar.themeMode", "auto"),
34-
fontFamily: store.get("taskbar.fontFamily", "system-ui"),
35-
fontWeight: store.get("taskbar.fontWeight", 0),
36-
animationMode: store.get("taskbar.animationMode", "slide-blur"),
37-
singleLineMode: store.get("taskbar.singleLineMode", false),
38-
showWordLyrics: store.get("taskbar.showWordLyrics", true),
39-
showTranslation: store.get("taskbar.showTranslation", true),
40-
taskbarLineHeight: store.get("taskbar.taskbarLineHeight", legacyLineHeight),
41-
floatingLineHeight: store.get("taskbar.floatingLineHeight", legacyLineHeight),
42-
taskbarFontSize: store.get("taskbar.taskbarFontSize", legacyFontSize),
43-
floatingFontSize: store.get("taskbar.floatingFontSize", legacyFontSize),
44-
taskbarMainScale: store.get("taskbar.taskbarMainScale", legacyMainScale),
45-
floatingMainScale: store.get("taskbar.floatingMainScale", legacyMainScale),
46-
taskbarSubScale: store.get("taskbar.taskbarSubScale", legacySubScale),
47-
floatingSubScale: store.get("taskbar.floatingSubScale", legacySubScale),
48-
};
13+
return useStore().get("taskbar");
4914
};
5015

16+
/** 根据配置更新窗口可见性 */
5117
const updateWindowVisibility = (config: TaskbarConfig) => {
5218
const tray = getMainTray();
5319
if (tray) tray.setTaskbarLyricShow(config.enabled);
@@ -60,29 +26,6 @@ const updateWindowVisibility = (config: TaskbarConfig) => {
6026
taskbarLyricManager.setVisibility(shouldBeVisible);
6127
};
6228

63-
const updateWindowLayout = (animate: boolean = true) => {
64-
taskbarLyricManager.updateLayout(animate);
65-
};
66-
67-
const applyTaskbarOption = (option: Partial<TaskbarConfig>, pushToWindow: boolean) => {
68-
const store = useStore();
69-
const prev = getTaskbarConfig();
70-
const next = { ...prev, ...option };
71-
Object.entries(next).forEach(([key, value]) => {
72-
store.set(`taskbar.${key}`, value);
73-
});
74-
if (pushToWindow) {
75-
taskbarLyricManager.send(TASKBAR_IPC_CHANNELS.SYNC_STATE, {
76-
type: "config-update",
77-
data: option,
78-
} as SyncStatePayload);
79-
}
80-
updateWindowVisibility(next);
81-
if (next.enabled) {
82-
updateWindowLayout(false);
83-
}
84-
};
85-
8629
const initTaskbarIpc = () => {
8730
const store = useStore();
8831
const initialConfig = getTaskbarConfig();
@@ -97,10 +40,30 @@ const initTaskbarIpc = () => {
9740

9841
ipcMain.handle(TASKBAR_IPC_CHANNELS.GET_OPTION, () => getTaskbarConfig());
9942

100-
ipcMain.on(TASKBAR_IPC_CHANNELS.SET_OPTION, (_event, option: Partial<TaskbarConfig>, pushToWindow = true) => {
101-
if (!option) return;
102-
applyTaskbarOption(option, pushToWindow);
103-
});
43+
// 设置配置(增量合并)
44+
ipcMain.on(
45+
TASKBAR_IPC_CHANNELS.SET_OPTION,
46+
(_event, option: Partial<TaskbarConfig>, pushToWindow = true) => {
47+
if (!option) return;
48+
// 增量更新
49+
const prev = getTaskbarConfig();
50+
const next = { ...prev, ...option };
51+
Object.entries(option).forEach(([key, value]) => {
52+
store.set(`taskbar.${key}`, value);
53+
});
54+
// 推送到歌词窗口
55+
if (pushToWindow) {
56+
taskbarLyricManager.send(TASKBAR_IPC_CHANNELS.SYNC_STATE, {
57+
type: "config-update",
58+
data: option,
59+
} as SyncStatePayload);
60+
}
61+
updateWindowVisibility(next);
62+
if (next.enabled) {
63+
taskbarLyricManager.updateLayout(false);
64+
}
65+
},
66+
);
10467

10568
ipcMain.on(TASKBAR_IPC_CHANNELS.SYNC_STATE, (_event, payload: SyncStatePayload) => {
10669
if (payload.type === "playback-state") {
@@ -144,9 +107,8 @@ const initTaskbarIpc = () => {
144107
// 把事件发射到 app 里不太好,但是我觉得也没有必要为了这一个事件创建一个事件总线
145108
// TODO: 如果有了事件总线,通过那个事件总线发射这个事件
146109
(app as EventEmitter).on("explorer-restarted", () => {
147-
const currentEnabled = store.get("taskbar.enabled");
148-
const currentMode = store.get("taskbar.mode", "taskbar");
149-
if (currentEnabled && currentMode === "taskbar") {
110+
const currentConfig = getTaskbarConfig();
111+
if (currentConfig.enabled && currentConfig.mode === "taskbar") {
150112
taskbarLyricManager.close(false);
151113
setTimeout(() => {
152114
taskbarLyricManager.create("taskbar");

electron/main/store/index.ts

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEFAULT_TASKBAR_CONFIG, type TaskbarConfig } from "@shared";
12
import { app, screen } from "electron";
23
import Store from "electron-store";
34
import { join } from "path";
@@ -40,32 +41,9 @@ export interface StoreType {
4041
config?: LyricConfig;
4142
};
4243
/** 任务栏歌词 */
43-
taskbar: {
44-
/** 是否启用 */
45-
enabled: boolean;
46-
/** 模式 */
47-
mode?: "taskbar" | "floating";
48-
/** 最大宽度 */
49-
maxWidth?: number;
50-
/** 显示封面 */
51-
showCover?: boolean;
52-
/** 位置 */
53-
position?: "automatic" | "left" | "right";
54-
/** 暂停时显示 */
55-
showWhenPaused?: boolean;
56-
/** 自动收缩 */
57-
autoShrink?: boolean;
58-
/** 边距 */
59-
margin?: number;
60-
/** 最小宽度 (百分比) */
61-
minWidth?: number;
44+
taskbar: TaskbarConfig & {
6245
floatingX?: number;
6346
floatingY?: number;
64-
floatingAlign?: "left" | "right";
65-
floatingAutoWidth?: boolean;
66-
floatingWidth?: number;
67-
floatingHeight?: number;
68-
floatingAlwaysOnTop?: boolean;
6947
};
7048
/** 代理 */
7149
proxy: string;
@@ -120,22 +98,9 @@ export const useStore = () => {
12098
config: defaultLyricConfig,
12199
},
122100
taskbar: {
123-
enabled: false,
124-
mode: "taskbar",
125-
maxWidth: 30,
126-
showCover: true,
127-
position: "automatic",
128-
showWhenPaused: true,
129-
autoShrink: false,
130-
margin: 10,
131-
minWidth: 10,
101+
...DEFAULT_TASKBAR_CONFIG,
132102
floatingX: screenData.workArea.x + screenData.workArea.width / 2 - 150,
133103
floatingY: screenData.workArea.y + screenData.workArea.height - 120,
134-
floatingAlign: "right",
135-
floatingAutoWidth: true,
136-
floatingWidth: 300,
137-
floatingHeight: 48,
138-
floatingAlwaysOnTop: false,
139104
},
140105
macos: {
141106
statusBarLyric: {

src/components/Setting/SettingItemRenderer.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,21 @@
103103
<!-- Slider -->
104104
<n-slider
105105
v-else-if="item.type === 'slider'"
106-
v-model:value="modelValue"
106+
:value="modelValue"
107+
@update:value="
108+
(val) => {
109+
if (!isForcedConditionMet) {
110+
baseModelValue = val;
111+
}
112+
}
113+
"
114+
@dragend="
115+
() => {
116+
if (!isForcedConditionMet) {
117+
handleAction();
118+
}
119+
}
120+
"
107121
class="set"
108122
:min="resolve(item.min)"
109123
:max="resolve(item.max)"

src/components/Setting/config/lyric.ts

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { usePlayerController } from "@/core/player/PlayerController";
44
import { useSettingStore, useStatusStore } from "@/stores";
55
import type { LyricConfig } from "@/types/desktop-lyric";
66
import type { SettingConfig } from "@/types/settings";
7-
import {
8-
DEFAULT_TASKBAR_CONFIG,
9-
TASKBAR_IPC_CHANNELS,
10-
type TaskbarConfig,
11-
} from "@/types/shared";
7+
import { DEFAULT_TASKBAR_CONFIG, TASKBAR_IPC_CHANNELS, type TaskbarConfig } from "@/types/shared";
128
import { isElectron, isWin, isMac } from "@/utils/env";
139
import { descMultiline } from "@/utils/format";
1410
import { openAMLLServer, openExcludeLyric, openFontManager } from "@/utils/modal";
@@ -903,7 +899,9 @@ export const useLyricSettings = (): SettingConfig => {
903899
get: () => taskbarLyricConfig.floatingAlwaysOnTop,
904900
set: (v) => {
905901
taskbarLyricConfig.floatingAlwaysOnTop = v ?? false;
906-
saveTaskbarLyricConfig({ floatingAlwaysOnTop: taskbarLyricConfig.floatingAlwaysOnTop });
902+
saveTaskbarLyricConfig({
903+
floatingAlwaysOnTop: taskbarLyricConfig.floatingAlwaysOnTop,
904+
});
907905
},
908906
}),
909907
},
@@ -1006,9 +1004,11 @@ export const useLyricSettings = (): SettingConfig => {
10061004
get: () => taskbarLyricConfig.maxWidth,
10071005
set: (v) => {
10081006
taskbarLyricConfig.maxWidth = v ?? 30;
1009-
saveTaskbarLyricConfig({ maxWidth: taskbarLyricConfig.maxWidth });
10101007
},
10111008
}),
1009+
action: () => {
1010+
saveTaskbarLyricConfig({ maxWidth: taskbarLyricConfig.maxWidth });
1011+
},
10121012
suffix: "%",
10131013
},
10141014
{
@@ -1024,9 +1024,11 @@ export const useLyricSettings = (): SettingConfig => {
10241024
get: () => taskbarLyricConfig.minWidth,
10251025
set: (v) => {
10261026
taskbarLyricConfig.minWidth = v ?? 10;
1027-
saveTaskbarLyricConfig({ minWidth: taskbarLyricConfig.minWidth });
10281027
},
10291028
}),
1029+
action: () => {
1030+
saveTaskbarLyricConfig({ minWidth: taskbarLyricConfig.minWidth });
1031+
},
10301032
suffix: "%",
10311033
},
10321034
{
@@ -1166,80 +1168,54 @@ export const useLyricSettings = (): SettingConfig => {
11661168
}),
11671169
},
11681170
{
1169-
key: "taskbarLyricLineHeight",
1170-
label: "行间距",
1171+
key: "taskbarLyricFontScale",
1172+
label: "文字缩放",
11711173
type: "input-number",
1172-
description: "只作用于当前显示模式的行高(倍数)",
1173-
min: 0.8,
1174-
max: 3.0,
1174+
description: "在自适应字体大小的基础上进行缩放",
1175+
min: 0.5,
1176+
max: 2.0,
11751177
step: 0.1,
11761178
value: computed({
1177-
get: () =>
1178-
taskbarLyricConfig.mode === "floating"
1179-
? taskbarLyricConfig.floatingLineHeight
1180-
: taskbarLyricConfig.taskbarLineHeight,
1179+
get: () => taskbarLyricConfig.fontScale,
11811180
set: (v) => {
1182-
const next = v ?? 1.1;
1183-
if (taskbarLyricConfig.mode === "floating") {
1184-
taskbarLyricConfig.floatingLineHeight = next;
1185-
saveTaskbarLyricConfig({ floatingLineHeight: next });
1186-
} else {
1187-
taskbarLyricConfig.taskbarLineHeight = next;
1188-
saveTaskbarLyricConfig({ taskbarLineHeight: next });
1189-
}
1181+
taskbarLyricConfig.fontScale = v ?? 1.0;
1182+
saveTaskbarLyricConfig({ fontScale: taskbarLyricConfig.fontScale });
11901183
},
11911184
}),
1192-
defaultValue: 1.1,
1185+
defaultValue: 1.0,
11931186
},
11941187
{
1195-
key: "taskbarLyricFontSize",
1196-
label: "文字大小",
1188+
key: "taskbarLyricLineHeight",
1189+
label: "行间距",
11971190
type: "input-number",
1198-
description: "只作用于当前显示模式的基准文字大小",
1199-
min: 10,
1200-
max: 30,
1201-
step: 1,
1202-
suffix: "px",
1191+
description: "歌词行高",
1192+
min: 0.8,
1193+
max: 3.0,
1194+
step: 0.1,
12031195
value: computed({
1204-
get: () =>
1205-
taskbarLyricConfig.mode === "floating"
1206-
? taskbarLyricConfig.floatingFontSize
1207-
: taskbarLyricConfig.taskbarFontSize,
1196+
get: () => taskbarLyricConfig.lineHeight,
12081197
set: (v) => {
1209-
const next = v ?? 14;
1210-
if (taskbarLyricConfig.mode === "floating") {
1211-
taskbarLyricConfig.floatingFontSize = next;
1212-
saveTaskbarLyricConfig({ floatingFontSize: next });
1213-
} else {
1214-
taskbarLyricConfig.taskbarFontSize = next;
1215-
saveTaskbarLyricConfig({ taskbarFontSize: next });
1216-
}
1198+
const next = v ?? 1.1;
1199+
taskbarLyricConfig.lineHeight = next;
1200+
saveTaskbarLyricConfig({ lineHeight: next });
12171201
},
12181202
}),
1219-
defaultValue: 14,
1203+
defaultValue: 1.1,
12201204
},
12211205
{
12221206
key: "taskbarLyricMainScale",
12231207
label: "主歌词缩放",
12241208
type: "input-number",
1225-
description: "只作用于当前显示模式的主歌词缩放比例",
1209+
description: "主歌词缩放比例",
12261210
min: 0.5,
12271211
max: 1.5,
12281212
step: 0.05,
12291213
value: computed({
1230-
get: () =>
1231-
taskbarLyricConfig.mode === "floating"
1232-
? taskbarLyricConfig.floatingMainScale
1233-
: taskbarLyricConfig.taskbarMainScale,
1214+
get: () => taskbarLyricConfig.mainScale,
12341215
set: (v) => {
12351216
const next = v ?? 1.0;
1236-
if (taskbarLyricConfig.mode === "floating") {
1237-
taskbarLyricConfig.floatingMainScale = next;
1238-
saveTaskbarLyricConfig({ floatingMainScale: next });
1239-
} else {
1240-
taskbarLyricConfig.taskbarMainScale = next;
1241-
saveTaskbarLyricConfig({ taskbarMainScale: next });
1242-
}
1217+
taskbarLyricConfig.mainScale = next;
1218+
saveTaskbarLyricConfig({ mainScale: next });
12431219
},
12441220
}),
12451221
defaultValue: 1.0,
@@ -1248,24 +1224,16 @@ export const useLyricSettings = (): SettingConfig => {
12481224
key: "taskbarLyricSubScale",
12491225
label: "副歌词缩放",
12501226
type: "input-number",
1251-
description: "只作用于当前显示模式的副歌词缩放比例",
1227+
description: "副歌词缩放比例",
12521228
min: 0.5,
12531229
max: 1.0,
12541230
step: 0.05,
12551231
value: computed({
1256-
get: () =>
1257-
taskbarLyricConfig.mode === "floating"
1258-
? taskbarLyricConfig.floatingSubScale
1259-
: taskbarLyricConfig.taskbarSubScale,
1232+
get: () => taskbarLyricConfig.subScale,
12601233
set: (v) => {
12611234
const next = v ?? 0.8;
1262-
if (taskbarLyricConfig.mode === "floating") {
1263-
taskbarLyricConfig.floatingSubScale = next;
1264-
saveTaskbarLyricConfig({ floatingSubScale: next });
1265-
} else {
1266-
taskbarLyricConfig.taskbarSubScale = next;
1267-
saveTaskbarLyricConfig({ taskbarSubScale: next });
1268-
}
1235+
taskbarLyricConfig.subScale = next;
1236+
saveTaskbarLyricConfig({ subScale: next });
12691237
},
12701238
}),
12711239
defaultValue: 0.8,

0 commit comments

Comments
 (0)