Lightcord/modules/discord_desktop_core/core/app/index.js

264 lines
7.6 KiB
JavaScript
Raw Normal View History

2020-12-12 11:56:28 +01:00
"use strict";
2020-05-16 23:24:51 +02:00
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.startup = startup;
2020-12-12 11:56:28 +01:00
exports.handleOpenUrl = handleOpenUrl;
2020-05-16 23:24:51 +02:00
exports.setMainWindowVisible = setMainWindowVisible;
2020-12-12 11:56:28 +01:00
const {
Menu,
BrowserWindow
} = require('electron');
2020-05-22 18:43:33 +02:00
const fetch = require("node-fetch").default
2020-05-16 23:24:51 +02:00
let mainScreen
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
function startup(bootstrapModules) {
// below modules are required and initted
// in this order to prevent dependency conflicts
// please don't tamper with the order unless you know what you're doing
require('./bootstrapModules').init(bootstrapModules);
require('./paths');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
require('./splashScreen');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
const moduleUpdater = require('./moduleUpdater');
2020-12-12 11:56:28 +01:00
const updater = require('./updater');
2020-05-16 23:24:51 +02:00
require('./autoStart');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
const buildInfo = require('./buildInfo');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
const appSettings = require('./appSettings');
const Constants = require('./Constants');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
Constants.init(bootstrapModules.Constants);
const appFeatures = require('./appFeatures');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
appFeatures.init();
const GPUSettings = require('./GPUSettings');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
bootstrapModules.GPUSettings.replace(GPUSettings);
const rootCertificates = require('./rootCertificates');
2020-12-12 11:56:28 +01:00
2020-05-16 23:24:51 +02:00
rootCertificates.init();
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/accessibility');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
const app = require('./discord_native/browser/app');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
app.injectBuildInfo(buildInfo);
app.injectModuleUpdater(moduleUpdater);
2020-12-12 11:56:28 +01:00
app.injectUpdater(updater);
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/clipboard');
2020-12-12 11:56:28 +01:00
require('./discord_native/browser/constants');
require('./discord_native/browser/crashReporter');
2020-06-27 21:02:48 +02:00
const features = require('./discord_native/browser/features');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
features.injectFeaturesBackend(appFeatures.getFeatures());
2020-12-12 11:56:28 +01:00
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/fileManager');
2020-12-12 11:56:28 +01:00
require('./discord_native/browser/userDataCache');
2020-06-27 21:02:48 +02:00
const gpuSettings = require('./discord_native/browser/gpuSettings');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
gpuSettings.injectGpuSettingsBackend(GPUSettings);
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
const nativeModules = require('./discord_native/browser/nativeModules');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
nativeModules.injectModuleUpdater(moduleUpdater);
2020-12-12 11:56:28 +01:00
nativeModules.injectUpdater(updater);
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/powerMonitor');
2020-12-12 11:56:28 +01:00
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/powerSaveBlocker');
2020-12-12 11:56:28 +01:00
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/processUtils');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
const settings = require('./discord_native/browser/settings');
2020-12-12 11:56:28 +01:00
2020-06-27 21:02:48 +02:00
settings.injectSettingsBackend(appSettings.getSettings());
2020-12-12 11:56:28 +01:00
2020-06-06 12:51:51 +02:00
require('./discord_native/browser/spellCheck');
require("./lightcordMainProcess")
2020-06-06 12:51:51 +02:00
2020-12-12 11:56:28 +01:00
const windowNative = require('./discord_native/browser/window'); // expose globals that will be imported by the webapp
2020-05-16 23:24:51 +02:00
// global.releaseChannel is set in bootstrap
2020-12-12 11:56:28 +01:00
const crashReporterSetup = require('./crashReporterSetup');
global.crashReporterMetadata = crashReporterSetup.metadata;
2020-05-16 23:24:51 +02:00
global.mainAppDirname = Constants.MAIN_APP_DIRNAME;
global.features = appFeatures.getFeatures();
2020-12-12 11:56:28 +01:00
global.appSettings = appSettings.getSettings(); // this gets updated when launching a new main app window
2020-05-16 23:24:51 +02:00
global.mainWindowId = Constants.DEFAULT_MAIN_WINDOW_ID;
global.moduleUpdater = moduleUpdater;
let applicationMenu = require('./applicationMenu');
if(appSettings().get("isTabs", false)){
applicationMenu = applicationMenu.map(e => {
if(["View", "&View"].includes(e.label)){
e.submenu.push({
type: "separator"
}, {
label: "New Tab",
click: () => {
mainScreen.webContentsSend("NEW_TAB")
},
2020-07-07 12:41:31 +02:00
accelerator: "CmdOrCtrl+T"
}, {
label: "Close Current Tab",
click: () => {
mainScreen.webContentsSend("CLOSE_TAB")
},
2020-07-07 12:41:31 +02:00
accelerator: "CmdOrCtrl+W"
})
}
e.submenu = e.submenu.map(e => {
if(["Command+r", "Control+R"].includes(e.accelerator)){
e.click = function(){
mainScreen.webContentsSend("RELOAD")
}
}
if(["&Developer", "Developer"].includes(e.label)){
e.submenu[0].click = () => {
mainScreen.webContentsSend("OPEN_DEVTOOLS")
}
}
return e
})
return e
})
}
2020-05-16 23:24:51 +02:00
2020-12-12 11:56:28 +01:00
const ipc = require("./ipcMain")
ipc.on("NEW_TAB", () => {
mainScreen.webContentsSend("NEW_TAB")
})
2020-12-12 11:56:28 +01:00
Menu.setApplicationMenu(Menu.buildFromTemplate(applicationMenu));
2020-05-16 23:24:51 +02:00
mainScreen = require('./mainScreen');
let version = bootstrapModules.Constants.version
2020-07-08 09:29:33 +02:00
bootstrapModules.splashScreen.events.once("APP_SHOULD_SHOW", () => {
2020-05-16 23:24:51 +02:00
mainScreen.setMainWindowVisible(true)
})
bootstrapModules.splashScreen.events.on("APP_SHOULD_LAUNCH", () => {
2020-05-22 18:43:33 +02:00
mainScreen.init(false)
2020-06-27 21:02:48 +02:00
const { getWindow: getPopoutWindowByKey } = require('./popoutWindows');
windowNative.injectGetWindow(key => {
return getPopoutWindowByKey(key) || BrowserWindow.fromId(mainScreen.getMainWindowId());
});
2020-05-16 23:24:51 +02:00
})
2020-07-08 09:29:33 +02:00
mainScreen.events.once("ready", () => {
2020-05-16 23:24:51 +02:00
if(initByUpdate){
bootstrapModules.splashScreen.pageReady()
}else{
mainScreen.setMainWindowVisible(true)
}
})
let initByUpdate = false
if(Date.now() - global.appSettings.get("LAST_UPDATE_CHECK_TIMESTAMP", 0) < 6.48e+8){
console.log("Starting with version "+version+" because it hasn't been 1 week since the last check.")
2020-05-22 18:43:33 +02:00
mainScreen.init(false)
2020-06-27 21:02:48 +02:00
const { getWindow: getPopoutWindowByKey } = require('./popoutWindows');
windowNative.injectGetWindow(key => {
return getPopoutWindowByKey(key) || BrowserWindow.fromId(mainScreen.getMainWindowId());
});
2020-05-16 23:24:51 +02:00
}else{
initByUpdate = true
console.log("Checking if version "+version+" is outdated...")
bootstrapModules.splashScreen.initSplash()
bootstrapModules.splashScreen.events.on("SPLASH_SCREEN_READY", () => {
2020-07-04 22:42:26 +02:00
fetch("https://raw.githubusercontent.com/Lightcord/Lightcord/master/package.json", {
2020-05-22 18:43:33 +02:00
headers: {
"User-Agent": "Lightcord-Updater/1.0"
}
}).then(async res => {
const body = await res.json()
if(res.status !== 200){
2020-05-16 23:24:51 +02:00
console.error("Couldn't check updates. Using installed version.")
bootstrapModules.splashScreen.launchMainWindow()
return
}
global.appSettings.set("LAST_UPDATE_CHECK_TIMESTAMP", Date.now())
global.appSettings.save()
2020-05-22 18:43:33 +02:00
if(body.version > version){
console.error("App Outdated. updating...")
bootstrapModules.splashScreen.updateSplashState("update-available")
updateApp()
}else{
console.error("Latest version already installed. Opening window.")
2020-05-16 23:24:51 +02:00
bootstrapModules.splashScreen.launchMainWindow()
}
2020-05-22 18:43:33 +02:00
}).catch(err => {
console.error("Couldn't check updates. Using installed version.")
console.log(err)
bootstrapModules.splashScreen.launchMainWindow()
2020-05-16 23:24:51 +02:00
})
})
}
2020-12-12 11:56:28 +01:00
const {
getWindow: getPopoutWindowByKey
} = require('./popoutWindows');
windowNative.injectGetWindow(key => {
return getPopoutWindowByKey(key) || BrowserWindow.fromId(mainScreen.getMainWindowId());
});
2020-05-16 23:24:51 +02:00
}
2020-12-12 11:56:28 +01:00
function handleOpenUrl(url) {
mainScreen.handleOpenUrl(url);
2020-05-16 23:24:51 +02:00
}
function setMainWindowVisible(visible) {
mainScreen.setMainWindowVisible(visible);
2020-05-22 18:43:33 +02:00
}
function updateApp(version){
const bootstrapModules = require('./bootstrapModules')
2020-07-04 22:42:26 +02:00
//const updateLink = "https://github.com/Lightcord/Lightcord/archive/master.zip"
2020-05-22 18:43:33 +02:00
bootstrapModules.splashScreen.setSplashState({
status: "downloading-updates",
progress: 0
})
bootstrapModules.splashScreen.setSplashState({
status: "update-manually"
})
bootstrapModules.splashScreen.focusWindow()
delete global.appSettings.settings["LAST_UPDATE_CHECK_TIMESTAMP"]
global.appSettings.save()
return
// TODO: DOWNLOAD UPDATES AUTOMATICALLY
fetch(updateLink)
.then(async res => {
if(res.status !== 200){
}
})
2020-05-16 23:24:51 +02:00
}