Add better handling for URIs requested during load

This commit is contained in:
Alex Liebowitz 2017-05-08 05:04:11 -04:00
parent 38ea41775b
commit ef4274012f
3 changed files with 44 additions and 6 deletions

View file

@ -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
@ -113,6 +117,12 @@ function createWindow () {
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.on('did-finish-load', () => {
win.webContents.send('open-uri-requested', openUri);
});
}
win.on('closed', () => {
win = null
})
@ -306,7 +316,12 @@ ipcMain.on('upgrade', upgrade);
if (process.platform == 'darwin') {
app.on('open-url', (event, uri) => {
win.webContents.send('open-uri-requested', 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

View file

@ -88,7 +88,12 @@ 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) => {
@ -96,7 +101,7 @@ var App = React.createClass({
});
ipcRenderer.on('open-uri-requested', (event, uri) => {
this.openUri(uri);
this.showUri(uri);
});
//open links in external browser and skip full redraw on changing page
@ -140,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);
@ -156,13 +166,13 @@ var App = React.createClass({
pageArgs: term
});
},
openUri: function(uri) {
showUri: function(uri) {
this._storeHistoryOfNextRender = true;
this.setState({
address: uri,
appUrl: "?show=" + encodeURIComponent(uri),
viewingPage: "show",
pageArgs: uri
pageArgs: uri,
})
},
handleUpgradeClicked: function() {
@ -279,7 +289,7 @@ var App = React.createClass({
this._fullScreenPages.includes(this.state.viewingPage) ?
mainContent :
<div id="window">
<Header onSearch={this.onSearch} onSubmit={this.openUri} address={address} wunderBarIcon={wunderBarIcon} viewingPage={this.state.viewingPage} />
<Header onSearch={this.onSearch} onSubmit={this.showUri} address={address} wunderBarIcon={wunderBarIcon} viewingPage={this.state.viewingPage} />
<div id="main-content">
{mainContent}
</div>

View file

@ -19,6 +19,18 @@ window.addEventListener('contextmenu', (event) => {
event.preventDefault();
});
let openUri = null;
function onOpenUriRequested(event, uri) {
/**
* If an external app requests a URI while we're still on the splash screen, we store it to
* later pass into the App component.
*/
openUri = uri;
};
ipcRenderer.on('open-uri-requested', onOpenUriRequested);
let init = function() {
window.lbry = lbry;
window.lighthouse = lighthouse;
@ -30,7 +42,8 @@ let init = function() {
function onDaemonReady() {
window.sessionStorage.setItem('loaded', 'y'); //once we've made it here once per session, we don't need to show splash again
ReactDOM.render(<div>{ lbryio.enabled ? <AuthOverlay/> : '' }<App /><SnackBar /></div>, canvas)
ipcRenderer.removeListener('open-uri-requested', onOpenUriRequested); // <App /> will handle listening for URI requests once it's loaded
ReactDOM.render(<div>{ lbryio.enabled ? <AuthOverlay/> : '' }<App {... openUri ? {openUri: openUri} : {}} /><SnackBar /></div>, canvas)
}
if (window.sessionStorage.getItem('loaded') == 'y') {