diff --git a/app/main.js b/app/main.js index eb867a2d7..cac430513 100644 --- a/app/main.js +++ b/app/main.js @@ -30,6 +30,10 @@ let daemonStopRequested = false; // this is set to true and app.quit() is called again to quit for real. let readyToQuit = false; +// If we receive a URI to open from an external app but there's no window to +// send it to, it's cached in this variable. +let openUri = null; + function checkForNewVersion(callback) { function formatRc(ver) { // Adds dash if needed to make RC suffix semver friendly @@ -110,9 +114,16 @@ function getPidsForProcessName(name) { function createWindow () { win = new BrowserWindow({backgroundColor: '#155B4A', minWidth: 800, minHeight: 600 }) //$color-primary + win.maximize() // win.webContents.openDevTools(); win.loadURL(`file://${__dirname}/dist/index.html`) + if (openUri) { // We stored and received a URI that an external app requested before we had a window object + win.webContents.on('did-finish-load', () => { + win.webContents.send('open-uri-requested', openUri); + }); + } + win.on('closed', () => { win = null }) @@ -303,3 +314,17 @@ function upgrade(event, installerPath) { } ipcMain.on('upgrade', upgrade); + +if (process.platform == 'darwin') { + app.on('open-url', (event, uri) => { + if (!win) { + // 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 >= 3) { + // No open-url event on Win, but we can still handle URIs provided at launch time + win.webContents.send('open-uri-requested', process.argv[2]); +} diff --git a/package.json b/package.json index 45142bbaa..af8cc1393 100644 --- a/package.json +++ b/package.json @@ -32,11 +32,19 @@ }, "backgroundColor": "155B4A" }, + "protocols": [{ + "name": "lbry", + "role": "Viewer", + "schemes": ["lbry"] + }], "linux": { "target": "deb" }, "win": { "target": "nsis" + }, + "nsis": { + "perMachine": true } }, "devDependencies": { diff --git a/ui/js/app.js b/ui/js/app.js index 85f8891a6..ee7a65f17 100644 --- a/ui/js/app.js +++ b/ui/js/app.js @@ -88,13 +88,22 @@ var App = React.createClass({ downloadComplete: false, }); }, + componentWillMount: function() { + if ('openUri' in this.props) { // A URI was requested by an external app + this.showUri(this.props.openUri); + } + window.addEventListener("popstate", this.onHistoryPop); document.addEventListener('unhandledError', (event) => { this.alertError(event.detail); }); + ipcRenderer.on('open-uri-requested', (event, uri) => { + this.showUri(uri); + }); + //open links in external browser and skip full redraw on changing page document.addEventListener('click', (event) => { var target = event.target; @@ -136,6 +145,11 @@ var App = React.createClass({ componentDidMount: function() { this._isMounted = true; }, + componentWillReceiveProps: function(nextProps) { + if ('openUri' in nextProps && (!('openUri' in this.props) || nextProps.openUri != this.props.openUri)) { + this.showUri(nextProps.openUri); + } + }, componentWillUnmount: function() { this._isMounted = false; window.removeEventListener("popstate", this.onHistoryPop); @@ -152,13 +166,13 @@ var App = React.createClass({ pageArgs: term }); }, - onSubmit: function(uri) { + showUri: function(uri) { this._storeHistoryOfNextRender = true; this.setState({ address: uri, appUrl: "?show=" + encodeURIComponent(uri), viewingPage: "show", - pageArgs: uri + pageArgs: uri, }) }, handleUpgradeClicked: function() { @@ -275,7 +289,7 @@ var App = React.createClass({ this._fullScreenPages.includes(this.state.viewingPage) ? mainContent :