-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathclose-example.mjs
More file actions
92 lines (81 loc) · 2.46 KB
/
close-example.mjs
File metadata and controls
92 lines (81 loc) · 2.46 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
import { Application, WebviewApplicationEvent } from '../index.js'
const app = new Application();
// Create a browser window with a webview
const browserWindow = app.createBrowserWindow({
title: 'Close Example',
width: 800,
height: 600,
});
const webview = browserWindow.createWebview({
html: `
<!DOCTYPE html>
<html>
<head>
<title>Close Example</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
display: flex;
flex-direction: column;
gap: 10px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Close Example</h1>
<p>This example demonstrates how to close the application gracefully.</p>
<button onclick="closeApp()">Close Application</button>
<button onclick="reloadWebview()">Reload Webview</button>
<script>
function closeApp() {
// This will trigger the ApplicationCloseRequested event
// and clean up all resources including temp folders
window.close();
}
function reloadWebview() {
// This will reload the webview
location.reload();
}
</script>
</body>
</html>
`,
});
// Set up event handler for application events
// You can use either onEvent() or bind() - they are equivalent
app.bind((event) => {
console.log('Application event:', event.event);
if (event.event === WebviewApplicationEvent.WindowCloseRequested) {
console.log('Window close requested');
// You can perform cleanup here before the window closes
// For example: save data, close connections, etc.
}
if (event.event === WebviewApplicationEvent.ApplicationCloseRequested) {
console.log('Application close requested');
// Perform final cleanup before the application exits
// This is where temp folders will be cleaned up
}
});
// Example: Programmatically close the application after 5 seconds
// setTimeout(() => {
// console.log('Closing application programmatically...');
// app.exit();
// }, 5000);
// Example: Programmatically hide the window
// setTimeout(() => {
// console.log('Hiding window...');
// browserWindow.hide();
// }, 3000);
// Example: Programmatically show the window
// setTimeout(() => {
// console.log('Showing window...');
// browserWindow.show();
// }, 4000);
// Run the application
await app.run();