Merge pull request #302 from lbryio/fix-link-focus

Reliably focus app window when opening lbry:// links (WIP)
This commit is contained in:
Jeremy Kauffman 2017-06-28 10:07:42 -04:00 committed by GitHub
commit 020f437a63

View file

@ -54,14 +54,15 @@ let readyToQuit = false;
// send it to, it's cached in this variable. // send it to, it's cached in this variable.
let openUri = null; let openUri = null;
function denormalizeUri(uri) { function processRequestedUri(uri) {
// Windows normalizes URIs when they're passed in from other apps. This tries // Windows normalizes URIs when they're passed in from other apps. On Windows,
// to restore the original URI that was typed. // this function tries to restore the original URI that was typed.
// - If the URI has no path, Windows adds a trailing slash. LBRY URIs // - If the URI has no path, Windows adds a trailing slash. LBRY URIs
// can't have a slash with no path, so we just strip it off. // can't have a slash with no path, so we just strip it off.
// - In a URI with a claim ID, like lbry://channel#claimid, Windows // - In a URI with a claim ID, like lbry://channel#claimid, Windows
// interprets the hash mark as an anchor and converts it to // interprets the hash mark as an anchor and converts it to
// lbry://channel/#claimid. We remove the slash here as well. // lbry://channel/#claimid. We remove the slash here as well.
// On Linux and Mac, we just return the URI as given.
if (process.platform == 'win32') { if (process.platform == 'win32') {
return uri.replace(/\/$/, '').replace('/#', '#'); return uri.replace(/\/$/, '').replace('/#', '#');
@ -170,6 +171,19 @@ function createWindow () {
}) })
}; };
function handleOpenUriRequested(uri) {
if (!win) {
// Window not created yet, so store up requested URI for when it is
openUri = uri;
} else {
if (win.isMinimized()) {
win.restore();
}
win.focus();
win.webContents.send('open-uri-requested', processRequestedUri(uri));
}
}
function handleDaemonSubprocessExited() { function handleDaemonSubprocessExited() {
console.log('The daemon has exited.'); console.log('The daemon has exited.');
daemonSubprocess = null; daemonSubprocess = null;
@ -222,17 +236,13 @@ if (process.platform != 'linux') {
// On Linux, this is always returning true due to an Electron bug, // On Linux, this is always returning true due to an Electron bug,
// so for now we just don't support single-instance apps on Linux. // so for now we just don't support single-instance apps on Linux.
const isSecondaryInstance = app.makeSingleInstance((argv) => { const isSecondaryInstance = app.makeSingleInstance((argv) => {
if (win) { if (argv.length >= 2) {
handleOpenUriRequested(argv[1]); // This will handle restoring and focusing the window
} else if (win) {
if (win.isMinimized()) { if (win.isMinimized()) {
win.restore(); win.restore();
} }
win.focus(); win.focus();
if (argv.length >= 2) {
win.webContents.send('open-uri-requested', denormalizeUri(argv[1]));
}
} else if (argv.length >= 2) {
openUri = denormalizeUri(argv[1]);
} }
}); });
@ -383,19 +393,10 @@ app.setAsDefaultProtocolClient('lbry');
if (process.platform == 'darwin') { if (process.platform == 'darwin') {
app.on('open-url', (event, uri) => { app.on('open-url', (event, uri) => {
if (!win) { handleOpenUriRequested(uri);
// Window not created yet, so store up requested URI for when it is
openUri = uri;
} else {
win.webContents.send('open-uri-requested', uri);
}
}); });
} else if (process.argv.length >= 2) { } else if (process.argv.length >= 2) {
if (!win) { handleOpenUriRequested(process.argv[1]);
openUri = denormalizeUri(process.argv[1]);
} else {
win.webContents.send('open-uri-requested', denormalizeUri(process.argv[1]));
}
} }
ipcMain.on('get-auth-token', (event) => { ipcMain.on('get-auth-token', (event) => {