Skip to content

Commit 90e198d

Browse files
authored
Merge pull request #1982 from mikeller/fix_link_opening
2 parents 555234d + 732cccc commit 90e198d

File tree

6 files changed

+113
-120
lines changed

6 files changed

+113
-120
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
"app": {
1515
"background": {
16-
"scripts": ["js/eventPage.js"],
16+
"scripts": ["js/chromeAppEventPage.js"],
1717
"persistent": false
1818
}
1919
},

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "betaflight-configurator",
33
"description": "Crossplatform configuration tool for Betaflight flight control system.",
44
"version": "10.7.0",
5-
"main": "main_nwjs.html",
6-
"bg-script": "js/eventPage.js",
5+
"main": "main.html",
6+
"chromium-args" : "--disable-features=nw2",
77
"default_locale": "en",
88
"scripts": {
99
"start": "gulp debug",
@@ -12,8 +12,10 @@
1212
"test": "karma start test/karma.conf.js"
1313
},
1414
"window": {
15-
"show": false,
16-
"icon": "images/bf_icon_128.png"
15+
"icon": "images/bf_icon_128.png",
16+
"id": "main-window",
17+
"min_width": 1024,
18+
"min_height": 550
1719
},
1820
"repository": {
1921
"type": "git",

src/js/chromeAppEventPage.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
If an id is also specified and a window with a matching id has been shown before, the remembered bounds of the window will be used instead.
3+
*/
4+
'use strict';
5+
6+
function startApplication() {
7+
chrome.app.window.create('main.html', {
8+
id: 'main-window',
9+
frame: 'chrome',
10+
innerBounds: {
11+
minWidth: 1024,
12+
minHeight: 550,
13+
},
14+
}, function (createdWindow) {
15+
if (getChromeVersion() >= 54) {
16+
createdWindow.icon = 'images/bf_icon_128.png';
17+
}
18+
});
19+
}
20+
21+
chrome.app.runtime.onLaunched.addListener(startApplication);
22+
23+
function getChromeVersion () {
24+
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
25+
26+
return raw ? parseInt(raw[2], 10) : false;
27+
}

src/js/eventPage.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/js/main.js

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,76 @@ function setupAnalytics(result) {
8080

8181
analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppStart', { sessionControl: 'start' });
8282

83-
function sendCloseEvent() {
84-
analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' });
85-
}
83+
$('.connect_b a.connect').removeClass('disabled');
84+
$('.firmware_b a.flash').removeClass('disabled');
85+
}
8686

87-
if (GUI.isNWJS()) {
88-
GUI.nwGui.Window.getAll(function (windows) {
89-
windows.forEach(function (win) {
90-
win.on('close', function () {
91-
sendCloseEvent();
87+
function closeSerial() {
88+
// automatically close the port when application closes
89+
const connectionId = serial.connectionId;
9290

93-
this.close(true);
94-
});
95-
win.on('new-win-policy', function(frame, url, policy) {
96-
// do not open the window
97-
policy.ignore();
98-
// and open it in external browser
99-
GUI.nwGui.Shell.openExternal(url);
91+
if (connectionId && CONFIGURATOR.connectionValid) {
92+
// code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
93+
// sending exit command just in case the cli tab was open.
94+
// reset motors to default (mincommand)
95+
96+
let bufferOut = new ArrayBuffer(5),
97+
bufView = new Uint8Array(bufferOut);
98+
99+
bufView[0] = 0x65; // e
100+
bufView[1] = 0x78; // x
101+
bufView[2] = 0x69; // i
102+
bufView[3] = 0x74; // t
103+
bufView[4] = 0x0D; // enter
104+
105+
chrome.serial.send(connectionId, bufferOut, function () {
106+
console.log('Send exit');
107+
});
108+
109+
setTimeout(function() {
110+
bufferOut = new ArrayBuffer(22);
111+
bufView = new Uint8Array(bufferOut);
112+
let checksum = 0;
113+
114+
bufView[0] = 36; // $
115+
bufView[1] = 77; // M
116+
bufView[2] = 60; // <
117+
bufView[3] = 16; // data length
118+
bufView[4] = 214; // MSP_SET_MOTOR
119+
120+
checksum = bufView[3] ^ bufView[4];
121+
122+
for (let i = 0; i < 16; i += 2) {
123+
bufView[i + 5] = MOTOR_CONFIG.mincommand & 0x00FF;
124+
bufView[i + 6] = MOTOR_CONFIG.mincommand >> 8;
125+
126+
checksum ^= bufView[i + 5];
127+
checksum ^= bufView[i + 6];
128+
}
129+
130+
bufView[5 + 16] = checksum;
131+
132+
chrome.serial.send(connectionId, bufferOut, function () {
133+
chrome.serial.disconnect(connectionId, function (result) {
134+
console.log(`SERIAL: Connection closed - ${result}`);
100135
});
101136
});
137+
}, 100);
138+
} else if (connectionId) {
139+
chrome.serial.disconnect(connectionId, function (result) {
140+
console.log(`SERIAL: Connection closed - ${result}`);
102141
});
103-
} else if (!GUI.isOther()) {
104-
// Looks like we're in Chrome - but the event does not actually get fired
105-
chrome.runtime.onSuspend.addListener(sendCloseEvent);
106142
}
143+
}
107144

108-
$('.connect_b a.connect').removeClass('disabled');
109-
$('.firmware_b a.flash').removeClass('disabled');
145+
function closeHandler() {
146+
this.hide();
147+
148+
analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' });
149+
150+
closeSerial();
151+
152+
this.close(true);
110153
}
111154

112155
//Process to execute to real start the app
@@ -119,6 +162,22 @@ function startProcess() {
119162
chromeVersion: window.navigator.appVersion.replace(/.*Chrome\/([0-9.]*).*/, "$1"),
120163
configuratorVersion: CONFIGURATOR.version }));
121164

165+
if (GUI.isNWJS()) {
166+
let nwWindow = GUI.nwGui.Window.get();
167+
nwWindow.on('new-win-policy', function(frame, url, policy) {
168+
// do not open the window
169+
policy.ignore();
170+
// and open it in external browser
171+
GUI.nwGui.Shell.openExternal(url);
172+
});
173+
nwWindow.on('close', closeHandler);
174+
} else if (!GUI.isOther()) {
175+
chrome.app.window.onClosed.addListener(closeHandler);
176+
// This event does not actually get fired:
177+
chrome.runtime.onSuspend.addListener(closeHandler);
178+
}
179+
180+
$('.connect_b a.connect').removeClass('disabled');
122181
$('#logo .version').text(CONFIGURATOR.version);
123182
updateStatusBarVersion();
124183
updateTopBarVersion();

src/main_nwjs.html

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)