Skip to content

Commit 8f69b56

Browse files
committed
🐞 fix: 尝试修复缩放问题
1 parent ccd0c6b commit 8f69b56

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

electron/main/ipc/ipc-lyric.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ const initLyricIpc = (): void => {
144144
store.set("lyric", { ...store.get("lyric"), height });
145145
});
146146

147+
// 是否固定当前最大宽高
148+
ipcMain.on(
149+
"toggle-fixed-max-size",
150+
(_, options: { width: number; height: number; fixed: boolean }) => {
151+
if (!isWinAlive(lyricWin)) return;
152+
const { width, height, fixed } = options;
153+
if (fixed) {
154+
lyricWin.setMaximumSize(width, height);
155+
} else {
156+
lyricWin.setMaximumSize(1400, 360);
157+
}
158+
},
159+
);
160+
147161
// 请求歌词数据及配置
148162
ipcMain.on("request-desktop-lyric-data", () => {
149163
if (!isWinAlive(lyricWin)) return;

electron/main/windows/lyric-window.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class LyricWindow {
4242
this.win = createWindow({
4343
width: width || 800,
4444
height: height || 180,
45-
minWidth: 440,
45+
minWidth: 640,
4646
minHeight: 140,
47-
maxWidth: 1600,
47+
maxWidth: 1400,
4848
maxHeight: 360,
4949
// 没有指定位置时居中显示
5050
center: !(x && y),

src/views/DesktopLyric/index.vue

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,20 @@ const dragState = reactive({
164164
const lyricDragStart = async (_position: Position, event: PointerEvent) => {
165165
if (lyricConfig.isLock) return;
166166
dragState.isDragging = true;
167-
const { x, y, width, height } = await window.electron.ipcRenderer.invoke("get-window-bounds");
167+
const { x, y } = await window.electron.ipcRenderer.invoke("get-window-bounds");
168+
const { width, height } = await window.api.store.get("lyric");
169+
// 直接限制最大宽高
170+
window.electron.ipcRenderer.send("toggle-fixed-max-size", {
171+
width,
172+
height,
173+
fixed: true,
174+
});
168175
dragState.startX = event?.screenX ?? 0;
169176
dragState.startY = event?.screenY ?? 0;
170177
dragState.startWinX = x;
171178
dragState.startWinY = y;
172-
dragState.winWidth = width;
173-
dragState.winHeight = height;
179+
dragState.winWidth = width ?? 0;
180+
dragState.winHeight = height ?? 0;
174181
};
175182
176183
/**
@@ -209,17 +216,26 @@ useDraggable(desktopLyricRef, {
209216
onMove: (position, event) => {
210217
lyricDragMove(position, event);
211218
},
212-
onEnd: async () => {
213-
const { width, height } = await window.electron.ipcRenderer.invoke("get-window-bounds");
214-
// 若不等于初始宽高
215-
if (width !== dragState.winWidth || height !== dragState.winHeight) {
219+
onEnd: () => {
220+
// 关闭拖拽状态
221+
dragState.isDragging = false;
222+
requestAnimationFrame(() => {
223+
// 恢复拖拽前宽高
216224
window.electron.ipcRenderer.send(
217225
"update-lyric-size",
218226
dragState.winWidth,
219227
dragState.winHeight,
220228
);
221-
}
222-
dragState.isDragging = false;
229+
// 根据字体大小恢复一次高度
230+
const height = fontSizeToHeight(lyricConfig.fontSize);
231+
if (height) pushWindowHeight(height);
232+
// 恢复最大宽高
233+
window.electron.ipcRenderer.send("toggle-fixed-max-size", {
234+
width: dragState.winWidth,
235+
height: dragState.winHeight,
236+
fixed: false,
237+
});
238+
});
223239
},
224240
});
225241
@@ -247,6 +263,7 @@ watch(
247263
computedFontSize,
248264
(size) => {
249265
if (!Number.isFinite(size)) return;
266+
if (dragState.isDragging) return;
250267
if (size === lyricConfig.fontSize) return;
251268
const next = { fontSize: size };
252269
window.electron.ipcRenderer.send("update-desktop-lyric-option", next, true);
@@ -272,6 +289,7 @@ const fontSizeToHeight = (size: number) => {
272289
// 防抖推送窗口高度更新,避免频繁抖动
273290
const pushWindowHeight = useDebounceFn((nextHeight: number) => {
274291
if (!Number.isFinite(nextHeight)) return;
292+
if (dragState.isDragging) return;
275293
window.electron.ipcRenderer.send("update-window-height", nextHeight);
276294
}, 100);
277295
@@ -302,6 +320,9 @@ onMounted(() => {
302320
});
303321
window.electron.ipcRenderer.on("update-desktop-lyric-option", (_event, config: LyricConfig) => {
304322
Object.assign(lyricConfig, config);
323+
// 根据文字大小改变一次高度
324+
const height = fontSizeToHeight(config.fontSize);
325+
if (height) pushWindowHeight(height);
305326
});
306327
// 请求歌词数据及配置
307328
window.electron.ipcRenderer.send("request-desktop-lyric-data");

0 commit comments

Comments
 (0)