Adapt window management to chrome app window api

Appify tabs, windows, browserAction

Port the extension.windows.focus function to new window api and
generalize its error handling in the case where the requested window
does not exist. An error will be passed to the callback.

Port extension.browserAction and rename it to the more generic
extension.onLaunched.

Use of the id option when opening a window ensures that attempting to
open a duplicate window merely focuses the existing window.

Finally, after registration, close the options window and open the
inbox.

Port extension.remove
This commit is contained in:
lilia 2015-05-12 14:01:45 -07:00
parent ddbaf87741
commit 002ff45312
4 changed files with 91 additions and 26 deletions

View file

@ -21,7 +21,7 @@
if (!storage.get('first_install_ran')) {
storage.put('first_install_ran', 1);
extension.navigator.tabs.create("options.html");
extension.install();
}
if (textsecure.registration.isDone()) {

View file

@ -23,12 +23,18 @@
var self = {},
tabs = {};
tabs.create = function (url) {
chrome.tabs.create({url: url});
if (chrome.tabs) {
chrome.tabs.create({url: url});
} else {
extension.windows.open({url: url});
}
};
self.tabs = tabs;
self.setBadgeText = function (text) {
chrome.browserAction.setBadgeText({text: String(text)});
if (chrome.browserAction) {
chrome.browserAction.setBadgeText({text: String(text)});
}
};
return self;
@ -53,23 +59,53 @@
extension.windows = {
open: function(options, callback) {
chrome.windows.create(options, callback);
if (chrome.windows) {
chrome.windows.create(options, callback);
} else if (chrome.app.window) {
var url = options.url;
delete options.url;
chrome.app.window.create(url, options, callback);
}
},
focus: function(id, callback) {
chrome.windows.update(id, { focused: true }, callback);
if (chrome.windows) {
chrome.windows.update(id, { focused: true }, function() {
callback(chrome.runtime.lastError);
});
} else if (chrome.app.window) {
var appWindow = chrome.app.window.get(id);
if (appWindow) {
appWindow.focus();
callback();
} else {
callback('No window found for id ' + id);
}
}
},
onClosed: function(callback) {
chrome.windows.onRemoved.addListener(callback);
if (chrome.windows) {
chrome.windows.onRemoved.addListener(callback);
} else if (chrome.app.window) {
chrome.app.window.onClosed.addListener(callback);
}
},
getCurrent: function(callback) {
chrome.windows.getCurrent(callback);
if (chrome.windows) {
chrome.windows.getCurrent(callback);
} else if (chrome.app.window) {
callback(chrome.app.window.current());
}
},
remove: function(windowId) {
chrome.windows.remove(windowId);
if (chrome.windows) {
chrome.windows.remove(windowId);
} else if (chrome.app.window) {
chrome.app.window.get(windowId).close();
}
},
getBackground: function(callback) {
@ -90,12 +126,24 @@
},
getViews: function() {
return chrome.extension.getViews();
if (chrome.extension) {
return chrome.extension.getViews();
} else if (chrome.app.window) {
return chrome.app.window.getAll().map(function(appWindow) {
return appWindow.contentWindow;
});
}
}
};
extension.browserAction = function(callback) {
chrome.browserAction.onClicked.addListener(callback);
extension.onLaunched = function(callback) {
if (chrome.browserAction) {
chrome.browserAction.onClicked.addListener(callback);
}
if (chrome.app && chrome.app.runtime) {
chrome.app.runtime.onLaunched.addListener(callback);
}
};
window.textsecure = window.textsecure || {};
@ -110,10 +158,19 @@
},
};
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
id: 'main',
bounds: { width: 620, height: 500 }
});
});
extension.install = function() {
extension.windows.open({
id: 'installer',
url: 'options.html',
innerBounds: { width: 800, height: 666 }
});
};
if (chrome.runtime) {
chrome.runtime.onInstalled.addListener(function(options) {
if (options.reason === 'install') {
extension.install();
}
});
}
}());

View file

@ -80,10 +80,10 @@
var accountManager = new bg.textsecure.AccountManager();
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
$('.modal-container').hide();
$('#init-setup').hide();
$('#setup-complete').show().addClass('in');
initOptions();
extension.windows.getCurrent(function(appWindow) {
bg.openInbox();
extension.windows.remove(appWindow.id);
});
});
}
});

View file

@ -82,6 +82,7 @@
if (!windowId) {
// open the panel
extension.windows.open({
id: modelId,
url: 'conversation.html',
type: 'panel',
focused: true,
@ -98,8 +99,8 @@
});
} else {
// focus the panel
extension.windows.focus(windowId, function () {
if (chrome.runtime.lastError) {
extension.windows.focus(windowId, function (error) {
if (error) {
closeConversation(windowId); // panel isn't actually open...
openConversation(modelId); // ...and so we try again.
}
@ -110,10 +111,11 @@
/* Inbox window controller */
var inboxOpened = false;
var inboxWindowId = 0;
extension.browserAction(function() {
window.openInbox = function() {
if (inboxOpened === false) {
inboxOpened = true;
extension.windows.open({
id: 'inbox',
url: 'index.html',
type: 'panel',
focused: true,
@ -129,9 +131,15 @@
});
});
} else if (inboxOpened === true) {
extension.windows.focus(inboxWindowId);
extension.windows.focus(inboxWindowId, function (error) {
if (error) {
inboxOpened = false;
openInbox();
}
});
}
});
};
extension.onLaunched(openInbox);
// make sure windows are cleaned up on close
extension.windows.onClosed(function (windowId) {