-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwindow-menus.mjs
More file actions
247 lines (232 loc) · 6.19 KB
/
window-menus.mjs
File metadata and controls
247 lines (232 loc) · 6.19 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Application, initMenuSystem, WebviewApplicationEvent } from '../index.js';
// Initialize menu system
initMenuSystem();
const app = new Application();
// Handle menu events
app.bind((event) => {
if (event.event === WebviewApplicationEvent.CustomMenuClick) {
const menuEvent = event.customMenuEvent;
console.log(`Menu "${menuEvent.id}" clicked on window ${menuEvent.windowId}`);
switch (menuEvent.id) {
case 'close-window':
console.log('Closing window...');
// In a real app, you would close the specific window
break;
case 'window-1-action':
console.log('Window 1 specific action!');
break;
case 'window-2-action':
console.log('Window 2 specific action!');
break;
case 'global-action':
console.log('Global action from any window!');
break;
case 'quit':
console.log('Quitting application...');
app.exit();
break;
}
}
});
// Set a global application menu
app.setMenu({
items: [
{
label: "App",
submenu: {
items: [
{ id: "global-action", label: "Global Action", accelerator: "CmdOrCtrl+G" },
{ role: "separator" },
{ id: "quit", label: "Quit", accelerator: "CmdOrCtrl+Q" }
]
}
}
]
});
console.log('🪟 Window-Specific Menu Example');
console.log('Creating two windows with different menus...\n');
// Create first window with custom menu
const window1 = app.createBrowserWindow({
title: "Window 1 - Custom Menu",
width: 400,
height: 300,
x: 100,
y: 100,
menu: {
items: [
{
label: "Window 1",
submenu: {
items: [
{ id: "window-1-action", label: "Window 1 Action", accelerator: "Ctrl+1" },
{ role: "separator" },
{ id: "close-window", label: "Close Window", accelerator: "Ctrl+W" }
]
}
},
{
label: "Edit",
submenu: {
items: [
{ role: "copy" },
{ role: "paste" }
]
}
}
]
}
});
window1.createWebview({
html: `<!DOCTYPE html>
<html>
<head>
<title>Window 1</title>
<style>
body {
font-family: system-ui, sans-serif;
padding: 20px;
background: linear-gradient(45deg, #ff6b6b, #ee5a24);
color: white;
text-align: center;
}
.card {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
backdrop-filter: blur(10px);
}
</style>
</head>
<body>
<div class="card">
<h1>🪟 Window 1</h1>
<p><strong>Custom Menu:</strong></p>
<p>• Window 1 → Window 1 Action (Ctrl+1)</p>
<p>• Window 1 → Close Window (Ctrl+W)</p>
<p>• Edit → Copy, Paste</p>
<br>
<p>This window has its <strong>own menu</strong>!</p>
<p>Try the menu items above 👆</p>
</div>
</body>
</html>`
});
// Create second window with different custom menu
const window2 = app.createBrowserWindow({
title: "Window 2 - Different Menu",
width: 400,
height: 300,
x: 520,
y: 100,
menu: {
items: [
{
label: "Window 2",
submenu: {
items: [
{ id: "window-2-action", label: "Window 2 Action", accelerator: "Ctrl+2" },
{ role: "separator" },
{ id: "close-window", label: "Close Window", accelerator: "Ctrl+W" }
]
}
},
{
label: "Tools",
submenu: {
items: [
{ role: "selectall" },
{ role: "separator" },
{ id: "tool-action", label: "Special Tool" }
]
}
}
]
}
});
window2.createWebview({
html: `<!DOCTYPE html>
<html>
<head>
<title>Window 2</title>
<style>
body {
font-family: system-ui, sans-serif;
padding: 20px;
background: linear-gradient(45deg, #4834d4, #686de0);
color: white;
text-align: center;
}
.card {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
backdrop-filter: blur(10px);
}
</style>
</head>
<body>
<div class="card">
<h1>🪟 Window 2</h1>
<p><strong>Different Menu:</strong></p>
<p>• Window 2 → Window 2 Action (Ctrl+2)</p>
<p>• Window 2 → Close Window (Ctrl+W)</p>
<p>• Tools → Select All, Special Tool</p>
<br>
<p>This window has a <strong>different menu</strong>!</p>
<p>Compare with Window 1 👈</p>
</div>
</body>
</html>`
});
// Create third window that uses the global menu (no custom menu specified)
const window3 = app.createBrowserWindow({
title: "Window 3 - Global Menu",
width: 400,
height: 300,
x: 310,
y: 420,
show_menu: true // Uses global menu
});
window3.createWebview({
html: `<!DOCTYPE html>
<html>
<head>
<title>Window 3</title>
<style>
body {
font-family: system-ui, sans-serif;
padding: 20px;
background: linear-gradient(45deg, #00d2d3, #54a0ff);
color: white;
text-align: center;
}
.card {
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 10px;
backdrop-filter: blur(10px);
}
</style>
</head>
<body>
<div class="card">
<h1>🌍 Window 3</h1>
<p><strong>Global Menu:</strong></p>
<p>• App → Global Action (Ctrl+G)</p>
<p>• App → Quit (Ctrl+Q)</p>
<br>
<p>This window uses the <strong>global menu</strong>!</p>
<p>Set with <code>app.setMenu()</code></p>
</div>
</body>
</html>`
});
// Log menu status for each window
console.log(`Window 1 has menu: ${window1.hasMenu()}`);
console.log(`Window 2 has menu: ${window2.hasMenu()}`);
console.log(`Window 3 has menu: ${window3.hasMenu()}`);
console.log('\n🎯 Try different menu items in each window!');
console.log('Notice how each window responds to different menu items.');
console.log('Use Ctrl+Q to quit the application.\n');
// Run the application
app.run();