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 isMaximized = await getWindowState("isMaximized") ?? false;
} 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);
}
if (isMaximized) {
@ -58,7 +58,7 @@ export function registerIpc() {
mainWindow.maximize()
} else {
mainWindow.setSize(width, height);
console.log("Not maximized.")
console.log("[Window state manager] Not maximized.")
}
});
ipcMain.on("restart", (event, arg) => {

View File

@ -26,22 +26,25 @@ if (process.platform == "linux") {
checkIfConfigExists();
injectElectronFlags();
app.whenReady().then(async () => {
switch (await getConfig("windowStyle")) {
case "default":
createCustomWindow();
customTitlebar = true;
break;
case "native":
createNativeWindow();
break;
case "basic":
createNativeWindow();
break;
default:
createCustomWindow();
customTitlebar = true;
break;
async function init() {
switch (await getConfig("windowStyle")) {
case "default":
createCustomWindow();
customTitlebar = true;
break;
case "native":
createNativeWindow();
break;
case "basic":
createNativeWindow();
break;
default:
createCustomWindow();
customTitlebar = true;
break;
}
}
await init()
session.fromPartition("some-partition").setPermissionRequestHandler((webContents, permission, callback) => {
if (permission === "notifications") {
// Approves the permissions request
@ -54,24 +57,6 @@ app.whenReady().then(async () => {
});
app.on("activate", async function () {
if (BrowserWindow.getAllWindows().length === 0)
switch (await getConfig("windowStyle")) {
case "default":
createCustomWindow();
break;
case "native":
createNativeWindow();
break;
case "discord":
createNativeWindow();
break;
default:
createCustomWindow();
break;
}
console.log("userDataPath = " + app.getPath("userData"));
await init()
});
});
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";
//https://github.com/electron/electron/issues/1334#issuecomment-716080005
// 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) {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
const langConfigFile = storagePath + "lang.json";
const langConfigFile = path.join(app.getPath("userData"), "/storage/") + "lang.json";
if (!fs.existsSync(langConfigFile)) {
fs.writeFileSync(langConfigFile, "{}", "utf-8");
}
@ -150,7 +148,6 @@ export async function getLang(object: string) {
} else {
return parsed[object];
}
}
//ArmCord Window State manager
@ -175,9 +172,8 @@ export async function getWindowState(object: string) {
const settingsFile = storagePath + "window.json";
let rawdata = fs.readFileSync(settingsFile, "utf-8");
let returndata = JSON.parse(rawdata);
console.log(object + ": " + returndata[object]);
console.log("[Window state manager] " + object + ": " + returndata[object]);
return returndata[object];
}
//ArmCord Settings/Storage manager
@ -194,60 +190,30 @@ export interface Settings {
trayIcon: string;
doneSetup: boolean;
}
export async function getConfig(object: string) {
try {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
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 function getConfigLocation() {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
return storagePath + "settings.json";
}
export async function getConfigLocation() {
try {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
return storagePath + "settings.json";
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
export async function getConfig(object: string) {
let rawdata = fs.readFileSync(getConfigLocation(), "utf-8");
let returndata = JSON.parse(rawdata);
console.log("[Config manager] " + object + ": " + returndata[object]);
return returndata[object];
}
export async function setConfig(object: string, toSet: any) {
try {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
const settingsFile = storagePath + "settings.json";
let rawdata = fs.readFileSync(settingsFile, "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";
}
let rawdata = fs.readFileSync(getConfigLocation(), "utf-8");
let parsed = JSON.parse(rawdata);
parsed[object] = toSet;
let toSave = JSON.stringify(parsed);
fs.writeFileSync(getConfigLocation(), toSave, "utf-8");
}
export async function setConfigBulk(object: Settings) {
try {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
const settingsFile = storagePath + "settings.json";
let toSave = JSON.stringify(object);
fs.writeFileSync(settingsFile, toSave, "utf-8");
} catch (e) {
console.log("Config probably doesn't exist yet. Returning setup value.");
firstRun = true;
return "setup";
}
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
const settingsFile = storagePath + "settings.json";
let toSave = JSON.stringify(object);
fs.writeFileSync(settingsFile, toSave, "utf-8");
}
export async function checkIfConfigExists() {
const userDataPath = app.getPath("userData");
@ -261,26 +227,14 @@ export async function checkIfConfigExists() {
}
console.log("First run of the ArmCord. Starting setup.");
setup();
isSetup = true;
contentPath = path.join(__dirname, "/content/setup.html");
if (!contentPath.includes("ts-out")) {
contentPath = path.join(__dirname, "/ts-out/content/setup.html");
}
firstRun = true;
} else {
if ((await getConfig("doneSetup")) == false) {
console.log("First run of the ArmCord. Starting setup.");
setup();
isSetup = true;
contentPath = path.join(__dirname, "/content/setup.html");
if (!contentPath.includes("ts-out")) {
contentPath = path.join(__dirname, "/ts-out/content/setup.html");
}
firstRun = true;
} else {
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);
if ((await getConfig("inviteWebsocket")) == true) {
startServer();
await startServer();
}
try {
mainWindow.loadFile(contentPath);
if (isSetup) {
await setLang(Intl.DateTimeFormat().resolvedOptions().locale)
mainWindow.setSize(390, 470);
}
} 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`);
}
}
if (firstRun) {
await setLang(Intl.DateTimeFormat().resolvedOptions().locale)
mainWindow.setSize(390, 470);
await mainWindow.loadFile(path.join(__dirname, "/content/setup.html"));
} else {
await mainWindow.loadFile(path.join(__dirname, "/content/splash.html"));
}
}
export function createCustomWindow() {