Skip to content

Commit

Permalink
Merge branch 'cvan-keys'
Browse files Browse the repository at this point in the history
  • Loading branch information
mykmelez committed Jun 28, 2017
2 parents a7acf20 + dce3170 commit e412de3
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {
'quote-props': ['error', 'consistent-as-needed'],
'semi': ['error', 'always', { omitLastInOneLineBlock: true }],
'space-before-function-paren': ['error', {
anonymous: 'always',
anonymous: 'never',
named: 'never',
asyncArrow: 'always',
}],
Expand Down
2 changes: 1 addition & 1 deletion bin/install-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function installRuntime() {
.then(() => {
return new Promise((resolve, reject) => {
function download(url) {
https.get(url, function (response) {
https.get(url, function(response) {
if (response.headers.location) {
let location = response.headers.location;
// Rewrite Windows installer links to point to the ZIP equivalent,
Expand Down
2 changes: 1 addition & 1 deletion components/CommandLineHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ CommandLineHandler.prototype = {

helpInfo: '',

handle: function (cmdLine) {
handle: function(cmdLine) {
// Prevent the runtime's default behavior so it doesn't happen in addition
// to the behavior we specify. This is disabled because the way we remove
// browser files means that default behavior no longer occurs.
Expand Down
21 changes: 21 additions & 0 deletions modules/Runtime.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const global = this;
this.Runtime = {
get commandLineArgs() { return global.commandLineArgs },
get packageJSON() { return global.packageJSON },
get devToolsOpened() { return global.devToolsOpened },

start(appFile, commandLineArgs, packageJSON) {
// TODO: stop assuming that appFile is in the topmost directory of the app.
Expand All @@ -41,6 +42,7 @@ this.Runtime = {

global.commandLineArgs = commandLineArgs;
global.packageJSON = packageJSON;
global.devToolsOpened = new WeakMap();

Services.scriptloader.loadSubScript(`chrome://app/content/${appFile.leafName}`, sandbox, 'UTF-8');
},
Expand Down Expand Up @@ -82,11 +84,13 @@ this.Runtime = {
const toolsWindow = Services.ww.openWindow(null, url, null, features, null);

const onLoad = () => {
global.devToolsOpened.set(target, toolsWindow);
toolsWindow.removeEventListener('load', onLoad);
toolsWindow.addEventListener('unload', onUnload);
};

const onUnload = () => {
global.devToolsOpened.delete(target);
toolsWindow.removeEventListener('unload', onUnload);
toolsWindow.removeEventListener('message', onMessage);
};
Expand All @@ -110,6 +114,7 @@ this.Runtime = {
switch (data.name) {
case 'toolbox-close':
toolsWindow.close();
onUnload();
break;
// We get both set-host-title and toolbox-title, and they provide
// the same title, although their semantics vary. Weird, but we simply
Expand All @@ -130,6 +135,22 @@ this.Runtime = {
return toolsWindow;
},

closeDevTools(target) {
const toolsWindow = this.devToolsOpened.get(target);

if (toolsWindow) {
toolsWindow.close();
}
},

toggleDevTools(target) {
if (global.devToolsOpened.has(target)) {
return this.closeDevTools(target);
}

return this.openDevTools(target);
},

};

function registerChromePrefix(appDir) {
Expand Down
155 changes: 141 additions & 14 deletions shell/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,149 @@

const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
const { Runtime } = Cu.import('resource://qbrt/modules/Runtime.jsm', {});
const { Services } = Cu.import('resource://gre/modules/Services.jsm', {});

window.addEventListener('load', event => {
const browser = document.getElementById('content');
const url = window.arguments[0];
const Accelerators = {
isMac: Services.appinfo.OS === 'Darwin',
keys: {
i: 73,
r: 82,
f5: 116,
},
Reload: function(event) {
const f5 = event.keyCode === this.keys.f5;
if (f5) {
return true;
}
if (this.isMac) {
// `Cmd + R`.
const cmdR = event.metaKey && event.keyCode === this.keys.r;
return cmdR;
}
// `Ctrl + R`.
const ctrlR = event.ctrlKey && event.keyCode === this.keys.r;
return ctrlR;
},
HardReload: function(event) {
// `Shift + F5`.
const shiftF5 = event.shiftKey && event.keyCode === this.keys.f5;
if (shiftF5) {
return true;
}
if (this.isMac) {
// `Cmd + Shift + R`.
const cmdShiftR = event.metaKey && event.shiftKey && event.keyCode === this.keys.r;
return cmdShiftR;
}
// `Ctrl + Shift + R`.
const ctrlShiftR = event.ctrlKey && event.shiftKey &&
event.keyCode === this.keys.r;
return ctrlShiftR;
},
ToggleDevTools: function(event) {
if (this.isMac) {
// `Cmd + Alt + I`.
const cmdAltI = event.metaKey && event.altKey && event.keyCode === this.keys.i;
return cmdAltI;
}
// `Ctrl + Shift + I`.
const ctrlShiftI = event.ctrlKey && event.shiftKey && event.keyCode === this.keys.i;
return ctrlShiftI;
},
};

function Shortcuts() {
this.accelerators = Accelerators;
this.registered = [];
}
Shortcuts.prototype.register = function(acceleratorName, callback) {
this.registered.push([acceleratorName, callback]);
};
Shortcuts.prototype.testEvent = function(event, acceleratorName) {
if (!(acceleratorName in this.accelerators)) {
return false;
}
return this.accelerators[acceleratorName](event);
};
Shortcuts.prototype.handleEvent = function(event) {
for (let idx = 0; idx < this.registered.length; idx++) {
let [ acceleratorName, callback ] = this.registered[idx];
if (this.testEvent(event, acceleratorName)) {
try {
callback();
}
catch (ex) {
dump(`error handling event: ${ex}\n`);
}
}
}
};

const UI = {
init: () => {
const browser = UI.browser = document.getElementById('content');
const url = UI.url = window.arguments[0];
const shortcuts = UI.shortcuts = new Shortcuts();

browser.loadURI(url, null, null);
// dump instead of console.log to write to stdout for tests.
dump(`opened ${url} in new window\n`);
Runtime.openDevTools(browser);
// Focus the browser window when the application is opened.
browser.focus();

browser.addEventListener('keydown', event => {
// Reload the web page when the F5 key is pressed.
if (event.keyCode && event.keyCode === 116 && !event.shiftKey &&
!event.altKey && !event.ctrlKey && !event.metaKey) {
browser.loadURI(url, null, null);

// Dump instead of log to write to stdout for tests.
dump(`opened ${url} in new window\n`);

// Hard-reload the web page.
// Windows/Linux: `Shift + F5` or `Ctrl + Shift + R`.
// Mac: `Shift + F5` or `Cmd + Shift + R`.
shortcuts.register('HardReload', () => {
// Bypass proxy and cache.
const reloadFlags = Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY |
Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
browser.webNavigation.reload(reloadFlags);
});

// Reload the web page.
// Windows/Linux: `F5` or `Ctrl + R`.
// Mac: `F5` or `Cmd + R`.
shortcuts.register('Reload', () => {
browser.reload();
}
}, false, true);
});

// Toggle the DevTools.
// Windows/Linux: `Ctrl + Shift + I`.
// Mac: `Cmd + Shift + I`.
shortcuts.register('ToggleDevTools', () => {
const toolsWindow = Runtime.toggleDevTools(browser);
if (toolsWindow) {
// DevTools window was created for the first time.
toolsWindow.addEventListener('keydown', onToolsKeydown);
toolsWindow.addEventListener('unload', onToolsUnload, { once: true });
}
});
const onToolsKeydown = event => {
// NB: the DevTools window handles its own reload key events,
// so we don't need to handle them ourselves.
if (shortcuts.testEvent(event, 'ToggleDevTools')) {
Runtime.toggleDevTools(browser);
}
};
const onToolsUnload = event => {
const toolsWindow = event.target;
toolsWindow.removeEventListener('keydown', onToolsKeydown);
};

// Handle browser-level keyboard shortcuts.
browser.addEventListener('keydown', event => {
shortcuts.handleEvent(event);
}, false, true);
},

destroy: () => {
UI.browser.removeEventListener('keydown', UI.shortcuts.handleEvent,
false, true);
},
};

}, false);
window.addEventListener('load', UI.init.bind(UI), { once: true });
window.addEventListener('unload', UI.destroy.bind(UI), { once: true });

0 comments on commit e412de3

Please sign in to comment.