Caret/background.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

var mainWindow = null;
var pending = null;
var upgrading = false;
2013-10-28 19:58:24 +00:00
var files = [];
var commands = [];
2013-10-28 19:58:24 +00:00
var openWindow = function() {
2013-10-28 19:58:24 +00:00
//if window exists, re-use it
if (mainWindow) {
2013-10-28 19:58:24 +00:00
mainWindow.contentWindow.launchData = files;
mainWindow.contentWindow.require(["command"], function(c) {
c.fire("session:open-launch");
});
2013-09-12 02:36:14 +00:00
mainWindow.focus();
mainWindow.drawAttention();
2013-10-28 19:58:24 +00:00
files = [];
pending = null;
return;
}
2013-10-28 19:58:24 +00:00
//otherwise, open a new window
var defaults = {
width: 800,
height: 600,
left: 50,
top: 50
};
chrome.app.window.create("main.html", {
bounds: defaults,
id: "caret:main"
}, function(win) {
mainWindow = win;
win.contentWindow.launchData = files;
win.contentWindow.launchCommands = commands;
mainWindow.onClosed.addListener(function() {
mainWindow = null;
2013-08-20 00:53:03 +00:00
});
files = [];
commands = [];
pending = null;
});
2013-10-28 19:58:24 +00:00
}
var launch = function(launchData) {
2013-10-29 06:13:35 +00:00
if (launchData && launchData.items) files.push.apply(files, launchData.items);
2013-10-28 19:58:24 +00:00
//we delay opening the actual window to give multiple file events time to fire
if (pending !== null) return;
if (upgrading) return;
pending = setTimeout(openWindow, 250);
};
var onMessage = function(message, sender, sendResponse) {
//main window will pick up the message, if it's open
//we also allow extensions to suppress launch behavior for spurious messages
if (mainWindow || message.quiet || !message.command) {
//if it is open, and the command is loud enough, flash the window
if (mainWindow && message.command && !message.quiet) {
mainWindow.drawAttention();
}
return;
}
commands.push({
message: message,
sender: sender,
sendResponse: sendResponse
});
if (pending !== null) return;
if (upgrading) return;
pending = setTimeout(openWindow, 250);
};
2013-10-28 19:58:24 +00:00
chrome.app.runtime.onLaunched.addListener(launch);
chrome.app.runtime.onRestarted.addListener(launch);
chrome.runtime.onMessageExternal.addListener(onMessage);