2 const {BrowserWindow, app} = require('electron');
3 const IPC_MESSAGE = require('./ipc_message');
4 const WAS_EVENT = require('./was_event');
5 const events = require('./events');
6 const target_debug = require('debug')('TARGET');
7 const webwindow_debug = require('debug')('WEBWINDOW');
10 constructor(webApplication, options) {
11 webwindow_debug('WebWindow : ' + options.path);
12 this.webApplication = webApplication;
13 webApplication.addWebWindow(this);
14 let winopt = this.getBrowserWindowOption();
15 webwindow_debug('winopt: ' + JSON.stringify(winopt));
16 this.mainWindow = new BrowserWindow(winopt);
17 if (process.env.DEBUG || options.devMode) {
18 this.mainWindow.webContents.openDevTools({
22 webwindow_debug('mainWindow id : ' + this.mainWindow.id);
23 this.handleEvents(winopt);
24 this.appID = options.appID;
25 webwindow_debug('this.appID ' + this.appID);
26 this.setUrl(options.path);
28 getBrowserWindowOption() {
34 'subpixel-font-scaling': false,
39 handleEvents(options) {
41 this.mainWindow.on('focus', function() {
42 webwindow_debug('WebWindow : focus');
43 events.emit(WAS_EVENT.RUNTIME.FOCUS, 'web_window', self.mainWindow.id);
45 this.mainWindow.on('blur', function() {
46 webwindow_debug('WebWindow : blur');
47 events.emit(WAS_EVENT.RUNTIME.UNFOCUS, 'web_window', self.mainWindow.id);
49 this.mainWindow.on('maximize', function(event, command, args) {
50 webwindow_debug('WebWindow : maximize');
51 webwindow_debug('WebWindow : maximize event = ' + (JSON.stringify(command)));
52 webwindow_debug('WebWindow : maximize event = ' + (JSON.stringify(args)));
54 this.mainWindow.on('unmaximize', function() {
55 webwindow_debug('WebWindow : unmaximize');
57 this.mainWindow.on('minimize', function() {
58 webwindow_debug('WebWindow : minimize');
60 this.mainWindow.on('restore', function() {
61 webwindow_debug('WebWindow : restore');
63 this.mainWindow.on('resize', function() {
64 webwindow_debug('WebWindow : resize');
66 this.mainWindow.on('move', function() {
67 webwindow_debug('WebWindow : move');
69 this.mainWindow.on('enter-full-screen', function() {
70 webwindow_debug('WebWindow : enter-full-screen');
72 this.mainWindow.on('leave-full-screen', function() {
73 webwindow_debug('WebWindow : leave-full-screen');
75 this.mainWindow.on('enter-html-full-screen', function() {
76 webwindow_debug('WebWindow : enter-html-full-screen');
78 this.mainWindow.on('leave-html-full-screen', function() {
79 webwindow_debug('WebWindow : leave-html-full-screen');
81 this.mainWindow.on('devtools-opened', function() {
82 webwindow_debug('WebWindow : devtools-opened');
84 this.mainWindow.on('devtools-closed', function() {
85 webwindow_debug('WebWindow : devtools-closed');
87 this.mainWindow.on('devtools-focused', function() {
88 webwindow_debug('WebWindow : devtools-focused');
90 this.mainWindow.on('app-command', function() {
91 webwindow_debug('WebWindow : app-command');
93 this.mainWindow.on('page-title-updated', function() {
94 webwindow_debug('WebWindow : page-title-updated');
96 this.mainWindow.on('closed', function() {
97 webwindow_debug('mainWindow closed!');
98 //TODO: should remove the reference to the window
99 self.webApplication.removeWebWindow(self);
101 this.mainWindow.on('responsive', function() {
102 webwindow_debug('WebWindow : responsive');
104 this.mainWindow.on('unresponsive', function() {
105 webwindow_debug('WebWindow : unresponsive');
107 this.mainWindow.on('language-changed', function(event, locale) {
108 webwindow_debug('WebWindow : language-changed event = ' + (JSON.stringify(locale)));
110 this.mainWindow.on('app-on-suspend', function() {
111 webwindow_debug('WebWindow : app-on-suspend');
112 events.emit(WAS_EVENT.WEBAPPLICATION.SUSPEND, 'web_window', self.mainWindow.id);
114 this.mainWindow.on('app-on-resume', function() {
115 webwindow_debug('WebWindow : app-on-resume');
116 events.emit(WAS_EVENT.WEBAPPLICATION.RESUME, 'web_window', self.mainWindow.id);
118 this.mainWindow.webContents.on('crashed', function() {
119 webwindow_debug('WebWindow : webContents crashed');
120 self.webApplication.exit(100);
122 this.mainWindow.webContents.on('did-finish-load', function() {
123 webwindow_debug('WebWindow : webContents did-finish-load');
125 webwindow_debug('WebWindow : browserWindow show options is ',options.show);
128 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FINISH_LOAD, self.mainWindow.id);
130 this.mainWindow.webContents.on('did-fail-load', function(event, errorCode, errorDescription, validatedUrl) {
131 webwindow_debug('WebWindow : webContents did-fail-load');
133 params.errorCode = errorCode;
134 params.errorDescription = errorDescription;
135 params.validatedUrl = validatedUrl;
136 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FAIL_LOAD, self.mainWindow.id, params);
138 this.mainWindow.webContents.on('did-frame-finish-load', function(event, isMainFrame) {
139 webwindow_debug('WebWindow : webContents did-frame-finish-load:');
141 params.isMainFrame = isMainFrame;
142 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_FRAME_FINISH_LOAD, self.mainWindow.id, params);
144 this.mainWindow.webContents.on('did-start-loading', function() {
145 webwindow_debug('WebWindow : webContents did-start-loading');
146 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_START_LOADING, self.mainWindow.id);
148 this.mainWindow.webContents.on('did-stop-loading', function() {
149 webwindow_debug('WebWindow : webContents did-stop-loading');
150 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_STOP_LOADING, self.mainWindow.id);
152 this.mainWindow.webContents.on('did-get-response-details', function(event, status, newUrl, originalUrl, httpResponseCode, requestMethod, referrer, headers) {
153 webwindow_debug('WebWindow : webContents did-get-response-details');
155 params.status = status;
156 params.newUrl = newUrl;
157 params.originalUrl = originalUrl;
158 params.httpResponseCode = httpResponseCode;
159 params.requestMethod = requestMethod;
160 params.referrer = referrer;
161 params.headers = headers;
162 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_RESPONSE_DETAILS, self.mainWindow.id, params);
164 this.mainWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl, isMainFrame, httpResponseCode, requestMethod, referrer, headers) {
165 webwindow_debug('WebWindow : webContents did-get-redirect-request');
167 params.oldUrl = oldUrl;
168 params.newUrl = newUrl;
169 params.isMainFrame = isMainFrame;
170 params.httpResponseCode = httpResponseCode;
171 params.requestMethod = requestMethod;
172 params.referrer = referrer;
173 params.headers = headers;
174 app.emit(IPC_MESSAGE.WEBCONTENTS.DID_GET_REDIRECT_REQUEST, self.mainWindow.id, params);
176 this.mainWindow.webContents.on('dom-ready', function(event) {
177 webwindow_debug('WebWindow : webContents dom-ready');
178 app.emit(IPC_MESSAGE.WEBCONTENTS.DOM_READY, self.mainWindow.id);
180 this.mainWindow.webContents.on('page-favicon-updated', function(event, favicons) {
181 webwindow_debug('WebWindow : webContents page-favicon-updated');
183 params.favicons = favicons;
184 app.emit(IPC_MESSAGE.WEBCONTENTS.PAGE_FAVICON_UPDATED, self.mainWindow.id, params);
186 this.mainWindow.webContents.on('new-window', function(event, url, frameName, disposition, options) {
187 webwindow_debug('WebWindow : webContents new-window');
190 params.frameName = frameName;
191 params.disposition = disposition;
192 params.options = options;
193 app.emit(IPC_MESSAGE.WEBCONTENTS.NEW_WINDOW, self.mainWindow.id, params);
195 this.mainWindow.webContents.on('will-navigate', function(event, url) {
196 webwindow_debug('WebWindow : webContents will-navigate');
198 this.mainWindow.webContents.on('crashed', function() {
199 webwindow_debug('WebWindow : webContents crashed');
200 app.emit(IPC_MESSAGE.WEBCONTENTS.CRASHED, self.mainWindow.id);
202 this.mainWindow.webContents.on('plugin-crashed', function(event, name, version) {
203 webwindow_debug('WebWindow : webContents plugin-crashed');
206 params.version = version;
207 app.emit(IPC_MESSAGE.WEBCONTENTS.PLUGIN_CRASHED, self.mainWindow.id, params);
209 this.mainWindow.webContents.on('destroyed', function() {
210 webwindow_debug('WebWindow : webContents destroyed');
211 app.emit(IPC_MESSAGE.WEBCONTENTS.DESTROYED, self.mainWindow.id);
215 if (path && (path.trim() != '')) {
216 this.mainWindow.loadURL(path);
218 const {pwrt} = require('electron');
219 this.mainWindow.loadURL(pwrt.getPath());
223 this.mainWindow.show();
226 this.mainWindow.close();
229 this.mainWindow.focus();
232 this.mainWindow.minimize();
235 this.mainWindow.maximize();
238 this.mainWindow.restore();
241 this.mainWindow.isFocused();
244 this.mainWindow.isMinimized();
246 isWebWindowFocused() {
247 this.mainWindow.isWebWindowFocused();
250 this.mainWindow.restart();
253 this.mainWindow.toggleDevTools();
256 this.mainWindow.webContents.send(event, data);
259 return this.mainWindow.getLocale();
262 module.exports = WebWindow;