code cleanup

This commit is contained in:
smartfridge 2022-07-11 20:19:50 +02:00
parent d6cbbcba7d
commit 0ebaa5627e
5 changed files with 52 additions and 153 deletions

View File

@ -50,7 +50,7 @@ export function registerIpc() {
var height= await getWindowState("height") ?? 600; var height= await getWindowState("height") ?? 600;
var isMaximized = await getWindowState("isMaximized") ?? false; var isMaximized = await getWindowState("isMaximized") ?? false;
} catch (e) { } catch (e) {
console.log("No window state file found. Fallbacking to default values.") console.log("[Window state manager] No window state file found. Fallbacking to default values.")
mainWindow.setSize(800, 600); mainWindow.setSize(800, 600);
} }
if (isMaximized) { if (isMaximized) {
@ -58,7 +58,7 @@ export function registerIpc() {
mainWindow.maximize() mainWindow.maximize()
} else { } else {
mainWindow.setSize(width, height); mainWindow.setSize(width, height);
console.log("Not maximized.") console.log("[Window state manager] Not maximized.")
} }
}); });
ipcMain.on("restart", (event, arg) => { ipcMain.on("restart", (event, arg) => {

View File

@ -26,22 +26,25 @@ if (process.platform == "linux") {
checkIfConfigExists(); checkIfConfigExists();
injectElectronFlags(); injectElectronFlags();
app.whenReady().then(async () => { app.whenReady().then(async () => {
switch (await getConfig("windowStyle")) { async function init() {
case "default": switch (await getConfig("windowStyle")) {
createCustomWindow(); case "default":
customTitlebar = true; createCustomWindow();
break; customTitlebar = true;
case "native": break;
createNativeWindow(); case "native":
break; createNativeWindow();
case "basic": break;
createNativeWindow(); case "basic":
break; createNativeWindow();
default: break;
createCustomWindow(); default:
customTitlebar = true; createCustomWindow();
break; customTitlebar = true;
break;
}
} }
await init()
session.fromPartition("some-partition").setPermissionRequestHandler((webContents, permission, callback) => { session.fromPartition("some-partition").setPermissionRequestHandler((webContents, permission, callback) => {
if (permission === "notifications") { if (permission === "notifications") {
// Approves the permissions request // Approves the permissions request
@ -54,24 +57,6 @@ app.whenReady().then(async () => {
}); });
app.on("activate", async function () { app.on("activate", async function () {
if (BrowserWindow.getAllWindows().length === 0) if (BrowserWindow.getAllWindows().length === 0)
switch (await getConfig("windowStyle")) { await init()
case "default":
createCustomWindow();
break;
case "native":
createNativeWindow();
break;
case "discord":
createNativeWindow();
break;
default:
createCustomWindow();
break;
}
console.log("userDataPath = " + app.getPath("userData"));
}); });
}); });
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});

View File

@ -2,22 +2,4 @@ import {app} from "electron";
import {mainWindow} from "./window"; import {mainWindow} from "./window";
//https://github.com/electron/electron/issues/1334#issuecomment-716080005 //https://github.com/electron/electron/issues/1334#issuecomment-716080005
// TO-DO add more // TO-DO add more
app.on("web-contents-created", (webContentsCreatedEvent, webContents) => {
webContents.on("before-input-event", (beforeInputEvent, input) => {
// console.log('Main console::', input)
const {code, alt, control, shift, meta} = input;
// Shortcut: toggle devTools
if (shift && control && !alt && !meta && code === "KeyI") {
mainWindow.webContents.toggleDevTools();
}
// Shortcut: window reload
if (shift && control && !alt && !meta && code === "KeyR") {
mainWindow.reload();
}
// Shortcut: app reload
if (alt && control && !shift && !meta && code === "KeyR") {
app.relaunch();
app.exit();
}
});
});

View File

@ -105,9 +105,7 @@ export async function injectElectronFlags() {
} }
} }
export async function setLang(language: string) { export async function setLang(language: string) {
const userDataPath = app.getPath("userData"); const langConfigFile = path.join(app.getPath("userData"), "/storage/") + "lang.json";
const storagePath = path.join(userDataPath, "/storage/");
const langConfigFile = storagePath + "lang.json";
if (!fs.existsSync(langConfigFile)) { if (!fs.existsSync(langConfigFile)) {
fs.writeFileSync(langConfigFile, "{}", "utf-8"); fs.writeFileSync(langConfigFile, "{}", "utf-8");
} }
@ -150,7 +148,6 @@ export async function getLang(object: string) {
} else { } else {
return parsed[object]; return parsed[object];
} }
} }
//ArmCord Window State manager //ArmCord Window State manager
@ -175,9 +172,8 @@ export async function getWindowState(object: string) {
const settingsFile = storagePath + "window.json"; const settingsFile = storagePath + "window.json";
let rawdata = fs.readFileSync(settingsFile, "utf-8"); let rawdata = fs.readFileSync(settingsFile, "utf-8");
let returndata = JSON.parse(rawdata); let returndata = JSON.parse(rawdata);
console.log(object + ": " + returndata[object]); console.log("[Window state manager] " + object + ": " + returndata[object]);
return returndata[object]; return returndata[object];
} }
//ArmCord Settings/Storage manager //ArmCord Settings/Storage manager
@ -194,60 +190,30 @@ export interface Settings {
trayIcon: string; trayIcon: string;
doneSetup: boolean; doneSetup: boolean;
} }
export async function getConfig(object: string) { export function getConfigLocation() {
try { const userDataPath = app.getPath("userData");
const userDataPath = app.getPath("userData"); const storagePath = path.join(userDataPath, "/storage/");
const storagePath = path.join(userDataPath, "/storage/"); return storagePath + "settings.json";
const settingsFile = storagePath + "settings.json";
let rawdata = fs.readFileSync(settingsFile, "utf-8");
let returndata = JSON.parse(rawdata);
console.log(object + ": " + returndata[object]);
return returndata[object];
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
} }
export async function getConfigLocation() { export async function getConfig(object: string) {
try { let rawdata = fs.readFileSync(getConfigLocation(), "utf-8");
const userDataPath = app.getPath("userData"); let returndata = JSON.parse(rawdata);
const storagePath = path.join(userDataPath, "/storage/"); console.log("[Config manager] " + object + ": " + returndata[object]);
return storagePath + "settings.json"; return returndata[object];
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
} }
export async function setConfig(object: string, toSet: any) { export async function setConfig(object: string, toSet: any) {
try { let rawdata = fs.readFileSync(getConfigLocation(), "utf-8");
const userDataPath = app.getPath("userData"); let parsed = JSON.parse(rawdata);
const storagePath = path.join(userDataPath, "/storage/"); parsed[object] = toSet;
const settingsFile = storagePath + "settings.json"; let toSave = JSON.stringify(parsed);
let rawdata = fs.readFileSync(settingsFile, "utf-8"); fs.writeFileSync(getConfigLocation(), toSave, "utf-8");
let parsed = JSON.parse(rawdata);
parsed[object] = toSet;
let toSave = JSON.stringify(parsed);
fs.writeFileSync(settingsFile, toSave, "utf-8");
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
} }
export async function setConfigBulk(object: Settings) { export async function setConfigBulk(object: Settings) {
try { const userDataPath = app.getPath("userData");
const userDataPath = app.getPath("userData"); const storagePath = path.join(userDataPath, "/storage/");
const storagePath = path.join(userDataPath, "/storage/"); const settingsFile = storagePath + "settings.json";
const settingsFile = storagePath + "settings.json"; let toSave = JSON.stringify(object);
let toSave = JSON.stringify(object); fs.writeFileSync(settingsFile, toSave, "utf-8");
fs.writeFileSync(settingsFile, toSave, "utf-8");
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
} }
export async function checkIfConfigExists() { export async function checkIfConfigExists() {
const userDataPath = app.getPath("userData"); const userDataPath = app.getPath("userData");
@ -261,26 +227,14 @@ export async function checkIfConfigExists() {
} }
console.log("First run of the ArmCord. Starting setup."); console.log("First run of the ArmCord. Starting setup.");
setup(); setup();
isSetup = true; firstRun = true;
contentPath = path.join(__dirname, "/content/setup.html");
if (!contentPath.includes("ts-out")) {
contentPath = path.join(__dirname, "/ts-out/content/setup.html");
}
} else { } else {
if ((await getConfig("doneSetup")) == false) { if ((await getConfig("doneSetup")) == false) {
console.log("First run of the ArmCord. Starting setup."); console.log("First run of the ArmCord. Starting setup.");
setup(); setup();
isSetup = true; firstRun = true;
contentPath = path.join(__dirname, "/content/setup.html");
if (!contentPath.includes("ts-out")) {
contentPath = path.join(__dirname, "/ts-out/content/setup.html");
}
} else { } else {
console.log("ArmCord has been run before. Skipping setup."); console.log("ArmCord has been run before. Skipping setup.");
contentPath = path.join(__dirname, "/content/splash.html");
if (!contentPath.includes("ts-out")) {
contentPath = path.join(__dirname, "/ts-out/content/splash.html");
}
} }
} }
} }

View File

@ -118,36 +118,14 @@ async function doAfterDefiningTheWindow() {
}) })
console.log(contentPath); console.log(contentPath);
if ((await getConfig("inviteWebsocket")) == true) { if ((await getConfig("inviteWebsocket")) == true) {
startServer(); await startServer();
} }
try { if (firstRun) {
mainWindow.loadFile(contentPath); await setLang(Intl.DateTimeFormat().resolvedOptions().locale)
if (isSetup) { mainWindow.setSize(390, 470);
await setLang(Intl.DateTimeFormat().resolvedOptions().locale) await mainWindow.loadFile(path.join(__dirname, "/content/setup.html"));
mainWindow.setSize(390, 470); } else {
} await mainWindow.loadFile(path.join(__dirname, "/content/splash.html"));
} catch (e) {
console.log(
"Major error detected while starting up. User is most likely on Windows platform. Fallback to alternative startup."
);
console.log(process.platform);
if (process.platform === "win32") {
if (firstRun) {
await setLang(Intl.DateTimeFormat().resolvedOptions().locale)
mainWindow.setSize(390, 470);
mainWindow.loadURL(`file://${__dirname}/content/setup.html`);
} else {
mainWindow.loadURL(`file://${__dirname}/content/splash.html`);
}
} else {
if (firstRun) {
await setLang(Intl.DateTimeFormat().resolvedOptions().locale)
mainWindow.setSize(390, 470);
mainWindow.loadURL(`file://${__dirname}/ts-out/content/setup.html`);
} else {
mainWindow.loadURL(`file://${__dirname}/ts-out/content/splash.html`);
}
}
} }
} }
export function createCustomWindow() { export function createCustomWindow() {