Skip to content

Commit 95e2313

Browse files
committed
feat: 封装相关的窗口操作api
1 parent f6a14bd commit 95e2313

File tree

12 files changed

+868
-76
lines changed

12 files changed

+868
-76
lines changed

components.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ declare module 'vue' {
1313
ElText: typeof import('element-plus/es')['ElText']
1414
FluentCheckBox: typeof import('./src/components/fluent-ui/FluentCheckBox.vue')['default']
1515
FluentInput: typeof import('./src/components/fluent-ui/FluentInput.vue')['default']
16-
FluentUIButton: typeof import('./src/components/fluent-ui/FluentUIButton.vue')['default']
17-
FluentUIInput: typeof import('./src/components/fluent-ui/FluentUIInput.vue')['default']
1816
RouterLink: typeof import('vue-router')['RouterLink']
1917
RouterView: typeof import('vue-router')['RouterView']
2018
}

electron/ipc/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import setupWindowIpcHandlers from "./window";
2+
3+
const setupIpcHandlers = (win: Electron.BrowserWindow | null) => {
4+
setupWindowIpcHandlers(win);
5+
};
6+
7+
export default setupIpcHandlers;

electron/ipc/window.ts

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
import { BrowserWindow, ipcMain } from "electron";
2+
3+
export default function setupWindowIpcHandlers(win: BrowserWindow | null) {
4+
5+
//改变窗口大小
6+
ipcMain.on("resize-window", (_event, width: number, height: number) => {
7+
if (win) {
8+
win.setSize(width, height);
9+
}
10+
});
11+
12+
//设置窗口边框是否可见
13+
ipcMain.on("set-window-frame-visible", (_event, isFrameVisible: boolean) => {
14+
if (win) {
15+
win.setMenuBarVisibility(isFrameVisible);
16+
win.setResizable(isFrameVisible);
17+
}
18+
});
19+
20+
//设置窗口标题
21+
ipcMain.on("set-window-title", (_event, title: string) => {
22+
if (win) {
23+
win.setTitle(title);
24+
}
25+
});
26+
27+
//设置窗口图标
28+
ipcMain.on("set-window-central", (_event) => {
29+
if (win) {
30+
const { width, height } = win.getBounds();
31+
const { workAreaSize } = require("electron").screen.getPrimaryDisplay();
32+
const x = (workAreaSize.width - width) / 2;
33+
const y = (workAreaSize.height - height) / 2;
34+
win.setBounds({ x, y });
35+
}
36+
});
37+
38+
//设置窗口是是否置顶
39+
ipcMain.on("set-window-always-on-top", (_event, alwaysOnTop: boolean) => {
40+
if (win) {
41+
win.setAlwaysOnTop(alwaysOnTop);
42+
}
43+
});
44+
45+
//设置窗口是否可见
46+
ipcMain.on("set-window-visible", (_event, visible: boolean) => {
47+
if (win) {
48+
visible ? win.show() : win.hide();
49+
}
50+
});
51+
52+
//设置窗口菜单栏是否可见
53+
ipcMain.on("set-window-menu-bar-visible", (_event, visible: boolean) => {
54+
if (win) {
55+
win.setMenuBarVisibility(visible);
56+
}
57+
});
58+
59+
//设置窗口是否可改变大小
60+
ipcMain.on("set-window-resizable", (_event, resizable: boolean) => {
61+
if (win) {
62+
win.setResizable(resizable);
63+
}
64+
});
65+
66+
//设置窗口是否可移动
67+
ipcMain.on("set-window-movable", (_event, movable: boolean) => {
68+
if (win) {
69+
win.setMovable(movable);
70+
}
71+
});
72+
73+
//设置窗口最小化
74+
ipcMain.on("set-window-minimizable", (_event, minimizable: boolean) => {
75+
if (win) {
76+
win.setMinimizable(minimizable);
77+
}
78+
});
79+
80+
//设置窗口最大化
81+
ipcMain.on("set-window-maximizable", (_event, maximizable: boolean) => {
82+
if (win) {
83+
win.setMaximizable(maximizable);
84+
}
85+
});
86+
87+
//设置窗口是否全屏
88+
ipcMain.on("set-window-full-screen", (_event, fullScreen: boolean) => {
89+
if (win) {
90+
win.setFullScreen(fullScreen);
91+
}
92+
});
93+
94+
//设置窗口背景颜色
95+
ipcMain.on("set-window-background-color", (_event, color: string) => {
96+
if (win) {
97+
win.setBackgroundColor(color);
98+
}
99+
});
100+
101+
102+
//设置窗口图标
103+
ipcMain.on("set-window-icon", (_event, iconPath: string) => {
104+
if (win) {
105+
win.setIcon(iconPath);
106+
}
107+
});
108+
109+
//设置窗口是否在所有工作区可见
110+
ipcMain.on("set-window-visible-on-all-workspaces", (_event, visible: boolean) => {
111+
if (win) {
112+
win.setVisibleOnAllWorkspaces(visible);
113+
}
114+
});
115+
116+
117+
//设置窗口大小
118+
ipcMain.on("set-window-size", (_event, width: number, height: number) => {
119+
if (win) {
120+
win.setSize(width, height);
121+
}
122+
});
123+
124+
//设置窗口位置
125+
ipcMain.on("set-window-position", (_event, x: number, y: number) => {
126+
if (win) {
127+
win.setPosition(x, y);
128+
}
129+
});
130+
131+
//设置窗口透明度
132+
ipcMain.on("set-window-opacity", (_event, opacity: number) => {
133+
if (win) {
134+
win.setOpacity(opacity);
135+
}
136+
});
137+
138+
//设置窗口是否在任务栏中显示
139+
ipcMain.on("set-window-in-taskbar", (_event, inTaskbar: boolean) => {
140+
if (win) {
141+
win.setSkipTaskbar(!inTaskbar);
142+
}
143+
});
144+
145+
//设置窗口最小化
146+
ipcMain.on("minimize-window", () => {
147+
if (win) {
148+
win.minimize();
149+
}
150+
});
151+
152+
//设置窗口最大化
153+
ipcMain.on("maximize-window", () => {
154+
if (win) {
155+
win.maximize();
156+
}
157+
});
158+
159+
//设置窗口还原
160+
ipcMain.on("restore-window", () => {
161+
if (win) {
162+
win.restore();
163+
}
164+
});
165+
166+
//关闭窗口
167+
ipcMain.on("close-window", () => {
168+
if (win) {
169+
win.close();
170+
}
171+
});
172+
173+
//销毁窗口
174+
ipcMain.on("destroy-window", () => {
175+
if (win) {
176+
win.destroy();
177+
}
178+
});
179+
180+
//获取窗口位置
181+
ipcMain.on("get-window-position", (_event) => {
182+
if (win) {
183+
const [x, y] = win.getPosition();
184+
_event.reply("get-window-position-reply", { x, y });
185+
}
186+
});
187+
188+
//获取窗口大小
189+
ipcMain.on("get-window-size", (_event) => {
190+
if (win) {
191+
const [width, height] = win.getSize();
192+
_event.reply("get-window-size-reply", { width, height });
193+
}
194+
});
195+
196+
//获取窗口标题
197+
ipcMain.on("get-window-title", (_event) => {
198+
if (win) {
199+
const title = win.getTitle();
200+
_event.reply("get-window-title-reply", title);
201+
}
202+
});
203+
204+
//获取窗口背景色
205+
ipcMain.on("get-window-background-color", (_event) => {
206+
if (win) {
207+
const color = win.getBackgroundColor();
208+
_event.reply("get-window-background-color-reply", color);
209+
}
210+
});
211+
212+
//获取窗口透明度
213+
ipcMain.on("get-window-opacity", (_event) => {
214+
if (win) {
215+
const opacity = win.getOpacity();
216+
_event.reply("get-window-opacity-reply", opacity);
217+
}
218+
});
219+
220+
//获取窗口是否可见
221+
ipcMain.on("is-window-visible", (_event) => {
222+
if (win) {
223+
const visible = win.isVisible();
224+
_event.reply("is-window-visible-reply", visible);
225+
}
226+
});
227+
228+
//获取窗口是否最大化
229+
ipcMain.on("is-window-maximized", (_event) => {
230+
if (win) {
231+
const maximized = win.isMaximized();
232+
_event.reply("is-window-maximized-reply", maximized);
233+
}
234+
});
235+
236+
//获取窗口是否最小化
237+
ipcMain.on("is-window-minimized", (_event) => {
238+
if (win) {
239+
const minimized = win.isMinimized();
240+
_event.reply("is-window-minimized-reply", minimized);
241+
}
242+
});
243+
244+
245+
//获取窗口是否全屏
246+
ipcMain.on("is-window-full-screen", (_event) => {
247+
if (win) {
248+
const fullScreen = win.isFullScreen();
249+
_event.reply("is-window-full-screen-reply", fullScreen);
250+
}
251+
});
252+
253+
254+
//获取窗口是否可以改变大小
255+
ipcMain.on("is-window-resizable", (_event) => {
256+
if (win) {
257+
const resizable = win.isResizable();
258+
_event.reply("is-window-resizable-reply", resizable);
259+
}
260+
});
261+
262+
//获取窗口是否在所有工作区可见
263+
ipcMain.on("is-window-visible-on-all-workspaces", (_event) => {
264+
if (win) {
265+
const visibleOnAllWorkspaces = win.isVisibleOnAllWorkspaces();
266+
_event.reply("is-window-visible-on-all-workspaces-reply", visibleOnAllWorkspaces);
267+
}
268+
});
269+
270+
271+
//获取窗口是否置顶
272+
ipcMain.on("is-window-always-on-top", (_event) => {
273+
if (win) {
274+
const alwaysOnTop = win.isAlwaysOnTop();
275+
_event.reply("is-window-always-on-top-reply", alwaysOnTop);
276+
}
277+
});
278+
279+
//获取窗口是否可移动
280+
ipcMain.on("is-window-movable", (_event) => {
281+
if (win) {
282+
const movable = win.isMovable();
283+
_event.reply("is-window-movable-reply", movable);
284+
}
285+
});
286+
287+
288+
//获取窗口是否可最小化
289+
ipcMain.on("is-window-minimizable", (_event) => {
290+
if (win) {
291+
const minimizable = win.isMinimizable();
292+
_event.reply("is-window-minimizable-reply", minimizable);
293+
}
294+
});
295+
296+
//获取窗口是否可最大化
297+
ipcMain.on("is-window-maximizable", (_event) => {
298+
if (win) {
299+
const maximizable = win.isMaximizable();
300+
_event.reply("is-window-maximizable-reply", maximizable);
301+
}
302+
});
303+
304+
//获取窗口是否可关闭
305+
ipcMain.on("is-window-closable", (_event) => {
306+
if (win) {
307+
const closable = win.isClosable();
308+
_event.reply("is-window-closable-reply", closable);
309+
}
310+
});
311+
312+
//获取窗口是否在任务栏中显示
313+
ipcMain.on("is-window-menu-bar-visible", (_event) => {
314+
if (win) {
315+
const menuBarVisible = win.isMenuBarVisible();
316+
_event.reply("is-window-menu-bar-visible-reply", menuBarVisible);
317+
}
318+
});
319+
}

0 commit comments

Comments
 (0)