lbry-desktop/src/main/createWindow.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-01-18 03:13:08 +01:00
import { app, BrowserWindow, dialog } from 'electron';
import isDev from 'electron-is-dev';
2018-01-18 03:13:08 +01:00
import setupBarMenu from './menu/setupBarMenu';
import setupContextMenu from './menu/setupContextMenu';
2018-02-24 00:20:12 +01:00
export default appState => {
2018-01-18 03:13:08 +01:00
let windowConfiguration = {
backgroundColor: '#155B4A',
minWidth: 800,
minHeight: 600,
autoHideMenuBar: true,
show: false,
};
// Disable renderer process's webSecurity on development to enable CORS.
windowConfiguration = isDev
? {
...windowConfiguration,
webPreferences: {
webSecurity: false,
},
}
: windowConfiguration;
2018-01-18 03:13:08 +01:00
const rendererURL = isDev
? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`
: `file://${__dirname}/index.html`;
2018-01-18 03:13:08 +01:00
let window = new BrowserWindow(windowConfiguration);
window.maximize();
window.loadURL(rendererURL);
let deepLinkingURI;
if (
(process.platform === 'win32' || process.platform === 'linux') &&
String(process.argv[1]).startsWith('lbry')
) {
[, deepLinkingURI] = process.argv;
2018-01-18 03:13:08 +01:00
// Keep only command line / deep linked arguments
// Windows normalizes URIs when they're passed in from other apps. On Windows, this tries to
// restore the original URI that was typed.
// - 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.
// - In a URI with a claim ID, like lbry://channel#claimid, Windows interprets the hash mark as
// an anchor and converts it to lbry://channel/#claimid. We remove the slash here as well.
if (process.platform === 'win32') {
deepLinkingURI = deepLinkingURI.replace(/\/$/, '').replace('/#', '#');
}
} else {
deepLinkingURI = appState.macDeepLinkingURI;
2018-01-18 03:13:08 +01:00
}
setupBarMenu();
setupContextMenu(window);
2018-02-24 00:20:12 +01:00
window.on('close', event => {
if (!appState.isQuitting && !appState.autoUpdateAccepted) {
2018-02-24 00:20:12 +01:00
event.preventDefault();
window.hide();
}
2018-01-18 03:13:08 +01:00
});
window.on('focus', () => {
window.webContents.send('window-is-focused', null);
});
window.on('unresponsive', () => {
dialog.showMessageBox(
window,
{
type: 'warning',
buttons: ['Wait', 'Quit'],
title: 'LBRY Unresponsive',
defaultId: 1,
message: 'LBRY is not responding. Would you like to quit?',
cancelId: 0,
},
buttonIndex => {
if (buttonIndex === 1) app.quit();
}
);
});
window.once('ready-to-show', () => {
window.show();
});
window.webContents.on('did-finish-load', () => {
window.webContents.send('open-uri-requested', deepLinkingURI, true);
window.webContents.session.setUserAgent(`LBRY/${app.getVersion()}`);
if (isDev) {
2018-01-18 03:13:08 +01:00
window.webContents.openDevTools();
}
});
window.webContents.on('crashed', () => {
window = null;
});
return window;
};