Skip to content

Commit

Permalink
Fixed opening of links in an external app.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeller committed Apr 25, 2020
1 parent aa98588 commit 410fa45
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 120 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

"app": {
"background": {
"scripts": ["js/eventPage.js"],
"scripts": ["js/chromeAppEventPage.js"],
"persistent": false
}
},
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "betaflight-configurator",
"description": "Crossplatform configuration tool for Betaflight flight control system.",
"version": "10.7.0",
"main": "main_nwjs.html",
"bg-script": "js/eventPage.js",
"main": "main.html",
"default_locale": "en",
"scripts": {
"start": "gulp debug",
Expand All @@ -12,8 +11,13 @@
"test": "karma start test/karma.conf.js"
},
"window": {
"show": false,
"icon": "images/bf_icon_128.png"
"icon": "images/bf_icon_128.png",
"id": "main-window",
"frame": "chrome",
"innerBounds": {
"minWidth": 1024,
"minHeight": 550
}
},
"repository": {
"type": "git",
Expand Down
27 changes: 27 additions & 0 deletions src/js/chromeAppEventPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
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.
*/
'use strict';

function startApplication() {
chrome.app.window.create('main.html', {
id: 'main-window',
frame: 'chrome',
innerBounds: {
minWidth: 1024,
minHeight: 550,
},
}, function (createdWindow) {
if (getChromeVersion() >= 54) {
createdWindow.icon = 'images/bf_icon_128.png';
}
});
}

chrome.app.runtime.onLaunched.addListener(startApplication);

function getChromeVersion () {
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);

return raw ? parseInt(raw[2], 10) : false;
}
89 changes: 0 additions & 89 deletions src/js/eventPage.js

This file was deleted.

102 changes: 82 additions & 20 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,76 @@ function setupAnalytics(result) {

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

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

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

this.close(true);
});
win.on('new-win-policy', function(frame, url, policy) {
// do not open the window
policy.ignore();
// and open it in external browser
GUI.nwGui.Shell.openExternal(url);
if (connectionId && CONFIGURATOR.connectionValid) {
// code below is handmade MSP message (without pretty JS wrapper), it behaves exactly like MSP.send_message
// sending exit command just in case the cli tab was open.
// reset motors to default (mincommand)

let bufferOut = new ArrayBuffer(5),
bufView = new Uint8Array(bufferOut);

bufView[0] = 0x65; // e
bufView[1] = 0x78; // x
bufView[2] = 0x69; // i
bufView[3] = 0x74; // t
bufView[4] = 0x0D; // enter

chrome.serial.send(connectionId, bufferOut, function () {
console.log('Send exit');
});

setTimeout(function() {
bufferOut = new ArrayBuffer(22);
bufView = new Uint8Array(bufferOut);
let checksum = 0;

bufView[0] = 36; // $
bufView[1] = 77; // M
bufView[2] = 60; // <
bufView[3] = 16; // data length
bufView[4] = 214; // MSP_SET_MOTOR

checksum = bufView[3] ^ bufView[4];

for (let i = 0; i < 16; i += 2) {
bufView[i + 5] = MOTOR_CONFIG.mincommand & 0x00FF;
bufView[i + 6] = MOTOR_CONFIG.mincommand >> 8;

checksum ^= bufView[i + 5];
checksum ^= bufView[i + 6];
}

bufView[5 + 16] = checksum;

chrome.serial.send(connectionId, bufferOut, function () {
chrome.serial.disconnect(connectionId, function (result) {
console.log(`SERIAL: Connection closed - ${result}`);
});
});
}, 100);
} else if (connectionId) {
chrome.serial.disconnect(connectionId, function (result) {
console.log(`SERIAL: Connection closed - ${result}`);
});
} else if (!GUI.isOther()) {
// Looks like we're in Chrome - but the event does not actually get fired
chrome.runtime.onSuspend.addListener(sendCloseEvent);
}
}

$('.connect_b a.connect').removeClass('disabled');
$('.firmware_b a.flash').removeClass('disabled');
function closeHandler() {
this.hide();

analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' });

closeSerial();

this.close(true);
}

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

if (GUI.isNWJS()) {
GUI.nwGui.Window.getAll(function (windows) {
windows.forEach(function (window) {
window.on('new-win-policy', function(frame, url, policy) {
// do not open the window
policy.ignore();
// and open it in external browser
GUI.nwGui.Shell.openExternal(url);
});
window.on('close', closeHandler);
});
});
} else if (!GUI.isOther()) {
chrome.app.window.onClosed.addListener(closeHandler);
// This event does not actually get fired:
chrome.runtime.onSuspend.addListener(closeHandler);
}

$('.connect_b a.connect').removeClass('disabled');
$('#logo .version').text(CONFIGURATOR.version);
updateStatusBarVersion();
updateTopBarVersion();
Expand Down
6 changes: 0 additions & 6 deletions src/main_nwjs.html

This file was deleted.

0 comments on commit 410fa45

Please sign in to comment.