-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmenu-system.mjs
More file actions
296 lines (275 loc) · 8.12 KB
/
menu-system.mjs
File metadata and controls
296 lines (275 loc) · 8.12 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { Application, initMenuSystem, WebviewApplicationEvent } from '../index.js';
// Initialize menu system (recommended, especially for macOS)
initMenuSystem();
const app = new Application();
// Set up menu event handler
app.bind((event) => {
if (event.event === WebviewApplicationEvent.CustomMenuClick) {
const menuEvent = event.customMenuEvent;
console.log(`Menu item clicked: "${menuEvent.id}" from window ${menuEvent.windowId}`);
// Handle specific menu items
switch (menuEvent.id) {
case 'new':
console.log('📄 Creating new document...');
break;
case 'open':
console.log('📂 Opening file...');
break;
case 'save':
console.log('💾 Saving file...');
break;
case 'about':
console.log('ℹ️ About this application...');
break;
case 'preferences':
console.log('⚙️ Opening preferences...');
break;
case 'quit':
console.log('👋 Goodbye!');
app.exit();
break;
case 'reload':
console.log('🔄 Reloading webview...');
// In a real app, you would reload the webview here
break;
case 'devtools':
console.log('🔧 Opening developer tools...');
// In a real app, you would toggle devtools here
break;
default:
console.log(`Unhandled menu item: ${menuEvent.id}`);
}
} else if (event.event === WebviewApplicationEvent.ApplicationCloseRequested) {
console.log('Application close requested');
app.exit();
} else if (event.event === WebviewApplicationEvent.WindowCloseRequested) {
console.log('Window close requested');
}
});
// Set up comprehensive application menu
app.setMenu({
items: [
// File menu
{
label: "File",
submenu: {
items: [
{
id: "new",
label: "New",
accelerator: "CmdOrCtrl+N",
enabled: true
},
{
id: "open",
label: "Open...",
accelerator: "CmdOrCtrl+O"
},
{ role: "separator" },
{
id: "save",
label: "Save",
accelerator: "CmdOrCtrl+S"
},
{
id: "save-as",
label: "Save As...",
accelerator: "CmdOrCtrl+Shift+S"
},
{ role: "separator" },
{
id: "quit",
label: "Quit",
accelerator: "CmdOrCtrl+Q"
}
]
}
},
// Edit menu with predefined roles
{
label: "Edit",
submenu: {
items: [
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
{ role: "selectall" },
{ role: "separator" },
{
id: "preferences",
label: "Preferences...",
accelerator: "CmdOrCtrl+,"
}
]
}
},
// View menu
{
label: "View",
submenu: {
items: [
{
id: "reload",
label: "Reload",
accelerator: "CmdOrCtrl+R"
},
{
id: "devtools",
label: "Developer Tools",
accelerator: "F12"
},
{ role: "separator" },
{
label: "Zoom",
submenu: {
items: [
{
id: "zoom-in",
label: "Zoom In",
accelerator: "CmdOrCtrl+Plus"
},
{
id: "zoom-out",
label: "Zoom Out",
accelerator: "CmdOrCtrl+-"
},
{
id: "zoom-reset",
label: "Actual Size",
accelerator: "CmdOrCtrl+0"
}
]
}
}
]
}
},
// Help menu
{
label: "Help",
submenu: {
items: [
{
id: "about",
label: "About Menu Example"
},
{
id: "docs",
label: "Documentation",
accelerator: "F1"
}
]
}
}
]
});
console.log('🎯 Menu System Example');
console.log('📋 Try the following:');
console.log(' • Click on menu items to see event handling');
console.log(' • Use keyboard shortcuts (Ctrl+N, Ctrl+O, etc.)');
console.log(' • Try copy/paste with Ctrl+C/Ctrl+V');
console.log(' • Use Ctrl+Q or File > Quit to exit');
console.log('');
// Create main window
const window = app.createBrowserWindow({
title: "Menu System Example",
width: 800,
height: 600
});
// Create webview with example content
const webview = window.createWebview({
html: `<!DOCTYPE html>
<html>
<head>
<title>Menu System Example</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
line-height: 1.6;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
min-height: 500px;
}
.card {
background: rgba(255, 255, 255, 0.1);
border-radius: 15px;
padding: 30px;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
h1 { color: #fff; margin-top: 0; }
h2 { color: #f0f0f0; }
.menu-item {
background: rgba(255, 255, 255, 0.2);
padding: 10px 15px;
margin: 5px 0;
border-radius: 8px;
border: 1px solid rgba(255, 255, 255, 0.3);
}
.shortcut {
float: right;
opacity: 0.8;
font-size: 0.9em;
}
textarea {
width: 100%;
height: 100px;
margin: 10px 0;
padding: 10px;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.3);
background: rgba(255, 255, 255, 0.1);
color: white;
font-family: inherit;
}
textarea::placeholder { color: rgba(255, 255, 255, 0.7); }
</style>
</head>
<body>
<div class="card">
<h1>🎯 Menu System Example</h1>
<p>This example demonstrates the cross-platform menu system in WebviewJS.</p>
<h2>📋 Available Menus:</h2>
<div class="menu-item">
<strong>File Menu:</strong> New, Open, Save, Quit
<div class="shortcut">Ctrl+N, Ctrl+O, Ctrl+S, Ctrl+Q</div>
</div>
<div class="menu-item">
<strong>Edit Menu:</strong> Cut, Copy, Paste, Select All
<div class="shortcut">Ctrl+X, Ctrl+C, Ctrl+V, Ctrl+A</div>
</div>
<div class="menu-item">
<strong>View Menu:</strong> Reload, Developer Tools, Zoom
<div class="shortcut">Ctrl+R, F12, Ctrl+/-, Ctrl+0</div>
</div>
<div class="menu-item">
<strong>Help Menu:</strong> About, Documentation
<div class="shortcut">F1</div>
</div>
<h2>✨ Try It Out:</h2>
<p>1. Click on the menu items above to trigger events</p>
<p>2. Use keyboard shortcuts for quick access</p>
<p>3. Test copy/paste with the text area below:</p>
<textarea placeholder="Type some text here and try Ctrl+A to select all, then Ctrl+C to copy..."></textarea>
<p><strong>Check the console</strong> to see menu events being handled!</p>
<h2>🌟 Features Demonstrated:</h2>
<ul>
<li>✅ Cross-platform menu system</li>
<li>✅ Keyboard accelerators/shortcuts</li>
<li>✅ Nested submenus</li>
<li>✅ Predefined menu roles (copy, paste, etc.)</li>
<li>✅ Custom menu items with IDs</li>
<li>✅ Menu event handling and dispatch</li>
<li>✅ Menu separators</li>
<li>✅ Enabled/disabled menu items</li>
</ul>
</div>
</body>
</html>`
});
// Run the application
console.log('🚀 Starting application with menu system...\n');
app.run();