|
1 | | -import { Application } from '../index.js' |
| 1 | +import { Application, WebviewApplicationEvent } from '../index.js'; |
2 | 2 |
|
3 | | -const app1 = new Application(); |
4 | | -const window1 = app1.createBrowserWindow() |
5 | | -const webview1 = window1.createWebview({ url: 'https://nodejs.org' }); |
6 | | -const app2 = new Application(); |
7 | | -const window2 = app2.createBrowserWindow() |
8 | | -const webview2 = window2.createWebview({ url: 'https://wikipedia.org' }); |
| 3 | +// Single Application instance - GTK is a singleton, so we can only have one |
| 4 | +const app = new Application(); |
| 5 | + |
| 6 | +// Set up event handler to track when windows close |
| 7 | +let window1Closed = false; |
| 8 | +let window2Closed = false; |
9 | 9 |
|
10 | | -const poll = () => { |
11 | | - if (app1.runIteration()) { |
12 | | - window1.id; |
13 | | - webview1.id; |
14 | | - setTimeout(poll, 10); |
15 | | - } else { |
16 | | - process.exit(0); |
| 10 | +app.bind((_err, event) => { |
| 11 | + if (event.event === WebviewApplicationEvent.WindowCloseRequested) { |
| 12 | + // Detect which window is closing |
| 13 | + if (!window1Closed) { |
| 14 | + window1Closed = true; |
| 15 | + console.log('Window 1 closed'); |
| 16 | + } else if (!window2Closed) { |
| 17 | + window2Closed = true; |
| 18 | + console.log('Window 2 closed'); |
17 | 19 | } |
18 | | - if (app2.runIteration()) { |
19 | | - window2.id; |
20 | | - webview2.id; |
21 | | - setTimeout(poll, 10); |
22 | | - } else { |
23 | | - process.exit(0); |
| 20 | + |
| 21 | + // If all windows are closed, exit the application |
| 22 | + if (window1Closed && window2Closed) { |
| 23 | + console.log('All windows closed, exiting application...'); |
| 24 | + app.exit(); |
24 | 25 | } |
25 | | -}; |
26 | | -poll(); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +// Create first window and webview |
| 30 | +const window1 = app.createBrowserWindow({ |
| 31 | + title: 'Window 1 - Node.js', |
| 32 | + width: 800, |
| 33 | + height: 600, |
| 34 | + x: 100, |
| 35 | + y: 100, |
| 36 | +}); |
| 37 | +const webview1 = window1.createWebview({ url: 'https://nodejs.org' }); |
| 38 | + |
| 39 | +// Create second window and webview from the same Application |
| 40 | +const window2 = app.createBrowserWindow({ |
| 41 | + title: 'Window 2 - Wikipedia', |
| 42 | + width: 800, |
| 43 | + height: 600, |
| 44 | + x: 920, |
| 45 | + y: 100, |
| 46 | +}); |
| 47 | +const webview2 = window2.createWebview({ url: 'https://wikipedia.org' }); |
| 48 | + |
| 49 | +console.log('Running application with 2 windows...'); |
| 50 | +console.log('Close both windows to exit the application.'); |
| 51 | + |
| 52 | +// Run the application - this will block and keep processing events |
| 53 | +// until all windows are closed and app.exit() is called |
| 54 | +app.run(); |
0 commit comments