Skip to content

Commit b3e8f30

Browse files
committed
fix multiple window example
1 parent 4fb984c commit b3e8f30

2 files changed

Lines changed: 80 additions & 22 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ npm install @webviewjs/webview
2727
| armv7-linux-androideabi ||
2828
| aarch64-pc-windows-msvc ||
2929

30+
# Important Notes
31+
32+
## GTK Singleton Limitation on Linux
33+
34+
On Linux, GTK is implemented as a singleton, which means you can only create **one `Application` instance**. If you try to create multiple `Application` instances, the second one will fail with an error like:
35+
36+
```
37+
Failed to initialize gtk backend!: Ya existe un objeto exportado para la interfaz org.gtk.Application
38+
```
39+
40+
**Correct approach for multiple windows:**
41+
42+
```js
43+
// ✅ Correct: Single Application with multiple BrowserWindows
44+
const app = new Application();
45+
const window1 = app.createBrowserWindow();
46+
const window2 = app.createBrowserWindow();
47+
app.run();
48+
```
49+
50+
**Incorrect approach:**
51+
52+
```js
53+
// ❌ Incorrect: Multiple Application instances (will fail on Linux)
54+
const app1 = new Application();
55+
const app2 = new Application(); // This will crash!
56+
```
57+
58+
This limitation is specific to Linux. On Windows and macOS, you can create multiple Application instances, though it's still recommended to use a single Application instance for consistency.
59+
3060
# Examples
3161

3262
## Load external url

examples/multiple.ts

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
1-
import { Application } from '../index.js'
1+
import { Application, WebviewApplicationEvent } from '../index.js';
22

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;
99

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');
1719
}
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();
2425
}
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

Comments
 (0)