-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmenu.js
187 lines (177 loc) · 5.09 KB
/
menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const { Menu, screen, dialog } = require("electron");
function createMenu(mainWindow) {
// Define your menu template
const template = [
{
label: "File", // Default file menu
role: "fileMenu",
},
{
label: "Edit", // Default edit menu
role: "editMenu",
},
{
label: "View", // Default view menu
role: "viewMenu",
},
{
label: "Window", // Combined window menu
submenu: [
{
label: "Position",
submenu: [
{
label: "Left",
submenu: [
{
label: "Top Left",
click: () => positionWindow(mainWindow, "left", "top"),
},
{
label: "Center Left",
click: () => positionWindow(mainWindow, "left", "center"),
},
{
label: "Bottom Left",
click: () => positionWindow(mainWindow, "left", "bottom"),
},
],
},
{
label: "Center",
submenu: [
{
label: "Top Center",
click: () => positionWindow(mainWindow, "center", "top"),
},
{
label: "Center",
click: () => positionWindow(mainWindow, "center", "center"),
},
{
label: "Bottom Center",
click: () => positionWindow(mainWindow, "center", "bottom"),
},
],
},
{
label: "Right",
submenu: [
{
label: "Top Right",
click: () => positionWindow(mainWindow, "right", "top"),
},
{
label: "Center Right",
click: () => positionWindow(mainWindow, "right", "center"),
},
{
label: "Bottom Right",
click: () => positionWindow(mainWindow, "right", "bottom"),
},
],
},
{ type: "separator" },
{
label: "Custom Offset",
click: () => editOffset(mainWindow),
},
],
},
{ type: "separator" },
{
label: "Minimize",
role: "minimize",
},
{
label: "Close",
role: "close",
},
],
},
{
label: "Help", // Default help menu
role: "help",
submenu: [
{
label: "Learn More",
click: () => {
require("electron").shell.openExternal("https://electronjs.org");
},
},
],
},
];
// Create the menu from the template
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
function positionWindow(mainWindow, horizontal, vertical) {
try {
const { width, height } = mainWindow.getBounds();
const { workAreaSize } = screen.getPrimaryDisplay();
let x = 0;
let y = 0;
// Calculate horizontal position
if (horizontal === "center") {
x = (workAreaSize.width - width) / 2;
} else if (horizontal === "left") {
x = 0;
} else if (horizontal === "right") {
x = workAreaSize.width - width;
}
// Calculate vertical position
if (vertical === "center") {
y = (workAreaSize.height - height) / 2;
} else if (vertical === "top") {
y = 0;
} else if (vertical === "bottom") {
y = workAreaSize.height - height;
}
// Set new window position
mainWindow.setPosition(Math.floor(x), Math.floor(y));
} catch (error) {
console.error("Error while positioning the window:", error);
mainWindow.webContents.executeJavaScript(
`alert('Failed to position the window: ${error.message}');`
);
}
}
function editOffset(mainWindow) {
try {
const offsetDialogOptions = {
type: "question",
buttons: ["OK"],
title: "Custom Offset",
message: "Enter X and Y offsets (e.g., 100, 150)",
detail: "Use the format: x,y",
defaultId: 0,
noLink: true,
};
// Show a dialog to get the offset values
dialog.showMessageBox(mainWindow, offsetDialogOptions).then((response) => {
if (response.response === 0) {
mainWindow.webContents
.executeJavaScript(
"prompt('Enter custom X and Y offsets (format: x,y):')"
)
.then((input) => {
const [x, y] = input.split(",").map(Number);
if (!isNaN(x) && !isNaN(y)) {
mainWindow.setPosition(x, y);
} else {
mainWindow.webContents.executeJavaScript(
`alert('Invalid offset values. Please enter numbers in the format: x,y.');`
);
}
});
}
});
} catch (error) {
console.error("Error while editing offset:", error);
mainWindow.webContents.executeJavaScript(
`alert('Failed to edit the offset: ${error.message}');`
);
}
}
module.exports = { createMenu };