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:
parent
ddbaf87741
commit
002ff45312
4 changed files with 91 additions and 26 deletions
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
if (!storage.get('first_install_ran')) {
|
if (!storage.get('first_install_ran')) {
|
||||||
storage.put('first_install_ran', 1);
|
storage.put('first_install_ran', 1);
|
||||||
extension.navigator.tabs.create("options.html");
|
extension.install();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textsecure.registration.isDone()) {
|
if (textsecure.registration.isDone()) {
|
||||||
|
|
|
@ -23,12 +23,18 @@
|
||||||
var self = {},
|
var self = {},
|
||||||
tabs = {};
|
tabs = {};
|
||||||
tabs.create = function (url) {
|
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.tabs = tabs;
|
||||||
|
|
||||||
self.setBadgeText = function (text) {
|
self.setBadgeText = function (text) {
|
||||||
chrome.browserAction.setBadgeText({text: String(text)});
|
if (chrome.browserAction) {
|
||||||
|
chrome.browserAction.setBadgeText({text: String(text)});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
@ -53,23 +59,53 @@
|
||||||
|
|
||||||
extension.windows = {
|
extension.windows = {
|
||||||
open: function(options, callback) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
getBackground: function(callback) {
|
||||||
|
@ -90,12 +126,24 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
getViews: function() {
|
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) {
|
extension.onLaunched = function(callback) {
|
||||||
chrome.browserAction.onClicked.addListener(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 || {};
|
window.textsecure = window.textsecure || {};
|
||||||
|
@ -110,10 +158,19 @@
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
chrome.app.runtime.onLaunched.addListener(function() {
|
extension.install = function() {
|
||||||
chrome.app.window.create('index.html', {
|
extension.windows.open({
|
||||||
id: 'main',
|
id: 'installer',
|
||||||
bounds: { width: 620, height: 500 }
|
url: 'options.html',
|
||||||
});
|
innerBounds: { width: 800, height: 666 }
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (chrome.runtime) {
|
||||||
|
chrome.runtime.onInstalled.addListener(function(options) {
|
||||||
|
if (options.reason === 'install') {
|
||||||
|
extension.install();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}());
|
}());
|
||||||
|
|
|
@ -80,10 +80,10 @@
|
||||||
|
|
||||||
var accountManager = new bg.textsecure.AccountManager();
|
var accountManager = new bg.textsecure.AccountManager();
|
||||||
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
|
accountManager.registerSecondDevice(setProvisioningUrl, confirmNumber, incrementCounter).then(function() {
|
||||||
$('.modal-container').hide();
|
extension.windows.getCurrent(function(appWindow) {
|
||||||
$('#init-setup').hide();
|
bg.openInbox();
|
||||||
$('#setup-complete').show().addClass('in');
|
extension.windows.remove(appWindow.id);
|
||||||
initOptions();
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -82,6 +82,7 @@
|
||||||
if (!windowId) {
|
if (!windowId) {
|
||||||
// open the panel
|
// open the panel
|
||||||
extension.windows.open({
|
extension.windows.open({
|
||||||
|
id: modelId,
|
||||||
url: 'conversation.html',
|
url: 'conversation.html',
|
||||||
type: 'panel',
|
type: 'panel',
|
||||||
focused: true,
|
focused: true,
|
||||||
|
@ -98,8 +99,8 @@
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// focus the panel
|
// focus the panel
|
||||||
extension.windows.focus(windowId, function () {
|
extension.windows.focus(windowId, function (error) {
|
||||||
if (chrome.runtime.lastError) {
|
if (error) {
|
||||||
closeConversation(windowId); // panel isn't actually open...
|
closeConversation(windowId); // panel isn't actually open...
|
||||||
openConversation(modelId); // ...and so we try again.
|
openConversation(modelId); // ...and so we try again.
|
||||||
}
|
}
|
||||||
|
@ -110,10 +111,11 @@
|
||||||
/* Inbox window controller */
|
/* Inbox window controller */
|
||||||
var inboxOpened = false;
|
var inboxOpened = false;
|
||||||
var inboxWindowId = 0;
|
var inboxWindowId = 0;
|
||||||
extension.browserAction(function() {
|
window.openInbox = function() {
|
||||||
if (inboxOpened === false) {
|
if (inboxOpened === false) {
|
||||||
inboxOpened = true;
|
inboxOpened = true;
|
||||||
extension.windows.open({
|
extension.windows.open({
|
||||||
|
id: 'inbox',
|
||||||
url: 'index.html',
|
url: 'index.html',
|
||||||
type: 'panel',
|
type: 'panel',
|
||||||
focused: true,
|
focused: true,
|
||||||
|
@ -129,9 +131,15 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if (inboxOpened === true) {
|
} 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
|
// make sure windows are cleaned up on close
|
||||||
extension.windows.onClosed(function (windowId) {
|
extension.windows.onClosed(function (windowId) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue